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


##########
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.

Review Comment:
   Thanks for your suggestion. But I haven't seen other DDL SqlNode contains 
any examples. And I think user can aware of how to use `CREATE TABLE LIKE` 
syntax by referring to `reference.md`.



##########
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);
+
+  /**
+   * The LikeOption specify which additional properties of the original table 
to copy.
+   */
+  public enum LikeOption implements Symbolizable {
+    ALL,
+    DEFAULTS,
+    GENERATED
+  }
+
+  public final SqlIdentifier name;
+  public final SqlIdentifier sourceTable;
+  public final SqlNodeList includingOptions;
+  public final SqlNodeList excludingOptions;
+
+
+  public SqlCreateTableLike(SqlParserPos pos, boolean replace, boolean 
ifNotExists,
+      SqlIdentifier name, SqlIdentifier sourceTable,
+      SqlNodeList includingOptions, SqlNodeList excludingOptions) {
+    super(OPERATOR, pos, replace, ifNotExists);
+    this.name = name;
+    this.sourceTable = sourceTable;
+    this.includingOptions = includingOptions;
+    this.excludingOptions = excludingOptions;
+
+    // validate like options
+    if (includingOptions.contains(LikeOption.ALL.symbol(SqlParserPos.ZERO))) {
+      Preconditions.checkArgument(
+          includingOptions.size() == 1 && excludingOptions.isEmpty(),
+          "ALL cannot be used with other options");
+    } else if 
(excludingOptions.contains(LikeOption.ALL.symbol(SqlParserPos.ZERO))) {
+      Preconditions.checkArgument(
+          excludingOptions.size() == 1 && includingOptions.isEmpty(),
+          "ALL cannot be used with other options");
+    }
+
+    includingOptions.forEach(option -> {
+      Preconditions.checkArgument(
+          !excludingOptions.contains(option),
+          "Cannot include and exclude option %s at same time", 
option.toString());
+    });
+  }
+
+  @Override public List<SqlNode> getOperandList() {
+    return ImmutableNullableList.of(name, sourceTable, includingOptions, 
excludingOptions);
+  }
+
+  public Set<LikeOption> options() {
+    return includingOptions.stream()
+        .map(c -> ((SqlLiteral) c).symbolValue(LikeOption.class))
+        .collect(Collectors.toSet());
+  }
+
+  @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) 
{

Review Comment:
   I think in Calcite, all DDL in `calcite-server` will be execute by calcite 
instead of executing in other adapter(you can refer to 
org.apache.calcite.prepare.CalcitePrepareImpl#prepare2_).
   So in THIS jira, we don't need to care about if the DDL command can execute 
in other dbms. Maybe we can log a new jira to discuss "Dialect for DDL".



##########
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 {

Review Comment:
   If `CREATE TABLE` syntax and `CREATE TABLE LIKE` syntax create a same 
SqlNode, we need to use `if else` code to figure out which behavior needs to be 
done during executing DDL command, it will make `execute(SqlCreateTable create, 
CalcitePrepare.Context context)` method more complex. So I think use a new sql 
parser tree `SqlCreateTable` would be better.



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

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