Skip to content

Commit 9c48df5

Browse files
authored
Fix wrong parameter count parse when function contains placeholder (#38019)
1 parent e1c924a commit 9c48df5

File tree

18 files changed

+420
-123
lines changed

18 files changed

+420
-123
lines changed

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
1. SQL Parser: Fix SQL parser error when SQL contains implicit concat expression for MySQL - [#34660](https://github.com/apache/shardingsphere/pull/34660)
100100
1. SQL Parser: Fix SQL parser error when SQL contains subquery with alias for Oracle - [#35239](https://github.com/apache/shardingsphere/pull/35239)
101101
1. SQL Parser: Fix multiple SQLs split error when comma contained - [#31609](https://github.com/apache/shardingsphere/pull/31609)
102+
1. SQL Parser: Fix wrong parameter count parse when function contains placeholder [#38019](https://github.com/apache/shardingsphere/pull/38019)
102103
1. SQL Binder: Fix unable to find the outer table in the NotExpressionBinder - [36135](https://github.com/apache/shardingsphere/pull/36135)
103104
1. SQL Binder: Fix unable to find the outer table in the ExistsSubqueryExpressionBinder - [#36068](https://github.com/apache/shardingsphere/pull/36068)
104105
1. SQL Binder: Fix column bind exception caused by oracle XMLELEMENT function first parameter without quote - [#36963](https://github.com/apache/shardingsphere/pull/36963)

parser/sql/engine/dialect/clickhouse/src/main/java/org/apache/shardingsphere/sql/parser/engine/clickhouse/visitor/statement/ClickHouseStatementVisitor.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
3838
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
3939
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.InExpression;
40+
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.IntervalExpression;
41+
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.IntervalUnit;
4042
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ListExpression;
4143
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.NotExpression;
4244
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.complex.CommonExpressionSegment;
@@ -367,6 +369,9 @@ public final ASTNode visitSimpleExpr(final ClickHouseStatementParser.SimpleExprC
367369
if (null != ctx.literals()) {
368370
return SQLUtils.createLiteralExpression(visit(ctx.literals()), startIndex, stopIndex, ctx.literals().start.getInputStream().getText(new Interval(startIndex, stopIndex)));
369371
}
372+
if (null != ctx.intervalExpression()) {
373+
return visit(ctx.intervalExpression());
374+
}
370375
if (null != ctx.functionCall()) {
371376
return visit(ctx.functionCall());
372377
}
@@ -378,8 +383,9 @@ public final ASTNode visitSimpleExpr(final ClickHouseStatementParser.SimpleExprC
378383

379384
@Override
380385
public final ASTNode visitIntervalExpression(final ClickHouseStatementParser.IntervalExpressionContext ctx) {
381-
calculateParameterCount(Collections.singleton(ctx.expr()));
382-
return new ExpressionProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getOriginalText(ctx));
386+
IntervalUnit intervalUnit = IntervalUnit.valueOf(ctx.intervalUnit().getText().toUpperCase());
387+
return new IntervalExpression(ctx.INTERVAL().getSymbol().getStartIndex(), ctx.getStop().getStopIndex(), (ExpressionSegment) visit(ctx.expr()), intervalUnit,
388+
getOriginalText(ctx));
383389
}
384390

385391
@Override
@@ -434,7 +440,6 @@ public final ASTNode visitSpecialFunction(final ClickHouseStatementParser.Specia
434440

435441
@Override
436442
public final ASTNode visitCastFunction(final ClickHouseStatementParser.CastFunctionContext ctx) {
437-
calculateParameterCount(Collections.singleton(ctx.expr()));
438443
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.CAST().getText(), getOriginalText(ctx));
439444
ASTNode exprSegment = visit(ctx.expr());
440445
if (exprSegment instanceof ColumnSegment) {
@@ -458,13 +463,6 @@ public final ASTNode visitDataTypeName(final ClickHouseStatementParser.DataTypeN
458463
return new KeywordValue(IntStream.range(0, ctx.getChildCount()).mapToObj(i -> ctx.getChild(i).getText()).collect(Collectors.joining(" ")));
459464
}
460465

461-
// TODO :FIXME, sql case id: insert_with_str_to_date
462-
private void calculateParameterCount(final Collection<ClickHouseStatementParser.ExprContext> exprContexts) {
463-
for (ClickHouseStatementParser.ExprContext each : exprContexts) {
464-
visit(each);
465-
}
466-
}
467-
468466
@Override
469467
public final ASTNode visitOrderByClause(final ClickHouseStatementParser.OrderByClauseContext ctx) {
470468
return new OrderBySegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.orderByItem().stream().map(each -> (OrderByItemSegment) visit(each)).collect(Collectors.toList()));

parser/sql/engine/dialect/doris/src/main/java/org/apache/shardingsphere/sql/parser/engine/doris/visitor/statement/DorisStatementVisitor.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,8 @@
9898
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.OnDuplicateKeyClauseContext;
9999
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.OrderByClauseContext;
100100
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.OrderByItemContext;
101-
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.OwnerContext;
102101
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.OutfilePropertyContext;
103-
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectFieldsIntoContext;
104-
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectIntoExpressionContext;
105-
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectLinesIntoContext;
102+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.OwnerContext;
106103
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ParameterMarkerContext;
107104
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.PositionFunctionContext;
108105
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.PrecisionContext;
@@ -121,6 +118,9 @@
121118
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ReplaceValuesClauseContext;
122119
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.RowConstructorListContext;
123120
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectContext;
121+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectFieldsIntoContext;
122+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectIntoExpressionContext;
123+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectLinesIntoContext;
124124
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectSpecificationContext;
125125
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SelectWithIntoContext;
126126
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.SetAssignmentsClauseContext;
@@ -151,17 +151,13 @@
151151
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.UserVariableContext;
152152
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ValuesFunctionContext;
153153
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.VariableContext;
154+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ViewColumnDefinitionContext;
155+
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ViewColumnDefinitionsContext;
154156
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ViewNameContext;
155157
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ViewNamesContext;
156158
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.WeightStringFunctionContext;
157159
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.WhereClauseContext;
158160
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.WithClauseContext;
159-
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ViewColumnDefinitionContext;
160-
import org.apache.shardingsphere.sql.parser.autogen.DorisStatementParser.ViewColumnDefinitionsContext;
161-
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.view.ViewColumnSegment;
162-
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.outfile.OutfileColumnsSegment;
163-
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.outfile.OutfileLinesSegment;
164-
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.outfile.OutfileSegment;
165161
import org.apache.shardingsphere.sql.parser.statement.core.enums.AggregationType;
166162
import org.apache.shardingsphere.sql.parser.statement.core.enums.CombineType;
167163
import org.apache.shardingsphere.sql.parser.statement.core.enums.JoinType;
@@ -173,6 +169,7 @@
173169
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.engine.EngineSegment;
174170
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexNameSegment;
175171
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.index.IndexSegment;
172+
import org.apache.shardingsphere.sql.parser.statement.core.segment.ddl.view.ViewColumnSegment;
176173
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.ColumnAssignmentSegment;
177174
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.InsertValuesSegment;
178175
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.assignment.SetAssignmentSegment;
@@ -214,6 +211,9 @@
214211
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.ExpressionOrderByItemSegment;
215212
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.IndexOrderByItemSegment;
216213
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.order.item.OrderByItemSegment;
214+
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.outfile.OutfileColumnsSegment;
215+
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.outfile.OutfileLinesSegment;
216+
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.outfile.OutfileSegment;
217217
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.PaginationValueSegment;
218218
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.LimitSegment;
219219
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.pagination.limit.NumberLiteralLimitValueSegment;
@@ -257,11 +257,11 @@
257257
import java.util.ArrayList;
258258
import java.util.Collection;
259259
import java.util.Collections;
260+
import java.util.LinkedHashMap;
260261
import java.util.LinkedList;
261262
import java.util.List;
262-
import java.util.stream.Collectors;
263263
import java.util.Map;
264-
import java.util.LinkedHashMap;
264+
import java.util.stream.Collectors;
265265

266266
/**
267267
* Statement visitor for Doris.
@@ -1161,7 +1161,6 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) {
11611161

11621162
@Override
11631163
public final ASTNode visitGroupConcatFunction(final GroupConcatFunctionContext ctx) {
1164-
calculateParameterCount(ctx.expr());
11651164
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.GROUP_CONCAT().getText(), getOriginalText(ctx));
11661165
for (ExprContext each : getTargetRuleContextFromParseTree(ctx, ExprContext.class)) {
11671166
result.getParameters().add((ExpressionSegment) visit(each));
@@ -1263,7 +1262,6 @@ public final ASTNode visitConvertFunction(final ConvertFunctionContext ctx) {
12631262

12641263
@Override
12651264
public final ASTNode visitPositionFunction(final PositionFunctionContext ctx) {
1266-
calculateParameterCount(ctx.expr());
12671265
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.POSITION().getText(), getOriginalText(ctx));
12681266
result.getParameters().add((ExpressionSegment) visit(ctx.expr(0)));
12691267
result.getParameters().add((ExpressionSegment) visit(ctx.expr(1)));
@@ -1283,7 +1281,6 @@ public final ASTNode visitSubstringFunction(final SubstringFunctionContext ctx)
12831281

12841282
@Override
12851283
public final ASTNode visitExtractFunction(final ExtractFunctionContext ctx) {
1286-
calculateParameterCount(Collections.singleton(ctx.expr()));
12871284
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.EXTRACT().getText(), getOriginalText(ctx));
12881285
result.getParameters().add(new LiteralExpressionSegment(ctx.intervalUnit().getStart().getStartIndex(), ctx.intervalUnit().getStop().getStopIndex(), ctx.intervalUnit().getText()));
12891286
result.getParameters().add((ExpressionSegment) visit(ctx.expr()));
@@ -1292,7 +1289,6 @@ public final ASTNode visitExtractFunction(final ExtractFunctionContext ctx) {
12921289

12931290
@Override
12941291
public final ASTNode visitCharFunction(final CharFunctionContext ctx) {
1295-
calculateParameterCount(ctx.expr());
12961292
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.CHAR().getText(), getOriginalText(ctx));
12971293
for (ExprContext each : ctx.expr()) {
12981294
ASTNode expr = visit(each);
@@ -1324,7 +1320,6 @@ public final ASTNode visitTrimFunction(final TrimFunctionContext ctx) {
13241320

13251321
@Override
13261322
public final ASTNode visitWeightStringFunction(final WeightStringFunctionContext ctx) {
1327-
calculateParameterCount(Collections.singleton(ctx.expr()));
13281323
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.WEIGHT_STRING().getText(), getOriginalText(ctx));
13291324
result.getParameters().add((ExpressionSegment) visit(ctx.expr()));
13301325
return result;
@@ -1464,13 +1459,6 @@ public final ASTNode visitMatchExpression(final MatchExpressionContext ctx) {
14641459
return result;
14651460
}
14661461

1467-
// TODO :FIXME, sql case id: insert_with_str_to_date
1468-
private void calculateParameterCount(final Collection<ExprContext> exprContexts) {
1469-
for (ExprContext each : exprContexts) {
1470-
visit(each);
1471-
}
1472-
}
1473-
14741462
@Override
14751463
public final ASTNode visitDataType(final DataTypeContext ctx) {
14761464
DataTypeSegment result = new DataTypeSegment();
@@ -1978,12 +1966,10 @@ public ASTNode visitTableReferences(final TableReferencesContext ctx) {
19781966
}
19791967
return result;
19801968
}
1981-
// DORIS ADDED BEGIN
19821969
if (null != ctx.regularFunction()) {
19831970
FunctionSegment functionSegment = (FunctionSegment) visit(ctx.regularFunction());
19841971
return new FunctionTableSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), functionSegment);
19851972
}
1986-
// DORIS ADDED END
19871973
return result;
19881974
}
19891975

parser/sql/engine/dialect/firebird/src/main/java/org/apache/shardingsphere/sql/parser/engine/firebird/visitor/statement/FirebirdStatementVisitor.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@
7878
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
7979
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.FunctionSegment;
8080
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.InExpression;
81+
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.IntervalExpression;
82+
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.IntervalUnit;
8183
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ListExpression;
8284
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.NotExpression;
8385
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.complex.CommonExpressionSegment;
@@ -430,8 +432,9 @@ public final ASTNode visitSimpleExpr(final SimpleExprContext ctx) {
430432

431433
@Override
432434
public final ASTNode visitIntervalExpression(final IntervalExpressionContext ctx) {
433-
calculateParameterCount(Collections.singleton(ctx.expr()));
434-
return new ExpressionProjectionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), getOriginalText(ctx));
435+
IntervalUnit intervalUnit = IntervalUnit.valueOf(ctx.intervalUnit().getText().toUpperCase());
436+
return new IntervalExpression(ctx.INTERVAL().getSymbol().getStartIndex(), ctx.getStop().getStopIndex(), (ExpressionSegment) visit(ctx.expr()), intervalUnit,
437+
getOriginalText(ctx));
435438
}
436439

437440
@Override
@@ -501,7 +504,6 @@ public final ASTNode visitSpecialFunction(final SpecialFunctionContext ctx) {
501504

502505
@Override
503506
public final ASTNode visitCastFunction(final CastFunctionContext ctx) {
504-
calculateParameterCount(Collections.singleton(ctx.expr()));
505507
FunctionSegment result = new FunctionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.CAST().getText(), getOriginalText(ctx));
506508
ASTNode exprSegment = visit(ctx.expr());
507509
if (exprSegment instanceof ColumnSegment) {
@@ -563,13 +565,6 @@ public final ASTNode visitDataTypeName(final DataTypeNameContext ctx) {
563565
return new KeywordValue(String.join(" ", dataTypeNames));
564566
}
565567

566-
// TODO :FIXME, sql case id: insert_with_str_to_date
567-
private void calculateParameterCount(final Collection<ExprContext> exprContexts) {
568-
for (ExprContext each : exprContexts) {
569-
visit(each);
570-
}
571-
}
572-
573568
@Override
574569
public final ASTNode visitOrderByClause(final OrderByClauseContext ctx) {
575570
Collection<OrderByItemSegment> items = new LinkedList<>();

0 commit comments

Comments
 (0)