Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support parse opengauss create index #28291

Closed
wants to merge 13 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ collationName
;

indexName
: (owner DOT_)? name
;

indexPartitionName
: identifier
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ accessMethod

createIndex
: CREATE createIndexSpecification INDEX concurrentlyClause (ifNotExists? indexName)? ON onlyClause tableName
accessMethodClause? LP_ indexParams RP_ include? (WITH reloptions)? tableSpace? whereClause?
accessMethodClause? LP_ indexParams RP_ include? createIndexPartitionClause? (WITH reloptions)? tableSpace? whereClause?
;

include
Expand Down Expand Up @@ -316,6 +316,19 @@ createIndexSpecification
: UNIQUE?
;

createIndexPartitionClause
: LOCAL (LP_ indexPartitionElemList RP_)?
| GLOBAL
;

indexPartitionElemList
: indexPartitionElem (COMMA_ indexPartitionElem)*
;

indexPartitionElem
: PARTITION indexPartitionName tableSpace?
;

concurrentlyClause
: CONCURRENTLY?
;
Expand Down Expand Up @@ -347,7 +360,13 @@ partitionCmd
;

alterIndexDefinitionClause
: renameIndexSpecification | alterIndexDependsOnExtension | alterIndexSetTableSpace | alterTableCmds | indexPartitionCmd
: renameIndexSpecification
| alterIndexDependsOnExtension
| alterIndexSetTableSpace
| alterTableCmds
| indexPartitionCmd
| alterIndexMovePartition
| alterIndexRenamePartition
;

indexPartitionCmd
Expand All @@ -366,6 +385,14 @@ alterIndexSetTableSpace
: (OWNED BY ignoredIdentifiers)? SET TABLESPACE name (NOWAIT)?
;

alterIndexMovePartition
: MOVE PARTITION indexPartitionName TABLESPACE name
;

alterIndexRenamePartition
: RENAME PARTITION indexPartitionName TO indexPartitionName
;

tableNamesClause
: tableNameClause (COMMA_ tableNameClause)*
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

package org.apache.shardingsphere.sql.parser.opengauss.visitor.statement;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import lombok.AccessLevel;
import lombok.Getter;
import org.antlr.v4.runtime.ParserRuleContext;
Expand All @@ -26,9 +32,12 @@
import org.apache.shardingsphere.infra.database.core.metadata.database.enums.NullsOrderType;
import org.apache.shardingsphere.sql.parser.api.ASTNode;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementBaseVisitor;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AExprContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AexprConstContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AliasClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AlterIndexMovePartitionContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AlterIndexRenamePartitionContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AnyNameContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AscDescContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.AssignmentContext;
Expand All @@ -42,6 +51,7 @@
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ColumnNamesContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ColumnrefContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.ConstraintNameContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.CreateIndexPartitionClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DataTypeContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DataTypeLengthContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.DataTypeNameContext;
Expand All @@ -61,6 +71,8 @@
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IdentifierContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InExprContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IndexNameContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IndexPartitionElemContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IndexPartitionElemListContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.IndirectionContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InsertColumnItemContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.InsertColumnListContext;
Expand Down Expand Up @@ -100,6 +112,7 @@
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TableNameContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TableNamesContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TableReferenceContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TableSpaceContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TargetElContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.TargetListContext;
import org.apache.shardingsphere.sql.parser.autogen.OpenGaussStatementParser.UnreservedWordContext;
Expand All @@ -120,6 +133,8 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.constraint.ConstraintSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexNameSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.partition.PartitionNameSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.tablespace.TablespaceSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.ReturningSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.AssignmentSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.ColumnAssignmentSegment;
Expand Down Expand Up @@ -194,13 +209,11 @@
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dml.OpenGaussInsertStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dml.OpenGaussSelectStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.dml.OpenGaussUpdateStatement;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.IndexPartitionSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.IndexPartitionTypeEnum;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.IndexPartitionsSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.MovePartitionSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.RenamePartitionSegment;

