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


##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java:
##########
@@ -111,11 +113,26 @@ public SelectStatementContext(final 
ShardingSphereMetaData metaData, final List<
         ColumnExtractor.extractColumnSegments(columnSegments, whereSegments);
         subqueryContexts = createSubqueryContexts(metaData, params, 
defaultDatabaseName);
         tablesContext = new TablesContext(getAllTableSegments(), 
subqueryContexts, getDatabaseType());
-        String databaseName = 
tablesContext.getDatabaseName().orElse(defaultDatabaseName);
+        Optional<SelectStatementContext> rownumSelectContext;
+        if (sqlStatement instanceof OracleSelectStatement && 
sqlStatement.getWhere().isPresent()) {

Review Comment:
   Can we extract if condition to a private method.



##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java:
##########
@@ -130,6 +147,42 @@ private Map<Integer, SelectStatementContext> 
createSubqueryContexts(final Shardi
         return result;
     }
     
+    private Optional<SelectStatementContext> 
findRownumSelectStatementContext() {
+        WhereSegment where = getSqlStatement().getWhere().get();
+        Optional<OracleSelectStatement> rownumSelectStatement = 
Optional.empty();
+        if (where.getExpr() instanceof BinaryOperationExpression) {
+            rownumSelectStatement = 
findRownumSelectStatement((BinaryOperationExpression) where.getExpr(), 
(OracleSelectStatement) getSqlStatement());
+        }
+        Optional<SelectStatementContext> rownumSelectContext = 
Optional.empty();
+        if (rownumSelectStatement.isPresent()) {
+            rownumSelectContext = 
findContextFromSubQueryContext(subqueryContexts.values(), 
rownumSelectStatement.get());
+        }
+        return rownumSelectContext;
+    }
+    
+    private Optional<OracleSelectStatement> findRownumSelectStatement(final 
BinaryOperationExpression expr, final OracleSelectStatement selectStatement) {
+        ExpressionSegment left = expr.getLeft();
+        if (left instanceof ColumnSegment) {
+            String rowNumAlias = ((ColumnSegment) 
left).getIdentifier().getValue();
+            return selectStatement.getRowNumSelect().containsKey(rowNumAlias) 
? Optional.of(selectStatement.getRowNumSelect().get(rowNumAlias)) : 
Optional.empty();
+        }
+        if (left instanceof BinaryOperationExpression) {
+            return findRownumSelectStatement((BinaryOperationExpression) left, 
selectStatement);
+        }
+        return Optional.empty();
+    }
+    
+    private Optional<SelectStatementContext> 
findContextFromSubQueryContext(final Collection<SelectStatementContext> 
subQueryContexts, final OracleSelectStatement selectStatement) {
+        Optional<SelectStatementContext> statementContext = Optional.empty();
+        for (SelectStatementContext subSelectContext : subQueryContexts) {
+            if (subSelectContext.getSqlStatement() == selectStatement) {
+                statementContext = Optional.of(subSelectContext);
+                break;
+            }
+        }
+        return statementContext;

Review Comment:
   Please rename rownumSelectContext to result.



##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/dml/SelectStatementContext.java:
##########
@@ -130,6 +147,42 @@ private Map<Integer, SelectStatementContext> 
createSubqueryContexts(final Shardi
         return result;
     }
     
+    private Optional<SelectStatementContext> 
findRownumSelectStatementContext() {
+        WhereSegment where = getSqlStatement().getWhere().get();
+        Optional<OracleSelectStatement> rownumSelectStatement = 
Optional.empty();
+        if (where.getExpr() instanceof BinaryOperationExpression) {
+            rownumSelectStatement = 
findRownumSelectStatement((BinaryOperationExpression) where.getExpr(), 
(OracleSelectStatement) getSqlStatement());
+        }
+        Optional<SelectStatementContext> rownumSelectContext = 
Optional.empty();
+        if (rownumSelectStatement.isPresent()) {
+            rownumSelectContext = 
findContextFromSubQueryContext(subqueryContexts.values(), 
rownumSelectStatement.get());
+        }
+        return rownumSelectContext;

Review Comment:
   Please rename rownumSelectContext to result.



##########
infra/binder/src/main/java/org/apache/shardingsphere/infra/binder/segment/select/projection/engine/ProjectionsContextEngine.java:
##########
@@ -115,32 +114,37 @@ 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.

Review Comment:
   Please keep the first letter uppercase in javadoc, and add a new line 
between javadoc and param doc.



##########
sql-parser/dialect/oracle/src/main/java/org/apache/shardingsphere/sql/parser/oracle/visitor/statement/impl/OracleDMLStatementSQLVisitor.java:
##########
@@ -512,6 +515,31 @@ public ASTNode visitQueryBlock(final QueryBlockContext 
ctx) {
         if (null != ctx.modelClause()) {
             result.setModelSegment((ModelSegment) visit(ctx.modelClause()));
         }
+        Map<String, OracleSelectStatement> rowNumSelect = 
getRowNumSelect(result);
+        if (rowNumSelect.size() > 0) {
+            result.setRowNumSelect(rowNumSelect);

Review Comment:
   Can we move this logic into StatementContext for processing? The SQLVisitor 
should be processed according to SQL semantics, and some processing for kernel 
functions should be placed in StatementContext.



-- 
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