Skip to content

Commit fabf648

Browse files
committed
[CALCITE-7339] Most classes in SqlDdlNodes use an incorrect SqlCallFactory
Signed-off-by: Mihai Budiu <[email protected]>
1 parent 1e92bbe commit fabf648

25 files changed

+483
-57
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package org.apache.calcite.sql;
1818

1919
import org.apache.calcite.config.CalciteSystemProperty;
20+
import org.apache.calcite.sql.parser.SqlParserPos;
2021
import org.apache.calcite.sql.parser.SqlParserUtil;
2122
import org.apache.calcite.util.Glossary;
23+
import org.apache.calcite.util.ImmutableNullableList;
2224
import org.apache.calcite.util.SerializableCharset;
2325
import org.apache.calcite.util.Util;
2426

@@ -37,6 +39,8 @@
3739

3840
import static org.apache.calcite.util.Static.RESOURCE;
3941

42+
import static java.util.Objects.requireNonNull;
43+
4044
/**
4145
* A <code>SqlCollation</code> is an object representing a <code>Collate</code>
4246
* statement. It is immutable.
@@ -113,6 +117,25 @@ public SqlCollation(
113117
this.collationName = generateCollationName(charset);
114118
}
115119

120+
/** Encode all the information require to reconstruct a SqlCollection in a SqlList object. */
121+
public SqlNodeList asList() {
122+
return new SqlNodeList(
123+
ImmutableNullableList.of(
124+
SqlLiteral.createCharString(this.getCollationName(), SqlParserPos.ZERO),
125+
SqlLiteral.createSymbol(coercibility, SqlParserPos.ZERO)),
126+
SqlParserPos.ZERO);
127+
}
128+
129+
/** The inverse of the {@link #asList} function. */
130+
public static SqlCollation fromSqlList(SqlNodeList list) {
131+
assert list.size() == 2;
132+
String name = ((SqlLiteral) list.get(0)).getValueAs(String.class);
133+
Coercibility coercibility = ((SqlLiteral) list.get(1)).symbolValue(Coercibility.class);
134+
return new SqlCollation(
135+
name,
136+
requireNonNull(coercibility, "coercibility"));
137+
}
138+
116139
/**
117140
* Creates a Collation by its coercibility, locale, charset and strength.
118141
*/

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ private static class SqlLambdaOperator extends SqlSpecialOperator {
107107
super("->", SqlKind.LAMBDA);
108108
}
109109

