strongduanmu commented on code in PR #20257:
URL: https://github.com/apache/shardingsphere/pull/20257#discussion_r1060293690


##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java:
##########
@@ -130,6 +162,53 @@ private Map<Integer, SelectStatementContext> 
createSubqueryContexts(final Shardi
         return result;
     }
     
+    private Map<String, SelectStatementContext> resolveRownumAndContextMap() {

Review Comment:
   Do you think getRownumSelectStatementContext may be better?



##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java:
##########
@@ -115,32 +114,38 @@ private boolean containsProjection(final 
OrderByItemSegment orderByItem, final C
     }
     
     private boolean isSameColumn(final Projection projection, final 
ColumnSegment columnSegment) {
-        Collection<ColumnProjection> columns = 
getColumnProjections(projection);
+        Collection<Projection> columns = expandProjection(projection);
         if (columns.isEmpty()) {
             return false;
         }
         boolean columnSegmentPresent = columnSegment.getOwner().isPresent();
-        for (ColumnProjection each : columns) {
+        for (Projection each : columns) {
             if (columnSegmentPresent ? isSameQualifiedName(each, 
columnSegment.getQualifiedName()) : isSameName(each, 
columnSegment.getQualifiedName())) {
                 return true;
             }
         }
         return false;
     }
     
-    private Collection<ColumnProjection> getColumnProjections(final Projection 
projection) {
-        Collection<ColumnProjection> result = new LinkedList<>();
-        if (projection instanceof ColumnProjection) {
-            result.add((ColumnProjection) projection);
-        }
+    /**
+     * Expand projection, such as ShorthandProjection.
+     * 
+     * @param projection the projection to expand
+     * @return expanded projections
+     */
+    public static Collection<Projection> expandProjection(final Projection 
projection) {
+        Collection<Projection> result = new LinkedList<>();
         if (projection instanceof ShorthandProjection) {
             result.addAll(((ShorthandProjection) 
projection).getColumnProjections());
+        } else if (!(projection instanceof DerivedProjection)) {
+            result.add(projection);
         }
         return result;
     }
     
-    private boolean isSameName(final ColumnProjection projection, final String 
text) {
-        return 
SQLUtil.getExactlyValue(text).equalsIgnoreCase(projection.getName());
+    private boolean isSameName(final Projection projection, final String text) 
{
+        String expression = projection.getExpression();
+        return 
SQLUtil.getExactlyValue(text).equalsIgnoreCase(expression.substring(expression.indexOf('.')
 + 1));

Review Comment:
   Why we need `expression.substring(expression.indexOf('.') + 1)`?



##########
infra/binder/src/test/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContextTest.java:
##########
@@ -605,4 +613,67 @@ private ProjectionSegment 
getColumnProjectionSegmentWithoutOwner(final boolean h
         columnProjectionSegment.setAlias(new AliasSegment(0, 0, new 
IdentifierValue(hasAlias ? "n" : null)));
         return columnProjectionSegment;
     }
+    
+    @Test
+    public void assertContainsOrderbyForOracleRownum() {
+        // select * from (select t.*, rownum r from (select * from a order by 
name)t ) where r<10
+        OracleSelectStatement selectStatement = new OracleSelectStatement();
+        ProjectionsSegment projections = new ProjectionsSegment(0, 0);
+        projections.getProjections().add(new ShorthandProjectionSegment(0, 0));
+        selectStatement.setProjections(projections);
+        ExpressionSegment expr = new BinaryOperationExpression(0, 0, new 
ColumnSegment(0, 0, new IdentifierValue("r")), new LiteralExpressionSegment(0, 
0, 10), "<", "r<10");
+        WhereSegment where = new WhereSegment(0, 0, expr);
+        selectStatement.setWhere(where);
+        SelectStatement subStatement = 
createSubStatementOfAssertContainsOrderbyForOracleRownum();
+        TableSegment from = new SubqueryTableSegment(new SubquerySegment(26, 
87, subStatement));
+        selectStatement.setFrom(from);
+        ShardingSphereDatabase database = mock(ShardingSphereDatabase.class);
+        when(database.getSchemas()).thenReturn(mockSchemas());
+        ShardingSphereMetaData metaData = new 
ShardingSphereMetaData(Collections.singletonMap(DefaultDatabase.LOGIC_NAME, 
database), mock(ShardingSphereRuleMetaData.class),
+                mock(ConfigurationProperties.class));
+        SelectStatementContext selectStatementContext = new 
SelectStatementContext(metaData, Collections.emptyList(), selectStatement, 
DefaultDatabase.LOGIC_NAME);
+        
assertTrue(selectStatementContext.getOrderByContext().getItems().size() > 0);
+    }
+    
+    private SelectStatement 
createSubStatementOfAssertContainsOrderbyForOracleRownum() {
+        // select t.*, rownum r from (select * from a order by a.name)t
+        final SelectStatement subStatement = new OracleSelectStatement();
+        ProjectionsSegment subProjections = new ProjectionsSegment(0, 0);
+        ShorthandProjectionSegment subShorthandProjection = new 
ShorthandProjectionSegment(0, 0);
+        subShorthandProjection.setOwner(new OwnerSegment(0, 0, new 
IdentifierValue("t")));
+        subProjections.getProjections().add(subShorthandProjection);
+        ColumnProjectionSegment subRownumProjection = new 
ColumnProjectionSegment(new ColumnSegment(0, 0, new IdentifierValue("rownum")));
+        subRownumProjection.setAlias(new AliasSegment(0, 0, new 
IdentifierValue("r")));
+        subProjections.getProjections().add(subRownumProjection);
+        subStatement.setProjections(subProjections);
+        SelectStatement subsubSelectStatement = 
createSubSubStatementOfAssertContainsOrderbyForOracleRownum();
+        SubquerySegment subsubSeg = new SubquerySegment(39, 70, 
subsubSelectStatement);
+        TableSegment subsubQuery = new SubqueryTableSegment(subsubSeg);
+        subsubQuery.setAlias(new AliasSegment(0, 0, new IdentifierValue("t")));
+        subStatement.setFrom(subsubQuery);
+        return subStatement;
+    }
+    
+    private SelectStatement 
createSubSubStatementOfAssertContainsOrderbyForOracleRownum() {
+        // select a.* from a order by a.name
+        SelectStatement subsubSelectStatement = new OracleSelectStatement();
+        ProjectionsSegment subsubProjections = new ProjectionsSegment(0, 0);
+        ShorthandProjectionSegment shorthandSegment = new 
ShorthandProjectionSegment(0, 0);
+        shorthandSegment.setOwner(new OwnerSegment(0, 0, new 
IdentifierValue("a")));
+        subsubProjections.getProjections().add(shorthandSegment);
+        subsubSelectStatement.setProjections(subsubProjections);
+        subsubSelectStatement.setFrom(new SimpleTableSegment(new 
TableNameSegment(28, 29, new IdentifierValue("a"))));
+        LinkedList<OrderByItemSegment> orderByItems = new 
LinkedList<OrderByItemSegment>();
+        orderByItems.add(new ColumnOrderByItemSegment(new ColumnSegment(0, 0, 
new IdentifierValue("name")), OrderDirection.ASC, NullsOrderType.FIRST));
+        subsubSelectStatement.setOrderBy(new OrderBySegment(0, 0, 
orderByItems));
+        return subsubSelectStatement;

Review Comment:
   Please rename subsubSelectStatement to result.



##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java:
##########
@@ -130,6 +162,53 @@ private Map<Integer, SelectStatementContext> 
createSubqueryContexts(final Shardi
         return result;
     }
     
+    private Map<String, SelectStatementContext> resolveRownumAndContextMap() {
+        Map<SelectStatement, SelectStatementContext> 
subqueryStatementAndContextMap = new HashMap<>();
+        for (SelectStatementContext subSelectContext : 
subqueryContexts.values()) {
+            
subqueryStatementAndContextMap.put(subSelectContext.getSqlStatement(), 
subSelectContext);
+        }
+        Map<String, SelectStatementContext> result = new HashMap<>();
+        for (ProjectionSegment proj : 
getSqlStatement().getProjections().getProjections()) {

Review Comment:
   Please rename proj to each.



##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java:
##########
@@ -115,32 +114,38 @@ private boolean containsProjection(final 
OrderByItemSegment orderByItem, final C
     }
     
     private boolean isSameColumn(final Projection projection, final 
ColumnSegment columnSegment) {
-        Collection<ColumnProjection> columns = 
getColumnProjections(projection);
+        Collection<Projection> columns = expandProjection(projection);

Review Comment:
   Do you think rename `expandProjection` to `getExpandedProjections` will be 
better?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to