Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlCollation.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.apache.calcite.sql;

import org.apache.calcite.config.CalciteSystemProperty;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.parser.SqlParserUtil;
import org.apache.calcite.util.Glossary;
import org.apache.calcite.util.ImmutableNullableList;
import org.apache.calcite.util.SerializableCharset;
import org.apache.calcite.util.Util;

Expand All @@ -37,6 +39,8 @@

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

import static java.util.Objects.requireNonNull;

/**
* A <code>SqlCollation</code> is an object representing a <code>Collate</code>
* statement. It is immutable.
Expand Down Expand Up @@ -113,6 +117,25 @@ public SqlCollation(
this.collationName = generateCollationName(charset);
}

/** Encode all the information require to reconstruct a SqlCollection in a SqlList object. */
public SqlNodeList asList() {
return new SqlNodeList(
ImmutableNullableList.of(
SqlLiteral.createCharString(this.getCollationName(), SqlParserPos.ZERO),
SqlLiteral.createSymbol(coercibility, SqlParserPos.ZERO)),
SqlParserPos.ZERO);
}

/** The inverse of the {@link #asList} function. */
public static SqlCollation fromSqlList(SqlNodeList list) {
assert list.size() == 2;
String name = ((SqlLiteral) list.get(0)).getValueAs(String.class);
Coercibility coercibility = ((SqlLiteral) list.get(1)).symbolValue(Coercibility.class);
return new SqlCollation(
name,
requireNonNull(coercibility, "coercibility"));
}

/**
* Creates a Collation by its coercibility, locale, charset and strength.
*/
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/java/org/apache/calcite/sql/SqlLambda.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ private static class SqlLambdaOperator extends SqlSpecialOperator {
super("->", SqlKind.LAMBDA);
}

@Override public SqlCall createCall(
@Nullable SqlLiteral functionQualifier, SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlLambda(pos,
(SqlNodeList) requireNonNull(operands[0], "parameters"),
requireNonNull(operands[1], "expression"));
}

