[CALCITE-6022] Support "CREATE TABLE LIKE" DDL#3442
[CALCITE-6022] Support "CREATE TABLE LIKE" DDL#3442JiajunBernoulli merged 1 commit intoapache:mainfrom
Conversation
656bd26 to
12441cc
Compare
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Parse tree for {@code CREATE TABLE LIKE} statement. |
There was a problem hiding this comment.
It is better to give a CREATE TABLE LIKE sql example.
There was a problem hiding this 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.
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) { |
There was a problem hiding this comment.
Could we add the sql dialect transform unit test for SqlCreateTableLike?
There was a problem hiding this 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".
| return Pair.of(schema, name); | ||
| } | ||
|
|
||
| static Table table(CalcitePrepare.Context context, SqlIdentifier id) { |
There was a problem hiding this comment.
Could we give some code comments for this method?It is better to help others understand this method.
|
@macroguo-ghy ,thank you for opening this PR, left a couple of comments! |
macroguo-ghy
left a comment
There was a problem hiding this comment.
Hi @LakeShen @JiajunBernoulli, thanks for your review and suggestion. I reply to your comment. Please help me review again if you have time.
| import java.util.stream.Collectors; | ||
|
|
||
| /** | ||
| * Parse tree for {@code CREATE TABLE LIKE} statement. |
There was a problem hiding this 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.
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) { |
There was a problem hiding this 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".
| return Pair.of(schema, name); | ||
| } | ||
|
|
||
| static Table table(CalcitePrepare.Context context, SqlIdentifier id) { |
| ) | ||
| } | ||
|
|
||
| SqlCreate SqlCreateTableLike(Span s, boolean replace, boolean ifNotExists, SqlIdentifier id) : |
There was a problem hiding this comment.
You calling SqlCreateTableLike by SqlCreateTable .
SqlCreateTableis added tocreateStatementParserMethods
Can we add SqlCreateTableLike to createStatementParserMethods?
There was a problem hiding this comment.
Fix this. Could you please review this PR again? @JiajunBernoulli
b8fe034 to
c3376c1
Compare
| ( | ||
| <#-- additional literal parser methods are included here --> | ||
| <#list (parser.createStatementParserMethods!default.parser.createStatementParserMethods) as method> | ||
| <#if method = "SqlCreateTableLike"> |
There was a problem hiding this comment.
I'm afraid not, we need to use LOOKAHEAD to distinguish CREATE TABLE and CREATE TABLE ... LIKE.
create = ${method}(s, replace), in this line , ${method} will be replaced by createStatementParserMethod, so we can not use LOOKAHEAD(...) SqlCreateTableLike replace SqlCreateTableLike. This will result in compilation errors.
There was a problem hiding this comment.
OK, I reverted and squashed the commits.
|
Hi @JiajunBernoulli, can you review this PR again? I believe we can merge it before 1.36.0 release. |
c3376c1 to
1c9fa5c
Compare
| */ | ||
| public class SqlCreateTableLike extends SqlCreate { | ||
| private static final SqlOperator OPERATOR = | ||
| new SqlSpecialOperator("CREATE TABLE LIKE", SqlKind.CREATE_TABLE); |
There was a problem hiding this comment.
We need SqlKind.CREATE_TABLE_LIKE because SqlCreateTableLike not extends SqlCreateTable.
User maybe mistake if them are both SqlKind.CREATE_TABLE.
| } | ||
|
|
||
| /** Return the table from the given {@code} context and {@code} name. */ | ||
| static Table table(CalcitePrepare.Context context, SqlIdentifier name) { |
There was a problem hiding this comment.
Used only once.
It doesn't seem necessary to abstract it as a method.
| final boolean includingDefaults = | ||
| optionSet.contains(SqlCreateTableLike.LikeOption.DEFAULTS) | ||
| || optionSet.contains(SqlCreateTableLike.LikeOption.ALL); | ||
| ief = new NullInitializerExpressionFactory() { |
There was a problem hiding this comment.
Need class name and document, not anonymous class.
| } | ||
| } | ||
|
|
||
| @Test void testCreateTableLike() throws Exception { |
There was a problem hiding this comment.
Need one JIRA link and document.
| } | ||
|
|
||
| try { | ||
| x = s.executeUpdate("insert into t2 values (3, 4, 5, 6)"); |
There was a problem hiding this comment.
We can use assertThrows.
| try { | ||
| x = s.executeUpdate("insert into t2 values (3, 4, 5)"); | ||
| fail("expected error, got " + x); | ||
| } catch (SQLException e) { |
| 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 |
There was a problem hiding this comment.
Why is 0, not null?
There was a problem hiding this 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.
1c9fa5c to
e550f60
Compare
macroguo-ghy
left a comment
There was a problem hiding this comment.
Thanks for your reviews, @JiajunBernoulli ! I have pushed a new commit, and I noticed that Julian updating the lint rule, so I have made a git rebase.
| 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 |
There was a problem hiding this 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.
| final boolean includingDefaults = | ||
| optionSet.contains(SqlCreateTableLike.LikeOption.DEFAULTS) | ||
| || optionSet.contains(SqlCreateTableLike.LikeOption.ALL); | ||
| ief = new NullInitializerExpressionFactory() { |
| } | ||
| } | ||
|
|
||
| @Test void testCreateTableLike() throws Exception { |
| } | ||
|
|
||
| try { | ||
| x = s.executeUpdate("insert into t2 values (3, 4, 5, 6)"); |
| try { | ||
| x = s.executeUpdate("insert into t2 values (3, 4, 5)"); | ||
| fail("expected error, got " + x); | ||
| } catch (SQLException e) { |
| */ | ||
| public class SqlCreateTableLike extends SqlCreate { | ||
| private static final SqlOperator OPERATOR = | ||
| new SqlSpecialOperator("CREATE TABLE LIKE", SqlKind.CREATE_TABLE); |
| } | ||
|
|
||
| /** Return the table from the given {@code} context and {@code} name. */ | ||
| static Table table(CalcitePrepare.Context context, SqlIdentifier name) { |
| } | ||
| } | ||
|
|
||
| if (pair.left.plus().getTable(pair.right) != null) { |
There was a problem hiding this comment.
Check can be first, we can moved them to line 568.
And then add one line comment to introduce InitializerExpressionFactory.
| public void execute(SqlCreateTableLike create, | ||
| CalcitePrepare.Context context) { | ||
| final Pair<CalciteSchema, String> pair = schema(context, true, create.name); | ||
| final JavaTypeFactory typeFactory = context.getTypeFactory(); |
There was a problem hiding this comment.
typeFactory and rowType can be moved to last because them are used by last code.
|
PR is already good, and the little suggestion can be made when you squash commits. |
3a4b1fe to
14d9772
Compare
|
OK, I have squashed the commits. Thanks for your reviews! @LakeShen @JiajunBernoulli |
|
@macroguo-ghy , Commit message should same as JIRA summary.
|
14d9772 to
593a398
Compare
|
Sorry, my mistake. I have edited the commit message. @JiajunBernoulli |
|
Kudos, SonarCloud Quality Gate passed! |










No description provided.