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 10 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 @@ -348,6 +361,7 @@ partitionCmd

alterIndexDefinitionClause
: renameIndexSpecification | alterIndexDependsOnExtension | alterIndexSetTableSpace | alterTableCmds | indexPartitionCmd
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write the rules one by one? According to the code specification, when there are more than 5 branches, a rule needs to be written one line.

| alterIndexMovePartition | alterIndexRenamePartition
;

indexPartitionCmd
Expand All @@ -366,6 +380,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 @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -120,6 +121,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 @@ -201,6 +204,11 @@
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 +269,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 +1420,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 OpenGaussStatementParser.CreateIndexPartitionClauseContext ctx) {
IndexPartitionTypeEnum indexPartitionType = null;
if (ctx.LOCAL() != null || ctx.GLOBAL() != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put null codition on the left side.

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 OpenGaussStatementParser.IndexPartitionElemListContext ctx) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please import subclass directly.

CollectionValue<IndexPartitionSegment> result = new CollectionValue<>();
for (OpenGaussStatementParser.IndexPartitionElemContext each : ctx.indexPartitionElem()) {
result.getValue().add((IndexPartitionSegment) visit(each));
}
return result;
}

@Override
public ASTNode visitIndexPartitionElem(final OpenGaussStatementParser.IndexPartitionElemContext ctx) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please import subclass directly.

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 OpenGaussStatementParser.TableSpaceContext ctx) {
return new TablespaceSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (IdentifierValue) visit(ctx.name()));
}

@Override
public ASTNode visitAlterIndexMovePartition(final OpenGaussStatementParser.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 OpenGaussStatementParser.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,38 @@
/*
* 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;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove useless blank line.

}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
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.statement.opengauss.OpenGaussStatement;

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

/**
* OpenGauss create index statement.
Expand All @@ -37,6 +39,10 @@ public final class OpenGaussCreateIndexStatement extends CreateIndexStatement im

private Integer generatedIndexStartIndex;

private IndexPartitionsSegment indexPartitionsSegment;

private TablespaceSegment tablespace;

/**
* Get generated index start index.
*
Expand All @@ -45,4 +51,21 @@ public final class OpenGaussCreateIndexStatement extends CreateIndexStatement im
public Optional<Integer> getGeneratedIndexStartIndex() {
return Optional.ofNullable(generatedIndexStartIndex);
}

/**
* Get index partitions segment.
*
* @return index partitions segment
*/
public Optional<IndexPartitionsSegment> getIndexPartitionsSegment() {
return Optional.ofNullable(indexPartitionsSegment);
}

/**
* Get tablespace.
* @return tablespace
*/
public Optional<TablespaceSegment> getTablespace() {
return Optional.ofNullable(tablespace);
}
}
Loading