You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/06/16 02:31:25 UTC
[shardingsphere] branch master updated: support postgresql create
index without index name (#10816)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new ecfd17d support postgresql create index without index name (#10816)
ecfd17d is described below
commit ecfd17d15586ebc4e5d8b3417af1966c343f1a51
Author: Zhengqiang Duan <st...@gmail.com>
AuthorDate: Wed Jun 16 10:31:00 2021 +0800
support postgresql create index without index name (#10816)
* support postgresql create index without index name
* add test case
* fix checkstyle
* optimize for code review
* fix unit test error
---
.../sharding/rewrite/token/pojo/IndexToken.java | 8 +++-
.../ShardingCreateIndexStatementValidator.java | 3 +-
.../ShardingCreateIndexStatementValidatorTest.java | 42 ++++++++++++++++++-
.../statement/ddl/CreateIndexStatementContext.java | 14 ++++++-
.../schema/builder/util/IndexMetaDataUtil.java | 25 +++++++++++-
.../type/CreateIndexStatementSchemaRefresher.java | 6 ++-
.../impl/PostgreSQLDDLStatementSQLVisitor.java | 17 ++++++++
.../common/statement/ddl/CreateIndexStatement.java | 6 +++
.../handler/ddl/CreateIndexStatementHandler.java | 47 ++++++++++++++++++++++
.../ddl/PostgreSQLCreateIndexStatement.java | 15 +++++++
.../resources/scenario/sharding/case/index.xml | 12 ++++++
11 files changed, 185 insertions(+), 10 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java
index 6d9c0d3..8acc4f7 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rewrite/token/pojo/IndexToken.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.sharding.rewrite.token.pojo;
import lombok.Getter;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.ddl.CreateIndexStatementContext;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.builder.util.IndexMetaDataUtil;
import org.apache.shardingsphere.infra.rewrite.sql.token.pojo.RouteUnitAware;
@@ -62,7 +63,12 @@ public final class IndexToken extends SQLToken implements Substitutable, RouteUn
@Override
public String toString(final RouteUnit routeUnit) {
- return identifier.getQuoteCharacter().wrap(getIndexValue(routeUnit));
+ String quotedIndexName = identifier.getQuoteCharacter().wrap(getIndexValue(routeUnit));
+ return isGeneratedIndex() ? " " + quotedIndexName + " " : quotedIndexName;
+ }
+
+ private boolean isGeneratedIndex() {
+ return sqlStatementContext instanceof CreateIndexStatementContext && ((CreateIndexStatementContext) sqlStatementContext).isGeneratedIndex();
}
private String getIndexValue(final RouteUnit routeUnit) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java
index 1e7cb1e..16ed97d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/impl/ShardingCreateIndexStatementValidator.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.type.IndexAvailable;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.route.context.RouteContext;
@@ -38,7 +39,7 @@ public final class ShardingCreateIndexStatementValidator extends ShardingDDLStat
final List<Object> parameters, final ShardingSphereSchema schema) {
validateTableExist(schema, Collections.singletonList(sqlStatementContext.getSqlStatement().getTable()));
String tableName = sqlStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
- String indexName = sqlStatementContext.getSqlStatement().getIndex().getIdentifier().getValue();
+ String indexName = ((IndexAvailable) sqlStatementContext).getIndexes().stream().map(each -> each.getIdentifier().getValue()).findFirst().orElse(null);
if (schema.get(tableName).getIndexes().containsKey(indexName)) {
throw new ShardingSphereException("Index '%s' already exists.", indexName);
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java
index 5709ce9..c020798 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/ddl/ShardingCreateIndexStatementValidatorTest.java
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.sharding.route.engine.exception.NoSuchTableExce
import org.apache.shardingsphere.sharding.route.engine.validator.ddl.impl.ShardingCreateIndexStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLCreateIndexStatement;
@@ -70,8 +71,6 @@ public final class ShardingCreateIndexStatementValidatorTest {
sqlStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("t_order")));
sqlStatement.setIndex(new IndexSegment(0, 0, new IdentifierValue("t_order_index")));
when(schema.containsTable("t_order")).thenReturn(false);
- TableMetaData tableMetaData = mock(TableMetaData.class);
- Map<String, IndexMetaData> indexes = mock(HashMap.class);
new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), schema);
}
@@ -88,4 +87,43 @@ public final class ShardingCreateIndexStatementValidatorTest {
when(indexes.containsKey("t_order_index")).thenReturn(true);
new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), schema);
}
+
+ @Test
+ public void assertPreValidateCreateIndexWithoutIndexNameWhenTableExistIndexNotExistForPostgreSQL() {
+ PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement();
+ sqlStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("t_order")));
+ sqlStatement.setColumns(Collections.singletonList(new ColumnSegment(0, 0, new IdentifierValue("content"))));
+ sqlStatement.setGeneratedIndexStartIndex(10);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ TableMetaData tableMetaData = mock(TableMetaData.class);
+ when(schema.get("t_order")).thenReturn(tableMetaData);
+ Map<String, IndexMetaData> indexes = mock(HashMap.class);
+ when(tableMetaData.getIndexes()).thenReturn(indexes);
+ new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), schema);
+ }
+
+ @Test(expected = NoSuchTableException.class)
+ public void assertPreValidateCreateIndexWithoutIndexNameWhenTableNotExistIndexNotExistForPostgreSQL() {
+ PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement();
+ sqlStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("t_order")));
+ sqlStatement.setColumns(Collections.singletonList(new ColumnSegment(0, 0, new IdentifierValue("content"))));
+ sqlStatement.setGeneratedIndexStartIndex(10);
+ when(schema.containsTable("t_order")).thenReturn(false);
+ new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), schema);
+ }
+
+ @Test(expected = ShardingSphereException.class)
+ public void assertPreValidateCreateIndexWithoutIndexNameWhenTableExistIndexExistForPostgreSQL() {
+ PostgreSQLCreateIndexStatement sqlStatement = new PostgreSQLCreateIndexStatement();
+ sqlStatement.setTable(new SimpleTableSegment(0, 0, new IdentifierValue("t_order")));
+ sqlStatement.setColumns(Collections.singletonList(new ColumnSegment(0, 0, new IdentifierValue("content"))));
+ sqlStatement.setGeneratedIndexStartIndex(10);
+ when(schema.containsTable("t_order")).thenReturn(true);
+ TableMetaData tableMetaData = mock(TableMetaData.class);
+ when(schema.get("t_order")).thenReturn(tableMetaData);
+ Map<String, IndexMetaData> indexes = mock(HashMap.class);
+ when(tableMetaData.getIndexes()).thenReturn(indexes);
+ when(indexes.containsKey("content_idx")).thenReturn(true);
+ new ShardingCreateIndexStatementValidator().preValidate(shardingRule, new CreateIndexStatementContext(sqlStatement), Collections.emptyList(), schema);
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateIndexStatementContext.java b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateIndexStatementContext.java
index 587c4c4..c43e47f 100644
--- a/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateIndexStatementContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/ddl/CreateIndexStatementContext.java
@@ -19,12 +19,15 @@ package org.apache.shardingsphere.infra.binder.statement.ddl;
import lombok.Getter;
import org.apache.shardingsphere.infra.binder.segment.table.TablesContext;
+import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.binder.type.IndexAvailable;
import org.apache.shardingsphere.infra.binder.type.TableAvailable;
-import org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
+import org.apache.shardingsphere.infra.metadata.schema.builder.util.IndexMetaDataUtil;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateIndexStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.CreateIndexStatementHandler;
import java.util.Collection;
import java.util.Collections;
@@ -37,9 +40,12 @@ public final class CreateIndexStatementContext extends CommonSQLStatementContext
private final TablesContext tablesContext;
+ private final boolean generatedIndex;
+
public CreateIndexStatementContext(final CreateIndexStatement sqlStatement) {
super(sqlStatement);
tablesContext = new TablesContext(sqlStatement.getTable());
+ generatedIndex = null == sqlStatement.getIndex();
}
@Override
@@ -49,6 +55,10 @@ public final class CreateIndexStatementContext extends CommonSQLStatementContext
@Override
public Collection<IndexSegment> getIndexes() {
- return null == getSqlStatement().getIndex() ? Collections.emptyList() : Collections.singletonList(getSqlStatement().getIndex());
+ if (null != getSqlStatement().getIndex()) {
+ return Collections.singletonList(getSqlStatement().getIndex());
+ }
+ return CreateIndexStatementHandler.getGeneratedIndexStartIndex(getSqlStatement()).map(each -> Collections.singletonList(new IndexSegment(each, each,
+ new IdentifierValue(IndexMetaDataUtil.getGeneratedLogicIndexName(getSqlStatement().getColumns()))))).orElse(Collections.emptyList());
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/util/IndexMetaDataUtil.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/util/IndexMetaDataUtil.java
index af20919..a305615 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/util/IndexMetaDataUtil.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/util/IndexMetaDataUtil.java
@@ -20,6 +20,9 @@ package org.apache.shardingsphere.infra.metadata.schema.builder.util;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+
+import java.util.Collection;
/**
* Index meta data utility class.
@@ -27,6 +30,10 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class IndexMetaDataUtil {
+ private static final String UNDERLINE = "_";
+
+ private static final String GENERATED_LOGIC_INDEX_NAME_SUFFIX = "idx";
+
/**
* Get logic index name.
*
@@ -35,7 +42,7 @@ public class IndexMetaDataUtil {
* @return logic index name
*/
public static String getLogicIndexName(final String actualIndexName, final String actualTableName) {
- String indexNameSuffix = "_" + actualTableName;
+ String indexNameSuffix = UNDERLINE + actualTableName;
return actualIndexName.endsWith(indexNameSuffix) ? actualIndexName.substring(0, actualIndexName.lastIndexOf(indexNameSuffix)) : actualIndexName;
}
@@ -47,6 +54,20 @@ public class IndexMetaDataUtil {
* @return actual index name
*/
public static String getActualIndexName(final String logicIndexName, final String actualTableName) {
- return Strings.isNullOrEmpty(actualTableName) ? logicIndexName : logicIndexName + "_" + actualTableName;
+ return Strings.isNullOrEmpty(actualTableName) ? logicIndexName : logicIndexName + UNDERLINE + actualTableName;
+ }
+
+ /**
+ * Get generated logic index name.
+ *
+ * @param columns column segments
+ * @return generated logic index name
+ */
+ public static String getGeneratedLogicIndexName(final Collection<ColumnSegment> columns) {
+ StringBuilder builder = new StringBuilder();
+ for (ColumnSegment each : columns) {
+ builder.append(each.getIdentifier().getValue()).append(UNDERLINE);
+ }
+ return builder.append(GENERATED_LOGIC_INDEX_NAME_SUFFIX).toString();
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/refresher/type/CreateIndexStatementSchemaRefresher.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/refresher/type/CreateIndexStatementSchemaRefresher.java
index 348a98c..5dd526a 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/refresher/type/CreateIndexStatementSchemaRefresher.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/refresher/type/CreateIndexStatementSchemaRefresher.java
@@ -17,8 +17,10 @@
package org.apache.shardingsphere.infra.metadata.schema.refresher.type;
+import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.builder.SchemaBuilderMaterials;
+import org.apache.shardingsphere.infra.metadata.schema.builder.util.IndexMetaDataUtil;
import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData;
import org.apache.shardingsphere.infra.metadata.schema.refresher.SchemaRefresher;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateIndexStatement;
@@ -32,11 +34,11 @@ public final class CreateIndexStatementSchemaRefresher implements SchemaRefreshe
@Override
public void refresh(final ShardingSphereSchema schema, final Collection<String> routeDataSourceNames, final CreateIndexStatement sqlStatement, final SchemaBuilderMaterials materials) {
- if (null == sqlStatement.getIndex()) {
+ String indexName = null != sqlStatement.getIndex() ? sqlStatement.getIndex().getIdentifier().getValue() : IndexMetaDataUtil.getGeneratedLogicIndexName(sqlStatement.getColumns());
+ if (Strings.isNullOrEmpty(indexName)) {
return;
}
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue();
- String indexName = sqlStatement.getIndex().getIdentifier().getValue();
schema.get(tableName).getIndexes().put(indexName, new IndexMetaData(indexName));
}
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
index 81c10b4..78b171f 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-postgresql/src/main/java/org/apache/shardingsphere/sql/parser/postgresql/visitor/statement/impl/PostgreSQLDDLStatementSQLVisitor.java
@@ -55,8 +55,10 @@ import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.Dr
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.DropTableContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.DropTablespaceContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.DropViewContext;
+import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.IndexElemContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.IndexNameContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.IndexNamesContext;
+import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.IndexParamsContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ModifyColumnSpecificationContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.ModifyConstraintSpecificationContext;
import org.apache.shardingsphere.sql.parser.autogen.PostgreSQLStatementParser.PrepareContext;
@@ -359,12 +361,27 @@ public final class PostgreSQLDDLStatementSQLVisitor extends PostgreSQLStatementS
return result;
}
+ @SuppressWarnings("unchecked")
@Override
public ASTNode visitCreateIndex(final CreateIndexContext ctx) {
PostgreSQLCreateIndexStatement result = new PostgreSQLCreateIndexStatement();
result.setTable((SimpleTableSegment) visit(ctx.tableName()));
+ result.setColumns(((CollectionValue<ColumnSegment>) visit(ctx.indexParams())).getValue());
if (null != ctx.indexName()) {
result.setIndex((IndexSegment) visit(ctx.indexName()));
+ } else {
+ result.setGeneratedIndexStartIndex(ctx.ON().getSymbol().getStartIndex() - 1);
+ }
+ return result;
+ }
+
+ @Override
+ public ASTNode visitIndexParams(final IndexParamsContext ctx) {
+ CollectionValue<ColumnSegment> result = new CollectionValue<>();
+ for (IndexElemContext each : ctx.indexElem()) {
+ if (null != each.colId()) {
+ result.getValue().add(new ColumnSegment(each.colId().start.getStartIndex(), each.colId().stop.getStopIndex(), new IdentifierValue(each.colId().getText())));
+ }
}
return result;
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/ddl/CreateIndexStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/ddl/CreateIndexStatement.java
index b80262d..0ba74f5 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/ddl/CreateIndexStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/statement/ddl/CreateIndexStatement.java
@@ -21,9 +21,13 @@ import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.AbstractSQLStatement;
+import java.util.Collection;
+import java.util.LinkedList;
+
/**
* Create index statement.
*/
@@ -35,4 +39,6 @@ public abstract class CreateIndexStatement extends AbstractSQLStatement implemen
private IndexSegment index;
private SimpleTableSegment table;
+
+ private Collection<ColumnSegment> columns = new LinkedList<>();
}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/CreateIndexStatementHandler.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/CreateIndexStatementHandler.java
new file mode 100644
index 0000000..caf2918
--- /dev/null
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/CreateIndexStatementHandler.java
@@ -0,0 +1,47 @@
+/*
+ * 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.dialect.handler.ddl;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+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.postgresql.PostgreSQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl.PostgreSQLCreateIndexStatement;
+
+import java.util.Optional;
+
+/**
+ * Create index statement handler for different dialect SQL statements.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class CreateIndexStatementHandler implements SQLStatementHandler {
+
+ /**
+ * Get generated index start index.
+ *
+ * @param createIndexStatement create index statement
+ * @return generated index start index
+ */
+ public static Optional<Integer> getGeneratedIndexStartIndex(final CreateIndexStatement createIndexStatement) {
+ if (createIndexStatement instanceof PostgreSQLStatement) {
+ return ((PostgreSQLCreateIndexStatement) createIndexStatement).getGeneratedIndexStartIndex();
+ }
+ return Optional.empty();
+ }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/ddl/PostgreSQLCreateIndexStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/ddl/PostgreSQLCreateIndexStatement.java
index 8463a4b..703ff0c 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/ddl/PostgreSQLCreateIndexStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/postgresql/ddl/PostgreSQLCreateIndexStatement.java
@@ -17,13 +17,28 @@
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.ddl;
+import lombok.Setter;
import lombok.ToString;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateIndexStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.postgresql.PostgreSQLStatement;
+import java.util.Optional;
+
/**
* PostgreSQL create index statement.
*/
+@Setter
@ToString
public final class PostgreSQLCreateIndexStatement extends CreateIndexStatement implements PostgreSQLStatement {
+
+ private Integer generatedIndexStartIndex;
+
+ /**
+ * Get generated index start index.
+ *
+ * @return generated index start index
+ */
+ public Optional<Integer> getGeneratedIndexStartIndex() {
+ return Optional.ofNullable(generatedIndexStartIndex);
+ }
}
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/index.xml b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/index.xml
index 7b35101..571ab44 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/index.xml
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/index.xml
@@ -29,6 +29,18 @@
<output sql="CREATE INDEX index_name_not_exist_t_account_1 ON t_account_1 (status)" />
</rewrite-assertion>
+ <rewrite-assertion id="create_index_without_index_name_with_single_column_for_postgresql" db-type="PostgreSQL">
+ <input sql="CREATE INDEX ON t_account (status)" />
+ <output sql="CREATE INDEX status_idx_t_account_0 ON t_account_0 (status)" />
+ <output sql="CREATE INDEX status_idx_t_account_1 ON t_account_1 (status)" />
+ </rewrite-assertion>
+
+ <rewrite-assertion id="create_index_without_index_name_with_multi_column_for_postgresql" db-type="PostgreSQL">
+ <input sql="CREATE INDEX ON t_account (status, account_id)" />
+ <output sql="CREATE INDEX status_account_id_idx_t_account_0 ON t_account_0 (status, account_id)" />
+ <output sql="CREATE INDEX status_account_id_idx_t_account_1 ON t_account_1 (status, account_id)" />
+ </rewrite-assertion>
+
<rewrite-assertion id="drop_index_for_mysql" db-type="MySQL">
<input sql="DROP INDEX index_name ON t_account" />
<output sql="DROP INDEX index_name_t_account_0 ON t_account_0" />