This is an automated email from the ASF dual-hosted git repository.
Jackie-Jiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push:
new 7d140168189 [multistage] Resolve correlated UNNEST array reference by
field index (#20314)
7d140168189 is described below
commit 7d140168189272c501814998fdb42a55f5aac96a
Author: rohit <[email protected]>
AuthorDate: Tue Nov 24 15:05:45 2026 +0530
[multistage] Resolve correlated UNNEST array reference by field index
(#20314)
---
.../planner/logical/RelToPlanNodeConverter.java | 25 ++++++++
.../pinot/query/queries/UnnestSqlPlannerTest.java | 37 +++++++++++
.../resources/queries/UnnestOverAggregate.json | 72 ++++++++++++++++++++++
3 files changed, 134 insertions(+)
diff --git
a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToPlanNodeConverter.java
b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToPlanNodeConverter.java
index 43f4c2d4dfa..0558e1722ec 100644
---
a/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToPlanNodeConverter.java
+++
b/pinot-query-planner/src/main/java/org/apache/pinot/query/planner/logical/RelToPlanNodeConverter.java
@@ -57,6 +57,7 @@ import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexNode;
import org.apache.calcite.rex.RexWindowExclusion;
import org.apache.calcite.sql.type.SqlTypeName;
+import org.apache.calcite.sql.type.SqlTypeUtil;
import org.apache.pinot.calcite.rel.hint.PinotHintOptions;
import org.apache.pinot.calcite.rel.logical.PinotLogicalAggregate;
import org.apache.pinot.calcite.rel.logical.PinotLogicalExchange;
@@ -544,6 +545,13 @@ public final class RelToPlanNodeConverter {
if (expr instanceof RexFieldAccess) {
RexFieldAccess fieldAccess = (RexFieldAccess) expr;
if (fieldAccess.getReferenceExpr() instanceof RexCorrelVariable) {
+ // The field index refers to the correlation variable's row type,
which is the left input's row type. Prefer
+ // it over the name: the left input's field names need not match the
names used in the query, e.g. when the
+ // left input is an aggregate whose output fields are named like agg#0.
+ Integer idx = resolveCorrelFieldByIndex(fieldAccess, leftRowType);
+ if (idx != null) {
+ return idx;
+ }
String fieldName = fieldAccess.getField().getName();
List<RelDataTypeField> fields = leftRowType.getFieldList();
// SQL field names are case-insensitive by default in Calcite, so we
use equalsIgnoreCase for matching.
@@ -573,6 +581,23 @@ public final class RelToPlanNodeConverter {
return null;
}
+ /// Resolves a correlated field reference by position, as long as the
correlation variable's row type still lines
+ /// up with the left input's row type (same field count and the same field
type at that position). Returns null
+ /// otherwise, so that the caller can fall back to matching by name.
+ @Nullable
+ private static Integer resolveCorrelFieldByIndex(RexFieldAccess fieldAccess,
RelDataType leftRowType) {
+ RelDataType correlRowType = fieldAccess.getReferenceExpr().getType();
+ if (!correlRowType.isStruct() || correlRowType.getFieldCount() !=
leftRowType.getFieldCount()) {
+ return null;
+ }
+ int index = fieldAccess.getField().getIndex();
+ if (index < 0 || index >= leftRowType.getFieldCount()) {
+ return null;
+ }
+ RelDataType leftFieldType =
leftRowType.getFieldList().get(index).getType();
+ return SqlTypeUtil.equalSansNullability(leftFieldType,
fieldAccess.getField().getType()) ? index : null;
+ }
+
private ExchangeNode convertLogicalExchange(Exchange node) {
RelDistribution distribution = node.getDistribution();
RelDistribution.Type distributionType = distribution.getType();
diff --git
a/pinot-query-planner/src/test/java/org/apache/pinot/query/queries/UnnestSqlPlannerTest.java
b/pinot-query-planner/src/test/java/org/apache/pinot/query/queries/UnnestSqlPlannerTest.java
index 58888ff4635..3afababe775 100644
---
a/pinot-query-planner/src/test/java/org/apache/pinot/query/queries/UnnestSqlPlannerTest.java
+++
b/pinot-query-planner/src/test/java/org/apache/pinot/query/queries/UnnestSqlPlannerTest.java
@@ -316,6 +316,43 @@ public class UnnestSqlPlannerTest extends
QueryEnvironmentTestBase {
"Ordinality filter should never be pushed below the UNNEST operator");
}
+ @DataProvider(name = "unnestOverAggregateProvider")
+ public Object[][] provideUnnestOverAggregateQueries() {
+ return new Object[][]{
+ new Object[]{
+ "SELECT f.col1, u.v FROM (SELECT col1, ARRAY_AGG(col3, 'INT') AS r
FROM a GROUP BY col1) f "
+ + "CROSS JOIN UNNEST(f.r) AS u(v)",
+ false
+ },
+ new Object[]{
+ "SELECT f.col1, u.v, u.o FROM (SELECT col1, ARRAY_AGG(col3, 'INT')
AS r FROM a GROUP BY col1) f "
+ + "CROSS JOIN UNNEST(f.r) WITH ORDINALITY AS u(v, o) WHERE u.v
> 0",
+ true
+ },
+ new Object[]{
+ // The array alias (col1) is also the name of a source column
+ "SELECT f.k, u.v FROM (SELECT a.col1 AS k, ARRAY_AGG(col3, 'INT')
AS col1 FROM a GROUP BY a.col1) f "
+ + "CROSS JOIN UNNEST(f.col1) AS u(v)",
+ false
+ }
+ };
+ }
+
+ @Test(dataProvider = "unnestOverAggregateProvider")
+ public void testUnnestOverAggregateOutput(String sql, boolean
withOrdinality) {
+ // After optimization the correlate's left input is the aggregate, whose
output field is not named after the
+ // subquery alias (r). The correlated reference must still resolve to the
aggregate column (index 1).
+ DispatchableSubPlan subPlan = _queryEnvironment.planQuery(sql);
+ List<UnnestNode> unnestNodes = findUnnestNodes(subPlan);
+ Assert.assertEquals(unnestNodes.size(), 1, "Expected exactly one
UnnestNode for query: " + sql);
+ UnnestNode unnestNode = unnestNodes.get(0);
+ Assert.assertEquals(unnestNode.getArrayExprs().size(), 1);
+ RexExpression arrayExpr = unnestNode.getArrayExprs().get(0);
+ Assert.assertTrue(arrayExpr instanceof RexExpression.InputRef, "Expected
InputRef, got: " + arrayExpr);
+ Assert.assertEquals(((RexExpression.InputRef) arrayExpr).getIndex(), 1);
+ assertOrdinality(unnestNode, withOrdinality);
+ }
+
private static class AggregateFilterUnnestChain {
private final FilterNode _filterNode;
private final UnnestNode _unnestNode;
diff --git
a/pinot-query-runtime/src/test/resources/queries/UnnestOverAggregate.json
b/pinot-query-runtime/src/test/resources/queries/UnnestOverAggregate.json
new file mode 100644
index 00000000000..9fa3832f033
--- /dev/null
+++ b/pinot-query-runtime/src/test/resources/queries/UnnestOverAggregate.json
@@ -0,0 +1,72 @@
+{
+ "unnest_over_aggregate_test": {
+ "tables": {
+ "tbl": {
+ "schema": [
+ {
+ "name": "series",
+ "type": "STRING"
+ },
+ {
+ "name": "val",
+ "type": "INT"
+ }
+ ],
+ "inputs": [
+ [
+ "a",
+ 10
+ ],
+ [
+ "a",
+ 40
+ ],
+ [
+ "b",
+ 100
+ ]
+ ]
+ }
+ },
+ "queries": [
+ {
+ "description": "UNNEST of an aggregate output column of a subquery",
+ "sql": "SELECT f.series, u.v FROM (SELECT series, ARRAY_AGG(val,
'INT') AS r FROM {tbl} GROUP BY series) f CROSS JOIN UNNEST(f.r) AS u(v) ORDER
BY f.series, u.v",
+ "keepOutputRowOrder": true,
+ "outputs": [
+ [
+ "a",
+ 10
+ ],
+ [
+ "a",
+ 40
+ ],
+ [
+ "b",
+ 100
+ ]
+ ],
+ "ignoreLiteMode": true,
+ "ignoreV2Optimizer": true
+ },
+ {
+ "description": "UNNEST WITH ORDINALITY of an aggregate output column,
with a filter on the element",
+ "sql": "SELECT f.series, u.v FROM (SELECT series, ARRAY_AGG(val,
'INT') AS r FROM {tbl} GROUP BY series) f CROSS JOIN UNNEST(f.r) WITH
ORDINALITY AS u(v, o) WHERE u.v > 10 AND u.o >= 1 ORDER BY f.series, u.v",
+ "keepOutputRowOrder": true,
+ "outputs": [
+ [
+ "a",
+ 40
+ ],
+ [
+ "b",
+ 100
+ ]
+ ],
+ "ignoreLiteMode": true,
+ "ignoreV2Optimizer": true
+ }
+ ]
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]