Skip to content

Commit

Permalink
Legend SQL - Assortment of changes (#2483)
Browse files Browse the repository at this point in the history
- left/right support
- make_date/make_timestamp/localtimestamp support
- trim support
  • Loading branch information
gs-jp1 authored Nov 29, 2023
1 parent 2ef9d8b commit a928b72
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,8 @@ private void registerStrings()
register("meta::pure::functions::string::trim_String_1__String_1_", true, ps -> res("String", "one"));
register("meta::pure::functions::string::ltrim_String_1__String_1_", true, ps -> res("String", "one"));
register("meta::pure::functions::string::rtrim_String_1__String_1_", true, ps -> res("String", "one"));
register("meta::pure::functions::string::left_String_1__Integer_1__String_1_", false, ps -> res("String", "one"));
register("meta::pure::functions::string::right_String_1__Integer_1__String_1_", false, ps -> res("String", "one"));
register(m(m(h("meta::pure::functions::string::lpad_String_1__Integer_1__String_1_", false, ps -> res("String", "one"), ps -> ps.size() == 2)),
m(h("meta::pure::functions::string::lpad_String_1__Integer_1__String_1__String_1_", false, ps -> res("String", "one"), ps -> true))));

Expand Down Expand Up @@ -2073,6 +2075,8 @@ private Map<String, Dispatch> buildDispatch()
map.put("meta::pure::functions::string::trim_String_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "String".equals(ps.get(0)._genericType()._rawType()._name())));
map.put("meta::pure::functions::string::ltrim_String_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "String".equals(ps.get(0)._genericType()._rawType()._name())));
map.put("meta::pure::functions::string::rtrim_String_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "String".equals(ps.get(0)._genericType()._rawType()._name())));
map.put("meta::pure::functions::string::left_String_1__Integer_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "String".equals(ps.get(0)._genericType()._rawType()._name())) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "Integer".equals(ps.get(1)._genericType()._rawType()._name())));
map.put("meta::pure::functions::string::right_String_1__Integer_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "String".equals(ps.get(0)._genericType()._rawType()._name())) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "Integer".equals(ps.get(1)._genericType()._rawType()._name())));
map.put("meta::pure::functions::string::lpad_String_1__Integer_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "String".equals(ps.get(0)._genericType()._rawType()._name())) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "Integer".equals(ps.get(1)._genericType()._rawType()._name())));
map.put("meta::pure::functions::string::lpad_String_1__Integer_1__String_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 3 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "String".equals(ps.get(0)._genericType()._rawType()._name())) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "Integer".equals(ps.get(1)._genericType()._rawType()._name())) && isOne(ps.get(2)._multiplicity()) && ("Nil".equals(ps.get(2)._genericType()._rawType()._name()) || "String".equals(ps.get(2)._genericType()._rawType()._name())));
map.put("meta::pure::functions::string::rpad_String_1__Integer_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "String".equals(ps.get(0)._genericType()._rawType()._name())) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "Integer".equals(ps.get(1)._genericType()._rawType()._name())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ Class meta::legend::test::handlers::model::TestString
lpad(){$this.string->lpad(1)}:String[1];
lpad2(){$this.string->lpad(1, '0')}:String[1];
rpad(){$this.string->rpad(1)}:String[1];
rpad2(){$this.string->rpad(1, '0')}:String[1];
rpad2(){$this.string->rpad(1, '0')}:String[1];
left(){$this.string->left(1)}:String[1];
right(){$this.string->right(1)}:String[1];

parseBoolean(){$this.string->parseBoolean()}:Boolean[1];
parseDate(){$this.string->parseDate()}:Date[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ function {doc.doc = 'Lower cases the first charater of the provided string'} met
})
}

function {doc.doc = 'Return first n characters in the string'}
meta::pure::functions::string::left(string:String[1], length:Integer[1]):String[1]
{
assert($length >= 0, 'length must be >= 0');

let strLength = $string->length();

let digits = if ($length > $strLength,
| $strLength,
| $length);

$string->substring(0, $digits);
}

function {doc.doc = 'Return last n characters in the string'}
meta::pure::functions::string::right(string:String[1], length:Integer[1]):String[1]
{
assert($length >= 0, 'length must be >= 0');

let strLength = $string->length();

let digits = if ($length > $strLength,
| 0,
| $strLength - $length);

$string->substring($digits, $strLength);
}

function meta::pure::functions::string::repeatString(str:String[0..1], times:Integer[1]):String[0..1]
{
if ($str->isNotEmpty(), | $str->toOne()->repeat($times)->joinStrings(), | []);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2023 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

function <<test.Test>> meta::pure::functions::string::tests::left::leftTest():Boolean[1] {

assertEquals('ab', 'abcde'->left(2));
assertEquals('', 'abcde'->left(0));
assertEquals('abcde', 'abcde'->left(7));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2023 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

function <<test.Test>> meta::pure::functions::string::tests::right::rightTest():Boolean[1] {

assertEquals('de', 'abcde'->right(2));
assertEquals('', 'abcde'->right(0));
assertEquals('abcde', 'abcde'->right(7));
}
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,8 @@ function meta::pure::router::routing::shouldStopFunctions(extensions:meta::pure:
lpad_String_1__Integer_1__String_1__String_1_,
rpad_String_1__Integer_1__String_1_,
rpad_String_1__Integer_1__String_1__String_1_,
left_String_1__Integer_1__String_1_,
right_String_1__Integer_1__String_1_,
meta::pure::tds::extensions::firstNotNull_T_MANY__T_$0_1$_,
meta::pure::functions::date::calendar::annualized_Date_1__String_1__Date_1__Number_$0_1$__Number_$0_1$_,
meta::pure::functions::date::calendar::cme_Date_1__String_1__Date_1__Number_$0_1$__Number_$0_1$_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::postg
dynaFnToSql('hour', $allStates, ^ToSql(format='date_part(\'hour\', %s)')),
dynaFnToSql('indexOf', $allStates, ^ToSql(format='strpos(%s, %s)')),
dynaFnToSql('joinStrings', $allStates, ^ToSql(format='string_agg(%s, %s)')),
dynaFnToSql('left', $allStates, ^ToSql(format='left(%s, %s)')),
dynaFnToSql('length', $allStates, ^ToSql(format='char_length(%s)')),
dynaFnToSql('minute', $allStates, ^ToSql(format='date_part(\'minute\', %s)')),
dynaFnToSql('mod', $allStates, ^ToSql(format='mod(%s,%s)')),
Expand All @@ -195,9 +196,10 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::postg
dynaFnToSql('parseFloat', $allStates, ^ToSql(format='cast(%s as float)')),
dynaFnToSql('parseInteger', $allStates, ^ToSql(format='cast(%s as integer)')),
dynaFnToSql('position', $allStates, ^ToSql(format='position(%s in %s)')),
dynaFnToSql('quarter', $allStates, ^ToSql(format='quarter(%s)')),
dynaFnToSql('quarter', $allStates, ^ToSql(format='date_part(\'quarter\', %s)')),
dynaFnToSql('quarterNumber', $allStates, ^ToSql(format='date_part(\'quarter\', %s)')),
dynaFnToSql('rem', $allStates, ^ToSql(format='mod(%s,%s)')),
dynaFnToSql('right', $allStates, ^ToSql(format='right(%s, %s)')),
dynaFnToSql('round', $allStates, ^ToSql(format='round((%s)::numeric, %s)', transform=transformRound_String_MANY__String_MANY_)),
dynaFnToSql('second', $allStates, ^ToSql(format='date_part(\'second\', %s)')),
dynaFnToSql('substring', $allStates, ^ToSql(format='substring%s', transform={p:String[*]|$p->joinStrings('(', ', ', ')')})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7739,6 +7739,8 @@ function meta::relational::functions::pureToSqlQuery::getSupportedFunctions():Ma
^PureFunctionToRelationalFunctionPair(first=meta::pure::functions::string::trim_String_1__String_1_,second=meta::relational::functions::pureToSqlQuery::processDynaFunction_FunctionExpression_1__PropertyMapping_MANY__SelectWithCursor_1__Map_1__State_1__JoinType_1__String_1__List_1__DebugContext_1__Extension_MANY__RelationalOperationElement_1_),
^PureFunctionToRelationalFunctionPair(first=meta::pure::functions::string::ltrim_String_1__String_1_,second=meta::relational::functions::pureToSqlQuery::processDynaFunction_FunctionExpression_1__PropertyMapping_MANY__SelectWithCursor_1__Map_1__State_1__JoinType_1__String_1__List_1__DebugContext_1__Extension_MANY__RelationalOperationElement_1_),
^PureFunctionToRelationalFunctionPair(first=meta::pure::functions::string::rtrim_String_1__String_1_,second=meta::relational::functions::pureToSqlQuery::processDynaFunction_FunctionExpression_1__PropertyMapping_MANY__SelectWithCursor_1__Map_1__State_1__JoinType_1__String_1__List_1__DebugContext_1__Extension_MANY__RelationalOperationElement_1_),
^PureFunctionToRelationalFunctionPair(first=meta::pure::functions::string::left_String_1__Integer_1__String_1_,second=meta::relational::functions::pureToSqlQuery::processDynaFunction_FunctionExpression_1__PropertyMapping_MANY__SelectWithCursor_1__Map_1__State_1__JoinType_1__String_1__List_1__DebugContext_1__Extension_MANY__RelationalOperationElement_1_),
^PureFunctionToRelationalFunctionPair(first=meta::pure::functions::string::right_String_1__Integer_1__String_1_,second=meta::relational::functions::pureToSqlQuery::processDynaFunction_FunctionExpression_1__PropertyMapping_MANY__SelectWithCursor_1__Map_1__State_1__JoinType_1__String_1__List_1__DebugContext_1__Extension_MANY__RelationalOperationElement_1_),
^PureFunctionToRelationalFunctionPair(first=meta::pure::functions::string::lpad_String_1__Integer_1__String_1_,second=meta::relational::functions::pureToSqlQuery::processDynaFunction_FunctionExpression_1__PropertyMapping_MANY__SelectWithCursor_1__Map_1__State_1__JoinType_1__String_1__List_1__DebugContext_1__Extension_MANY__RelationalOperationElement_1_),
^PureFunctionToRelationalFunctionPair(first=meta::pure::functions::string::lpad_String_1__Integer_1__String_1__String_1_,second=meta::relational::functions::pureToSqlQuery::processDynaFunction_FunctionExpression_1__PropertyMapping_MANY__SelectWithCursor_1__Map_1__State_1__JoinType_1__String_1__List_1__DebugContext_1__Extension_MANY__RelationalOperationElement_1_),
^PureFunctionToRelationalFunctionPair(first=meta::pure::functions::string::rpad_String_1__Integer_1__String_1_,second=meta::relational::functions::pureToSqlQuery::processDynaFunction_FunctionExpression_1__PropertyMapping_MANY__SelectWithCursor_1__Map_1__State_1__JoinType_1__String_1__List_1__DebugContext_1__Extension_MANY__RelationalOperationElement_1_),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::db2::
dynaFnToSql('firstDayOfYear', $allStates, ^ToSql(format='date(1) + (year(%s)-1) YEARS')),
dynaFnToSql('hour', $allStates, ^ToSql(format='hour(%s)')),
dynaFnToSql('joinStrings', $allStates, ^ToSql(format='listagg(%s,%s)')),
dynaFnToSql('left', $allStates, ^ToSql(format='trim(left(%s, %s))')),
dynaFnToSql('length', $allStates, ^ToSql(format='CHARACTER_LENGTH(%s,CODEUNITS32)')),
dynaFnToSql('minute', $allStates, ^ToSql(format='minute(%s)')),
dynaFnToSql('md5', $allStates, ^ToSql(format='hash_md5(%s)')),
Expand All @@ -114,6 +115,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::db2::
dynaFnToSql('quarter', $allStates, ^ToSql(format='quarter(%s)')),
dynaFnToSql('quarterNumber', $allStates, ^ToSql(format='quarter(%s)')),
dynaFnToSql('rem', $allStates, ^ToSql(format='mod(%s,%s)')),
dynaFnToSql('right', $allStates, ^ToSql(format='trim(right(%s, %s))')),
dynaFnToSql('round', $allStates, ^ToSql(format='round(%s, %s)', transform=transformRound_String_MANY__String_MANY_)),
dynaFnToSql('second', $allStates, ^ToSql(format='second(%s)')),
dynaFnToSql('sha1', $allStates, ^ToSql(format='hash_sha1(%s)')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ function <<paramTest.Test>> meta::relational::tests::dbSpecificTests::sqlQueryTe
runDynaFunctionDatabaseTest($dynaFunc, $expected, $config);
}

function <<paramTest.Test>> meta::relational::tests::dbSpecificTests::sqlQueryTests::dynaFunctions::left::testLonger(config:DbTestConfig[1]):Boolean[1]
{
let dynaFunc = ^DynaFunction(name='left', parameters=[^Literal(value='Bloggs'), ^Literal(value=8)]);
let expected = ^Literal(value='Bloggs');
runDynaFunctionDatabaseTest($dynaFunc, $expected, $config);
}

function <<paramTest.Test>> meta::relational::tests::dbSpecificTests::sqlQueryTests::dynaFunctions::right::testNoSpace(config:DbTestConfig[1]):Boolean[1]
{
let dynaFunc = ^DynaFunction(name='right', parameters=[^Literal(value='Smith'), ^Literal(value=3)]);
Expand All @@ -265,6 +272,13 @@ function <<paramTest.Test>> meta::relational::tests::dbSpecificTests::sqlQueryTe
runDynaFunctionDatabaseTest($dynaFunc, $expected, $config);
}

function <<paramTest.Test>> meta::relational::tests::dbSpecificTests::sqlQueryTests::dynaFunctions::right::testLonger(config:DbTestConfig[1]):Boolean[1]
{
let dynaFunc = ^DynaFunction(name='right', parameters=[^Literal(value='Bloggs'), ^Literal(value=8)]);
let expected = ^Literal(value='Bloggs');
runDynaFunctionDatabaseTest($dynaFunc, $expected, $config);
}

// contains, endsWith , startsWith
function <<paramTest.Test>> meta::relational::tests::dbSpecificTests::sqlQueryTests::dynaFunctions::contains::testNoSpace(config:DbTestConfig[1]):Boolean[1]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,31 @@ function <<test.Test>> meta::relational::tests::functions::sqlstring::testPad():
)->distinct() == [true];
}

function <<test.Test>> meta::relational::tests::functions::sqlstring::testLeftRight():Boolean[1]
{

let expected = [
pair(DatabaseType.DB2, 'select trim(left("root".FIRSTNAME, 1)) as "left", trim(right("root".FIRSTNAME, 1)) as "right" from personTable as "root"'),
pair(DatabaseType.H2, 'select left("root".FIRSTNAME,1) as "left", right("root".FIRSTNAME,1) as "right" from personTable as "root"')
];

$expected->map(p|
let driver = $p.first;
let expectedSql = $p.second;

let result = toSQLString(
|Person.all()->project([
a | $a.firstName->left(1),
a | $a.firstName->right(1)
],
['left', 'right']),
simpleRelationalMapping,
$driver, meta::relational::extension::relationalExtensions());

assertEquals($expectedSql, $result, '\nSQL not as expected for %s\n\nexpected: %s\nactual: %s', [$driver, $expectedSql, $result]);
)->distinct() == [true];
}

function <<test.Test>> meta::relational::tests::functions::sqlstring::testCbrt():Boolean[1]
{
let common = 'select cbrt("root".quantity) as "cbrt" from tradeTable as "root"';
Expand Down
Loading

0 comments on commit a928b72

Please sign in to comment.