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

strongduanmu 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 7ead6df8111 Optimize SQL rewrite token traversal (#38796)
7ead6df8111 is described below

commit 7ead6df8111111ad8848f29e0179195fd02731ff
Author: ZhangCheng <[email protected]>
AuthorDate: Thu Jun 4 14:00:30 2026 +0800

    Optimize SQL rewrite token traversal (#38796)
---
 .../infra/rewrite/context/SQLRewriteContext.java   |  3 +--
 .../infra/rewrite/sql/impl/AbstractSQLBuilder.java | 28 ++++++++++------------
 2 files changed, 14 insertions(+), 17 deletions(-)

diff --git 
a/infra/rewrite/core/src/main/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContext.java
 
b/infra/rewrite/core/src/main/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContext.java
index 0c6f74289d5..377affdd528 100644
--- 
a/infra/rewrite/core/src/main/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContext.java
+++ 
b/infra/rewrite/core/src/main/java/org/apache/shardingsphere/infra/rewrite/context/SQLRewriteContext.java
@@ -33,7 +33,6 @@ import 
org.apache.shardingsphere.infra.session.query.QueryContext;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.LinkedList;
 import java.util.List;
 
 /**
@@ -52,7 +51,7 @@ public final class SQLRewriteContext {
     
     private final ParameterBuilder parameterBuilder;
     
-    private final List<SQLToken> sqlTokens = new LinkedList<>();
+    private final List<SQLToken> sqlTokens = new ArrayList<>();
     
     @Getter(AccessLevel.NONE)
     private final SQLTokenGenerators sqlTokenGenerators = new 
SQLTokenGenerators();
diff --git 
a/infra/rewrite/core/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/impl/AbstractSQLBuilder.java
 
b/infra/rewrite/core/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/impl/AbstractSQLBuilder.java
index cd4cbd7c440..c8e29ed263e 100644
--- 
a/infra/rewrite/core/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/impl/AbstractSQLBuilder.java
+++ 
b/infra/rewrite/core/src/main/java/org/apache/shardingsphere/infra/rewrite/sql/impl/AbstractSQLBuilder.java
@@ -46,9 +46,10 @@ public abstract class AbstractSQLBuilder implements 
SQLBuilder {
         StringBuilder result = new StringBuilder(sql.length());
         result.append(sql, 0, sqlTokens.get(0).getStartIndex());
         Optional<SQLToken> previousToken = Optional.empty();
-        for (SQLToken each : sqlTokens) {
+        for (int i = 0; i < sqlTokens.size(); i++) {
+            SQLToken each = sqlTokens.get(i);
             if (isContainsAttachableToken(each, previousToken.orElse(null)) || 
each.getStartIndex() > previousToken.map(SQLToken::getStopIndex).orElse(0)) {
-                appendRewriteSQL(each, result);
+                appendRewriteSQL(each, result, i);
                 previousToken = Optional.of(each);
             }
         }
@@ -59,16 +60,17 @@ public abstract class AbstractSQLBuilder implements 
SQLBuilder {
         return sqlToken instanceof Attachable || previousToken instanceof 
Attachable;
     }
     
-    private void appendRewriteSQL(final SQLToken sqlToken, final StringBuilder 
builder) {
+    private void appendRewriteSQL(final SQLToken sqlToken, final StringBuilder 
builder, final int currentSQLTokenIndex) {
         builder.append(getSQLTokenText(sqlToken));
-        builder.append(getConjunctionText(sqlToken, sqlTokens, sql.length()));
+        builder.append(getConjunctionText(sqlTokens, sql.length(), 
currentSQLTokenIndex));
     }
     
     protected abstract String getSQLTokenText(SQLToken sqlToken);
     
-    private String getConjunctionText(final SQLToken sqlToken, final 
List<SQLToken> sqlTokens, final int sqlLength) {
+    private String getConjunctionText(final List<SQLToken> sqlTokens, final 
int sqlLength, final int currentSQLTokenIndex) {
+        SQLToken sqlToken = sqlTokens.get(currentSQLTokenIndex);
         int startIndex = getStartIndex(sqlToken, sqlLength);
-        int stopIndex = getStopIndex(sqlToken, sqlTokens, sqlLength, 
startIndex);
+        int stopIndex = getStopIndex(sqlTokens, sqlLength, startIndex, 
currentSQLTokenIndex);
         return sql.substring(startIndex, stopIndex);
     }
     
@@ -77,20 +79,16 @@ public abstract class AbstractSQLBuilder implements 
SQLBuilder {
         return Math.min(startIndex, sqlLength);
     }
     
-    private int getStopIndex(final SQLToken sqlToken, final List<SQLToken> 
sqlTokens, final int sqlLength, final int startIndex) {
-        Optional<SQLToken> nextSQLToken = getNextSQLToken(sqlToken, sqlTokens);
+    private int getStopIndex(final List<SQLToken> sqlTokens, final int 
sqlLength, final int startIndex, final int currentSQLTokenIndex) {
+        Optional<SQLToken> nextSQLToken = getNextSQLToken(sqlTokens, 
currentSQLTokenIndex);
         if (!nextSQLToken.isPresent()) {
             return sqlLength;
         }
         int stopIndex = nextSQLToken.get().getStartIndex();
-        return startIndex <= stopIndex ? stopIndex : 
getStopIndex(nextSQLToken.get(), sqlTokens, sqlLength, startIndex);
+        return startIndex <= stopIndex ? stopIndex : getStopIndex(sqlTokens, 
sqlLength, startIndex, currentSQLTokenIndex + 1);
     }
     
-    private Optional<SQLToken> getNextSQLToken(final SQLToken sqlToken, final 
List<SQLToken> sqlTokens) {
-        int currentSQLTokenIndex = sqlTokens.indexOf(sqlToken);
-        if (sqlTokens.size() - 1 == currentSQLTokenIndex) {
-            return Optional.empty();
-        }
-        return Optional.ofNullable(sqlTokens.get(currentSQLTokenIndex + 1));
+    private Optional<SQLToken> getNextSQLToken(final List<SQLToken> sqlTokens, 
final int currentSQLTokenIndex) {
+        return sqlTokens.size() - 1 == currentSQLTokenIndex ? Optional.empty() 
: Optional.ofNullable(sqlTokens.get(currentSQLTokenIndex + 1));
     }
 }

Reply via email to