Skip to content

Commit

Permalink
IGNITE-24019 Extend SQL parser grammar with CREATE/DROP SCHEMA statem…
Browse files Browse the repository at this point in the history
…ents.
  • Loading branch information
xtern committed Dec 24, 2024
1 parent 8e49333 commit b20f1d9
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 2 deletions.
9 changes: 7 additions & 2 deletions modules/sql-engine/src/main/codegen/config.fmpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ data: {
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlPrimaryKeyConstraint",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlPrimaryKeyIndexType",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlCreateZone",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlCreateSchema",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlZoneOption",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlDropIndex",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlDropTable",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlDropZone",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlDropSchema",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlDropSchemaPolicy",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlStartTransaction",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlStartTransactionMode",
"org.apache.ignite.internal.sql.engine.sql.IgniteSqlCommitTransaction",
Expand Down Expand Up @@ -322,7 +325,8 @@ data: {
createStatementParserMethods: [
"SqlCreateTable",
"SqlCreateIndex",
"SqlCreateZone"
"SqlCreateZone",
"SqlCreateSchema"
]

# List of methods for parsing extensions to "DROP" calls.
Expand All @@ -331,7 +335,8 @@ data: {
dropStatementParserMethods: [
"SqlDropTable",
"SqlDropIndex",
"SqlDropZone"
"SqlDropZone",
"SqlDropSchema"
]

# List of methods for parsing extensions to "DROP" calls.
Expand Down
44 changes: 44 additions & 0 deletions modules/sql-engine/src/main/codegen/includes/parserImpls.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,24 @@ SqlCreate SqlCreateIndex(Span s, boolean replace) :
}
}

SqlCreate SqlCreateSchema(Span s, boolean replace) :
{
final boolean ifNotExists;
final SqlIdentifier id;
SqlNodeList optionList = null;
}
{
<SCHEMA> { s.add(this); }
ifNotExists = IfNotExistsOpt()
id = CompoundIdentifier()
[
<WITH> { s.add(this); } optionList = CreateZoneOptionList()
]
{
return new IgniteSqlCreateSchema(s.end(this), ifNotExists, id);
}
}

boolean IfExistsOpt() :
{
}
Expand Down Expand Up @@ -317,6 +335,32 @@ SqlDrop SqlDropIndex(Span s, boolean replace) :
}
}

SqlDrop SqlDropSchema(Span s, boolean replace) :
{
final SqlIdentifier schemaName;
final boolean ifExists;
IgniteSqlDropSchemaPolicy dropPolicy = IgniteSqlDropSchemaPolicy.IMPLICIT_RESTRICT;
}
{
<SCHEMA>
ifExists = IfExistsOpt()
schemaName = CompoundIdentifier()
[
(
<CASCADE> {
dropPolicy = IgniteSqlDropSchemaPolicy.CASCADE;
}
|
<RESTRICT> {
dropPolicy = IgniteSqlDropSchemaPolicy.RESTRICT;
}
)
]
{
return new IgniteSqlDropSchema(s.end(this), ifExists, schemaName, dropPolicy);
}
}

