[CALCITE-2382] Sub-query join lateral table function (pengzhiwei)

Project: http://git-wip-us.apache.org/repos/asf/calcite/repo
Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/ccbc656d
Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/ccbc656d
Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/ccbc656d

Branch: refs/heads/master
Commit: ccbc656d1681dc322a30107f096cd57c4e5353fe
Parents: 189b50d
Author: zhiwei.pzw <[email protected]>
Authored: Thu Jun 28 22:25:12 2018 -0700
Committer: Julian Hyde <[email protected]>
Committed: Fri Jun 29 01:27:33 2018 -0700

----------------------------------------------------------------------
 .../calcite/sql/validate/SqlValidatorImpl.java  |  2 +-
 .../apache/calcite/test/TableFunctionTest.java  | 61 +++++++++++++++-----
 2 files changed, 46 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/calcite/blob/ccbc656d/core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
----------------------------------------------------------------------
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 ad1d3af..61b7c82 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
@@ -2159,7 +2159,7 @@ public class SqlValidatorImpl implements 
SqlValidatorWithHints {
       return registerFrom(
           parentScope,
           usingScope,
-          true,
+          register,
           ((SqlCall) node).operand(0),
           enclosingNode,
           alias,

http://git-wip-us.apache.org/repos/asf/calcite/blob/ccbc656d/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java 
b/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java
index b0ed547..b56d2f6 100644
--- a/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java
+++ b/core/src/test/java/org/apache/calcite/test/TableFunctionTest.java
@@ -20,6 +20,7 @@ import org.apache.calcite.config.CalciteConnectionProperty;
 import org.apache.calcite.jdbc.CalciteConnection;
 import org.apache.calcite.schema.ScannableTable;
 import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.schema.Table;
 import org.apache.calcite.schema.TableFunction;
 import org.apache.calcite.schema.impl.AbstractSchema;
 import org.apache.calcite.schema.impl.TableFunctionImpl;
@@ -87,22 +88,22 @@ public class TableFunctionTest {
   /**
    * Tests a table function with literal arguments.
    */
-  @Test public void testTableFunction()
-      throws SQLException, ClassNotFoundException {
-    Connection connection =
-        DriverManager.getConnection("jdbc:calcite:");
-    CalciteConnection calciteConnection =
-        connection.unwrap(CalciteConnection.class);
-    SchemaPlus rootSchema = calciteConnection.getRootSchema();
-    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
-    final TableFunction table =
-        TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD);
-    schema.add("GenerateStrings", table);
-    ResultSet resultSet = connection.createStatement().executeQuery("select 
*\n"
-        + "from table(\"s\".\"GenerateStrings\"(5)) as t(n, c)\n"
-        + "where char_length(c) > 3");
-    assertThat(CalciteAssert.toString(resultSet),
-        equalTo("N=4; C=abcd\n"));
+  @Test public void testTableFunction() throws SQLException {
+    try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) 
{
+      CalciteConnection calciteConnection =
+          connection.unwrap(CalciteConnection.class);
+      SchemaPlus rootSchema = calciteConnection.getRootSchema();
+      SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
+      final TableFunction table =
+          TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD);
+      schema.add("GenerateStrings", table);
+      final String sql = "select *\n"
+          + "from table(\"s\".\"GenerateStrings\"(5)) as t(n, c)\n"
+          + "where char_length(c) > 3";
+      ResultSet resultSet = connection.createStatement().executeQuery(sql);
+      assertThat(CalciteAssert.toString(resultSet),
+          equalTo("N=4; C=abcd\n"));
+    }
   }
 
   /**
@@ -434,6 +435,34 @@ public class TableFunctionTest {
               "C=5; N=5");
     }
   }
+
+  /** Test case for
+   * <a 
href="https://issues.apache.org/jira/browse/CALCITE-2382";>[CALCITE-2382]
+   * Sub-query lateral joined to table function</a>. */
+  @Test public void testInlineViewLateralTableFunction() throws SQLException {
+    try (Connection connection = DriverManager.getConnection("jdbc:calcite:")) 
{
+      CalciteConnection calciteConnection =
+          connection.unwrap(CalciteConnection.class);
+      SchemaPlus rootSchema = calciteConnection.getRootSchema();
+      SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
+      final TableFunction table =
+          TableFunctionImpl.create(Smalls.GENERATE_STRINGS_METHOD);
+      schema.add("GenerateStrings", table);
+      Table tbl = new ScannableTableTest.SimpleTable();
+      schema.add("t", tbl);
+
+      final String sql = "select *\n"
+          + "from (select 5 as f0 from \"s\".\"t\") \"a\",\n"
+          + "  lateral table(\"s\".\"GenerateStrings\"(f0)) as t(n, c)\n"
+          + "where char_length(c) > 3";
+      ResultSet resultSet = connection.createStatement().executeQuery(sql);
+      final String expected = "F0=5; N=4; C=abcd\n"
+          + "F0=5; N=4; C=abcd\n"
+          + "F0=5; N=4; C=abcd\n"
+          + "F0=5; N=4; C=abcd\n";
+      assertThat(CalciteAssert.toString(resultSet), equalTo(expected));
+    }
+  }
 }
 
 // End TableFunctionTest.java

Reply via email to