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

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


The following commit(s) were added to refs/heads/master by this push:
     new d0a06f7  [CALCITE-3061] Query with WITH clause fails when alias is the 
same as the table with rolled up column
d0a06f7 is described below

commit d0a06f78644906c2ca73e6fb5ea08f7d17f55dab
Author: Volodymyr Vysotskyi <[email protected]>
AuthorDate: Fri May 10 19:16:34 2019 +0300

    [CALCITE-3061] Query with WITH clause fails when alias is the same as the 
table with rolled up column
    
    Close #1206
---
 .../calcite/sql/validate/SqlValidatorImpl.java     | 89 ++++++----------------
 .../org/apache/calcite/test/SqlValidatorTest.java  | 12 +++
 .../calcite/test/catalog/MockCatalogReader.java    | 11 +++
 3 files changed, 46 insertions(+), 66 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java 
b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
index f293dea..e2a2e59 100644
--- a/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
+++ b/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
@@ -17,7 +17,6 @@
 package org.apache.calcite.sql.validate;
 
 import org.apache.calcite.config.NullCollation;
-import org.apache.calcite.jdbc.CalciteSchema;
 import org.apache.calcite.linq4j.Ord;
 import org.apache.calcite.linq4j.function.Function2;
 import org.apache.calcite.linq4j.function.Functions;
@@ -3128,17 +3127,19 @@ public class SqlValidatorImpl implements 
SqlValidatorWithHints {
     validateQuery(call, scope, targetRowType);
   }
 
-  private void checkRollUpInUsing(SqlIdentifier identifier, SqlNode 
leftOrRight) {
-    leftOrRight = stripAs(leftOrRight);
-    // if it's not a SqlIdentifier then that's fine, it'll be validated 
somewhere else.
-    if (leftOrRight instanceof SqlIdentifier) {
-      SqlIdentifier from = (SqlIdentifier) leftOrRight;
-      Table table = findTable(catalogReader.getRootSchema(),
-          Util.last(from.names));
-      String name = Util.last(identifier.names);
+  private void checkRollUpInUsing(SqlIdentifier identifier,
+      SqlNode leftOrRight, SqlValidatorScope scope) {
+    SqlValidatorNamespace namespace = getNamespace(leftOrRight, scope);
+    if (namespace != null) {
+      SqlValidatorTable sqlValidatorTable = namespace.getTable();
+      if (sqlValidatorTable != null) {
+        Table table = sqlValidatorTable.unwrap(Table.class);
+        String column = Util.last(identifier.names);
 
-      if (table != null && table.isRolledUp(name)) {
-        throw newValidationError(identifier, RESOURCE.rolledUpNotAllowed(name, 
"USING"));
+        if (table.isRolledUp(column)) {
+          throw newValidationError(identifier,
+              RESOURCE.rolledUpNotAllowed(column, "USING"));
+        }
       }
     }
   }
@@ -3181,8 +3182,8 @@ public class SqlValidatorImpl implements 
SqlValidatorWithHints {
               RESOURCE.naturalOrUsingColumnNotCompatible(id.getSimple(),
                   leftColType.toString(), rightColType.toString()));
         }
-        checkRollUpInUsing(id, left);
-        checkRollUpInUsing(id, right);
+        checkRollUpInUsing(id, left, scope);
+        checkRollUpInUsing(id, right, scope);
       }
       break;
     default:
@@ -3516,13 +3517,14 @@ public class SqlValidatorImpl implements 
SqlValidatorWithHints {
       return true;
     }
 
-    String tableAlias = pair.left;
     String columnName = pair.right;
 
-    Table table = findTable(tableAlias);
-    if (table != null) {
+    SqlValidatorTable sqlValidatorTable =
+        scope.fullyQualify(identifier).namespace.getTable();
+    if (sqlValidatorTable != null) {
+      Table table = sqlValidatorTable.unwrap(Table.class);
       return table.rolledUpColumnValidInsideAgg(columnName, aggCall, parent,
-              catalogReader.getConfig());
+          catalogReader.getConfig());
     }
     return true;
   }
@@ -3536,62 +3538,17 @@ public class SqlValidatorImpl implements 
SqlValidatorWithHints {
       return false;
     }
 
