Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/kumuwu/legend-engine into…
Browse files Browse the repository at this point in the history
… zhlizh-append-only-digest
  • Loading branch information
kumuwu committed Nov 16, 2023
2 parents d38f116 + 84c62a1 commit 9975d56
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@ public Handlers(PureModel pureModel)
register(m(h("meta::pure::mutation::save_T_MANY__RootGraphFetchTree_1__Mapping_1__Runtime_1__T_MANY_", false, ps -> res(ps.get(0)._genericType(), "zeroMany"), ps -> true)));

register("meta::pure::tds::extensions::firstNotNull_T_MANY__T_$0_1$_", false, ps -> res(ps.get(0)._genericType(), "zeroOne"));

register("meta::pure::functions::hash::hash_String_1__HashType_1__String_1_", true, ps -> res("String","one"));

// Extensions
CompileContext context = this.pureModel.getContext();
Expand Down Expand Up @@ -1889,6 +1891,7 @@ private Map<String, Dispatch> buildDispatch()
map.put("meta::pure::functions::date::weekOfYear_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::year_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::year_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::hash::hash_String_1__HashType_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()) || "HashType".equals(ps.get(1)._genericType()._rawType()._name())));
map.put("meta::pure::functions::lang::cast_Any_m__T_1__T_m_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(1)._multiplicity()));
map.put("meta::pure::functions::lang::compare_T_1__T_1__Integer_1_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && isOne(ps.get(1)._multiplicity()));
map.put("meta::pure::functions::lang::eval_Function_1__T_n__V_m_", (List<ValueSpecification> ps) -> ps.size() == 2 && isOne(ps.get(0)._multiplicity()) && ("Nil".equals(ps.get(0)._genericType()._rawType()._name()) || check(funcType(ps.get(0)._genericType()), (FunctionType ft) -> check(ft._parameters().toList(), (List<? extends VariableExpression> nps) -> nps.size() == 1))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ Class meta::legend::test::handlers::model::TestString
toString(){3->toString()}:String[1];
toUpper(){$this.string->toUpper()}:String[1];
trim(){$this.string->trim()}:String[1];

hashString(){$this.string->meta::pure::functions::hash::hash(meta::pure::functions::hash::HashType.MD5)}:String[1];
}

Class meta::legend::test::handlers::model::TestDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ function <<access.private>> meta::external::query::sql::transformation::queryToP
$expression->match([
a:ArithmeticExpression[1] | $a.left->extractAggregatesFromExpression()->concatenate($a.right->extractAggregatesFromExpression()),
b:BetweenPredicate[1] | $b.min->extractAggregatesFromExpression()->concatenate($b.value->extractAggregatesFromExpression())->concatenate($b.max->extractAggregatesFromExpression()),
c:Cast[1] | $c.expression->extractAggregatesFromExpression(),
c:ComparisonExpression[1] | $c.left->extractAggregatesFromExpression()->concatenate($c.right->extractAggregatesFromExpression()),
e:Extract[1] | $e.expression->extractAggregatesFromExpression(),
f:FunctionCall[1] | if (isExpressionAggregate($f, false, false), | $f, | $f.arguments->map(a | $a->extractAggregatesFromExpression())),
Expand Down Expand Up @@ -356,6 +357,7 @@ function <<access.private>> meta::external::query::sql::transformation::queryToP
let select = ^$originalSelect(selectItems = $selectItems);

let isAggregate = $groupBy->isNotEmpty() || anyColumnAggregate($select);

let isWindow = $windows->isNotEmpty();

let project = if ($standard->isNotEmpty() && !($isAggregate || $isWindow || $havingExtensions->isNotEmpty()),
Expand Down Expand Up @@ -613,6 +615,7 @@ function <<access.private>> meta::external::query::sql::transformation::queryToP
$e->match([
a:ArithmeticExpression[1] | $a.left->isExpressionAggregate($includeParameters, $includeWindow) || $a.right->isExpressionAggregate($includeParameters, $includeWindow),
b:BetweenPredicate[1] | $b.min->isExpressionAggregate($includeParameters, $includeWindow) || $b.value->isExpressionAggregate($includeParameters, $includeWindow) || $b.max->isExpressionAggregate($includeParameters, $includeWindow),
c:Cast[1] | $c.expression->isExpressionAggregate($includeParameters, $includeWindow),
c:ComparisonExpression[1] | $c.left->isExpressionAggregate($includeParameters, $includeWindow) || $c.right->isExpressionAggregate($includeParameters, $includeWindow),
e:Extract[1] | $e.expression->isExpressionAggregate($includeParameters, $includeWindow),
f:FunctionCall[1] |
Expand Down Expand Up @@ -2033,8 +2036,7 @@ function meta::external::query::sql::transformation::queryToPure::functionProces
}),
processor('date_part', Integer, {args, fc, ctx |
assertEquals(2, $args->size(), 'incorrect number of args for date_part');
let part = $args->at(0);
let value = $part->reactivate()->toOne()->cast(@String);
let part = $args->at(0)->reactivate()->toOne()->cast(@String);

let func = [
pair('year', year_Date_1__Integer_1_),
Expand All @@ -2048,7 +2050,7 @@ function meta::external::query::sql::transformation::queryToPure::functionProces
pair('minute', minute_Date_1__Integer_1_),
pair('second', second_Date_1__Integer_1_),
pair('epoch', toEpochValue_Date_1__Integer_1_)
]->getValue($value->toLower());
]->getValue($part->toLower());

nullOrSfe($func, $args->at(1));
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -808,17 +808,23 @@ function <<test.Test>> meta::external::query::sql::transformation::queryToPure::
}, false)
}

function <<test.Test>> meta::external::query::sql::transformation::queryToPure::tests::testGroupByAggWithinCase():Boolean[1]
function <<test.Test>> meta::external::query::sql::transformation::queryToPure::tests::testGroupByAggWithinFunc():Boolean[1]
{
test('SELECT CASE WHEN(SUM("Integer") <10) THEN max("Integer") ELSE min("Integer") END AS "MIN/MAX", ' +
'CASE WHEN(SUM("Integer") < 10) THEN \'LOW\' ELSE \'HIGH\' END AS "HIGH/LOW" FROM service."/service/service1"',
test('SELECT ' +
'CASE WHEN(SUM("Integer") <10) THEN max("Integer") ELSE min("Integer") END AS "MIN/MAX", ' +
'CASE WHEN(SUM("Integer") < 10) THEN \'LOW\' ELSE \'HIGH\' END AS "HIGH/LOW", ' +
'cast(sum("Integer") AS VARCHAR) AS "CAST", ' +
'floor(sum("Integer")) AS "FUNC" ' +
'FROM service."/service/service1"',

{| FlatInput.all()->project(
[ x | $x.booleanIn, x | $x.integerIn, x | $x.floatIn, x | $x.decimalIn, x | $x.strictDateIn, x | $x.dateTimeIn, x | $x.stringIn ],
[ 'Boolean', 'Integer', 'Float', 'Decimal', 'StrictDate', 'DateTime', 'String' ]
)->groupBy([], [
agg('MIN/MAX', row | $row.getInteger('Integer'), y | if ($y->sum() < 10, | max($y), | min($y))),
agg('HIGH/LOW', row | $row.getInteger('Integer'), y | if ($y->sum() < 10, | 'LOW', | 'HIGH'))
agg('HIGH/LOW', row | $row.getInteger('Integer'), y | if ($y->sum() < 10, | 'LOW', | 'HIGH')),
agg('CAST', row | $row.getInteger('Integer'), y | $y->sum()->toString()),
agg('FUNC', row | $row.getInteger('Integer'), y | floor($y->sum()))
])
}, false)
}
Expand Down

0 comments on commit 9975d56

Please sign in to comment.