Skip to content

Commit fd224da

Browse files
committed
Fix server SqlNode's and add unparse test to babel
1 parent 22b62cb commit fd224da

29 files changed

+340
-78
lines changed

babel/src/main/codegen/config.fmpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ data: {
2727
# List of additional classes and packages to import.
2828
# Example: "org.apache.calcite.sql.*", "java.util.List".
2929
imports: [
30+
"org.apache.calcite.schema.ColumnStrategy",
3031
"org.apache.calcite.sql.SqlCreate",
3132
"org.apache.calcite.sql.babel.SqlBabelCreateTable",
3233
"org.apache.calcite.sql.babel.TableCollectionType",

babel/src/main/codegen/includes/parserImpls.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void ColumnWithType(List<SqlNode> list) :
145145
]
146146
{
147147
list.add(SqlDdlNodes.column(s.add(id).end(this), id,
148-
type.withNullable(nullable), null, null));
148+
type.withNullable(nullable), null, ColumnStrategy.DEFAULT));
149149
}
150150
}
151151

babel/src/main/java/org/apache/calcite/sql/babel/SqlBabelCreateTable.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,39 @@
1717
package org.apache.calcite.sql.babel;
1818

1919
import org.apache.calcite.sql.SqlIdentifier;
20+
import org.apache.calcite.sql.SqlKind;
21+
import org.apache.calcite.sql.SqlLiteral;
2022
import org.apache.calcite.sql.SqlNode;
2123
import org.apache.calcite.sql.SqlNodeList;
24+
import org.apache.calcite.sql.SqlOperator;
2225
import org.apache.calcite.sql.SqlWriter;
2326
import org.apache.calcite.sql.ddl.SqlCreateTable;
27+
import org.apache.calcite.sql.fun.SqlBasicOperator;
2428
import org.apache.calcite.sql.parser.SqlParserPos;
29+
import org.apache.calcite.util.ImmutableNullableList;
30+
31+
import org.checkerframework.checker.nullness.qual.Nullable;
32+
33+
import java.util.List;
34+
35+
import static java.util.Objects.requireNonNull;
2536