@Override public RelDataType deriveType(
SqlValidator validator, SqlValidatorScope scope, SqlCall call) {
final SqlLambda lambdaExpr = (SqlLambda) call;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,37 @@
import org.apache.calcite.sql.SqlDataTypeSpec;
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.parser.SqlParserPos;

import com.google.common.collect.ImmutableList;
import org.apache.calcite.util.ImmutableNullableList;

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

import java.util.List;

import static java.util.Objects.requireNonNull;

/**
* Parse tree for SqlAttributeDefinition,
* which is part of a {@link SqlCreateType}.
*/
public class SqlAttributeDefinition extends SqlCall {
private static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("ATTRIBUTE_DEF", SqlKind.ATTRIBUTE_DEF);
private static final SqlOperator OPERATOR =
new SqlSpecialOperator("ATTRIBUTE_DEF", SqlKind.ATTRIBUTE_DEF) {
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlAttributeDefinition(pos,
(SqlIdentifier) requireNonNull(operands[0], "name"),
(SqlDataTypeSpec) requireNonNull(operands[1], "dataType"),
operands[2],
operands[3] != null ? SqlCollation.fromSqlList((SqlNodeList) operands[3]) : null);
}
};

public final SqlIdentifier name;
public final SqlDataTypeSpec dataType;
Expand All @@ -60,8 +72,10 @@ public class SqlAttributeDefinition extends SqlCall {
return OPERATOR;
}

@SuppressWarnings("nullness")
@Override public List<SqlNode> getOperandList() {
return ImmutableList.of(name, dataType);
return ImmutableNullableList.of(name, dataType, expression,
collation != null ? collation.asList() : null);
}

@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.calcite.sql.SqlCall;
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.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
Expand All @@ -30,14 +31,23 @@

import java.util.List;

import static java.util.Objects.requireNonNull;

/**
* Parse tree for {@code UNIQUE}, {@code PRIMARY KEY} constraints.
*
* <p>And {@code FOREIGN KEY}, when we support it.
*/
public class SqlCheckConstraint extends SqlCall {
private static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("CHECK", SqlKind.CHECK);
private static final SqlOperator OPERATOR =
new SqlSpecialOperator("CHECK", SqlKind.CHECK) {
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlCheckConstraint(pos,
(SqlIdentifier) operands[0],
requireNonNull(operands[1], "expression"));
}
};

private final @Nullable SqlIdentifier name;
private final SqlNode expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,50 @@
import org.apache.calcite.sql.SqlDataTypeSpec;
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.SqlOperator;
import org.apache.calcite.sql.SqlSpecialOperator;
import org.apache.calcite.sql.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;

import com.google.common.collect.ImmutableList;
import org.apache.calcite.util.ImmutableNullableList;

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

import java.util.List;

import static java.util.Objects.requireNonNull;

/**
* Parse tree for {@code UNIQUE}, {@code PRIMARY KEY} constraints.
*
* <p>And {@code FOREIGN KEY}, when we support it.
*/
public class SqlColumnDeclaration extends SqlCall {
private static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("COLUMN_DECL", SqlKind.COLUMN_DECL);
private static final SqlOperator OPERATOR =
new SqlSpecialOperator("COLUMN_DECL", SqlKind.COLUMN_DECL) {
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlColumnDeclaration(pos,
(SqlIdentifier) requireNonNull(operands[0], "name"),
(SqlDataTypeSpec) requireNonNull(operands[1], "dataType"),
operands[2],
operands[3] != null
? ColumnStrategy.valueOf(((SqlIdentifier) operands[3]).getSimple())
: null);
}
};

public final SqlIdentifier name;
public final SqlDataTypeSpec dataType;
public final @Nullable SqlNode expression;
public final ColumnStrategy strategy;
// The Babel parser can supply null for the strategy
public final @Nullable ColumnStrategy strategy;

/** Creates a SqlColumnDeclaration; use {@link SqlDdlNodes#column}. */
SqlColumnDeclaration(SqlParserPos pos, SqlIdentifier name,
SqlDataTypeSpec dataType, @Nullable SqlNode expression,
ColumnStrategy strategy) {
@Nullable ColumnStrategy strategy) {
super(pos);
this.name = name;
this.dataType = dataType;
Expand All @@ -62,8 +76,10 @@ public class SqlColumnDeclaration extends SqlCall {
return OPERATOR;
}

@SuppressWarnings("nullness")
@Override public List<SqlNode> getOperandList() {
return ImmutableList.of(name, dataType);
return ImmutableNullableList.of(name, dataType, expression,
strategy != null ? new SqlIdentifier(strategy.name(), SqlParserPos.ZERO) : null);
}

@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
Expand All @@ -73,7 +89,7 @@ public class SqlColumnDeclaration extends SqlCall {
writer.keyword("NOT NULL");
}
SqlNode expression = this.expression;
if (expression != null) {
if (expression != null && strategy != null) {
switch (strategy) {
case VIRTUAL:
case STORED:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
*/
package org.apache.calcite.sql.ddl;

import org.apache.calcite.sql.SqlCall;
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;
Expand Down Expand Up @@ -51,8 +53,18 @@ public class SqlCreateForeignSchema extends SqlCreate {
private final @Nullable SqlNodeList optionList;

private static final SqlOperator OPERATOR =
new SqlSpecialOperator("CREATE FOREIGN SCHEMA",
SqlKind.CREATE_FOREIGN_SCHEMA);
new SqlSpecialOperator("CREATE FOREIGN SCHEMA", SqlKind.CREATE_FOREIGN_SCHEMA) {
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlCreateForeignSchema(pos,
((SqlLiteral) requireNonNull(operands[0], "replace")).booleanValue(),
((SqlLiteral) requireNonNull(operands[1], "ifNotExists")).booleanValue(),
(SqlIdentifier) requireNonNull(operands[2], "name"),
operands[3],
operands[4],
(SqlNodeList) operands[5]);
}
};

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

@SuppressWarnings("nullness")
@Override public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(name, type, library, optionList);
return ImmutableNullableList.of(
SqlLiteral.createBoolean(getReplace(), SqlParserPos.ZERO),
SqlLiteral.createBoolean(ifNotExists, SqlParserPos.ZERO),
name, type, library, optionList);
}

@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.calcite.sql.ddl;

import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlCreate;
import org.apache.calcite.sql.SqlIdentifier;
import org.apache.calcite.sql.SqlKind;
Expand All @@ -29,7 +30,10 @@
import org.apache.calcite.util.Pair;
import org.apache.calcite.util.Util;

import java.util.Arrays;
import com.google.common.collect.ImmutableList;

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

import java.util.List;

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

private static final SqlSpecialOperator OPERATOR =
new SqlSpecialOperator("CREATE FUNCTION", SqlKind.CREATE_FUNCTION);
private static final SqlOperator OPERATOR =
new SqlSpecialOperator("CREATE FUNCTION", SqlKind.CREATE_FUNCTION) {
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlCreateFunction(pos,
((SqlLiteral) requireNonNull(operands[0], "replace")).booleanValue(),
((SqlLiteral) requireNonNull(operands[1], "ifNotExists")).booleanValue(),
(SqlIdentifier) requireNonNull(operands[2], "name"),
requireNonNull(operands[3], "className"),
(SqlNodeList) requireNonNull(operands[4], "usingList"));
}
};

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

@Override public List<SqlNode> getOperandList() {
return Arrays.asList(name, className, usingList);
return ImmutableList.of(
SqlLiteral.createBoolean(getReplace(), SqlParserPos.ZERO),
SqlLiteral.createBoolean(ifNotExists, SqlParserPos.ZERO),
name, className, usingList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
*/
package org.apache.calcite.sql.ddl;

import org.apache.calcite.sql.SqlCall;
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;
Expand All @@ -42,8 +44,17 @@ public class SqlCreateMaterializedView extends SqlCreate {
public final SqlNode query;

private static final SqlOperator OPERATOR =
new SqlSpecialOperator("CREATE MATERIALIZED VIEW",
SqlKind.CREATE_MATERIALIZED_VIEW);
new SqlSpecialOperator("CREATE MATERIALIZED VIEW", SqlKind.CREATE_MATERIALIZED_VIEW) {
@Override public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {
return new SqlCreateMaterializedView(pos,
((SqlLiteral) requireNonNull(operands[0], "replace")).booleanValue(),
((SqlLiteral) requireNonNull(operands[1], "ifNotExists")).booleanValue(),
(SqlIdentifier) requireNonNull(operands[2], "name"),
(SqlNodeList) operands[3],
requireNonNull(operands[4], "query"));
}
};

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

@SuppressWarnings("nullness")
@Override public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(name, columnList, query);
return ImmutableNullableList.of(
SqlLiteral.createBoolean(getReplace(), SqlParserPos.ZERO),
SqlLiteral.createBoolean(ifNotExists, SqlParserPos.ZERO),
name, columnList, query);
}

@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
Expand Down
Loading
Loading