You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/09/05 15:30:29 UTC

[shardingsphere] branch master updated: Add ColumnLabelNotFoundException (#20806)

This is an automated email from the ASF dual-hosted git repository.

zhaojinchao 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 aa026394ca5 Add ColumnLabelNotFoundException (#20806)
aa026394ca5 is described below

commit aa026394ca505a93ae7153c9ca7bbc177c6cabc8
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Sep 5 23:30:19 2022 +0800

    Add ColumnLabelNotFoundException (#20806)
    
    * Add ColumnLabelNotFoundException
    
    * Add ColumnLabelNotFoundException
---
 .../user-manual/error-code/sql-error-code.cn.md    |  1 +
 .../user-manual/error-code/sql-error-code.en.md    |  1 +
 .../shardingsphere/sharding/rule/ShardingRule.java | 14 ++++-----
 .../core/resultset/DatabaseMetaDataResultSet.java  |  6 ++--
 .../exception/ColumnLabelNotFoundException.java    | 33 ++++++++++++++++++++++
 .../ddl/AlterIndexStatementHandlerTest.java        | 16 +++++------
 .../ddl/AlterSchemaStatementHandlerTest.java       |  2 +-
 7 files changed, 55 insertions(+), 18 deletions(-)

diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
index eff898aaac9..294e0adcbc8 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md
@@ -21,6 +21,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | HY000     | 10300       | Could not support variable \`%s\` |
 | HY004     | 10301       | Invalid value \`%s\` |
 | HV008     | 10302       | Column index \`%d\` is out of range |
+| 42S02     | 10303       | Can not find column label \`%s`\ |
 | HY004     | 10400       | Invalid format for actual data node \`%s\` |
 | HY000     | 10500       | Result set has been closed |
 | 42000     | 11000       | You have an error in your SQL syntax: %s |
diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md
index 0293d95312f..2f1eeceb4c1 100644
--- a/docs/document/content/user-manual/error-code/sql-error-code.en.md
+++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md
@@ -21,6 +21,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
 | HY000     | 10300       | Could not support variable \`%s\` |
 | HY004     | 10301       | Invalid value \`%s\` |
 | HV008     | 10302       | Column index \`%d\` is out of range |
+| 42S02     | 10303       | Can not find column label \`%s`\ |
 | HY004     | 10400       | Invalid format for actual data node \`%s\` |
 | HY000     | 10500       | Result set has been closed |
 | 42000     | 11000       | You have an error in your SQL syntax: %s |
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index 76276802aca..3c247477da4 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -22,7 +22,7 @@ import com.google.common.base.Splitter;
 import lombok.Getter;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.infra.config.algorithm.InstanceAwareAlgorithm;
+import org.apache.shardingsphere.infra.instance.InstanceContextAware;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine;
 import org.apache.shardingsphere.infra.datanode.DataNode;
@@ -135,9 +135,9 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
         Preconditions.checkArgument(isValidBindingTableConfiguration(tableRules, new BindingTableCheckedConfiguration(this.dataSourceNames, shardingAlgorithms, config.getBindingTableGroups(),
                 broadcastTables, defaultDatabaseShardingStrategyConfig, defaultTableShardingStrategyConfig, defaultShardingColumn)),
                 "Invalid binding table configuration in ShardingRuleConfiguration.");
-        keyGenerators.values().stream().filter(each -> each instanceof InstanceAwareAlgorithm).forEach(each -> ((InstanceAwareAlgorithm) each).setInstanceContext(instanceContext));
-        if (defaultKeyGenerateAlgorithm instanceof InstanceAwareAlgorithm) {
-            ((InstanceAwareAlgorithm) defaultKeyGenerateAlgorithm).setInstanceContext(instanceContext);
+        keyGenerators.values().stream().filter(each -> each instanceof InstanceContextAware).forEach(each -> ((InstanceContextAware) each).setInstanceContext(instanceContext));
+        if (defaultKeyGenerateAlgorithm instanceof InstanceContextAware) {
+            ((InstanceContextAware) defaultKeyGenerateAlgorithm).setInstanceContext(instanceContext);
         }
     }
     
@@ -162,9 +162,9 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
         Preconditions.checkArgument(isValidBindingTableConfiguration(tableRules, new BindingTableCheckedConfiguration(this.dataSourceNames, shardingAlgorithms, config.getBindingTableGroups(),
                 broadcastTables, defaultDatabaseShardingStrategyConfig, defaultTableShardingStrategyConfig, defaultShardingColumn)),
                 "Invalid binding table configuration in ShardingRuleConfiguration.");
-        keyGenerators.values().stream().filter(each -> each instanceof InstanceAwareAlgorithm).forEach(each -> ((InstanceAwareAlgorithm) each).setInstanceContext(instanceContext));
-        if (defaultKeyGenerateAlgorithm instanceof InstanceAwareAlgorithm) {
-            ((InstanceAwareAlgorithm) defaultKeyGenerateAlgorithm).setInstanceContext(instanceContext);
+        keyGenerators.values().stream().filter(each -> each instanceof InstanceContextAware).forEach(each -> ((InstanceContextAware) each).setInstanceContext(instanceContext));
+        if (defaultKeyGenerateAlgorithm instanceof InstanceContextAware) {
+            ((InstanceContextAware) defaultKeyGenerateAlgorithm).setInstanceContext(instanceContext);
         }
     }
     
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java
index 1d33bbd3377..f357a0c3c55 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/resultset/DatabaseMetaDataResultSet.java
@@ -18,6 +18,8 @@
 package org.apache.shardingsphere.driver.jdbc.core.resultset;
 
 import lombok.EqualsAndHashCode;
+import org.apache.shardingsphere.driver.jdbc.exception.ColumnIndexOutOfRangeException;
+import org.apache.shardingsphere.driver.jdbc.exception.ColumnLabelNotFoundException;
 import org.apache.shardingsphere.driver.jdbc.exception.ResultSetClosedException;
 import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedDatabaseMetaDataResultSet;
 import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.driver.jdbc.type.util.ResultSetUtil;
@@ -361,7 +363,7 @@ public final class DatabaseMetaDataResultSet extends AbstractUnsupportedDatabase
     public int findColumn(final String columnLabel) throws SQLException {
         checkClosed();
         if (!columnLabelIndexMap.containsKey(columnLabel)) {
-            throw new SQLException(String.format("Can not find columnLabel %s", columnLabel));
+            throw new ColumnLabelNotFoundException(columnLabel).toSQLException();
         }
         return columnLabelIndexMap.get(columnLabel);
     }
@@ -411,7 +413,7 @@ public final class DatabaseMetaDataResultSet extends AbstractUnsupportedDatabase
     
     private void checkColumnIndex(final int columnIndex) throws SQLException {
         if (columnIndex < 1 || columnIndex > resultSetMetaData.getColumnCount()) {
-            throw new SQLException(String.format("ColumnIndex %d out of range from %d to %d", columnIndex, 1, resultSetMetaData.getColumnCount()));
+            throw new ColumnIndexOutOfRangeException(columnIndex).toSQLException();
         }
     }
     
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/exception/ColumnLabelNotFoundException.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/exception/ColumnLabelNotFoundException.java
new file mode 100644
index 00000000000..f3ceb10b887
--- /dev/null
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/main/java/org/apache/shardingsphere/driver/jdbc/exception/ColumnLabelNotFoundException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.driver.jdbc.exception;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.ShardingSphereSQLException;
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+
+/**
+ * Column label not found exception.
+ */
+public final class ColumnLabelNotFoundException extends ShardingSphereSQLException {
+    
+    private static final long serialVersionUID = -4634399403612501335L;
+    
+    public ColumnLabelNotFoundException(final String columnLabel) {
+        super(XOpenSQLState.NOT_FOUND.getValue(), 10303, "Can not find column label `%s`", columnLabel);
+    }
+}
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/AlterIndexStatementHandlerTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/AlterIndexStatementHandlerTest.java
index 0300f980a20..fdb484a389b 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/AlterIndexStatementHandlerTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/AlterIndexStatementHandlerTest.java
@@ -17,11 +17,6 @@
 
 package org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl;
 
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-
 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.generic.table.SimpleTableSegment;
@@ -35,6 +30,11 @@ import org.junit.Test;
 
 import java.util.Optional;
 
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
 public final class AlterIndexStatementHandlerTest {
     
     @Test
@@ -66,7 +66,7 @@ public final class AlterIndexStatementHandlerTest {
         Optional<IndexSegment> indexSegment = AlterIndexStatementHandler.getRenameIndexSegment(alterIndexStatement);
         assertFalse(indexSegment.isPresent());
     }
-
+    
     @Test
     public void assertGetRenameIndexSegmentForOpenGauss() {
         IndexSegment indexSegment = new IndexSegment(0, 0, new IndexNameSegment(0, 0, new IdentifierValue("")));
@@ -76,14 +76,14 @@ public final class AlterIndexStatementHandlerTest {
         assertTrue(actual.isPresent());
         assertThat(actual.get(), is(indexSegment));
     }
-
+    
     @Test
     public void assertGetSimpleTableSegmentForOtherDatabases() {
         assertFalse(AlterIndexStatementHandler.getSimpleTableSegment(new OpenGaussAlterIndexStatement()).isPresent());
         assertFalse(AlterIndexStatementHandler.getSimpleTableSegment(new OracleAlterIndexStatement()).isPresent());
         assertFalse(AlterIndexStatementHandler.getSimpleTableSegment(new PostgreSQLAlterIndexStatement()).isPresent());
     }
-
+    
     @Test
     public void assertGetRenameIndexSegmentForOtherDatabases() {
         assertFalse(AlterIndexStatementHandler.getRenameIndexSegment(new OracleAlterIndexStatement()).isPresent());
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/AlterSchemaStatementHandlerTest.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/AlterSchemaStatementHandlerTest.java
index 55a36d65d84..911c353b860 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/AlterSchemaStatementHandlerTest.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/dialect/handler/ddl/AlterSchemaStatementHandlerTest.java
@@ -49,7 +49,7 @@ public final class AlterSchemaStatementHandlerTest {
         assertTrue(actual.isPresent());
         assertThat(actual.get().getValue(), is("new_schema"));
     }
-
+    
     @Test
     public void assertGetRenameSchemaForSQLServer() {
         SQLServerAlterSchemaStatement alterSchemaStatement = new SQLServerAlterSchemaStatement();