diff --git a/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/fromPure.pure b/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/fromPure.pure index 63d9f56e820..c1c38849f61 100644 --- a/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/fromPure.pure +++ b/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/fromPure.pure @@ -297,6 +297,7 @@ function <> 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())), @@ -356,6 +357,7 @@ function <> 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()), @@ -613,6 +615,7 @@ function <> 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] | @@ -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_), @@ -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)); }), diff --git a/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/tests/testTranspile.pure b/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/tests/testTranspile.pure index 9f54538f7a2..926356cd1e2 100644 --- a/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/tests/testTranspile.pure +++ b/legend-engine-xts-sql/legend-engine-xt-sql-pure/src/main/resources/core_external_query_sql/binding/fromPure/tests/testTranspile.pure @@ -808,17 +808,23 @@ function <> meta::external::query::sql::transformation::queryToPure:: }, false) } -function <> meta::external::query::sql::transformation::queryToPure::tests::testGroupByAggWithinCase():Boolean[1] +function <> 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) }