110+
@Override public SqlCall createCall(
111+
@Nullable SqlLiteral functionQualifier, SqlParserPos pos, @Nullable SqlNode... operands) {
112+
return new SqlLambda(pos,
113+
(SqlNodeList) requireNonNull(operands[0], "parameters"),
114+
requireNonNull(operands[1], "expression"));
115+
}
116+
110117
@Override public RelDataType deriveType(
111118
SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
112119
final SqlLambda lambdaExpr = (SqlLambda) call;

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +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;
26+
import org.apache.calcite.sql.SqlNodeList;
2527
import org.apache.calcite.sql.SqlOperator;
2628
import org.apache.calcite.sql.SqlSpecialOperator;
2729
import org.apache.calcite.sql.SqlWriter;
2830
import org.apache.calcite.sql.parser.SqlParserPos;
29-
30-
import com.google.common.collect.ImmutableList;
31+
import org.apache.calcite.util.ImmutableNullableList;
3132

3233
import org.checkerframework.checker.nullness.qual.Nullable;
3334

3435
import java.util.List;
3536

37+
import static java.util.Objects.requireNonNull;
38+
3639
/**
3740
* Parse tree for SqlAttributeDefinition,
3841
* which is part of a {@link SqlCreateType}.
3942
*/
4043
public class SqlAttributeDefinition extends SqlCall {
41-
private static final SqlSpecialOperator OPERATOR =
42-
new SqlSpecialOperator("ATTRIBUTE_DEF", SqlKind.ATTRIBUTE_DEF);
44+
private static final SqlOperator OPERATOR =
45+
new SqlSpecialOperator("ATTRIBUTE_DEF", SqlKind.ATTRIBUTE_DEF) {
46+
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
47+
SqlParserPos pos, @Nullable SqlNode... operands) {
48+
return new SqlAttributeDefinition(pos,
49+
(SqlIdentifier) requireNonNull(operands[0], "name"),
50+
(SqlDataTypeSpec) requireNonNull(operands[1], "dataType"),
51+
operands[2],
52+
operands[3] != null ? SqlCollation.fromSqlList((SqlNodeList) operands[3]) : null);
53+
}
54+
};
4355

4456
public final SqlIdentifier name;
4557
public final SqlDataTypeSpec dataType;
@@ -60,8 +72,10 @@ public class SqlAttributeDefinition extends SqlCall {
6072
return OPERATOR;
6173
}
6274

75+
@SuppressWarnings("nullness")
6376
@Override public List<SqlNode> getOperandList() {
64-
return ImmutableList.of(name, dataType);
77+
return ImmutableNullableList.of(name, dataType, expression,
78+
collation != null ? collation.asList() : null);
6579
}
6680

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

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.calcite.sql.SqlCall;
2020
import org.apache.calcite.sql.SqlIdentifier;
2121
import org.apache.calcite.sql.SqlKind;
22+
import org.apache.calcite.sql.SqlLiteral;
2223
import org.apache.calcite.sql.SqlNode;
2324
import org.apache.calcite.sql.SqlOperator;
2425
import org.apache.calcite.sql.SqlSpecialOperator;
@@ -30,14 +31,23 @@
3031

3132
import java.util.List;
3233

34+
import static java.util.Objects.requireNonNull;
35+
3336
/**
3437
* Parse tree for {@code UNIQUE}, {@code PRIMARY KEY} constraints.
3538
*
3639
* <p>And {@code FOREIGN KEY}, when we support it.
3740
*/
3841
public class SqlCheckConstraint extends SqlCall {
39-
private static final SqlSpecialOperator OPERATOR =
40-
new SqlSpecialOperator("CHECK", SqlKind.CHECK);
42+
private static final SqlOperator OPERATOR =
43+
new SqlSpecialOperator("CHECK", SqlKind.CHECK) {
44+
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
45+
SqlParserPos pos, @Nullable SqlNode... operands) {
46+
return new SqlCheckConstraint(pos,
47+
(SqlIdentifier) operands[0],
48+
requireNonNull(operands[1], "expression"));
49+
}
50+
};
4151

4252
private final @Nullable SqlIdentifier name;
4353
private final SqlNode expression;

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,50 @@
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;
2627
import org.apache.calcite.sql.SqlSpecialOperator;
2728
import org.apache.calcite.sql.SqlWriter;
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+
new SqlSpecialOperator("COLUMN_DECL", SqlKind.COLUMN_DECL) {
46+
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
47+
SqlParserPos pos, @Nullable SqlNode... operands) {
48+
return new SqlColumnDeclaration(pos,
49+
(SqlIdentifier) requireNonNull(operands[0], "name"),
50+
(SqlDataTypeSpec) requireNonNull(operands[1], "dataType"),
51+
operands[2],
52+
operands[3] != null
53+
? ColumnStrategy.valueOf(((SqlIdentifier) operands[3]).getSimple())
54+
: null);
55+
}
56+
};
4457

4558
public final SqlIdentifier name;
4659
public final SqlDataTypeSpec dataType;
4760
public final @Nullable SqlNode expression;
48-
public final ColumnStrategy strategy;
61+
// The Babel parser can supply null for the strategy
62+
public final @Nullable ColumnStrategy strategy;
4963

