macroguo-ghy commented on code in PR #3442:
URL: https://github.com/apache/calcite/pull/3442#discussion_r1359465022


##########
server/src/main/java/org/apache/calcite/server/ServerDdlExecutor.java:
##########
@@ -154,6 +156,14 @@ static Pair<CalciteSchema, String> 
schema(CalcitePrepare.Context context,
     return Pair.of(schema, name);
   }
 
+  /** Return the table from the given {@code} context and {@code} name. */
+  static Table table(CalcitePrepare.Context context, SqlIdentifier name) {

Review Comment:
   Deleted.



##########
core/src/main/java/org/apache/calcite/sql/ddl/SqlCreateTableLike.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+package org.apache.calcite.sql.ddl;
+
+import org.apache.calcite.sql.SqlCreate;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.SqlLiteral;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.calcite.sql.SqlSpecialOperator;
+import org.apache.calcite.sql.SqlWriter;
+import org.apache.calcite.sql.Symbolizable;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.util.ImmutableNullableList;
+
+import com.google.common.base.Preconditions;
+
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Parse tree for {@code CREATE TABLE LIKE} statement.
+ */
+public class SqlCreateTableLike extends SqlCreate {
+  private static final SqlOperator OPERATOR =
+      new SqlSpecialOperator("CREATE TABLE LIKE", SqlKind.CREATE_TABLE);

Review Comment:
   Fixed.



##########
server/src/test/java/org/apache/calcite/test/ServerTest.java:
##########
@@ -277,6 +279,156 @@ static Connection connect() throws SQLException {
     }
   }
 
+  @Test void testCreateTableLike() throws Exception {

Review Comment:
   Fixed.



##########
server/src/test/java/org/apache/calcite/test/ServerTest.java:
##########
@@ -277,6 +279,156 @@ static Connection connect() throws SQLException {
     }
   }
 
+  @Test void testCreateTableLike() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (i int not null)");
+      s.execute("create table t2 like t");
+      int x = s.executeUpdate("insert into t2 values 1");
+      assertThat(x, is(1));
+      x = s.executeUpdate("insert into t2 values 3");
+      assertThat(x, is(1));
+      try (ResultSet r = s.executeQuery("select sum(i) from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt(1), is(4));
+        assertThat(r.next(), is(false));
+      }
+    }
+  }
+
+  @Test void testCreateTableLikeWithStoredGeneratedColumn() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (\n"
+          + " h int not null,\n"
+          + " i int,\n"
+          + " j int as (i + 1) stored,\n"
+          + " k int default -1)\n");
+      s.execute("create table t2 like t including defaults including 
generated");
+
+      int x = s.executeUpdate("insert into t2 (h, i) values (3, 4)");
+      assertThat(x, is(1));
+
+      final String sql1 = "explain plan for\n"
+          + "insert into t2 (h, i) values (3, 4)";
+      try (ResultSet r = s.executeQuery(sql1)) {
+        assertThat(r.next(), is(true));
+        final String plan = ""
+            + "EnumerableTableModify(table=[[T2]], operation=[INSERT], 
flattened=[false])\n"
+            + "  EnumerableCalc(expr#0..1=[{inputs}], expr#2=[1], 
expr#3=[+($t1, $t2)], expr#4=[-1], proj#0..1=[{exprs}], J=[$t3], K=[$t4])\n"
+            + "    EnumerableValues(tuples=[[{ 3, 4 }]])\n";
+        assertThat(r.getString(1), isLinux(plan));
+        assertThat(r.next(), is(false));
+      }
+
+      try (ResultSet r = s.executeQuery("select * from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt("H"), is(3));
+        assertThat(r.wasNull(), is(false));
+        assertThat(r.getInt("I"), is(4));
+        assertThat(r.getInt("J"), is(5)); // j = i + 1
+        assertThat(r.getInt("K"), is(-1)); // k = -1 (default)
+        assertThat(r.next(), is(false));
+      }
+
+      try {
+        x = s.executeUpdate("insert into t2 values (3, 4, 5, 6)");
+        fail("expected error, got " + x);
+      } catch (SQLException e) {
+        assertThat(e.getMessage(),
+            containsString("Cannot INSERT into generated column 'J'"));
+      }
+    }
+  }
+
+  @Test void testCreateTableLikeWithVirtualGeneratedColumn() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (\n"
+          + " h int not null,\n"
+          + " i int,\n"
+          + " j int as (i + 1) virtual)\n");
+      s.execute("create table t2 like t including defaults including 
generated");
+
+      int x = s.executeUpdate("insert into t2 (h, i) values (3, 4)");
+      assertThat(x, is(1));
+
+      final String sql1 = "explain plan for\n"
+          + "insert into t (h, i) values (3, 4)";
+      try (ResultSet r = s.executeQuery(sql1)) {
+        final String sql2 = "explain plan for\n"
+            + "insert into t2 (h, i) values (3, 4)";
+        assertThat(r.next(), is(true));
+        final String plan = r.getString(1);
+        assertThat(r.next(), is(false));
+
+        ResultSet r2 = s.executeQuery(sql2);
+        assertThat(r2.next(), is(true));
+        assertEquals(plan, r2.getString(1).replace("T2", "T"));
+        assertThat(r2.next(), is(false));
+      }
+
+      try (ResultSet r = s.executeQuery("select * from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt("H"), is(3));
+        assertThat(r.wasNull(), is(false));
+        assertThat(r.getInt("I"), is(4));
+        assertThat(r.getInt("J"), is(5)); // j = i + 1
+        assertThat(r.next(), is(false));
+      }
+
+      try {
+        x = s.executeUpdate("insert into t2 values (3, 4, 5)");
+        fail("expected error, got " + x);
+      } catch (SQLException e) {

Review Comment:
   Fixed.



##########
server/src/test/java/org/apache/calcite/test/ServerTest.java:
##########
@@ -277,6 +279,156 @@ static Connection connect() throws SQLException {
     }
   }
 
+  @Test void testCreateTableLike() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (i int not null)");
+      s.execute("create table t2 like t");
+      int x = s.executeUpdate("insert into t2 values 1");
+      assertThat(x, is(1));
+      x = s.executeUpdate("insert into t2 values 3");
+      assertThat(x, is(1));
+      try (ResultSet r = s.executeQuery("select sum(i) from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt(1), is(4));
+        assertThat(r.next(), is(false));
+      }
+    }
+  }
+
+  @Test void testCreateTableLikeWithStoredGeneratedColumn() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (\n"
+          + " h int not null,\n"
+          + " i int,\n"
+          + " j int as (i + 1) stored,\n"
+          + " k int default -1)\n");
+      s.execute("create table t2 like t including defaults including 
generated");
+
+      int x = s.executeUpdate("insert into t2 (h, i) values (3, 4)");
+      assertThat(x, is(1));
+
+      final String sql1 = "explain plan for\n"
+          + "insert into t2 (h, i) values (3, 4)";
+      try (ResultSet r = s.executeQuery(sql1)) {
+        assertThat(r.next(), is(true));
+        final String plan = ""
+            + "EnumerableTableModify(table=[[T2]], operation=[INSERT], 
flattened=[false])\n"
+            + "  EnumerableCalc(expr#0..1=[{inputs}], expr#2=[1], 
expr#3=[+($t1, $t2)], expr#4=[-1], proj#0..1=[{exprs}], J=[$t3], K=[$t4])\n"
+            + "    EnumerableValues(tuples=[[{ 3, 4 }]])\n";
+        assertThat(r.getString(1), isLinux(plan));
+        assertThat(r.next(), is(false));
+      }
+
+      try (ResultSet r = s.executeQuery("select * from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt("H"), is(3));
+        assertThat(r.wasNull(), is(false));
+        assertThat(r.getInt("I"), is(4));
+        assertThat(r.getInt("J"), is(5)); // j = i + 1
+        assertThat(r.getInt("K"), is(-1)); // k = -1 (default)
+        assertThat(r.next(), is(false));
+      }
+
+      try {
+        x = s.executeUpdate("insert into t2 values (3, 4, 5, 6)");
+        fail("expected error, got " + x);
+      } catch (SQLException e) {
+        assertThat(e.getMessage(),
+            containsString("Cannot INSERT into generated column 'J'"));
+      }
+    }
+  }
+
+  @Test void testCreateTableLikeWithVirtualGeneratedColumn() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (\n"
+          + " h int not null,\n"
+          + " i int,\n"
+          + " j int as (i + 1) virtual)\n");
+      s.execute("create table t2 like t including defaults including 
generated");
+
+      int x = s.executeUpdate("insert into t2 (h, i) values (3, 4)");
+      assertThat(x, is(1));
+
+      final String sql1 = "explain plan for\n"
+          + "insert into t (h, i) values (3, 4)";
+      try (ResultSet r = s.executeQuery(sql1)) {
+        final String sql2 = "explain plan for\n"
+            + "insert into t2 (h, i) values (3, 4)";
+        assertThat(r.next(), is(true));
+        final String plan = r.getString(1);
+        assertThat(r.next(), is(false));
+
+        ResultSet r2 = s.executeQuery(sql2);
+        assertThat(r2.next(), is(true));
+        assertEquals(plan, r2.getString(1).replace("T2", "T"));
+        assertThat(r2.next(), is(false));
+      }
+
+      try (ResultSet r = s.executeQuery("select * from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt("H"), is(3));
+        assertThat(r.wasNull(), is(false));
+        assertThat(r.getInt("I"), is(4));
+        assertThat(r.getInt("J"), is(5)); // j = i + 1
+        assertThat(r.next(), is(false));
+      }
+
+      try {
+        x = s.executeUpdate("insert into t2 values (3, 4, 5)");
+        fail("expected error, got " + x);
+      } catch (SQLException e) {
+        assertThat(e.getMessage(),
+            containsString("Cannot INSERT into generated column 'J'"));
+      }
+    }
+  }
+
+  @Test void testCreateTableLikeWithoutLikeOptions() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (\n"
+          + " h int not null,\n"
+          + " i int,\n"
+          + " j int as (i + 1) stored,\n"
+          + " k int default -1)");
+      // In table t2, only copy the column and type information from t,
+      // excluding generated expression and default expression
+      s.execute("create table t2 like t");
+
+      int x = s.executeUpdate("insert into t2 (h, i) values (3, 4)");
+      assertThat(x, is(1));
+
+      final String sql1 = "explain plan for\n"
+          + "insert into t2 (h, i) values (3, 4)";
+      try (ResultSet r = s.executeQuery(sql1)) {
+        assertThat(r.next(), is(true));
+        final String plan = ""
+            + "EnumerableTableModify(table=[[T2]], operation=[INSERT], 
flattened=[false])\n"
+            + "  EnumerableCalc(expr#0..1=[{inputs}], expr#2=[null:INTEGER], 
proj#0..2=[{exprs}], K=[$t2])\n"
+            + "    EnumerableValues(tuples=[[{ 3, 4 }]])\n";
+        assertThat(r.getString(1), isLinux(plan));
+        assertThat(r.next(), is(false));
+      }
+
+      try (ResultSet r = s.executeQuery("select * from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt("H"), is(3));
+        assertThat(r.wasNull(), is(false));
+        assertThat(r.getInt("I"), is(4));
+        assertThat(r.getInt("J"), is(0)); // excluding generated column

Review Comment:
   Because `java.sql.ResultSet#getInt(java.lang.String)` will return 0 if the 
value is `SQL NULL`. I add `assertThat(r.wasNull(), is(true))` to clarify.



##########
server/src/test/java/org/apache/calcite/test/ServerTest.java:
##########
@@ -277,6 +279,156 @@ static Connection connect() throws SQLException {
     }
   }
 
+  @Test void testCreateTableLike() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (i int not null)");
+      s.execute("create table t2 like t");
+      int x = s.executeUpdate("insert into t2 values 1");
+      assertThat(x, is(1));
+      x = s.executeUpdate("insert into t2 values 3");
+      assertThat(x, is(1));
+      try (ResultSet r = s.executeQuery("select sum(i) from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt(1), is(4));
+        assertThat(r.next(), is(false));
+      }
+    }
+  }
+
+  @Test void testCreateTableLikeWithStoredGeneratedColumn() throws Exception {
+    try (Connection c = connect();
+         Statement s = c.createStatement()) {
+      s.execute("create table t (\n"
+          + " h int not null,\n"
+          + " i int,\n"
+          + " j int as (i + 1) stored,\n"
+          + " k int default -1)\n");
+      s.execute("create table t2 like t including defaults including 
generated");
+
+      int x = s.executeUpdate("insert into t2 (h, i) values (3, 4)");
+      assertThat(x, is(1));
+
+      final String sql1 = "explain plan for\n"
+          + "insert into t2 (h, i) values (3, 4)";
+      try (ResultSet r = s.executeQuery(sql1)) {
+        assertThat(r.next(), is(true));
+        final String plan = ""
+            + "EnumerableTableModify(table=[[T2]], operation=[INSERT], 
flattened=[false])\n"
+            + "  EnumerableCalc(expr#0..1=[{inputs}], expr#2=[1], 
expr#3=[+($t1, $t2)], expr#4=[-1], proj#0..1=[{exprs}], J=[$t3], K=[$t4])\n"
+            + "    EnumerableValues(tuples=[[{ 3, 4 }]])\n";
+        assertThat(r.getString(1), isLinux(plan));
+        assertThat(r.next(), is(false));
+      }
+
+      try (ResultSet r = s.executeQuery("select * from t2")) {
+        assertThat(r.next(), is(true));
+        assertThat(r.getInt("H"), is(3));
+        assertThat(r.wasNull(), is(false));
+        assertThat(r.getInt("I"), is(4));
+        assertThat(r.getInt("J"), is(5)); // j = i + 1
+        assertThat(r.getInt("K"), is(-1)); // k = -1 (default)
+        assertThat(r.next(), is(false));
+      }
+
+      try {
+        x = s.executeUpdate("insert into t2 values (3, 4, 5, 6)");

Review Comment:
   Fixed.



##########
server/src/main/java/org/apache/calcite/server/ServerDdlExecutor.java:
##########
@@ -558,6 +568,71 @@ public void execute(SqlCreateTable create,
     }
   }
 
+  /** Executes a {@code CREATE TABLE LIKE} command. */
+  public void execute(SqlCreateTableLike create,
+      CalcitePrepare.Context context) {
+    final Pair<CalciteSchema, String> pair = schema(context, true, 
create.name);
+    final JavaTypeFactory typeFactory = context.getTypeFactory();
+    final Table table = table(context, create.sourceTable);
+    final RelDataType rowType = table.getRowType(typeFactory);
+    InitializerExpressionFactory ief = 
NullInitializerExpressionFactory.INSTANCE;
+    if (table instanceof Wrapper) {
+      final InitializerExpressionFactory sourceIef =
+          ((Wrapper) table).unwrap(InitializerExpressionFactory.class);
+      if (sourceIef != null) {
+        final Set<SqlCreateTableLike.LikeOption> optionSet = create.options();
+        final boolean includingGenerated =
+            optionSet.contains(SqlCreateTableLike.LikeOption.GENERATED)
+                || optionSet.contains(SqlCreateTableLike.LikeOption.ALL);
+        final boolean includingDefaults =
+            optionSet.contains(SqlCreateTableLike.LikeOption.DEFAULTS)
+                || optionSet.contains(SqlCreateTableLike.LikeOption.ALL);
+        ief = new NullInitializerExpressionFactory() {

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to