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

duanzhengqiang 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 b9c940dde75 make subquery expression bind return column projection 
(#30726)
b9c940dde75 is described below

commit b9c940dde754899a2f49f66effeffcab5459a1eb
Author: Chuxin Chen <[email protected]>
AuthorDate: Mon Apr 1 17:51:38 2024 +0800

    make subquery expression bind return column projection (#30726)
    
    * make subquery expression bind return column projection
    
    * make subquery expression bind return column projection
---
 .../from/impl/SubqueryTableSegmentBinder.java      | 38 ++++++++++++++++++++--
 1 file changed, 35 insertions(+), 3 deletions(-)

diff --git 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinder.java
 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinder.java
index 22e6dffd2eb..a10785e2691 100644
--- 
a/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinder.java
+++ 
b/infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/from/impl/SubqueryTableSegmentBinder.java
@@ -25,10 +25,14 @@ import 
org.apache.shardingsphere.infra.binder.segment.from.TableSegmentBinderCon
 import 
org.apache.shardingsphere.infra.binder.segment.parameter.impl.ParameterMarkerExpressionSegmentBinder;
 import 
org.apache.shardingsphere.infra.binder.statement.SQLStatementBinderContext;
 import 
org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubquerySegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.AggregationProjectionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ColumnProjectionSegment;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
@@ -70,7 +74,7 @@ public final class SubqueryTableSegmentBinder {
         SubqueryTableSegment result = new 
SubqueryTableSegment(boundedSubquerySegment);
         segment.getAliasSegment().ifPresent(result::setAlias);
         tableBinderContexts.put(subqueryTableName.getValue().toLowerCase(),
-                new 
SimpleTableSegmentBinderContext(createSubqueryProjections(boundedSelect.getProjections().getProjections(),
 subqueryTableName)));
+                new 
SimpleTableSegmentBinderContext(createSubqueryProjections(boundedSelect.getProjections().getProjections(),
 subqueryTableName, statementBinderContext.getDatabaseType())));
         return result;
     }
     
@@ -94,13 +98,17 @@ public final class SubqueryTableSegmentBinder {
         segment.getPivot().ifPresent(optional -> 
optional.getPivotColumns().forEach(each -> 
statementBinderContext.getPivotColumnNames().add(each.getIdentifier().getValue().toLowerCase())));
     }
     
-    private static Collection<ProjectionSegment> 
createSubqueryProjections(final Collection<ProjectionSegment> projections, 
final IdentifierValue subqueryTableName) {
+    private static Collection<ProjectionSegment> 
createSubqueryProjections(final Collection<ProjectionSegment> projections, 
final IdentifierValue subqueryTableName, final DatabaseType databaseType) {
         Collection<ProjectionSegment> result = new LinkedList<>();
         for (ProjectionSegment each : projections) {
             if (each instanceof ColumnProjectionSegment) {
                 result.add(createColumnProjection((ColumnProjectionSegment) 
each, subqueryTableName));
             } else if (each instanceof ShorthandProjectionSegment) {
-                
result.addAll(createSubqueryProjections(((ShorthandProjectionSegment) 
each).getActualProjectionSegments(), subqueryTableName));
+                
result.addAll(createSubqueryProjections(((ShorthandProjectionSegment) 
each).getActualProjectionSegments(), subqueryTableName, databaseType));
+            } else if (each instanceof ExpressionProjectionSegment) {
+                
result.add(createColumnProjection((ExpressionProjectionSegment) each, 
subqueryTableName, databaseType));
+            } else if (each instanceof AggregationProjectionSegment) {
+                
result.add(createColumnProjection((AggregationProjectionSegment) each, 
subqueryTableName, databaseType));
             } else {
                 result.add(each);
             }
@@ -120,4 +128,28 @@ public final class SubqueryTableSegmentBinder {
         result.setVisible(originalColumn.isVisible());
         return result;
     }
+    
+    private static ColumnProjectionSegment createColumnProjection(final 
ExpressionProjectionSegment expressionProjectionSegment, final IdentifierValue 
subqueryTableName,
+                                                                  final 
DatabaseType databaseType) {
+        ColumnSegment newColumnSegment = new ColumnSegment(0, 0, 
expressionProjectionSegment.getAlias().orElseGet(() -> new 
IdentifierValue(expressionProjectionSegment.getText(),
+                new 
DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().getQuoteCharacter())));
+        if (!Strings.isNullOrEmpty(subqueryTableName.getValue())) {
+            newColumnSegment.setOwner(new OwnerSegment(0, 0, 
subqueryTableName));
+        }
+        ColumnProjectionSegment result = new 
ColumnProjectionSegment(newColumnSegment);
+        result.setVisible(true);
+        return result;
+    }
+    
+    private static ProjectionSegment createColumnProjection(final 
AggregationProjectionSegment aggregationProjectionSegment, final 
IdentifierValue subqueryTableName,
+                                                            final DatabaseType 
databaseType) {
+        ColumnSegment newColumnSegment = new ColumnSegment(0, 0, 
aggregationProjectionSegment.getAlias().orElseGet(() -> new 
IdentifierValue(aggregationProjectionSegment.getText(),
+                new 
DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().getQuoteCharacter())));
+        if (!Strings.isNullOrEmpty(subqueryTableName.getValue())) {
+            newColumnSegment.setOwner(new OwnerSegment(0, 0, 
subqueryTableName));
+        }
+        ColumnProjectionSegment result = new 
ColumnProjectionSegment(newColumnSegment);
+        result.setVisible(true);
+        return result;
+    }
 }

Reply via email to