This is an automated email from the ASF dual-hosted git repository.
chengzhang 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 5f7d21ed591 support extract table from function segment (#29372)
5f7d21ed591 is described below
commit 5f7d21ed591a3c02630729afdb463727f0feff0a
Author: Chuxin Chen <[email protected]>
AuthorDate: Tue Dec 12 14:31:48 2023 +0800
support extract table from function segment (#29372)
---
.../parser/sql/common/extractor/TableExtractor.java | 9 +++++++++
.../sql/common/extractor/TableExtractorTest.java | 20 ++++++++++++++++++++
2 files changed, 29 insertions(+)
diff --git
a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractor.java
b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractor.java
index 3b7a2c7308a..8985479dbef 100644
---
a/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractor.java
+++
b/parser/sql/statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractor.java
@@ -27,11 +27,13 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenE
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExistsSubqueryExpression;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ListExpression;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
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.ProjectionsSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
@@ -158,6 +160,11 @@ public final class TableExtractor {
extractTablesFromExpression(((BinaryOperationExpression)
expressionSegment).getLeft());
extractTablesFromExpression(((BinaryOperationExpression)
expressionSegment).getRight());
}
+ if (expressionSegment instanceof FunctionSegment) {
+ for (ExpressionSegment each : ((FunctionSegment)
expressionSegment).getParameters()) {
+ extractTablesFromExpression(each);
+ }
+ }
}
private void extractTablesFromProjections(final ProjectionsSegment
projections) {
@@ -176,6 +183,8 @@ public final class TableExtractor {
}
} else if (each instanceof AggregationProjectionSegment) {
((AggregationProjectionSegment)
each).getParameters().forEach(this::extractTablesFromExpression);
+ } else if (each instanceof ExpressionProjectionSegment) {
+ extractTablesFromExpression(((ExpressionProjectionSegment)
each).getExpr());
}
}
}
diff --git
a/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractorTest.java
b/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractorTest.java
index c5cb2501c8f..ebd59e4d090 100644
---
a/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractorTest.java
+++
b/parser/sql/statement/src/test/java/org/apache/shardingsphere/sql/parser/sql/common/extractor/TableExtractorTest.java
@@ -27,10 +27,13 @@ import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.Column
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.OnDuplicateKeyColumnsSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.combine.CombineSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.FunctionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
+import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.subquery.SubqueryExpressionSegment;
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.ProjectionsSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ShorthandProjectionSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
@@ -78,6 +81,23 @@ class TableExtractorTest {
assertTableSegment(tableSegmentIterator.next(), 130, 132, "t_order");
}
+ @Test
+ void assertExtractTablesFromSelectProjectsWithFunctionWithSubQuery() {
+ FunctionSegment functionSegment = new FunctionSegment(0, 0, "", "");
+ MySQLSelectStatement subQuerySegment = new MySQLSelectStatement();
+ subQuerySegment.setFrom(new SimpleTableSegment(new TableNameSegment(0,
0, new IdentifierValue("t_order"))));
+ SubquerySegment subquerySegment = new SubquerySegment(0, 0,
subQuerySegment, "");
+ SubqueryExpressionSegment subqueryExpressionSegment = new
SubqueryExpressionSegment(subquerySegment);
+ functionSegment.getParameters().add(subqueryExpressionSegment);
+ ExpressionProjectionSegment expressionProjectionSegment = new
ExpressionProjectionSegment(0, 0, "", functionSegment);
+ ProjectionsSegment projectionsSegment = new ProjectionsSegment(0, 0);
+ projectionsSegment.getProjections().add(expressionProjectionSegment);
+ MySQLSelectStatement selectStatement = new MySQLSelectStatement();
+ selectStatement.setProjections(projectionsSegment);
+ tableExtractor.extractTablesFromSelect(selectStatement);
+ assertThat(tableExtractor.getRewriteTables().size(), is(1));
+ }
+
@Test
void assertExtractTablesFromSelectLockWithValue() {
MySQLSelectStatement selectStatement = new MySQLSelectStatement();