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 2022/07/13 04:29:00 UTC

[shardingsphere] branch master updated: Revise #19039 (#19087)

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 04f011aa1b9 Revise #19039 (#19087)
04f011aa1b9 is described below

commit 04f011aa1b9892c93679da192db0e1000c00bcc2
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Wed Jul 13 12:28:53 2022 +0800

    Revise #19039 (#19087)
---
 .../mysql/constant/MySQLServerErrorCode.java       |  2 +-
 .../postgresql/constant/PostgreSQLErrorCode.java   | 28 +++++++++++++++++++-
 .../impl/InsertClauseShardingConditionEngine.java  | 12 ++++-----
 .../InsertClauseShardingConditionEngineTest.java   | 11 +++-----
 ...InsertColumnsAndValuesMismatchedException.java} | 30 ++++++++++------------
 .../frontend/mysql/err/MySQLErrPacketFactory.java  |  6 ++---
 .../postgresql/err/PostgreSQLErrPacketFactory.java |  6 ++---
 7 files changed, 57 insertions(+), 38 deletions(-)

diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java
index dfc570763ad..7dc22c1a0d8 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/constant/MySQLServerErrorCode.java
@@ -58,7 +58,7 @@ public enum MySQLServerErrorCode implements SQLErrorCode {
     
     ER_UNKNOWN_CHARACTER_SET(1115, "42000", "Unknown character set: '%s'"),
     
-    ER_MISMATCH_COLUMN_COUNT(1136, "21S01", "Column count doesn't match value count at row %d"),
+    ER_WRONG_VALUE_COUNT_ON_ROW(1136, "21S01", "Column count doesn't match value count at row %d"),
     
     ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE(3176, "HY000",
             "Please do not modify the %s table with an XA transaction. This is an internal system table used to store GTIDs for committed transactions. "
diff --git a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLErrorCode.java b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLErrorCode.java
index 1e312e3ca3e..652f6248f49 100644
--- a/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLErrorCode.java
+++ b/shardingsphere-db-protocol/shardingsphere-db-protocol-postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/constant/PostgreSQLErrorCode.java
@@ -30,31 +30,57 @@ import lombok.RequiredArgsConstructor;
 public enum PostgreSQLErrorCode {
     
     SUCCESSFUL_COMPLETION("00000", "successful_completion"),
+    
     WARNING("01000", "warning"),
+    
     DYNAMIC_RESULT_SETS_RETURNED("0100C", "dynamic_result_sets_returned"),
+    
     IMPLICIT_ZERO_BIT_PADDING("01008", "implicit_zero_bit_padding"),
+    
     NULL_VALUE_ELIMINATED_IN_SET_FUNCTION("01003", "null_value_eliminated_in_set_function"),
+    
     PRIVILEGE_NOT_GRANTED("01007", "privilege_not_granted"),
+    
     PRIVILEGE_NOT_REVOKED("01006", "privilege_not_revoked"),
+    
     STRING_DATA_RIGHT_TRUNCATION("01004", "string_data_right_truncation"),
+    
     DEPRECATED_FEATURE("01P01", "deprecated_feature"),
+    
     CONNECTION_EXCEPTION("08000", "connection_exception"),
+    
     CONNECTION_DOES_NOT_EXIST("08003", "connection_does_not_exist"),
+    
     CONNECTION_FAILURE("08006", "connection_failure"),
+    
     SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION("08001", "sqlclient_unable_to_establish_sqlconnection"),
+    
     SQLSERVER_REJECTED_ESTABLISHMENT_OF_SQLCONNECTION("08004", "sqlserver_rejected_establishment_of_sqlconnection"),
+    
     TRANSACTION_RESOLUTION_UNKNOWN("08007", "transaction_resolution_unknown"),
+    
     MODIFYING_SQL_DATA_NOT_PERMITTED("38002", "modifying_sql_data_not_permitted"),
+    
     PROTOCOL_VIOLATION("08P01", "protocol_violation"),
+    
     FEATURE_NOT_SUPPORTED("0A000", "feature_not_supported"),
+    
     DUPLICATE_DATABASE("42P04", "Database '%s' already exists"),
+    
     INVALID_AUTHORIZATION_SPECIFICATION("28000", "invalid_authorization_specification"),
-    ER_MISMATCH_COLUMN_COUNT("42601", "syntax_error"),
+    
+    SYNTAX_ERROR("42601", "syntax_error"),
+    
     INVALID_PARAMETER_VALUE("22023", "invalid_parameter_value"),
+    
     INVALID_PASSWORD("28P01", "invalid_password"),
+    
     INVALID_CATALOG_NAME("3D000", "invalid_catalog_name"),
+    
     INVALID_SCHEMA_NAME("3F000", "invalid_schema_name"),
+    
     UNDEFINED_COLUMN("42703", "undefined_column"),
+    
     SYSTEM_ERROR("58000", "system_error");
     
     private final String errorCode;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
index f347607aeba..f468b085147 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/impl/InsertClauseShardingConditionEngine.java
@@ -25,7 +25,7 @@ import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementConte
 import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
 import org.apache.shardingsphere.infra.datetime.DatetimeService;
 import org.apache.shardingsphere.infra.datetime.DatetimeServiceFactory;
-import org.apache.shardingsphere.infra.exception.ColumnMismatchException;
+import org.apache.shardingsphere.infra.exception.InsertColumnsAndValuesMismatchedException;
 import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.sharding.route.engine.condition.ExpressionConditionUtils;
@@ -73,9 +73,9 @@ public final class InsertClauseShardingConditionEngine implements ShardingCondit
         Collection<String> columnNames = getColumnNames(sqlStatementContext);
         List<InsertValueContext> insertValueContexts = sqlStatementContext.getInsertValueContexts();
         List<ShardingCondition> result = new ArrayList<>(insertValueContexts.size());
-        int row = 0;
+        int rowNumber = 0;
         for (InsertValueContext each : insertValueContexts) {
-            result.add(createShardingCondition(tableName, columnNames.iterator(), each, parameters, ++row));
+            result.add(createShardingCondition(tableName, columnNames.iterator(), each, parameters, ++rowNumber));
         }
         return result;
     }
@@ -90,13 +90,13 @@ public final class InsertClauseShardingConditionEngine implements ShardingCondit
         return insertStatementContext.getColumnNames();
     }
     
-    private ShardingCondition createShardingCondition(final String tableName, final Iterator<String> columnNames, final InsertValueContext insertValueContext,
-                                                      final List<Object> parameters, final int row) {
+    private ShardingCondition createShardingCondition(final String tableName, final Iterator<String> columnNames,
+                                                      final InsertValueContext insertValueContext, final List<Object> parameters, final int rowNumber) {
         ShardingCondition result = new ShardingCondition();
         DatetimeService datetimeService = null;
         for (ExpressionSegment each : insertValueContext.getValueExpressions()) {
             if (!columnNames.hasNext()) {
-                throw new ColumnMismatchException(row);
+                throw new InsertColumnsAndValuesMismatchedException(rowNumber);
             }
             Optional<String> shardingColumn = shardingRule.findShardingColumn(columnNames.next(), tableName);
             if (!shardingColumn.isPresent()) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
index a468c738352..008d2dabe90 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/route/engine/condition/engine/InsertClauseShardingConditionEngineTest.java
@@ -21,7 +21,7 @@ import org.apache.shardingsphere.infra.binder.segment.insert.keygen.GeneratedKey
 import org.apache.shardingsphere.infra.binder.segment.insert.values.InsertSelectContext;
 import org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext;
 import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
-import org.apache.shardingsphere.infra.exception.ColumnMismatchException;
+import org.apache.shardingsphere.infra.exception.InsertColumnsAndValuesMismatchedException;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.sharding.route.engine.condition.ShardingCondition;
 import org.apache.shardingsphere.sharding.route.engine.condition.engine.impl.InsertClauseShardingConditionEngine;
@@ -109,18 +109,15 @@ public final class InsertClauseShardingConditionEngineTest {
         assertThat(shardingConditions.get(0).getValues().size(), is(1));
     }
     
-    @Test(expected = ColumnMismatchException.class)
+    @Test(expected = InsertColumnsAndValuesMismatchedException.class)
     public void assertCreateShardingConditionsInsertStatementWithMismatchColumns() {
-        when(insertStatementContext.getInsertValueContexts()).thenReturn(Collections.singletonList(createInsertValueContextWithMultiAssignments()));
+        InsertValueContext insertValueContext = new InsertValueContext(Arrays.asList(new LiteralExpressionSegment(0, 10, "1"), new LiteralExpressionSegment(0, 10, "1")), Collections.emptyList(), 0);
+        when(insertStatementContext.getInsertValueContexts()).thenReturn(Collections.singletonList(insertValueContext));
         when(shardingRule.findShardingColumn(any(), any())).thenReturn(Optional.of("foo_sharding_col"));
         when(insertStatementContext.getColumnNames()).thenReturn(Collections.singletonList("foo_col1"));
         shardingConditionEngine.createShardingConditions(insertStatementContext, Collections.emptyList());
     }
     
-    private InsertValueContext createInsertValueContextWithMultiAssignments() {
-        return new InsertValueContext(Arrays.asList(new LiteralExpressionSegment(0, 10, "1"), new LiteralExpressionSegment(0, 10, "1")), Collections.emptyList(), 0);
-    }
-    
     @Test
     public void assertCreateShardingConditionsInsertStatementWithGeneratedKeyContextUsingCommonExpressionSegmentNow() {
         when(insertStatementContext.getInsertValueContexts()).thenReturn(Collections.singletonList(createInsertValueContextAsCommonExpressionSegmentWithNow()));
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/ColumnMismatchException.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/InsertColumnsAndValuesMismatchedException.java
similarity index 65%
rename from shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/ColumnMismatchException.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/InsertColumnsAndValuesMismatchedException.java
index dfea6225b8b..53f6ac3a7ca 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/ColumnMismatchException.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/exception/InsertColumnsAndValuesMismatchedException.java
@@ -19,24 +19,20 @@ package org.apache.shardingsphere.infra.exception;
 
 import lombok.Getter;
 
+/**
+ * Insert columns and values mismatched exception.
+ */
 @Getter
-public final class ColumnMismatchException extends ShardingSphereException {
-
+public final class InsertColumnsAndValuesMismatchedException extends ShardingSphereException {
+    
     private static final long serialVersionUID = 5676889868213244575L;
-
-    private static final String DEFAULT_MSG = "INSERT has more expressions than target columns";
-
-    private final int row;
-
-    private final String message;
-
-    public ColumnMismatchException(final int row, final String message) {
-        super(message);
-        this.row = row;
-        this.message = message;
-    }
-
-    public ColumnMismatchException(final int row) {
-        this(row, DEFAULT_MSG);
+    
+    private static final String ERROR_MESSAGE = "The count columns and values are mismatched in INSERT statement.";
+    
+    private final int mismatchedRowNumber;
+    
+    public InsertColumnsAndValuesMismatchedException(final int mismatchedRowNumber) {
+        super(ERROR_MESSAGE);
+        this.mismatchedRowNumber = mismatchedRowNumber;
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
index 7f5cae40bb8..69eff040188 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrPacketFactory.java
@@ -25,7 +25,7 @@ import org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
 import org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
 import org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
 import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
-import org.apache.shardingsphere.infra.exception.ColumnMismatchException;
+import org.apache.shardingsphere.infra.exception.InsertColumnsAndValuesMismatchedException;
 import org.apache.shardingsphere.infra.exception.DatabaseNotExistedException;
 import org.apache.shardingsphere.proxy.backend.exception.CircuitBreakException;
 import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
@@ -75,8 +75,8 @@ public final class MySQLErrPacketFactory {
         if (cause instanceof TableModifyInTransactionException) {
             return new MySQLErrPacket(1, MySQLServerErrorCode.ER_ERROR_ON_MODIFYING_GTID_EXECUTED_TABLE, ((TableModifyInTransactionException) cause).getTableName());
         }
-        if (cause instanceof ColumnMismatchException) {
-            return new MySQLErrPacket(1, MySQLServerErrorCode.ER_MISMATCH_COLUMN_COUNT, ((ColumnMismatchException) cause).getRow());
+        if (cause instanceof InsertColumnsAndValuesMismatchedException) {
+            return new MySQLErrPacket(1, MySQLServerErrorCode.ER_WRONG_VALUE_COUNT_ON_ROW, ((InsertColumnsAndValuesMismatchedException) cause).getMismatchedRowNumber());
         }
         if (cause instanceof UnknownDatabaseException) {
             return new MySQLErrPacket(1, MySQLServerErrorCode.ER_BAD_DB_ERROR, ((UnknownDatabaseException) cause).getDatabaseName());
diff --git a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
index e5dfe98f88c..af1a46f71bd 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
@@ -23,7 +23,7 @@ import lombok.NoArgsConstructor;
 import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLErrorCode;
 import org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLMessageSeverityLevel;
 import org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
-import org.apache.shardingsphere.infra.exception.ColumnMismatchException;
+import org.apache.shardingsphere.infra.exception.InsertColumnsAndValuesMismatchedException;
 import org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.InTransactionException;
 import org.apache.shardingsphere.proxy.backend.exception.UnsupportedUpdateOperationException;
@@ -58,8 +58,8 @@ public final class PostgreSQLErrPacketFactory {
         if (cause instanceof SQLException) {
             return createErrorResponsePacket((SQLException) cause);
         }
-        if (cause instanceof ColumnMismatchException) {
-            return PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.ERROR, PostgreSQLErrorCode.ER_MISMATCH_COLUMN_COUNT, cause.getMessage()).build();
+        if (cause instanceof InsertColumnsAndValuesMismatchedException) {
+            return PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.ERROR, PostgreSQLErrorCode.SYNTAX_ERROR, cause.getMessage()).build();
         }
         if (cause instanceof InvalidAuthorizationSpecificationException) {
             return PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.FATAL, PostgreSQLErrorCode.INVALID_AUTHORIZATION_SPECIFICATION, cause.getMessage()).build();