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

sunnianjun 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 51309509591 Optimize AmbiguousColumnException and 
UnknownColumnException message (#28234)
51309509591 is described below

commit 51309509591bb2f5b41c683fb56cf9c20682552f
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Aug 23 16:24:16 2023 +0800

    Optimize AmbiguousColumnException and UnknownColumnException message 
(#28234)
---
 .../user-manual/error-code/sql-error-code.cn.md    |  4 +--
 .../user-manual/error-code/sql-error-code.en.md    |  4 +--
 .../infra/binder/enums/SegmentType.java            |  2 +-
 .../expression/impl/ColumnSegmentBinder.java       | 32 ++++++++++++++--------
 .../segment/from/impl/JoinTableSegmentBinder.java  |  4 +--
 .../infra/exception/AmbiguousColumnException.java  |  4 +--
 .../infra/exception/UnknownColumnException.java    |  4 +--
 7 files changed, 31 insertions(+), 23 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 6f024a3dfa3..2966dd4ae89 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
@@ -19,8 +19,8 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | 42000     | 10002       | Can not support 3-tier structure for actual data 
node \`%s\` with JDBC \`%s\`. |
 | HY004     | 10003       | Invalid format for actual data node \`%s\`.        
                            |
 | 42000     | 10004       | Unsupported SQL node conversion for SQL statement 
\`%s\`.                      |
-| HY000     | 10005       | Column '%s' in field list is ambiguous.            
                            |
-| 42S02     | 10006       | Unknown column '%s' in 'field list'.               
                            |
+| HY000     | 10005       | Column '%s' in %s is ambiguous.                    
                            |
+| 42S02     | 10006       | Unknown column '%s' in '%s'.                       
                            |
 | 42000     | 10010       | Rule does not exist.                               
                            |
 | 42S02     | 10020       | Schema \`%s\` does not exist.                      
                            |
 | 42S02     | 10021       | Single table \`%s\` does not exist.                
                            |
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 0aa3bc05dbf..6e0746c654f 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
@@ -19,8 +19,8 @@ SQL error codes provide by standard `SQL State`, `Vendor 
Code` and `Reason`, whi
 | 42000     | 10002       | Can not support 3-tier structure for actual data 
node \`%s\` with JDBC \`%s\`. |
 | HY004     | 10003       | Invalid format for actual data node \`%s\`.        
                            |
 | 42000     | 10004       | Unsupported SQL node conversion for SQL statement 
\`%s\`.                      |
-| HY000     | 10005       | Column '%s' in field list is ambiguous.            
                            |
-| 42S02     | 10006       | Unknown column '%s' in 'field list'.               
                            |
+| HY000     | 10005       | Column '%s' in %s is ambiguous.                    
                            |
+| 42S02     | 10006       | Unknown column '%s' in '%s'.                       
                            |
 | 42000     | 10010       | Rule does not exist.                               
                            |
 | 42S02     | 10020       | Schema \`%s\` does not exist.                      
                            |
 | 42S02     | 10021       | Single table \`%s\` does not exist.                
                            |
diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/enums/SegmentType.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/enums/SegmentType.java
index f7b4429de8a..c723170fd05 100644
--- 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/enums/SegmentType.java
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/enums/SegmentType.java
@@ -22,5 +22,5 @@ package org.apache.shardingsphere.infra.binder.enums;
  */
 public enum SegmentType {
     
-    PROJECTION, PREDICATE, JOIN
+    PROJECTION, PREDICATE, JOIN_ON, JOIN_USING, ORDER_BY, GROUP_BY
 }
diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinder.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinder.java
index 72e038d3a80..bf809ff0de2 100644
--- 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinder.java
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/expression/impl/ColumnSegmentBinder.java
@@ -19,6 +19,7 @@ package 
org.apache.shardingsphere.infra.binder.segment.expression.impl;
 
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
+import org.apache.groovy.util.Maps;
 import org.apache.shardingsphere.infra.binder.enums.SegmentType;
 import 
org.apache.shardingsphere.infra.binder.segment.from.TableSegmentBinderContext;
 import 
org.apache.shardingsphere.infra.binder.statement.SQLStatementBinderContext;
@@ -48,6 +49,11 @@ public final class ColumnSegmentBinder {
     
     private static final Collection<String> EXCLUDE_BIND_COLUMNS = new 
LinkedHashSet<>(Arrays.asList("ROWNUM", "ROW_NUMBER", "ROWNUM_"));
     
+    private static final Map<SegmentType, String> SEGMENT_TYPE_MESSAGES = 
Maps.of(SegmentType.PROJECTION, "field list", SegmentType.JOIN_ON, "on clause", 
SegmentType.JOIN_USING, "from clause",
+            SegmentType.PREDICATE, "where clause", SegmentType.ORDER_BY, 
"order clause", SegmentType.GROUP_BY, "group statement");
+    
+    private static final String UNKNOWN_SEGMENT_TYPE_MESSAGE = "unknown 
clause";
+    
     /**
      * Bind column segment with metadata.
      *
@@ -67,7 +73,7 @@ public final class ColumnSegmentBinder {
         segment.getOwner().ifPresent(result::setOwner);
         Collection<TableSegmentBinderContext> tableBinderContextValues =
                 getTableSegmentBinderContexts(segment, parentSegmentType, 
statementBinderContext, tableBinderContexts, outerTableBinderContexts);
-        Optional<ColumnSegment> inputColumnSegment = 
findInputColumnSegment(segment.getIdentifier().getValue(), 
tableBinderContextValues);
+        Optional<ColumnSegment> inputColumnSegment = 
findInputColumnSegment(segment, parentSegmentType, tableBinderContextValues);
         result.setColumnBoundedInfo(createColumnSegmentBoundedInfo(segment, 
inputColumnSegment.orElse(null)));
         return result;
     }
@@ -101,18 +107,19 @@ public final class ColumnSegmentBinder {
         return Collections.emptyList();
     }
     
-    private static Optional<ColumnSegment> findInputColumnSegment(final String 
columnName, final Collection<TableSegmentBinderContext> tableBinderContexts) {
+    private static Optional<ColumnSegment> findInputColumnSegment(final 
ColumnSegment segment, final SegmentType parentSegmentType, final 
Collection<TableSegmentBinderContext> tableBinderContexts) {
         ProjectionSegment projectionSegment = null;
         ColumnSegment result = null;
         for (TableSegmentBinderContext each : tableBinderContexts) {
-            projectionSegment = 
each.getProjectionSegmentByColumnLabel(columnName);
+            projectionSegment = 
each.getProjectionSegmentByColumnLabel(segment.getIdentifier().getValue());
             if (projectionSegment instanceof ColumnProjectionSegment) {
-                ShardingSpherePreconditions.checkState(null == result, () -> 
new AmbiguousColumnException(columnName));
+                ShardingSpherePreconditions.checkState(null == result,
+                        () -> new 
AmbiguousColumnException(segment.getExpression(), 
SEGMENT_TYPE_MESSAGES.getOrDefault(parentSegmentType, 
UNKNOWN_SEGMENT_TYPE_MESSAGE)));
                 result = ((ColumnProjectionSegment) 
projectionSegment).getColumn();
             }
         }
-        // TODO optimize exception message according to different segment
-        ShardingSpherePreconditions.checkState(null != projectionSegment, () 
-> new UnknownColumnException(columnName));
+        ShardingSpherePreconditions.checkState(null != projectionSegment,
+                () -> new UnknownColumnException(segment.getExpression(), 
SEGMENT_TYPE_MESSAGES.getOrDefault(parentSegmentType, 
UNKNOWN_SEGMENT_TYPE_MESSAGE)));
         return Optional.ofNullable(result);
     }
     
@@ -132,19 +139,20 @@ public final class ColumnSegmentBinder {
      * Bind using column segment with metadata.
      *
      * @param segment using column segment
+     * @param parentSegmentType parent segment type
      * @param tableBinderContexts table binder contexts
      * @return bounded using column segment
      */
-    public static ColumnSegment bindUsingColumn(final ColumnSegment segment, 
final Map<String, TableSegmentBinderContext> tableBinderContexts) {
+    public static ColumnSegment bindUsingColumn(final ColumnSegment segment, 
final SegmentType parentSegmentType, final Map<String, 
TableSegmentBinderContext> tableBinderContexts) {
         ColumnSegment result = new ColumnSegment(segment.getStartIndex(), 
segment.getStopIndex(), segment.getIdentifier());
         segment.getOwner().ifPresent(result::setOwner);
         Collection<TableSegmentBinderContext> tableBinderContextValues = 
tableBinderContexts.values();
         Collection<ColumnSegment> usingInputColumnSegments = 
findUsingInputColumnSegments(segment.getIdentifier().getValue(), 
tableBinderContextValues);
-        if (usingInputColumnSegments.size() >= 2) {
-            Iterator<ColumnSegment> iterator = 
usingInputColumnSegments.iterator();
-            
result.setColumnBoundedInfo(createColumnSegmentBoundedInfo(segment, 
iterator.next()));
-            
result.setOtherUsingColumnBoundedInfo(createColumnSegmentBoundedInfo(segment, 
iterator.next()));
-        }
+        ShardingSpherePreconditions.checkState(usingInputColumnSegments.size() 
>= 2,
+                () -> new UnknownColumnException(segment.getExpression(), 
SEGMENT_TYPE_MESSAGES.getOrDefault(parentSegmentType, 
UNKNOWN_SEGMENT_TYPE_MESSAGE)));
+        Iterator<ColumnSegment> iterator = usingInputColumnSegments.iterator();
+        result.setColumnBoundedInfo(createColumnSegmentBoundedInfo(segment, 
iterator.next()));
+        
result.setOtherUsingColumnBoundedInfo(createColumnSegmentBoundedInfo(segment, 
iterator.next()));
         return result;
     }
     
diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/JoinTableSegmentBinder.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/JoinTableSegmentBinder.java
index 7a60bcf362e..558116010d3 100644
--- 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/JoinTableSegmentBinder.java
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/JoinTableSegmentBinder.java
@@ -69,7 +69,7 @@ public final class JoinTableSegmentBinder {
         result.setNatural(segment.isNatural());
         result.setJoinType(segment.getJoinType());
         result.setRight(TableSegmentBinder.bind(segment.getRight(), 
statementBinderContext, tableBinderContexts));
-        
result.setCondition(ExpressionSegmentBinder.bind(segment.getCondition(), 
SegmentType.JOIN, statementBinderContext, tableBinderContexts, 
Collections.emptyMap()));
+        
result.setCondition(ExpressionSegmentBinder.bind(segment.getCondition(), 
SegmentType.JOIN_ON, statementBinderContext, tableBinderContexts, 
Collections.emptyMap()));
         result.setUsing(bindUsingColumns(segment.getUsing(), 
tableBinderContexts));
         result.getUsing().forEach(each -> 
statementBinderContext.getUsingColumnNames().add(each.getIdentifier().getValue().toLowerCase()));
         Map<String, ProjectionSegment> usingColumnsByNaturalJoin = 
Collections.emptyMap();
@@ -98,7 +98,7 @@ public final class JoinTableSegmentBinder {
     private static List<ColumnSegment> bindUsingColumns(final 
Collection<ColumnSegment> usingColumns, final Map<String, 
TableSegmentBinderContext> tableBinderContexts) {
         List<ColumnSegment> result = new LinkedList<>();
         for (ColumnSegment each : usingColumns) {
-            result.add(ColumnSegmentBinder.bindUsingColumn(each, 
tableBinderContexts));
+            result.add(ColumnSegmentBinder.bindUsingColumn(each, 
SegmentType.JOIN_USING, tableBinderContexts));
         }
         return result;
     }
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/AmbiguousColumnException.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/AmbiguousColumnException.java
index fa874525cb9..a54ce623447 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/AmbiguousColumnException.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/AmbiguousColumnException.java
@@ -27,7 +27,7 @@ public final class AmbiguousColumnException extends 
MetaDataSQLException {
     
     private static final long serialVersionUID = -9002743483594729164L;
     
-    public AmbiguousColumnException(final String columnName) {
-        super(XOpenSQLState.GENERAL_ERROR, 5, "Column '%s' in field list is 
ambiguous.", columnName);
+    public AmbiguousColumnException(final String columnExpression, final 
String segmentTypeMessage) {
+        super(XOpenSQLState.GENERAL_ERROR, 5, "Column '%s' in %s is 
ambiguous.", columnExpression, segmentTypeMessage);
     }
 }
diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/UnknownColumnException.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/UnknownColumnException.java
index 262a21067fe..6df57bfa84a 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/UnknownColumnException.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/UnknownColumnException.java
@@ -27,7 +27,7 @@ public final class UnknownColumnException extends 
MetaDataSQLException {
     
     private static final long serialVersionUID = -1305402273592303335L;
     
-    public UnknownColumnException(final String columnName) {
-        super(XOpenSQLState.NOT_FOUND, 6, "Unknown column '%s' in 'field 
list'.", columnName);
+    public UnknownColumnException(final String columnExpression, final String 
segmentTypeMessage) {
+        super(XOpenSQLState.NOT_FOUND, 6, "Unknown column '%s' in '%s'.", 
columnExpression, segmentTypeMessage);
     }
 }

Reply via email to