/**
* Statement visitor for openGauss.
Expand Down Expand Up @@ -261,8 +274,13 @@ public final ASTNode visitColumnName(final ColumnNameContext ctx) {

@Override
public final ASTNode visitIndexName(final IndexNameContext ctx) {
IndexNameSegment indexName = new IndexNameSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), (IdentifierValue) visit(ctx.identifier()));
return new IndexSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), indexName);
IndexNameSegment indexName = new IndexNameSegment(ctx.name().getStart().getStartIndex(), ctx.name().getStop().getStopIndex(), (IdentifierValue) visit(ctx.name()));
IndexSegment result = new IndexSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), indexName);
OwnerContext owner = ctx.owner();
if (null != owner) {
result.setOwner(new OwnerSegment(owner.getStart().getStartIndex(), owner.getStop().getStopIndex(), (IdentifierValue) visit(owner.identifier())));
}
return result;
}

@Override
Expand Down Expand Up @@ -1407,4 +1425,60 @@ public ASTNode visitAttrs(final AttrsContext ctx) {
public ASTNode visitSignedIconst(final SignedIconstContext ctx) {
return new NumberLiteralValue(ctx.getText());
}

@SuppressWarnings("unchecked")
@Override
public ASTNode visitCreateIndexPartitionClause(final CreateIndexPartitionClauseContext ctx) {
IndexPartitionTypeEnum indexPartitionType = null;
if (null != ctx.LOCAL() || null != ctx.GLOBAL()) {
indexPartitionType = ctx.LOCAL() != null ? IndexPartitionTypeEnum.LOCAL : IndexPartitionTypeEnum.GLOBAL;
}
IndexPartitionsSegment result = new IndexPartitionsSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), indexPartitionType);
if (null != ctx.indexPartitionElemList()) {
result.getIndexPartitionSegment().addAll(((CollectionValue<IndexPartitionSegment>) visit(ctx.indexPartitionElemList())).getValue());
}
return result;
}

@Override
public ASTNode visitIndexPartitionElemList(final IndexPartitionElemListContext ctx) {
CollectionValue<IndexPartitionSegment> result = new CollectionValue<>();
for (OpenGaussStatementParser.IndexPartitionElemContext each : ctx.indexPartitionElem()) {
result.getValue().add((IndexPartitionSegment) visit(each));
}
return result;
}

@Override
public ASTNode visitIndexPartitionElem(final IndexPartitionElemContext ctx) {
PartitionNameSegment partitionName =
new PartitionNameSegment(ctx.indexPartitionName().getStart().getStartIndex(), ctx.indexPartitionName().getStop().getStopIndex(), (IdentifierValue) visit(ctx.indexPartitionName()));
IndexPartitionSegment result = new IndexPartitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), partitionName);
if (null != ctx.tableSpace()) {
result.setTablespace((TablespaceSegment) visit(ctx.tableSpace()));
}
return result;
}

@Override
public ASTNode visitTableSpace(final TableSpaceContext ctx) {
return new TablespaceSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (IdentifierValue) visit(ctx.name()));
}

@Override
public ASTNode visitAlterIndexMovePartition(final AlterIndexMovePartitionContext ctx) {
PartitionNameSegment partitionName = new PartitionNameSegment(ctx.indexPartitionName().getStart().getStartIndex(), ctx.indexPartitionName().getStop().getStopIndex(),
(IdentifierValue) visit(ctx.indexPartitionName().identifier()));
TablespaceSegment tablespace = new TablespaceSegment(ctx.name().getStart().getStartIndex(), ctx.name().getStop().getStopIndex(), (IdentifierValue) visit(ctx.name().identifier()));
return new MovePartitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), partitionName, tablespace);
}

@Override
public ASTNode visitAlterIndexRenamePartition(final AlterIndexRenamePartitionContext ctx) {
PartitionNameSegment oldPartition = new PartitionNameSegment(ctx.indexPartitionName(0).getStart().getStartIndex(),
ctx.indexPartitionName(0).getStop().getStopIndex(), (IdentifierValue) visit(ctx.indexPartitionName(0).identifier()));
PartitionNameSegment newPartition = new PartitionNameSegment(ctx.indexPartitionName(1).getStart().getStartIndex(),
ctx.indexPartitionName(1).getStop().getStopIndex(), (IdentifierValue) visit(ctx.indexPartitionName(1).identifier()));
return new RenamePartitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), oldPartition, newPartition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexNameSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.table.RenameTableDefinitionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.tablespace.TablespaceSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
Expand Down Expand Up @@ -253,6 +254,9 @@
import java.util.LinkedList;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.IndexPartitionsSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.MovePartitionSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.RenamePartitionSegment;

/**
* DDL statement visitor for openGauss.
Expand Down Expand Up @@ -529,6 +533,12 @@ public ASTNode visitCreateIndex(final CreateIndexContext ctx) {
} else {
result.setIndex((IndexSegment) visit(ctx.indexName()));
}
if (null != ctx.createIndexPartitionClause()) {
result.setIndexPartitionsSegment((IndexPartitionsSegment) visit(ctx.createIndexPartitionClause()));
}
if (null != ctx.tableSpace()) {
result.setTablespace((TablespaceSegment) visit(ctx.tableSpace()));
}
return result;
}

Expand Down Expand Up @@ -576,6 +586,12 @@ public ASTNode visitAlterIndex(final AlterIndexContext ctx) {
if (null != ctx.alterIndexDefinitionClause().renameIndexSpecification()) {
result.setRenameIndex((IndexSegment) visit(ctx.alterIndexDefinitionClause().renameIndexSpecification().indexName()));
}
if (null != ctx.alterIndexDefinitionClause().alterIndexMovePartition()) {
result.setMovePartition((MovePartitionSegment) visit(ctx.alterIndexDefinitionClause().alterIndexMovePartition()));
}
if (null != ctx.alterIndexDefinitionClause().alterIndexRenamePartition()) {
result.setRenamePartition((RenamePartitionSegment) visit(ctx.alterIndexDefinitionClause().alterIndexRenamePartition()));
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.shardingsphere.sql.parser.sql.common.segment.ddl.partition;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.common.segment.SQLSegment;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;

/**
* Partition name segment.
*/
@RequiredArgsConstructor
@Getter
public class PartitionNameSegment implements SQLSegment {

private final int startIndex;

private final int stopIndex;

private final IdentifierValue identifier;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.table.AlgorithmTypeSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.table.LockTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.tablespace.TablespaceSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateIndexStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.SQLStatementHandler;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateIndexStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.OpenGaussStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.ddl.OpenGaussCreateIndexStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.IndexPartitionsSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLCreateIndexStatement;

Expand Down Expand Up @@ -94,4 +96,31 @@ public static Optional<LockTableSegment> getLockTableSegment(final CreateIndexSt
}
return Optional.empty();
}

