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 0c15c50e26e..abd47b01d01 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 @@ -242,11 +242,12 @@ function <> 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 ) ) ); @@ -312,7 +313,7 @@ function <> meta::external::query::sql::transformation::queryToP } -function <> 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 <> 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[1] { debug('processProjection', $context.debug); @@ -377,7 +378,7 @@ function <> 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 <> meta::external::query::sql::transformation::queryToPure::extractColumnNameFromExpression(expression:meta::external::query::sql::metamodel::Expression[1], selectItems: SelectItem[*], context: SqlTransformContext[1]):String[1] @@ -890,6 +891,7 @@ function <> meta::external::query::sql::transformation::queryToP function <> 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(), @@ -909,6 +911,7 @@ function <> 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_) 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 a2dbd301beb..51b191d6194 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 @@ -480,6 +480,26 @@ function <> meta::external::query::sql::transformation::queryToPure:: }) } +function <> 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 <> meta::external::query::sql::transformation::queryToPure::tests::testWhere():Boolean[1] {