diff --git a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/pureToSQLQuery/pureToSQLQuery.pure b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/pureToSQLQuery/pureToSQLQuery.pure index 275924eb368..d6cced92bd4 100644 --- a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/pureToSQLQuery/pureToSQLQuery.pure +++ b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/pureToSQLQuery/pureToSQLQuery.pure @@ -4855,6 +4855,14 @@ function meta::relational::functions::pureToSqlQuery::processGroupBy(expression: ); } +// TODO: since we use SELECT * when processing PIVOT (effectively always force pivot to be part of a subselect in subsequent processing) +// we perform the following check to see if the result columns can be dynamic. The following check is still pretty naive +// it should (in theory) walk the chain of SELECT * (i.e. $select.columns->isEmpty()) and determine if the source of this is from a pivot() +function meta::relational::functions::pureToSqlQuery::isResultColumnsDynamic(select:SelectSQLQuery[1]):Boolean[1] +{ + !$select.pivot->isEmpty() || $select.columns->isEmpty(); +} + function meta::relational::functions::pureToSqlQuery::processPivot(expression:FunctionExpression[1], currentPropertyMapping:PropertyMapping[*], operation:SelectWithCursor[1], vars:Map[1], state:State[1], joinType:JoinType[1], nodeId:String[1], aggFromMap:List[1], context:DebugContext[1], extensions:Extension[*]):RelationalOperationElement[1] { let mainQuery = processValueSpecification($expression.parametersValues->at(0), [], $operation, $vars, $state, JoinType.LEFT_OUTER, $nodeId, $aggFromMap, $context, $extensions)->toOne()->cast(@SelectWithCursor); diff --git a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/relationalMappingExecution.pure b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/relationalMappingExecution.pure index 291e272264c..af282b13b1c 100644 --- a/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/relationalMappingExecution.pure +++ b/legend-engine-xts-relationalStore/legend-engine-xt-relationalStore-generation/legend-engine-xt-relationalStore-pure/src/main/resources/core_relational/relational/relationalMappingExecution.pure @@ -183,7 +183,7 @@ function meta::relational::mapping::generateSQLExecutionNode(query:SQLQuery[1], metadata = ^meta::relational::mapping::TableInfo(info = $query->meta::relational::milestoning::getQueryTableAliases()->map(a|$a.relationalElement)->cast(@Table)->removeDuplicatesBy(t|$t.schema.name+'.'+$t.name)->map(tab| ^meta::relational::mapping::TableIdentifier(schema=$tab.schema.name, table=$tab.name))), // the only case that we yield result with dynamic columns is when pivot() is used and no casting is done afterwards, resulting // in a select statement with no columns being specified - isResultColumnsDynamic = $query->match([sel:SelectSQLQuery[1] | if(!$sel.pivot->isEmpty() && $sel.columns->isEmpty(), |true,|[]), a:Any[1] | []]) + isResultColumnsDynamic = $query->match([sel:SelectSQLQuery[1] | if(isResultColumnsDynamic($sel), |true,|[]), a:Any[1] | []]) ); }