-    String tableAlias = pair.left;
     String columnName = pair.right;
 
-    Table table = findTable(tableAlias);
-    if (table != null) {
+    SqlValidatorTable sqlValidatorTable =
+        scope.fullyQualify(identifier).namespace.getTable();
+    if (sqlValidatorTable != null) {
+      Table table = sqlValidatorTable.unwrap(Table.class);
       return table.isRolledUp(columnName);
     }
     return false;
   }
 
-  private Table findTable(CalciteSchema schema, String tableName) {
-    boolean caseSensitive = catalogReader.nameMatcher().isCaseSensitive();
-    CalciteSchema.TableEntry entry = schema.getTable(tableName, caseSensitive);
-    if (entry != null) {
-      return entry.getTable();
-    }
-
-    // Check sub schemas
-    for (CalciteSchema subSchema : schema.getSubSchemaMap().values()) {
-      Table table = findTable(subSchema, tableName);
-      if (table != null) {
-        return table;
-      }
-    }
-
-    return null;
-  }
-
-  /**
-   * Given a table alias, find the corresponding {@link Table} associated with 
it
-   * */
-  private Table findTable(String alias) {
-    List<String> names = null;
-    if (tableScope == null) {
-      // no tables to find
-      return null;
-    }
-
-    for (ScopeChild child : tableScope.children) {
-      if (catalogReader.nameMatcher().matches(child.name, alias)) {
-        names = ((SqlIdentifier) child.namespace.getNode()).names;
-        break;
-      }
-    }
-    if (names == null || names.size() == 0) {
-      return null;
-    } else if (names.size() == 1) {
-      return findTable(catalogReader.getRootSchema(), names.get(0));
-    }
-
-    CalciteSchema.TableEntry entry =
-        SqlValidatorUtil.getTableEntry(catalogReader, names);
-
-    return entry == null ? null : entry.getTable();
-  }
-
   private boolean shouldCheckForRollUp(SqlNode from) {
     if (from != null) {
       SqlKind kind = stripAs(from).getKind();
diff --git a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
index 739f1dd..ce7e585 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlValidatorTest.java
@@ -10797,6 +10797,18 @@ public class SqlValidatorTest extends 
SqlValidatorTestCase {
 
     sql("select ^slackingmin^ from nest.emp_r")
             .fails(error);
+
+    sql("with emp_r as (select 1 as slackingmin) select slackingmin from 
emp_r")
+            .ok();
+
+    sql("with emp_r as (select ^slackingmin^ from emp_r) select slackingmin 
from emp_r")
+            .fails(error);
+
+    sql("with emp_r1 as (select 1 as slackingmin) select emp_r1.slackingmin 
from emp_r, emp_r1")
+            .ok();
+
+    sql("with emp_r1 as (select 1 as slackingmin) select ^emp_r.slackingmin^ 
from emp_r, emp_r1")
+            .fails(error);
   }
 
   @Test public void testSelectAggregateOnRolledUpColumn() {
diff --git 
a/core/src/test/java/org/apache/calcite/test/catalog/MockCatalogReader.java 
b/core/src/test/java/org/apache/calcite/test/catalog/MockCatalogReader.java
index 5fced0a..d7f303e 100644
--- a/core/src/test/java/org/apache/calcite/test/catalog/MockCatalogReader.java
+++ b/core/src/test/java/org/apache/calcite/test/catalog/MockCatalogReader.java
@@ -400,6 +400,17 @@ public abstract class MockCatalogReader extends 
CalciteCatalogReader {
       @Override public int getExtendedColumnOffset() {
         return rowType.getFieldCount();
       }
+
+      @Override public boolean isRolledUp(String column) {
+        return rolledUpColumns.contains(column);
+      }
+
+      @Override public boolean rolledUpColumnValidInsideAgg(String column,
+          SqlCall call, SqlNode parent, CalciteConnectionConfig config) {
+        // For testing
+        return call.getKind() != SqlKind.MAX
+            && (parent.getKind() == SqlKind.SELECT || parent.getKind() == 
SqlKind.FILTER);
+      }
     }
 
     @Override protected RelOptTable extend(final Table extendedTable) {

Reply via email to