void InfixCast(List<Object> list, ExprContext exprContext, Span s) :
{
final SqlDataTypeSpec dt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.ignite.internal.sql.engine.sql;

import java.util.List;
import java.util.Objects;
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.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.ImmutableNullableList;
import org.jetbrains.annotations.Nullable;

/**
* Parse tree for {@code CREATE SCHEMA} statement with Ignite specific features.
*/
public class IgniteSqlCreateSchema extends SqlCreate {

/** CREATE ZONE operator. */
protected static class Operator extends IgniteDdlOperator {

/** Constructor. */
protected Operator(boolean existFlag) {
super("CREATE SCHEMA", SqlKind.OTHER_DDL, existFlag);
}

/** {@inheritDoc} */
@Override
public SqlCall createCall(@Nullable SqlLiteral functionQualifier,
SqlParserPos pos, @Nullable SqlNode... operands) {

return new IgniteSqlCreateSchema(pos, existFlag(), (SqlIdentifier) operands[0]);
}
}

private final SqlIdentifier name;

/** Creates a SqlCreateSchema. */
public IgniteSqlCreateSchema(
SqlParserPos pos,
boolean ifNotExists,
SqlIdentifier name
) {
super(new Operator(ifNotExists), pos, false, ifNotExists);

this.name = Objects.requireNonNull(name, "name");
}

/** {@inheritDoc} */
@Override
public IgniteDdlOperator getOperator() {
return (IgniteDdlOperator) super.getOperator();
}

/** {@inheritDoc} */
@SuppressWarnings("nullness")
@Override
public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(name);
}

/** {@inheritDoc} */
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword("CREATE");
writer.keyword("SCHEMA");
if (ifNotExists()) {
writer.keyword("IF NOT EXISTS");
}

name.unparse(writer, leftPrec, rightPrec);
}

/**
* Get name of the schema.
*/
public SqlIdentifier name() {
return name;
}

/**
* Get whether the IF NOT EXISTS is specified.
*/
public boolean ifNotExists() {
return ifNotExists;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.ignite.internal.sql.engine.sql;

import java.util.List;
import java.util.Objects;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDrop;
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.SqlWriter;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.util.ImmutableNullableList;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Parse tree for {@code DROP SCHEMA} statement.
*/
public class IgniteSqlDropSchema extends SqlDrop {

/** DROP ZONE operator. */
protected static class Operator extends IgniteDdlOperator {
private final IgniteSqlDropSchemaPolicy dropPolicy;

/** Constructor. */
protected Operator(boolean existFlag, IgniteSqlDropSchemaPolicy dropPolicy) {
super("DROP SCHEMA", SqlKind.OTHER_DDL, existFlag);

this.dropPolicy = dropPolicy;
}

/** {@inheritDoc} */
@Override
public SqlCall createCall(@Nullable SqlLiteral functionQualifier, SqlParserPos pos,
@Nullable SqlNode... operands) {
return new IgniteSqlDropSchema(pos, existFlag(), (SqlIdentifier) operands[0], dropPolicy);
}
}

/** Zone name. */
private final SqlIdentifier name;

private final IgniteSqlDropSchemaPolicy dropPolicy;

/** Constructor. */
public IgniteSqlDropSchema(SqlParserPos pos, boolean ifExists, SqlIdentifier name, IgniteSqlDropSchemaPolicy dropPolicy) {
super(new Operator(ifExists, dropPolicy), pos, ifExists);

this.name = Objects.requireNonNull(name, "schema name");
this.dropPolicy = dropPolicy;
}

/** {@inheritDoc} */
@Override
public IgniteDdlOperator getOperator() {
return (IgniteDdlOperator) super.getOperator();
}

/** {@inheritDoc} */
@Override
public List<SqlNode> getOperandList() {
return ImmutableNullableList.of(name);
}

/** {@inheritDoc} */
@Override
public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
writer.keyword(getOperator().getName()); // "DROP ..."

if (ifExists) {
writer.keyword("IF EXISTS");
}

name.unparse(writer, leftPrec, rightPrec);

if (dropPolicy != IgniteSqlDropSchemaPolicy.IMPLICIT_RESTRICT) {
writer.keyword(dropPolicy.name());
}
}

public SqlIdentifier name() {
return name;
}

public boolean ifExists() {
Operator operator = (Operator) getOperator();
return operator.existFlag();
}

public IgniteSqlDropSchemaPolicy dropPolicy() {
return dropPolicy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.apache.ignite.internal.sql.engine.sql;

public enum IgniteSqlDropSchemaPolicy {
CASCADE,
RESTRICT,
IMPLICIT_RESTRICT
}
Loading

0 comments on commit b20f1d9

Please sign in to comment.