Skip to content

Commit

Permalink
Legend SQL - fix orderby on extend column (#2426)
Browse files Browse the repository at this point in the history
  • Loading branch information
gs-jp1 authored Nov 1, 2023
1 parent 3c5083b commit 68b9a09
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,12 @@ function <<access.private>> meta::external::query::sql::transformation::queryToP
//need to subnest the context
let fromContext = if ($from.root || $from.name == $context.name, | $from, | ^$context(contexts = ^$from(contexts = []), expression = $from.expression));

let projection = $querySpec.select->processProjection($querySpec.groupBy, $querySpec.having, $querySpec.orderBy.sortKey, $querySpec.where->processWhere($fromContext));

let query = $querySpec.limit->processLimitOffset($querySpec.offset,
$querySpec.orderBy->processOrderBy($querySpec.select.selectItems,
$querySpec.having->processHaving($querySpec.select,
$querySpec.select->processProjection($querySpec.groupBy, $querySpec.having, $querySpec.orderBy.sortKey, $querySpec.where->processWhere($fromContext))
$querySpec.orderBy->processOrderBy($projection.second.selectItems,
$querySpec.having->processHaving($projection.second,
$projection.first
)
)
);
Expand Down Expand Up @@ -312,7 +313,7 @@ function <<access.private>> meta::external::query::sql::transformation::queryToP
}


function <<access.private>> meta::external::query::sql::transformation::queryToPure::processProjection(originalSelect: Select[1], groupBy: meta::external::query::sql::metamodel::Expression[*], having:meta::external::query::sql::metamodel::Expression[0..1], orderBy:meta::external::query::sql::metamodel::Expression[*], context: SqlTransformContext[1]): SqlTransformContext[1]
function <<access.private>> meta::external::query::sql::transformation::queryToPure::processProjection(originalSelect: Select[1], groupBy: meta::external::query::sql::metamodel::Expression[*], having:meta::external::query::sql::metamodel::Expression[0..1], orderBy:meta::external::query::sql::metamodel::Expression[*], context: SqlTransformContext[1]): Pair<SqlTransformContext, Select>[1]
{
debug('processProjection', $context.debug);

Expand Down Expand Up @@ -377,7 +378,7 @@ function <<access.private>> meta::external::query::sql::transformation::queryToP
| appendTdsFunc($olapGroupBy, distinct_TabularDataSet_1__TabularDataSet_1_, []),
| $olapGroupBy);

^$context(expression = $distinctExp, aliases = $aliases);
pair(^$context(expression = $distinctExp, aliases = $aliases), $select);
}

function <<access.private>> meta::external::query::sql::transformation::queryToPure::extractColumnNameFromExpression(expression:meta::external::query::sql::metamodel::Expression[1], selectItems: SelectItem[*], context: SqlTransformContext[1]):String[1]
Expand Down Expand Up @@ -890,6 +891,7 @@ function <<access.private>> meta::external::query::sql::transformation::queryToP
function <<access.private>> meta::external::query::sql::transformation::queryToPure::processOrderBy(sortItems: meta::external::query::sql::metamodel::SortItem[*], selectItems:SelectItem[*], context: SqlTransformContext[1]): SqlTransformContext[1]
{
debug('processOrderBy', $context.debug);

let sortInformation = $sortItems->map(si| createSortItemFunction($si, $selectItems, $context));

let newExp = if ($sortInformation->isEmpty(),
Expand All @@ -909,6 +911,7 @@ function <<access.private>> meta::external::query::sql::transformation::queryToP
assert($si.nullOrdering == SortItemNullOrdering.UNDEFINED, 'null ordering type not yet supported');

let column = extractColumnNameFromExpression($si.sortKey, $selectItems, $context);

let sortFunc = [
pair(SortItemOrdering.ASCENDING, asc_String_1__SortInformation_1_),
pair(SortItemOrdering.DESCENDING, desc_String_1__SortInformation_1_)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,26 @@ function <<test.Test>> meta::external::query::sql::transformation::queryToPure::
})
}

function <<test.Test>> meta::external::query::sql::transformation::queryToPure::tests::testOrderByWithExtendAlias():Boolean[1]
{
test(
'SELECT Calc AS Calc, Calc AS Calc2 FROM (select 1 AS Calc from service."/service/service1") GROUP BY 1 ORDER BY 1, Calc2 ASC',

{| 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' ])
->project(col(row:TDSRow[1] | 1, 'Calc'))
->extend([
col(row:TDSRow[1] | $row.getInteger('Calc'), 'Calc_1'),
col(row:TDSRow[1] | $row.getInteger('Calc'), 'Calc2')
])
->restrict(['Calc_1', 'Calc2'])
->distinct()
->sort([asc('Calc_1'), asc('Calc2')])
->renameColumns(pair('Calc_1', 'Calc'))
})
}

//WHERE
function <<test.Test>> meta::external::query::sql::transformation::queryToPure::tests::testWhere():Boolean[1]
{
Expand Down

0 comments on commit 68b9a09

Please sign in to comment.