You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@asterixdb.apache.org by AsterixDB Code Review <do...@asterix-gerrit.ics.uci.edu> on 2026/10/21 05:10:44 UTC

[M] Change in asterixdb[helios]: [NO ISSUE][COMP] Keep dead FROM variables out of window frame records

From Ali Alsuliman <al...@gmail.com>:

Ali Alsuliman has uploaded this change for review. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/22128?usp=email )


Change subject: [NO ISSUE][COMP] Keep dead FROM variables out of window frame records
......................................................................

[NO ISSUE][COMP] Keep dead FROM variables out of window frame records

- user model changes: no
- storage format changes: no
- interface changes: no

Details: After a GROUP BY, a window function's frame record is built
from the variables that are still in scope. The check was a lookup by
name, so a FROM variable of the block was kept whenever a WITH alias or
an enclosing query bound the same name, although the FROM variable
itself is dead above the GROUP BY. WITH inlining then substituted the
alias body into that record field, and the translator kept only the
body's output variable, leaving a reference that nothing produces: the
internal sanity check rejects the plan with "undefined used variables",
and without it the field is removed before anything reads it.

Keep a variable only when its name resolves to that very variable.

Generated-by: Claude Fable 5.1 <no...@anthropic.com>
Change-Id: Ia8cd866563cf314656dcd56b2f089d4a643e25a0
---
A asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.1.query.sqlpp
A asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.2.query.sqlpp
A asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.3.query.sqlpp
A asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.4.query.sqlpp
A asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.1.adm
A asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.2.adm
A asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.3.adm
A asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.4.adm
M asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml
M asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java
10 files changed, 116 insertions(+), 1 deletion(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/28/22128/1

diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.1.query.sqlpp
new file mode 100644
index 0000000..d318723
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.1.query.sqlpp
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*
+ * Description  : A FROM alias that is also the name of a WITH alias or of an
+ *                outer query variable, followed by GROUP BY and a window
+ *                function. The window frame record must not reference the
+ *                FROM variable, which is dead after the GROUP BY.
+ */
+
+WITH t AS (SELECT x.a FROM [{"a":1},{"a":2}] AS x)
+SELECT a, COUNT(*) OVER () AS c
+FROM t
+GROUP BY t.a
+ORDER BY a;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.2.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.2.query.sqlpp
new file mode 100644
index 0000000..8e3bb95
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.2.query.sqlpp
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+WITH t AS (SELECT x.a, x.b FROM [{"a":1,"b":10},{"a":1,"b":20},{"a":2,"b":30}] AS x)
+SELECT a, s, SUM(s) OVER () AS tot
+FROM t
+GROUP BY t.a
+LET s = SUM(t.b)
+ORDER BY a;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.3.query.sqlpp
new file mode 100644
index 0000000..50b14fb
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.3.query.sqlpp
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+SELECT (SELECT a, COUNT(*) OVER () AS c FROM [{"a":1},{"a":2}] AS r GROUP BY r.a ORDER BY a) AS inr
+FROM [{"x":1}] AS r;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.4.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.4.query.sqlpp
new file mode 100644
index 0000000..562936e
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/win_outer_alias_01/win_outer_alias_01.4.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+WITH t AS (SELECT x.a, x.b FROM [{"a":1,"b":10},{"a":1,"b":20},{"a":2,"b":30}] AS x)
+SELECT a, (SELECT VALUE y.row.b FROM g AS y) AS bs, SUM(t.b) AS s, COUNT(*) OVER () AS c
+FROM t
+GROUP BY t.a GROUP AS g(t AS row)
+ORDER BY a;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.1.adm
new file mode 100644
index 0000000..946c323
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.1.adm
@@ -0,0 +1,2 @@
+{ "a": 1, "c": 2 }
+{ "a": 2, "c": 2 }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.2.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.2.adm
new file mode 100644
index 0000000..ce4aeb2
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.2.adm
@@ -0,0 +1,2 @@
+{ "a": 1, "s": 30, "tot": 60 }
+{ "a": 2, "s": 30, "tot": 60 }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.3.adm
new file mode 100644
index 0000000..da94637
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.3.adm
@@ -0,0 +1 @@
+{ "inr": [ { "a": 1, "c": 2 }, { "a": 2, "c": 2 } ] }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.4.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.4.adm
new file mode 100644
index 0000000..8fd113f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/win_outer_alias_01/win_outer_alias_01.4.adm
@@ -0,0 +1,2 @@
+{ "a": 1, "bs": [ 10, 20 ], "s": 30, "c": 2 }
+{ "a": 2, "bs": [ 30 ], "s": 30, "c": 2 }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml b/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml
index b45705d..b4e7dc9 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/sqlpp_queries.xml
@@ -16851,6 +16851,11 @@
         <output-dir compare="Text">win_opt_02</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="window">
+      <compilation-unit name="win_outer_alias_01">
+        <output-dir compare="Text">win_outer_alias_01</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="column">
     <test-case FilePath="column">
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java
index 38419c8..2fc5bca 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java
@@ -154,7 +154,9 @@
 
     private void addToFieldList(List<Pair<Expression, Identifier>> outFieldList, List<VariableExpr> varList) {
         for (VariableExpr varExpr : varList) {
-            if (scopeChecker.lookupSymbol(varExpr.getVar().getValue()) != null) {
+            // Identity, not name: after GROUP BY a WITH or outer-query variable of the same name still resolves,
+            // and a name match would put the block's dead FROM/LET variable into the window record.
+            if (scopeChecker.lookupSymbol(varExpr.getVar().getValue()) == varExpr.getVar()) {
                 SqlppVariableUtil.addToFieldVariableList(varExpr, outFieldList);
             }
         }

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/22128?usp=email
To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: asterixdb
Gerrit-Branch: helios
Gerrit-Change-Id: Ia8cd866563cf314656dcd56b2f089d4a643e25a0
Gerrit-Change-Number: 22128
Gerrit-PatchSet: 1
Gerrit-Owner: Ali Alsuliman <al...@gmail.com>