/**
* Get index partitions segment.
*
* @param createIndexStatement create index statement
* @return index partitions segment
*/
public static Optional<IndexPartitionsSegment> getIndexPartitionsSegment(final CreateIndexStatement createIndexStatement) {
if (createIndexStatement instanceof OpenGaussCreateIndexStatement) {
return ((OpenGaussCreateIndexStatement) createIndexStatement).getIndexPartitionsSegment();
}
return Optional.empty();
}

/**
* Get tablespace segment.
*
* @param createIndexStatement create index statement
* @return tablespace segment
*/
public static Optional<TablespaceSegment> getTablespaceSegment(final CreateIndexStatement createIndexStatement) {
if (createIndexStatement instanceof OpenGaussCreateIndexStatement) {
return ((OpenGaussCreateIndexStatement) createIndexStatement).getTablespace();
}
return Optional.empty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.OpenGaussStatement;

import java.util.Optional;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.MovePartitionSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.opengauss.segment.RenamePartitionSegment;

/**
* OpenGauss alter index statement.
Expand All @@ -32,6 +34,10 @@ public final class OpenGaussAlterIndexStatement extends AlterIndexStatement impl

private IndexSegment renameIndex;

private MovePartitionSegment movePartition;

private RenamePartitionSegment renamePartition;

/**
* Get rename index segment.
*
Expand All @@ -40,4 +46,22 @@ public final class OpenGaussAlterIndexStatement extends AlterIndexStatement impl
public Optional<IndexSegment> getRenameIndex() {
return Optional.ofNullable(renameIndex);
}

/**
* Get move partition segment.
*
* @return move partition segment
*/
public Optional<MovePartitionSegment> getMovePartition() {
return Optional.ofNullable(movePartition);
}

/**
* Get rename partition segment.
*
* @return rename partition segment
*/
public Optional<RenamePartitionSegment> getRenamePartition() {
return Optional.ofNullable(renamePartition);
}
}
Loading
Loading