This is an automated email from the ASF dual-hosted git repository.

guohongyu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 1beb5ff266 [CALCITE-6374] LatticeSuggester throw NullPointerException 
when agg call covered with cast
1beb5ff266 is described below

commit 1beb5ff26699fcbe6eef3da7c9fce654e170aedb
Author: YiwenWu <[email protected]>
AuthorDate: Thu Sep 12 21:38:04 2024 +0800

    [CALCITE-6374] LatticeSuggester throw NullPointerException when agg call 
covered with cast
---
 .../calcite/materialize/LatticeSuggester.java      |  7 ++++-
 .../calcite/materialize/LatticeSuggesterTest.java  | 30 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git 
a/core/src/main/java/org/apache/calcite/materialize/LatticeSuggester.java 
b/core/src/main/java/org/apache/calcite/materialize/LatticeSuggester.java
index 67045320fa..1edb9063c5 100644
--- a/core/src/main/java/org/apache/calcite/materialize/LatticeSuggester.java
+++ b/core/src/main/java/org/apache/calcite/materialize/LatticeSuggester.java
@@ -37,6 +37,7 @@ import org.apache.calcite.rex.RexInputRef;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.runtime.FlatLists;
 import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.validate.SqlValidatorUtil;
 import org.apache.calcite.tools.FrameworkConfig;
 import org.apache.calcite.util.CompositeList;
 import org.apache.calcite.util.ImmutableBitSet;
@@ -59,6 +60,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.IdentityHashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
@@ -435,10 +437,12 @@ public class LatticeSuggester {
       }
       final List<MutableMeasure> measures = new ArrayList<>();
       for (AggregateCall call : aggregate.getAggCallList()) {
+        String name =
+            SqlValidatorUtil.uniquify(call.name, q.usedNames, 
SqlValidatorUtil.ATTEMPT_SUGGESTER);
         measures.add(
             new MutableMeasure(call.getAggregation(), call.isDistinct(),
                 Util.<Integer, @Nullable ColRef>transform(call.getArgList(), 
h::column),
-                call.name));
+                name));
       }
       final int fieldCount = r.getRowType().getFieldCount();
       return new Frame(fieldCount, h.hops, measures, ImmutableList.of(h)) {
@@ -559,6 +563,7 @@ public class LatticeSuggester {
     final LatticeSpace space;
     final Map<Integer, TableRef> tableRefs = new HashMap<>();
     int stepRefCount = 0;
+    final Set<String> usedNames = new HashSet<>();
 
     Query(LatticeSpace space) {
       this.space = space;
diff --git 
a/core/src/test/java/org/apache/calcite/materialize/LatticeSuggesterTest.java 
b/core/src/test/java/org/apache/calcite/materialize/LatticeSuggesterTest.java
index db5af56417..02f26666bc 100644
--- 
a/core/src/test/java/org/apache/calcite/materialize/LatticeSuggesterTest.java
+++ 
b/core/src/test/java/org/apache/calcite/materialize/LatticeSuggesterTest.java
@@ -721,6 +721,36 @@ class LatticeSuggesterTest {
     checkFoodmartSimpleJoin(CalciteAssert.SchemaSpec.FAKE_FOODMART);
   }
 
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-6374";>[CALCITE-6374]
+   * LatticeSuggester throw NullPointerException when agg call covered with 
cast </a>. */
+  @Test void testCastAggrNameExpression() throws Exception {
+    final Tester t = new Tester().foodmart().withEvolve(true);
+    final String q0 = "select\n"
+        + "  \"num_children_at_home\" + 12 as \"n12\",\n"
+        + "  sum(\"num_children_at_home\" + 10) as \"n10\",\n"
+        + "  cast(sum(\"num_children_at_home\" + 11) as double) as \"n11\",\n"
+        + "  count(*) as c\n"
+        + "from \"customer\"\n"
+        + "group by \"num_children_at_home\" + 12";
+    final String l0 = "customer:[COUNT(), SUM(n10), SUM($f2)]";
+    t.addQuery(q0);
+    assertThat(t.s.latticeMap, aMapWithSize(1));
+    assertThat(Iterables.getOnlyElement(t.s.latticeMap.keySet()),
+        is(l0));
+    final Lattice lattice = Iterables.getOnlyElement(t.s.latticeMap.values());
+    final List<Lattice.DerivedColumn> derivedColumns = lattice.columns.stream()
+        .filter(c -> c instanceof Lattice.DerivedColumn)
+        .map(c -> (Lattice.DerivedColumn) c)
+        .collect(Collectors.toList());
+    assertThat(derivedColumns, hasSize(4));
+    final List<String> tables = ImmutableList.of("customer");
+    checkDerivedColumn(lattice, tables, derivedColumns, 0, "n10", true);
+    checkDerivedColumn(lattice, tables, derivedColumns, 1, "$f2", true);
+    checkDerivedColumn(lattice, tables, derivedColumns, 2, "n12", false);
+    checkDerivedColumn(lattice, tables, derivedColumns, 3, "n11", false);
+  }
+
   private void checkFoodmartSimpleJoin(CalciteAssert.SchemaSpec schemaSpec)
       throws Exception {
     final FrameworkConfig config = Frameworks.newConfigBuilder()

Reply via email to