2637
/**
2738
* Parse tree for {@code CREATE TABLE} statement, with extensions for particular
2839
* SQL dialects supported by Babel.
2940
*/
3041
public class SqlBabelCreateTable extends SqlCreateTable {
42+
private static final SqlOperator OPERATOR =
43+
SqlBasicOperator.create("CREATE TABLE", SqlKind.CREATE_TABLE).withCallFactory(
44+
(operator, functionQualifier, pos, operands) ->
45+
new SqlBabelCreateTable(pos,
46+
requireNonNull((SqlLiteral) operands[0]).booleanValue(),
47+
requireNonNull((SqlLiteral) operands[1]).symbolValue(TableCollectionType.class),
48+
requireNonNull((SqlLiteral) operands[2]).booleanValue(),
49+
requireNonNull((SqlLiteral) operands[3]).booleanValue(),
50+
(SqlIdentifier) requireNonNull(operands[4]),
51+
(SqlNodeList) operands[5], operands[6]));
52+
3153
private final TableCollectionType tableCollectionType;
3254
// CHECKSTYLE: IGNORE 2; can't use 'volatile' because it is a Java keyword
3355
// but checkstyle does not like trailing '_'.
@@ -36,13 +58,21 @@ public class SqlBabelCreateTable extends SqlCreateTable {
3658
/** Creates a SqlBabelCreateTable. */
3759
public SqlBabelCreateTable(SqlParserPos pos, boolean replace,
3860
TableCollectionType tableCollectionType, boolean volatile_,
39-
boolean ifNotExists, SqlIdentifier name, SqlNodeList columnList,
40-
SqlNode query) {
41-
super(pos, replace, ifNotExists, name, columnList, query);
61+
boolean ifNotExists, SqlIdentifier name, @Nullable SqlNodeList columnList,
62+
@Nullable SqlNode query) {
63+
super(OPERATOR, pos, replace, ifNotExists, name, columnList, query);
4264
this.tableCollectionType = tableCollectionType;
4365
this.volatile_ = volatile_;
4466
}
4567

68+
@SuppressWarnings("nullness")
69+
@Override public List<SqlNode> getOperandList() {
70+
return ImmutableNullableList.of(SqlLiteral.createBoolean(getReplace(), pos),
71+
SqlLiteral.createSymbol(tableCollectionType, pos),
72+
SqlLiteral.createBoolean(volatile_, pos),
73+
SqlLiteral.createBoolean(ifNotExists, pos), name, columnList, query);
74+
}
75+
4676
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
4777
writer.keyword("CREATE");
4878
switch (tableCollectionType) {

babel/src/test/java/org/apache/calcite/test/BabelParserTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.hamcrest.CoreMatchers.is;
4141
import static org.hamcrest.MatcherAssert.assertThat;
4242
import static org.hamcrest.Matchers.hasToString;
43+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
4344

4445
/**
4546
* Tests the "Babel" SQL parser, that understands all dialects of SQL.
@@ -390,7 +391,12 @@ private void checkParseInfixCast(String sqlType) {
390391
}
391392

392393
@Test void testPostgresSqlSetOption() {
393-
SqlParserFixture f = fixture().withDialect(PostgresqlSqlDialect.DEFAULT);
394+
// UnparsingTesterImpl has a check where it unparses a SqlNode into a SQL string
395+
// using the calcite dialect, and then parses it back into a SqlNode.
396+
// But the SQL string produced by the calcite dialect for `SET` cannot always be parsed back.
397+
assumeFalse(fixture().tester.isUnparserTest());
398+
SqlParserFixture f = fixture()
399+
.withDialect(PostgresqlSqlDialect.DEFAULT);
394400
f.sql("SET SESSION autovacuum = true")
395401
.ok("SET \"autovacuum\" = TRUE");
396402
f.sql("SET SESSION autovacuum = DEFAULT")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.calcite.test;
18+
19+
import org.apache.calcite.sql.parser.SqlParserFixture;
20+
21+
/**
22+
* Extension to {@link BabelParserTest} that ensures that every expression can
23+
* un-parse successfully.
24+
*/
25+
public class BabelUnParserTest extends BabelParserTest {
26+
@Override public SqlParserFixture fixture() {
27+
return super.fixture()
28+
.withTester(new UnparsingTesterImpl());
29+
}
30+
}

core/src/main/java/org/apache/calcite/sql/SqlCreate.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,4 @@ public boolean getReplace() {
5151
public void setReplace(boolean replace) {
5252
this.replace = replace;
5353
}
54-
5554
}

core/src/main/java/org/apache/calcite/sql/SqlSetOption.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public SqlSetOption(SqlParserPos pos, @Nullable String scope, SqlIdentifier name
143143
} else {
144144
operandList.add(new SqlIdentifier(scope, SqlParserPos.ZERO));
145145
}
146-
operandList.add(name);
146+
operandList.add(nameAsSqlNode);
147147
operandList.add(value);
148148
return ImmutableNullableList.copyOf(operandList);
149149
}

core/src/main/java/org/apache/calcite/sql/ddl/SqlAttributeDefinition.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,30 @@
2323
import org.apache.calcite.sql.SqlKind;
2424
import org.apache.calcite.sql.SqlNode;
2525
import org.apache.calcite.sql.SqlOperator;
26-
import org.apache.calcite.sql.SqlSpecialOperator;
2726
import org.apache.calcite.sql.SqlWriter;
27+
import org.apache.calcite.sql.fun.SqlBasicOperator;
2828
import org.apache.calcite.sql.parser.SqlParserPos;
29-
30-
import com.google.common.collect.ImmutableList;
29+
import org.apache.calcite.util.ImmutableNullableList;
3130

3231
import org.checkerframework.checker.nullness.qual.Nullable;
3332

3433
import java.util.List;
3534

35+
import static java.util.Objects.requireNonNull;
36+
3637
/**
3738
* Parse tree for SqlAttributeDefinition,
3839
* which is part of a {@link SqlCreateType}.
3940
*/
4041
public class SqlAttributeDefinition extends SqlCall {
41-
private static final SqlSpecialOperator OPERATOR =
42-
new SqlSpecialOperator("ATTRIBUTE_DEF", SqlKind.ATTRIBUTE_DEF);
42+
private static final SqlOperator OPERATOR =
43+
SqlBasicOperator.create("ATTRIBUTE_DEF", SqlKind.ATTRIBUTE_DEF)
44+
.withCallFactory((operator, functionQualifier, pos, operands) ->
45+
new SqlAttributeDefinition(
46+
pos,
47+
(SqlIdentifier) requireNonNull(operands[0]),
48+
(SqlDataTypeSpec) requireNonNull(operands[1]),
49+
operands[2], null));
4350

4451
public final SqlIdentifier name;
4552
public final SqlDataTypeSpec dataType;
@@ -60,8 +67,9 @@ public class SqlAttributeDefinition extends SqlCall {
6067
return OPERATOR;
6168
}
6269

70+
@SuppressWarnings("nullness")
6371
@Override public List<SqlNode> getOperandList() {
64-
return ImmutableList.of(name, dataType);
72+
return ImmutableNullableList.of(name, dataType, expression);
6573
}
6674

6775
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {

core/src/main/java/org/apache/calcite/sql/ddl/SqlCheckConstraint.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,30 @@
2121
import org.apache.calcite.sql.SqlKind;
2222
import org.apache.calcite.sql.SqlNode;
2323
import org.apache.calcite.sql.SqlOperator;
24-
import org.apache.calcite.sql.SqlSpecialOperator;
2524
import org.apache.calcite.sql.SqlWriter;
25+
import org.apache.calcite.sql.fun.SqlBasicOperator;
2626
import org.apache.calcite.sql.parser.SqlParserPos;
2727
import org.apache.calcite.util.ImmutableNullableList;
2828

2929
import org.checkerframework.checker.nullness.qual.Nullable;
3030

3131
import java.util.List;
3232

33+
import static java.util.Objects.requireNonNull;
34+
3335
/**
3436
* Parse tree for {@code UNIQUE}, {@code PRIMARY KEY} constraints.
3537
*
3638
* <p>And {@code FOREIGN KEY}, when we support it.
3739
*/
3840
public class SqlCheckConstraint extends SqlCall {
39-
private static final SqlSpecialOperator OPERATOR =
40-
new SqlSpecialOperator("CHECK", SqlKind.CHECK);
41+
private static final SqlOperator OPERATOR =
42+
SqlBasicOperator.create("CHECK", SqlKind.CHECK)
43+
.withCallFactory((operator, functionQualifier, pos, operands) ->
44+
new SqlCheckConstraint(
45+
pos,
46+
(SqlIdentifier) operands[0],
47+
requireNonNull(operands[1])));
4148

4249
private final @Nullable SqlIdentifier name;
4350
private final SqlNode expression;

core/src/main/java/org/apache/calcite/sql/ddl/SqlColumnDeclaration.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,37 @@
2121
import org.apache.calcite.sql.SqlDataTypeSpec;
2222
import org.apache.calcite.sql.SqlIdentifier;
2323
import org.apache.calcite.sql.SqlKind;
24+
import org.apache.calcite.sql.SqlLiteral;
2425
import org.apache.calcite.sql.SqlNode;
2526
import org.apache.calcite.sql.SqlOperator;
26-
import org.apache.calcite.sql.SqlSpecialOperator;
2727
import org.apache.calcite.sql.SqlWriter;
28+
import org.apache.calcite.sql.fun.SqlBasicOperator;
2829
import org.apache.calcite.sql.parser.SqlParserPos;
29-
30-
import com.google.common.collect.ImmutableList;
30+
import org.apache.calcite.util.ImmutableNullableList;
3131

3232
import org.checkerframework.checker.nullness.qual.Nullable;
3333

3434
import java.util.List;
3535

36+
import static java.util.Objects.requireNonNull;
37+
3638
/**
3739
* Parse tree for {@code UNIQUE}, {@code PRIMARY KEY} constraints.
3840
*
3941
* <p>And {@code FOREIGN KEY}, when we support it.
4042
*/
4143
public class SqlColumnDeclaration extends SqlCall {
42-
private static final SqlSpecialOperator OPERATOR =
43-
new SqlSpecialOperator("COLUMN_DECL", SqlKind.COLUMN_DECL);
44+
private static final SqlOperator OPERATOR =
45+
SqlBasicOperator.create("COLUMN_DECL", SqlKind.COLUMN_DECL)
46+
.withCallFactory((operator, functionQualifier, pos, operands) ->
47+
new SqlColumnDeclaration(
48+
pos,
49+
(SqlIdentifier) requireNonNull(operands[0]),
50+
(SqlDataTypeSpec) requireNonNull(operands[1]),
51+
operands[2],
52+
requireNonNull(
53+
((SqlLiteral) requireNonNull(operands[3])).symbolValue(
54+
ColumnStrategy.class))));
4455

4556
public final SqlIdentifier name;
4657
public final SqlDataTypeSpec dataType;
@@ -63,7 +74,8 @@ public class SqlColumnDeclaration extends SqlCall {
6374
}
6475

6576
@Override public List<SqlNode> getOperandList() {
66-
return ImmutableList.of(name, dataType);
77+
return ImmutableNullableList.of(name, dataType, expression,
78+
SqlLiteral.createSymbol(strategy, pos));
6779
}
6880

6981
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {

0 commit comments

Comments
 (0)