diff --git a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/postprocessor/postProcessor.pure b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/postprocessor/postProcessor.pure index 062e6d4c8d9..a4d6287170b 100644 --- a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/postprocessor/postProcessor.pure +++ b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/postprocessor/postProcessor.pure @@ -135,47 +135,49 @@ function meta::relational::postProcessor::transform(r:RelationalOperationElement c:CommonTableExpression[1] | let transformedSql= $c.sqlQuery->transform($f, $transformed); pair(^$c(sqlQuery=$transformedSql.first->cast(@SQLQuery)), $transformedSql.second);, - s:SelectSQLQuery[1] | let transformedData = $s.data->toOne()->transform($f, $transformed); - let transformedColumns = $s.columns->fold({c, agg | - let colTransform = $c->transform($f, $agg.second); - pair(list($agg.first.values->add($colTransform.first)), $colTransform.second); - }, pair(^List(), $transformedData.second)); - let transformedFilterOp = $s.filteringOperation->fold({fo, agg | - let foTransform = $fo->transform($f, $agg.second); - pair(list($agg.first.values->add($foTransform.first)), $foTransform.second); - }, pair(^List(), $transformedColumns.second)); - let transformedGroupBy = $s.groupBy->fold({gp, agg | - let gpTransform = $gp->transform($f, $agg.second); - pair(list($agg.first.values->add($gpTransform.first)), $gpTransform.second); - }, pair(^List(), $transformedFilterOp.second)); - let transformedHavingOp = $s.havingOperation->fold({ho, agg | - let hoTransform = $ho->transform($f, $agg.second); - pair(list($agg.first.values->add($hoTransform.first)), $hoTransform.second); - }, pair(^List(), $transformedGroupBy.second)); - let transformedOrderBy = $s.orderBy->fold({ob, agg | - let obTransform = $ob.column->transform($f, $agg.second); - pair(list($agg.first.values->add(^$ob(column=$obTransform.first))), $obTransform.second); - }, pair(^List(), $transformedHavingOp.second)); - let newS = ^$s( data=$transformedData.first->cast(@RootJoinTreeNode), - columns=$transformedColumns.first.values, - filteringOperation=$transformedFilterOp.first.values, - groupBy=$transformedGroupBy.first.values, - havingOperation=$transformedHavingOp.first.values, - orderBy=$transformedOrderBy.first.values - ); - let newSWithLeftSideModified = if(!$newS.leftSideOfFilter->isEmpty(),| ^$newS(leftSideOfFilter=$s.leftSideOfFilter->toOne()->meta::relational::functions::pureToSqlQuery::findOneNode($s.data->toOne(), $newS.data->toOne())),|$newS); - if($newSWithLeftSideModified->instanceOf(SelectSQLQueryWithCommonTableExpressions), - | - let selectWithCte = $newSWithLeftSideModified->cast(@SelectSQLQueryWithCommonTableExpressions); - let transformedCommonTableExpressions = $selectWithCte.commonTableExpressions->fold({cte, agg | - let cteTransform = $cte->transform($f, $agg.second); - pair(list($agg.first.values->add($cteTransform.first->cast(@CommonTableExpression))), $cteTransform.second); - }, pair(^List(), $transformedOrderBy.second)); - let newSelectWithCte = ^$selectWithCte(commonTableExpressions = $transformedCommonTableExpressions.first.values); - pair($newSelectWithCte, $transformedCommonTableExpressions.second);, - | - pair($newSWithLeftSideModified, $transformedOrderBy.second); - );, + s:SelectSQLQuery[1] | if($s.data->isNotEmpty(), + | let transformedData = $s.data->toOne()->transform($f, $transformed); + let transformedColumns = $s.columns->fold({c, agg | + let colTransform = $c->transform($f, $agg.second); + pair(list($agg.first.values->add($colTransform.first)), $colTransform.second); + }, pair(^List(), $transformedData.second)); + let transformedFilterOp = $s.filteringOperation->fold({fo, agg | + let foTransform = $fo->transform($f, $agg.second); + pair(list($agg.first.values->add($foTransform.first)), $foTransform.second); + }, pair(^List(), $transformedColumns.second)); + let transformedGroupBy = $s.groupBy->fold({gp, agg | + let gpTransform = $gp->transform($f, $agg.second); + pair(list($agg.first.values->add($gpTransform.first)), $gpTransform.second); + }, pair(^List(), $transformedFilterOp.second)); + let transformedHavingOp = $s.havingOperation->fold({ho, agg | + let hoTransform = $ho->transform($f, $agg.second); + pair(list($agg.first.values->add($hoTransform.first)), $hoTransform.second); + }, pair(^List(), $transformedGroupBy.second)); + let transformedOrderBy = $s.orderBy->fold({ob, agg | + let obTransform = $ob.column->transform($f, $agg.second); + pair(list($agg.first.values->add(^$ob(column=$obTransform.first))), $obTransform.second); + }, pair(^List(), $transformedHavingOp.second)); + let newS = ^$s( data=$transformedData.first->cast(@RootJoinTreeNode), + columns=$transformedColumns.first.values, + filteringOperation=$transformedFilterOp.first.values, + groupBy=$transformedGroupBy.first.values, + havingOperation=$transformedHavingOp.first.values, + orderBy=$transformedOrderBy.first.values + ); + let newSWithLeftSideModified = if(!$newS.leftSideOfFilter->isEmpty(),| ^$newS(leftSideOfFilter=$s.leftSideOfFilter->toOne()->meta::relational::functions::pureToSqlQuery::findOneNode($s.data->toOne(), $newS.data->toOne())),|$newS); + if($newSWithLeftSideModified->instanceOf(SelectSQLQueryWithCommonTableExpressions), + | + let selectWithCte = $newSWithLeftSideModified->cast(@SelectSQLQueryWithCommonTableExpressions); + let transformedCommonTableExpressions = $selectWithCte.commonTableExpressions->fold({cte, agg | + let cteTransform = $cte->transform($f, $agg.second); + pair(list($agg.first.values->add($cteTransform.first->cast(@CommonTableExpression))), $cteTransform.second); + }, pair(^List(), $transformedOrderBy.second)); + let newSelectWithCte = ^$selectWithCte(commonTableExpressions = $transformedCommonTableExpressions.first.values); + pair($newSelectWithCte, $transformedCommonTableExpressions.second);, + | + pair($newSWithLeftSideModified, $transformedOrderBy.second); + );, + |pair($s, $transformed));, v:ViewSelectSQLQuery[1] | let transformedSql = $v.selectSQLQuery->transform($f, $transformed); pair(^$v(selectSQLQuery=$transformedSql.first->cast(@SelectSQLQuery)), $transformedSql.second);,