Skip to content

Commit

Permalink
Legend SQL - handle cast of aggregate expressions (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-jp1 authored Nov 14, 2023
1 parent 9aa0a75 commit 07e7129
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
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 07e7129

Please sign in to comment.