5064
/** Creates a SqlColumnDeclaration; use {@link SqlDdlNodes#column}. */
5165
SqlColumnDeclaration(SqlParserPos pos, SqlIdentifier name,
5266
SqlDataTypeSpec dataType, @Nullable SqlNode expression,
53-
ColumnStrategy strategy) {
67+
@Nullable ColumnStrategy strategy) {
5468
super(pos);
5569
this.name = name;
5670
this.dataType = dataType;
@@ -62,8 +76,10 @@ public class SqlColumnDeclaration extends SqlCall {
6276
return OPERATOR;
6377
}
6478

79+
@SuppressWarnings("nullness")
6580
@Override public List<SqlNode> getOperandList() {
66-
return ImmutableList.of(name, dataType);
81+
return ImmutableNullableList.of(name, dataType, expression,
82+
strategy != null ? new SqlIdentifier(strategy.name(), SqlParserPos.ZERO) : null);
6783
}
6884

6985
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
@@ -73,7 +89,7 @@ public class SqlColumnDeclaration extends SqlCall {
7389
writer.keyword("NOT NULL");
7490
}
7591
SqlNode expression = this.expression;
76-
if (expression != null) {
92+
if (expression != null && strategy != null) {
7793
switch (strategy) {
7894
case VIRTUAL:
7995
case STORED:

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
*/
1717
package org.apache.calcite.sql.ddl;
1818

19+
import org.apache.calcite.sql.SqlCall;
1920
import org.apache.calcite.sql.SqlCreate;
2021
import org.apache.calcite.sql.SqlIdentifier;
2122
import org.apache.calcite.sql.SqlKind;
23+
import org.apache.calcite.sql.SqlLiteral;
2224
import org.apache.calcite.sql.SqlNode;
2325
import org.apache.calcite.sql.SqlNodeList;
2426
import org.apache.calcite.sql.SqlOperator;
@@ -51,8 +53,18 @@ public class SqlCreateForeignSchema extends SqlCreate {
5153
private final @Nullable SqlNodeList optionList;
5254

5355
private static final SqlOperator OPERATOR =
54-
new SqlSpecialOperator("CREATE FOREIGN SCHEMA",
55-
SqlKind.CREATE_FOREIGN_SCHEMA);
56+
new SqlSpecialOperator("CREATE FOREIGN SCHEMA", SqlKind.CREATE_FOREIGN_SCHEMA) {
57+
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
58+
SqlParserPos pos, @Nullable SqlNode... operands) {
59+
return new SqlCreateForeignSchema(pos,
60+
((SqlLiteral) requireNonNull(operands[0], "replace")).booleanValue(),
61+
((SqlLiteral) requireNonNull(operands[1], "ifNotExists")).booleanValue(),
62+
(SqlIdentifier) requireNonNull(operands[2], "name"),
63+
operands[3],
64+
operands[4],
65+
(SqlNodeList) operands[5]);
66+
}
67+
};
5668

5769
/** Creates a SqlCreateForeignSchema. */
5870
SqlCreateForeignSchema(SqlParserPos pos, boolean replace, boolean ifNotExists,
@@ -69,7 +81,10 @@ public class SqlCreateForeignSchema extends SqlCreate {
6981

7082
@SuppressWarnings("nullness")
7183
@Override public List<SqlNode> getOperandList() {
72-
return ImmutableNullableList.of(name, type, library, optionList);
84+
return ImmutableNullableList.of(
85+
SqlLiteral.createBoolean(getReplace(), SqlParserPos.ZERO),
86+
SqlLiteral.createBoolean(ifNotExists, SqlParserPos.ZERO),
87+
name, type, library, optionList);
7388
}
7489

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

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.calcite.sql.ddl;
1818

19+
import org.apache.calcite.sql.SqlCall;
1920
import org.apache.calcite.sql.SqlCreate;
2021
import org.apache.calcite.sql.SqlIdentifier;
2122
import org.apache.calcite.sql.SqlKind;
@@ -29,7 +30,10 @@
2930
import org.apache.calcite.util.Pair;
3031
import org.apache.calcite.util.Util;
3132

32-
import java.util.Arrays;
33+
import com.google.common.collect.ImmutableList;
34+
35+
import org.checkerframework.checker.nullness.qual.Nullable;
36+
3337
import java.util.List;
3438

3539
import static com.google.common.base.Preconditions.checkArgument;
@@ -44,8 +48,18 @@ public class SqlCreateFunction extends SqlCreate {
4448
private final SqlNode className;
4549
private final SqlNodeList usingList;
4650

47-
private static final SqlSpecialOperator OPERATOR =
48-
new SqlSpecialOperator("CREATE FUNCTION", SqlKind.CREATE_FUNCTION);
51+
private static final SqlOperator OPERATOR =
52+
new SqlSpecialOperator("CREATE FUNCTION", SqlKind.CREATE_FUNCTION) {
53+
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
54+
SqlParserPos pos, @Nullable SqlNode... operands) {
55+
return new SqlCreateFunction(pos,
56+
((SqlLiteral) requireNonNull(operands[0], "replace")).booleanValue(),
57+
((SqlLiteral) requireNonNull(operands[1], "ifNotExists")).booleanValue(),
58+
(SqlIdentifier) requireNonNull(operands[2], "name"),
59+
requireNonNull(operands[3], "className"),
60+
(SqlNodeList) requireNonNull(operands[4], "usingList"));
61+
}
62+
};
4963

5064
/** Creates a SqlCreateFunction. */
5165
public SqlCreateFunction(SqlParserPos pos, boolean replace,
@@ -91,6 +105,9 @@ private List<Pair<SqlLiteral, SqlLiteral>> pairs() {
91105
}
92106

93107
@Override public List<SqlNode> getOperandList() {
94-
return Arrays.asList(name, className, usingList);
108+
return ImmutableList.of(
109+
SqlLiteral.createBoolean(getReplace(), SqlParserPos.ZERO),
110+
SqlLiteral.createBoolean(ifNotExists, SqlParserPos.ZERO),
111+
name, className, usingList);
95112
}
96113
}

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
*/
1717
package org.apache.calcite.sql.ddl;
1818

19+
import org.apache.calcite.sql.SqlCall;
1920
import org.apache.calcite.sql.SqlCreate;
2021
import org.apache.calcite.sql.SqlIdentifier;
2122
import org.apache.calcite.sql.SqlKind;
23+
import org.apache.calcite.sql.SqlLiteral;
2224
import org.apache.calcite.sql.SqlNode;
2325
import org.apache.calcite.sql.SqlNodeList;
2426
import org.apache.calcite.sql.SqlOperator;
@@ -42,8 +44,17 @@ public class SqlCreateMaterializedView extends SqlCreate {
4244
public final SqlNode query;
4345

4446
private static final SqlOperator OPERATOR =
45-
new SqlSpecialOperator("CREATE MATERIALIZED VIEW",
46-
SqlKind.CREATE_MATERIALIZED_VIEW);
47+
new SqlSpecialOperator("CREATE MATERIALIZED VIEW", SqlKind.CREATE_MATERIALIZED_VIEW) {
48+
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
49+
SqlParserPos pos, @Nullable SqlNode... operands) {
50+
return new SqlCreateMaterializedView(pos,
51+
((SqlLiteral) requireNonNull(operands[0], "replace")).booleanValue(),
52+
((SqlLiteral) requireNonNull(operands[1], "ifNotExists")).booleanValue(),
53+
(SqlIdentifier) requireNonNull(operands[2], "name"),
54+
(SqlNodeList) operands[3],
55+
requireNonNull(operands[4], "query"));
56+
}
57+
};
4758

4859
/** Creates a SqlCreateView. */
4960
SqlCreateMaterializedView(SqlParserPos pos, boolean replace,
@@ -57,7 +68,10 @@ public class SqlCreateMaterializedView extends SqlCreate {
5768

5869
@SuppressWarnings("nullness")
5970
@Override public List<SqlNode> getOperandList() {
60-
return ImmutableNullableList.of(name, columnList, query);
71+
return ImmutableNullableList.of(
72+
SqlLiteral.createBoolean(getReplace(), SqlParserPos.ZERO),
73+
SqlLiteral.createBoolean(ifNotExists, SqlParserPos.ZERO),
74+
name, columnList, query);
6175
}
6276

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

0 commit comments

Comments
 (0)