Skip to content

Commit

Permalink
Legend SQL - add to_char, lpad, rpad (#2454)
Browse files Browse the repository at this point in the history
* Legend SQL - add to_char, lpad, rpad

1. add lpad/rpad
2. remove deprecated sql classes
3. added lpad/rpad pure string functions
4. added pure2sql support for month
5. more dialect handling of month/dayofweek
  • Loading branch information
gs-jp1 authored Nov 13, 2023
1 parent e6138a5 commit 445a476
Show file tree
Hide file tree
Showing 36 changed files with 635 additions and 397 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,11 @@ 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(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))));

register(m(m(h("meta::pure::functions::string::rpad_String_1__Integer_1__String_1_", false, ps -> res("String", "one"), ps -> ps.size() == 2)),
m(h("meta::pure::functions::string::rpad_String_1__Integer_1__String_1__String_1_", false, ps -> res("String", "one"), ps -> true))));
register("meta::pure::functions::string::matches_String_1__String_1__Boolean_1_", true, ps -> res("Boolean", "one"));
register("meta::pure::functions::string::isAlphaNumeric_String_1__Boolean_1_", false, ps -> res("Boolean", "one"));
register("meta::pure::functions::string::isNoLongerThan_String_$0_1$__Integer_1__Boolean_1_", false, ps -> res("Boolean", "one"));
Expand Down Expand Up @@ -2052,6 +2057,12 @@ private Map<String, Dispatch> buildDispatch()
map.put("meta::pure::functions::string::toString_Any_1__String_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && isOne(ps.get(0)._multiplicity()));
map.put("meta::pure::functions::string::toUpper_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::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::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())));
map.put("meta::pure::functions::string::rpad_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::graphFetch::calculateSourceTree_RootGraphFetchTree_1__Mapping_1__Extension_MANY__RootGraphFetchTree_1_", (List<ValueSpecification> ps) -> ps.size() == 3 && isOne(ps.get(0)._multiplicity()) && Sets.immutable.with("Nil", "RootGraphFetchTree", "ExtendedRootGraphFetchTree", "RoutedRootGraphFetchTree", "SerializeTopRootGraphFetchTree").contains(ps.get(0)._genericType()._rawType()._name()) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "Mapping".equals(ps.get(1)._genericType()._rawType()._name())) && ("Nil".equals(ps.get(2)._genericType()._rawType()._name()) || "Extension".equals(ps.get(2)._genericType()._rawType()._name())));
map.put("meta::pure::graphFetch::execution::graphFetchChecked_T_MANY__RootGraphFetchTree_1__Checked_MANY_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(1)._multiplicity()) && Sets.immutable.with("Nil", "RootGraphFetchTree", "ExtendedRootGraphFetchTree", "RoutedRootGraphFetchTree", "SerializeTopRootGraphFetchTree").contains(ps.get(1)._genericType()._rawType()._name()));
map.put("meta::pure::graphFetch::execution::graphFetch_T_MANY__RootGraphFetchTree_1__Integer_1__T_MANY_", (List<ValueSpecification> ps) -> ps.size() == 3 && isOne(ps.get(1)._multiplicity()) && Sets.immutable.with("Nil", "RootGraphFetchTree", "ExtendedRootGraphFetchTree", "RoutedRootGraphFetchTree", "SerializeTopRootGraphFetchTree").contains(ps.get(1)._genericType()._rawType()._name()) && isOne(ps.get(2)._multiplicity()) && ("Nil".equals(ps.get(2)._genericType()._rawType()._name()) || "Integer".equals(ps.get(2)._genericType()._rawType()._name())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ Class meta::legend::test::handlers::model::TestString

length(){$this.string->length()}:Integer[1];
ltrim(){$this.string->ltrim()}:String[1];
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];

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 @@ -167,6 +167,36 @@ function {doc.doc = 'Un-camel case / humanize the provided string using provided
->joinStrings(' ');
}

function meta::pure::functions::string::lpad(str:String[1], length:Integer[1]):String[1]
{
lpad($str, $length, ' ')
}

function meta::pure::functions::string::lpad(str:String[1], length:Integer[1], char:String[1]):String[1]
{
pad($str, $length, $char, true)
}

function meta::pure::functions::string::rpad(str:String[1], length:Integer[1]):String[1]
{
rpad($str, $length, ' ')
}

function meta::pure::functions::string::rpad(str:String[1], length:Integer[1], char:String[1]):String[1]
{
pad($str, $length, $char, false)
}

function <<access.private>> meta::pure::functions::string::pad(str:String[1], length:Integer[1], char:String[1], left:Boolean[1]):String[1]
{
let result = if ($str->length() > $length,
| $str->substring(0, $length),
| range($length - $str->length())->fold({acc, s | if ($left, | $char + $s, | $s + $char)}, $str->toOne())
);

if ($result->length() > $length, | $result->substring(0, $length), | $result);
}

function {doc.doc = 'Split the string and select the part'}
meta::pure::functions::string::splitPart(str:String[0..1], token:String[1], part:Integer[1]):String[0..1]
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ function <<test.Test>> {test.excludePlatform = 'Java compiled'} meta::pure::func
assertEquals('abab', repeatString('ab', 2));
}

function <<test.Test>> meta::pure::functions::string::tests::testLpad():Boolean[1]
{
assertEquals('abc', lpad('abcd', 3, '_'));
assertEquals('______abcd', lpad('abcd', 10, '_'));
assertEquals(' abcd', lpad('abcd', 10));
assertEquals('abcd', lpad('abcd', 10, ''));
}

function <<test.Test>> meta::pure::functions::string::tests::testRpad():Boolean[1]
{
assertEquals('abc', rpad('abcd', 3, '_'));
assertEquals('abcd______', rpad('abcd', 10, '_'));
assertEquals('abcd ', rpad('abcd', 10));
assertEquals('abcd', rpad('abcd', 10, ''));
}

function <<test.Test>> meta::pure::functions::string::tests::testSplitPart():Boolean[1]
{
assertEquals([], splitPart([], 'a', 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,10 @@ function meta::pure::router::routing::shouldStopFunctions(extensions:meta::pure:
tdsContains_T_1__Function_MANY__TabularDataSet_1__Boolean_1_,
tdsContains_T_1__Function_MANY__String_MANY__TabularDataSet_1__Function_1__Boolean_1_,
splitPart_String_$0_1$__String_1__Integer_1__String_$0_1$_,
lpad_String_1__Integer_1__String_1_,
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_,
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 @@ -84,6 +84,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::memsq
dynaFnToSql('dateDiff', $allStates, ^ToSql(format='%s', transform={p:String[*]|generateDateDiffExpressionForMemSQL ([$p->at(0), $p->at(1), $p->at(2)->replace('\'', '')])})),
dynaFnToSql('datePart', $allStates, ^ToSql(format='date(%s)')),
dynaFnToSql('dayOfMonth', $allStates, ^ToSql(format='day(%s)')),
dynaFnToSql('dayOfWeek', $allStates, ^ToSql(format='dayname(%s)')),
dynaFnToSql('dayOfWeekNumber', $allStates, ^ToSql(format='dayofweek(%s)')),
dynaFnToSql('dayOfYear', $allStates, ^ToSql(format='dayofyear(%s)')),
dynaFnToSql('decodeBase64', $allStates, ^ToSql(format='cast(from_base64(%s) as char)')),
Expand All @@ -103,6 +104,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::memsq
dynaFnToSql('minute', $allStates, ^ToSql(format='minute(%s)')),
dynaFnToSql('mod', $allStates, ^ToSql(format='mod(%s,%s)')),
dynaFnToSql('month', $allStates, ^ToSql(format='month(%s)')),
dynaFnToSql('monthName', $allStates, ^ToSql(format='monthname(%s)')),
dynaFnToSql('monthNumber', $allStates, ^ToSql(format='month(%s)')),
dynaFnToSql('mostRecentDayOfWeek', $allStates, ^ToSql(format='adddate(%s, INTERVAL case when %s - dayofweek(%s) > 0 then %s - dayofweek(%s) - 7 else %s - dayofweek(%s) end DAY)', transform={p:String[1..2] | $p->formatMostRecentMemSQL('curdate()')}, parametersWithinWhenClause = [false, false])),
dynaFnToSql('now', $allStates, ^ToSql(format='now()')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::postg
dynaFnToSql('dateDiff', $allStates, ^ToSql(format='%s', transform={p:String[*]|generateDateDiffExpressionForPostgres($p)})),
dynaFnToSql('datePart', $allStates, ^ToSql(format='Date(%s)')),
dynaFnToSql('dayOfMonth', $allStates, ^ToSql(format='date_part(\'day\', %s)')),
dynaFnToSql('dayOfWeek', $allStates, ^ToSql(format='to_char(%s, \'FMDay\')')),
dynaFnToSql('dayOfWeekNumber', $allStates, ^ToSql(format='date_part(\'dow\', %s)')),
dynaFnToSql('dayOfYear', $allStates, ^ToSql(format='date_part(\'doy\', %s)')),
dynaFnToSql('firstDayOfMonth', $allStates, ^ToSql(format='date_trunc(\'month\', %s)')),
Expand All @@ -185,6 +186,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::postg
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)')),
dynaFnToSql('monthName', $allStates, ^ToSql(format='to_char(%s, \'FMMonth\')')),
dynaFnToSql('monthNumber', $allStates, ^ToSql(format='date_part(\'month\', %s)')),
dynaFnToSql('now', $allStates, ^ToSql(format='now()')),
dynaFnToSql('parseDecimal', $allStates, ^ToSql(format='cast(%s as decimal)')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::prest
dynaFnToSql('convertVarchar128', $allStates, ^ToSql(format='cast(%s as VARCHAR(128))')),
dynaFnToSql('dateDiff', $allStates, ^ToSql(format='date_diff(%s,%s,%s)', transform={p:String[*]|[$p->at(2)->replace('\'', '')->processDateDiffDurationUnitForPresto(),$p->at(0),$p->at(1)]})),
dynaFnToSql('datePart', $allStates, ^ToSql(format='Date(%s)')),
dynaFnToSql('dayOfWeek', $allStates, ^ToSql(format='date_format(%s, \'%W\')')),
dynaFnToSql('dayOfWeekNumber', $allStates, ^ToSql(format='day_of_week(%s)')),
dynaFnToSql('dayOfYear', $allStates, ^ToSql(format='day_of_year(%s)')),
dynaFnToSql('firstDayOfMonth', $allStates, ^ToSql(format='date_trunc(\'month\', %s)')),
Expand All @@ -79,6 +80,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::prest
dynaFnToSql('minute', $allStates, ^ToSql(format='minute(%s)')),
dynaFnToSql('mod', $allStates, ^ToSql(format='mod(%s,%s)')),
dynaFnToSql('month', $allStates, ^ToSql(format='month(%s)')),
dynaFnToSql('monthName', $allStates, ^ToSql(format='date_format(%s, \'%M\')')),
dynaFnToSql('monthNumber', $allStates, ^ToSql(format='month(%s)')),
dynaFnToSql('mostRecentDayOfWeek', $allStates, ^ToSql(format='date_add(\'day\', case when %s - day_of_week(%s) > 0 then %s - day_of_week(%s) - 7 else %s - day_of_week(%s) end, %s)', transform={p:String[1..2] | $p->formatMostRecentPresto('current_date')}, parametersWithinWhenClause = [false, false])),
dynaFnToSql('now', $allStates, ^ToSql(format='current_timestamp')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::redsh
dynaFnToSql('atan2', $allStates, ^ToSql(format='atan2(%s,%s)')),
dynaFnToSql('concat', $allStates, ^ToSql(format='%s', transform={p:String[*]|$p->joinStrings(' + ')})),
dynaFnToSql('datePart', $allStates, ^ToSql(format='trunc(%s)')),
dynaFnToSql('dayOfWeek', $allStates, ^ToSql(format='to_char(%s, \'FMDay\')')),
dynaFnToSql('hour', $allStates, ^ToSql(format='date_part(hour, %s)')),
dynaFnToSql('joinStrings', $allStates, ^ToSql(format='listagg(%s, %s)')),
dynaFnToSql('log10', $allStates, ^ToSql(format='log(%s)')),
dynaFnToSql('minute', $allStates, ^ToSql(format='extract(minute from %s)')),
dynaFnToSql('mod', $allStates, ^ToSql(format='mod(%s,%s)')),
dynaFnToSql('month', $allStates, ^ToSql(format='extract(month from %s)')),
dynaFnToSql('monthName', $allStates, ^ToSql(format='to_char(%s, \'FMMonth\')')),
dynaFnToSql('monthNumber', $allStates, ^ToSql(format='extract(month from %s)')),
dynaFnToSql('now', $allStates, ^ToSql(format='now()')),
dynaFnToSql('parseDecimal', $allStates, ^ToSql(format='cast(%s as decimal)')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::snowf
dynaFnToSql('dateDiff', $allStates, ^ToSql(format='datediff(%s,%s,%s)', transform={p:String[*]|[$p->at(2)->replace('\'', '')->processDateDiffDurationUnitForSnowflake(),$p->at(0),$p->at(1)]})),
dynaFnToSql('datePart', $allStates, ^ToSql(format='Date(%s)')),
dynaFnToSql('dayOfMonth', $allStates, ^ToSql(format='DAYOFMONTH(%s)')),
dynaFnToSql('dayOfWeek', $allStates, ^ToSql(format='to_char(%s, \'DYDY\')')),
dynaFnToSql('dayOfWeekNumber', $allStates, ^ToSql(format='DAYOFWEEKISO(%s)')),
dynaFnToSql('dayOfYear', $allStates, ^ToSql(format='DAYOFYEAR(%s)')),
dynaFnToSql('extractFromSemiStructured', $allStates, ^ToSql(format='%s', transform={p:String[3]|$p->processExtractFromSemiStructuredParamsForSnowflake()})),
Expand All @@ -136,6 +137,7 @@ function <<access.private>> meta::relational::functions::sqlQueryToString::snowf
dynaFnToSql('minute', $allStates, ^ToSql(format='minute(%s)')),
dynaFnToSql('mod', $allStates, ^ToSql(format='mod(%s,%s)')),
dynaFnToSql('month', $allStates, ^ToSql(format='MONTH(%s)')),
dynaFnToSql('monthName', $allStates, ^ToSql(format='to_char(%s, \'MMMM\')')),
dynaFnToSql('monthNumber', $allStates, ^ToSql(format='MONTH(%s)')),
dynaFnToSql('mostRecentDayOfWeek', $allStates, ^ToSql(format='DATE_TRUNC(\'WEEK\', CURRENT_DATE)%s', transform={p:String[*] | ''})),
dynaFnToSql('now', $allStates, ^ToSql(format='current_timestamp')),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ function meta::relational::functions::sqlQueryToString::sybaseASE::getDynaFuncti
dynaFnToSql('minute', $allStates, ^ToSql(format='minute(%s)')),
dynaFnToSql('mod', $allStates, ^ToSql(format='mod(%s,%s)')),
dynaFnToSql('month', $allStates, ^ToSql(format='month(%s)')),
dynaFnToSql('monthName', $allStates, ^ToSql(format='datename(MONTH, %s)')),
dynaFnToSql('monthNumber', $allStates, ^ToSql(format='month(%s)')),
dynaFnToSql('mostRecentDayOfWeek', $allStates, ^ToSql(format='dateadd(Day, case when %s - dow(%s) > 0 then %s - dow(%s) - 7 else %s - dow(%s) end, %s)', transform={p:String[1..2] | $p->formatMostRecentSybase('today()')}, parametersWithinWhenClause = [false, false])),
dynaFnToSql('now', $allStates, ^ToSql(format='now(%s)', transform={p:String[*] | ''})),
Expand Down
Loading

0 comments on commit 445a476

Please sign in to comment.