Skip to content

Commit

Permalink
Legend SQL - assortment of fixes (#2473)
Browse files Browse the repository at this point in the history
1. do not possibly cast all function parameters by default
2. further alias adjustments
3. handle in filter using subbselect column 
4. add greatest/least
  • Loading branch information
gs-jp1 authored Nov 22, 2023
1 parent e3c6c78 commit 85fef69
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 127 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ public Handlers(PureModel pureModel)
register(h("meta::pure::tds::concatenate_TabularDataSet_1__TabularDataSet_1__TabularDataSet_1_", false, ps -> res("meta::pure::tds::TabularDataSet", "one"), ps -> "TabularDataSet".equals(ps.get(0)._genericType()._rawType()._name())),
h("meta::pure::functions::collection::concatenate_T_MANY__T_MANY__T_MANY_", true, ps -> res(ps.get(0)._genericType(), "zeroMany"), ps -> true));

register(m(h("meta::pure::functions::collection::greatest_X_$1_MANY$__X_1_", false, ps -> res(ps.get(0)._genericType(), "one"), ps -> matchOneMany(ps.get(0)._multiplicity())),
h("meta::pure::functions::collection::greatest_X_MANY__X_$0_1$_", false, ps -> res(ps.get(0)._genericType(), "zeroOne"), ps -> true)));

register(m(h("meta::pure::functions::collection::least_X_$1_MANY$__X_1_", false, ps -> res(ps.get(0)._genericType(), "one"), ps -> matchOneMany(ps.get(0)._multiplicity())),
h("meta::pure::functions::collection::least_X_MANY__X_$0_1$_", false, ps -> res(ps.get(0)._genericType(), "zeroOne"), ps -> true)));

register("meta::pure::functions::collection::first_T_MANY__T_$0_1$_", true, ps -> res(ps.get(0)._genericType(), "zeroOne"));
register("meta::pure::functions::collection::last_T_MANY__T_$0_1$_", true, ps -> res(ps.get(0)._genericType(), "zeroOne"));
Expand Down Expand Up @@ -1872,6 +1877,10 @@ private Map<String, Dispatch> buildDispatch()
map.put("meta::pure::functions::date::min_Date_MANY__Date_$0_1$_", (List<ValueSpecification> ps) -> ps.size() == 1 && Sets.immutable.with("Nil", "Date", "StrictDate", "DateTime", "LatestDate").contains(ps.get(0)._genericType()._rawType()._name()));
map.put("meta::pure::functions::date::min_StrictDate_1__StrictDate_1__StrictDate_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "StrictDate".equals(ps.get(0)._genericType()._rawType()._name())) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "StrictDate".equals(ps.get(1)._genericType()._rawType()._name())));
map.put("meta::pure::functions::date::min_StrictDate_MANY__StrictDate_$0_1$_", (List<ValueSpecification> ps) -> ps.size() == 1 && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "StrictDate".equals(ps.get(0)._genericType()._rawType()._name())));
map.put("meta::pure::functions::collection::greatest_X_$1_MANY$__X_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && matchOneMany(ps.get(0)._multiplicity()));
map.put("meta::pure::functions::collection::greatest_X_MANY__X_$0_1$_", (List<ValueSpecification> ps) -> ps.size() == 1);
map.put("meta::pure::functions::collection::least_X_$1_MANY$__X_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && matchOneMany(ps.get(0)._multiplicity()));
map.put("meta::pure::functions::collection::least_X_MANY__X_$0_1$_", (List<ValueSpecification> ps) -> ps.size() == 1);
map.put("meta::pure::functions::date::minute_Date_1__Integer_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && isOne(ps.get(0)._multiplicity()) && Sets.immutable.with("Nil", "Date", "StrictDate", "DateTime", "LatestDate").contains(ps.get(0)._genericType()._rawType()._name()));
map.put("meta::pure::functions::date::monthNumber_Date_$0_1$__Integer_$0_1$_", (List<ValueSpecification> ps) -> ps.size() == 1 && matchZeroOne(ps.get(0)._multiplicity()) && Sets.immutable.with("Nil", "Date", "StrictDate", "DateTime", "LatestDate").contains(ps.get(0)._genericType()._rawType()._name()));
map.put("meta::pure::functions::date::monthNumber_Date_1__Integer_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && isOne(ps.get(0)._multiplicity()) && Sets.immutable.with("Nil", "Date", "StrictDate", "DateTime", "LatestDate").contains(ps.get(0)._genericType()._rawType()._name()));
Expand Down Expand Up @@ -1927,8 +1936,8 @@ private Map<String, Dispatch> buildDispatch()
map.put("meta::pure::functions::math::log_Number_1__Float_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && isOne(ps.get(0)._multiplicity()) && Sets.immutable.with("Nil", "Number", "Integer", "Float", "Decimal").contains(ps.get(0)._genericType()._rawType()._name()));
map.put("meta::pure::functions::math::log10_Number_1__Float_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && isOne(ps.get(0)._multiplicity()) && Sets.immutable.with("Nil", "Number", "Integer", "Float", "Decimal").contains(ps.get(0)._genericType()._rawType()._name()));
map.put("meta::pure::functions::math::max_Float_$1_MANY$__Float_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && matchOneMany(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "Float".equals(ps.get(0)._genericType()._rawType()._name())));
map.put("meta::pure::functions::math::max_Float_1__Float_1__Float_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "Float".equals(ps.get(0)._genericType()._rawType()._name())) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "Float".equals(ps.get(1)._genericType()._rawType()._name())));
map.put("meta::pure::functions::math::max_Float_MANY__Float_$0_1$_", (List<ValueSpecification> ps) -> ps.size() == 1 && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "Float".equals(ps.get(0)._genericType()._rawType()._name())));
map.put("meta::pure::functions::math::max_Float_1__Float_1__Float_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "Float".equals(ps.get(0)._genericType()._rawType()._name())) && isOne(ps.get(1)._multiplicity()) && ("Nil".equals(ps.get(1)._genericType()._rawType()._name()) || "Float".equals(ps.get(1)._genericType()._rawType()._name())));
map.put("meta::pure::functions::math::max_Integer_$1_MANY$__Integer_1_", (List<ValueSpecification> ps) -> ps.size() == 1 && matchOneMany(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "Integer".equals(ps.get(0)._genericType()._rawType()._name())));
map.put("meta::pure::functions::math::max_Integer_1__Integer_1__Integer_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "Integer".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::math::max_Integer_MANY__Integer_$0_1$_", (List<ValueSpecification> ps) -> ps.size() == 1 && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || "Integer".equals(ps.get(0)._genericType()._rawType()._name())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ Class meta::legend::test::handlers::model::TestMaxMin

minDateTime() {$this.dateTimes->min()}:DateTime[0..1];
minTwoDateTime() {$this.dateTime->min($this.dateTime)}:DateTime[1];

greatest() {$this.numbers->greatest()}:Number[0..1];
greatestMinOne() {$this.numbersMinOne->greatest()}:Number[1];
least() {$this.numbers->least()}:Number[0..1];
leastMinOne() {$this.numbersMinOne->least()}:Number[1];
}

Class meta::legend::test::handlers::model::TestAlgebra
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,36 @@ function meta::pure::functions::collection::uniqueValueOnly<T>(values : T[*], de
);
}

function meta::pure::functions::collection::greatest<X>(values : X[*]):X[0..1]
{
max($values);
}

function meta::pure::functions::collection::least<X>(values : X[*]):X[0..1]
{
min($values);
}

function meta::pure::functions::collection::greatest<X>(values : X[1..*]):X[1]
{
max($values);
}

function meta::pure::functions::collection::least<X>(values : X[1..*]):X[1]
{
min($values);
}

function meta::pure::functions::collection::max<X>(values : X[1..*]):X[1]
{
$values->sort()->last()->toOne();
}

function meta::pure::functions::collection::min<X>(values : X[1..*]):X[1]
{
$values->sort()->first()->toOne();
}

function meta::pure::functions::collection::max<X>(values : X[*]):X[0..1]
{
$values->sort()->last();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ function meta::pure::router::routing::isGetAllFunction(f:Function<Any>[1]):Boole

function meta::pure::router::routing::shouldStop(f:Function<Any>[1], extensions:meta::pure::extension::Extension[*]):Boolean[1]
{
shouldStopFunctions($extensions)->contains($f);
shouldStopFunctions($extensions)->contains($f);
}

function meta::pure::router::routing::shouldStopFunctions(extensions:meta::pure::extension::Extension[*]):Function<Any>[*]
Expand All @@ -620,6 +620,8 @@ function meta::pure::router::routing::shouldStopFunctions(extensions:meta::pure:
max_Float_$1_MANY$__Float_1_,
max_Number_MANY__Number_$0_1$_,
max_Number_$1_MANY$__Number_1_,
greatest_X_MANY__X_$0_1$_,
greatest_X_$1_MANY$__X_1_,
getAllForEachDate_Class_1__Date_MANY__T_MANY_,
greaterThan_Number_1__Number_1__Boolean_1_,
greaterThan_String_1__String_1__Boolean_1_,
Expand Down Expand Up @@ -683,6 +685,8 @@ function meta::pure::router::routing::shouldStopFunctions(extensions:meta::pure:
min_Date_MANY__Date_$0_1$_,
min_StrictDate_MANY__StrictDate_$0_1$_,
min_DateTime_MANY__DateTime_$0_1$_,
least_X_MANY__X_$0_1$_,
least_X_$1_MANY$__X_1_,
variancePopulation_Number_MANY__Number_1_,
varianceSample_Number_MANY__Number_1_,
makeString_Any_MANY__String_1__String_1_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ function meta::pure::executionPlan::platformBinding::legendJava::library::collec
fc2(min_T_$1_MANY$__Function_1__T_1_, {ctx,collection,comp | if( $collection.type->isJavaList(), | minComp($ctx, $collection, $comp, $library), | $collection )}),
fc1(max_X_MANY__X_$0_1$_, {ctx,collection | if( $collection.type->isJavaList(), | max($ctx, $collection, $library), | $collection )}),
fc1(min_X_MANY__X_$0_1$_, {ctx,collection | if( $collection.type->isJavaList(), | min($ctx, $collection, $library), | $collection )}),
fc(max_X_$1_MANY$__X_1_, fcAlias( max_X_MANY__X_$0_1$_)),
fc(min_X_$1_MANY$__X_1_, fcAlias( min_X_MANY__X_$0_1$_)),

fc(greatest_X_MANY__X_$0_1$_, fcAlias( max_X_MANY__X_$0_1$_)),
fc(least_X_MANY__X_$0_1$_, fcAlias( min_X_MANY__X_$0_1$_)),
fc(greatest_X_$1_MANY$__X_1_, fcAlias( max_X_MANY__X_$0_1$_)),
fc(least_X_$1_MANY$__X_1_, fcAlias( min_X_MANY__X_$0_1$_)),

fc1(range_Integer_1__Integer_MANY_, {ctx,stop | javaLongStream()->j_invoke('range', [j_long(0), $stop])->j_invoke('boxed', [])}),
fc2(range_Integer_1__Integer_1__Integer_MANY_, {ctx,start,stop | javaLongStream()->j_invoke('range', [$start, $stop])->j_invoke('boxed', [])}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function meta::relational::functions::sqlQueryToString::sybaseASE::getDynaFuncti
dynaFnToSql('firstDayOfThisYear', $allStates, ^ToSql(format='dateadd(DAY, -(datepart(dayofyear, today()) - 1), today())%s', transform={p:String[*] | ''})),
dynaFnToSql('firstDayOfWeek', $allStates, ^ToSql(format='dateadd(DAY, -(mod(datepart(weekday, %s)+5, 7)), %s)', transform={p:String[1] | $p->repeat(2)})),
dynaFnToSql('firstDayOfYear', $allStates, ^ToSql(format='dateadd(DAY, -(datepart(dayofyear, %s) - 1), %s)', transform={p:String[1] | $p->repeat(2)})),
dynaFnToSql('greatest', $allStates, ^ToSql(format='%s', transform={p:String[*] | convertGreatestLeastToCaseStatement('>=', $p)})),
dynaFnToSql('hour', $allStates, ^ToSql(format='hour(%s)')),
dynaFnToSql('indexOf', $allStates, ^ToSql(format='LOCATE(%s)', transform={p:String[2] | $p->at(0) + ', ' + $p->at(1)})),
dynaFnToSql('isEmpty', $selectOutsideWhen, ^ToSql(format='case when (%s is null) then \'true\' else \'false\' end', parametersWithinWhenClause=true)),
Expand All @@ -117,6 +118,7 @@ function meta::relational::functions::sqlQueryToString::sybaseASE::getDynaFuncti
dynaFnToSql('isNull', $notSelectOutsideWhen, ^ToSql(format='%s is null')),
dynaFnToSql('isNumeric', $allStates, ^ToSql(format='isnumeric(%s)')),
dynaFnToSql('joinStrings', $allStates, ^ToSql(format='list(%s,%s)')),
dynaFnToSql('least', $allStates, ^ToSql(format='%s', transform={p:String[*] | convertGreatestLeastToCaseStatement('<=', $p)})),
dynaFnToSql('left', $allStates, ^ToSql(format='left(%s,%s)')),
dynaFnToSql('length', $allStates, ^ToSql(format='char_length(%s)')),
dynaFnToSql('matches', $allStates, ^ToSql(format=regexpPattern('%s'), transform={p:String[2]|$p->transformRegexpParams()})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ function meta::relational::functions::sqlQueryToString::sybaseIQ::getDynaFunctio
dynaFnToSql('firstMillisecondOfSecond', $allStates, ^ToSql(format='dateadd(microsecond, -(datepart(microsecond, %s)), %s)', transform={p:String[1] | $p->repeat(2)})),
dynaFnToSql('firstMinuteOfHour', $allStates, ^ToSql(format='dateadd(hour, datepart(hour, %s), date(%s))', transform={p:String[1] | $p->repeat(2)})),
dynaFnToSql('firstSecondOfMinute', $allStates, ^ToSql(format='dateadd(minute, datepart(minute, %s), dateadd(hour, datepart(hour, %s), date(%s)))', transform={p:String[1] | $p->repeat(3)})),
dynaFnToSql('greatest', $allStates, ^ToSql(format='%s', transform={p:String[*] | convertGreatestLeastToCaseStatement('>=', $p)})),
dynaFnToSql('hour', $allStates, ^ToSql(format='hour(%s)')),
dynaFnToSql('indexOf', $allStates, ^ToSql(format='LOCATE(%s)', transform={p:String[2] | $p->at(0) + ', ' + $p->at(1)})),
dynaFnToSql('isEmpty', $selectOutsideWhen, ^ToSql(format='case when (%s is null) then \'true\' else \'false\' end', parametersWithinWhenClause=true)),
Expand All @@ -114,6 +115,7 @@ function meta::relational::functions::sqlQueryToString::sybaseIQ::getDynaFunctio
dynaFnToSql('isNull', $notSelectOutsideWhen, ^ToSql(format='%s is null')),
dynaFnToSql('isNumeric', $allStates, ^ToSql(format='isnumeric(%s)')),
dynaFnToSql('joinStrings', $allStates, ^ToSql(format='list(%s,%s)')),
dynaFnToSql('least', $allStates, ^ToSql(format='%s', transform={p:String[*] | convertGreatestLeastToCaseStatement('<=', $p)})),
dynaFnToSql('left', $allStates, ^ToSql(format='left(%s,%s)')),
dynaFnToSql('length', $allStates, ^ToSql(format='char_length(%s)')),
dynaFnToSql('matches', $allStates, ^ToSql(format=regexpPattern('%s'), transform={p:String[2]|$p->transformRegexpParams()})),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ function <<test.Test>> meta::relational::tests::query::function::sybaseIQ::testD
let s = toSQLString($fn, simpleRelationalMapping, meta::relational::runtime::DatabaseType.SybaseIQ, meta::relational::extension::relationalExtensions());
assertEquals('select datename(WEEKDAY,"root".tradeDate) as "WeekDay Name" from tradeTable as "root"',$s);
}

function <<test.Test>> meta::relational::tests::query::function::sybaseIQ::testGreatestLeast():Boolean[1]
{
let fn = {|Trade.all()
->project([
col(t| greatest([$t.quantity, 1, 3]), 'greatest'),
col(t| greatest([]->cast(@String)), 'greatest_empty'),
col(t| least([$t.quantity, 1, 3]), 'least'),
col(t| least([]->cast(@String)), 'least_empty')
])};

let s = toSQLString($fn, simpleRelationalMapping, meta::relational::runtime::DatabaseType.SybaseIQ, meta::relational::extension::relationalExtensions());
assertEquals('select case when "root".quantity >= "root".quantity and "root".quantity >= 1 and "root".quantity >= 3 then "root".quantity when 1 >= "root".quantity and 1 >= 1 and 1 >= 3 then 1 when 3 >= "root".quantity and 3 >= 1 and 3 >= 3 then 3 else null end as "greatest", ' +
'null as "greatest_empty", ' +
'case when "root".quantity <= "root".quantity and "root".quantity <= 1 and "root".quantity <= 3 then "root".quantity when 1 <= "root".quantity and 1 <= 1 and 1 <= 3 then 1 when 3 <= "root".quantity and 3 <= 1 and 3 <= 3 then 3 else null end as "least", ' +
'null as "least_empty" from tradeTable as "root"',$s);
}
Loading

0 comments on commit 85fef69

Please sign in to comment.