Skip to content

Commit f700620

Browse files
AndreasLanz01claude
andcommitted
fix: traverse all expression children in adapters
- Visit skipped children: named function parameters, TRIM FROM, ANY/ALL subquery, ESCAPE, AT TIME ZONE, JSON operators and aggregates, column array subscripts - Visit VALUES rows, LIMIT, LIMIT BY, CONNECT BY and set-operation WITH/ORDER BY/LIMIT/OFFSET/FETCH - StatementFeatureVisitor now checks functions, locks and writes there Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 26d3e02 commit f700620

3 files changed

Lines changed: 148 additions & 32 deletions

File tree

‎src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,17 @@ public <S> T visit(Function function, S context) {
113113
if (function.getParameters() != null) {
114114
subExpressions.addAll(function.getParameters());
115115
}
116+
if (function.getNamedParameters() != null) {
117+
subExpressions.addAll(function.getNamedParameters());
118+
}
116119
if (function.getChainedParameters() != null) {
117120
subExpressions.addAll(function.getChainedParameters());
118121
}
122+
// an attribute name is not a column reference, only an attribute expression is visited
123+
if (function.getAttribute() instanceof Expression
124+
&& function.getAttribute() != function.getAttributeColumn()) {
125+
subExpressions.add((Expression) function.getAttribute());
126+
}
119127
if (function.getKeep() != null) {
120128
subExpressions.add(function.getKeep());
121129
}
@@ -284,7 +292,8 @@ public <S> T visit(IsUnknownExpression isUnknownExpression, S context) {
284292

285293
@Override
286294
public <S> T visit(LikeExpression likeExpression, S context) {
287-
return visitBinaryExpression(likeExpression, context);
295+
return visitExpressions(likeExpression, context, likeExpression.getLeftExpression(),
296+
likeExpression.getRightExpression(), likeExpression.getEscape());
288297
}
289298

290299
@Override
@@ -319,7 +328,11 @@ public <S> T visit(ContainedBy containedBy, S context) {
319328

320329
@Override
321330
public <S> T visit(Column column, S context) {
322-
return applyExpression(column, context);
331+
T result = applyExpression(column, context);
332+
if (column.getArrayConstructor() != null) {
333+
column.getArrayConstructor().accept(this, context);
334+
}
335+
return result;
323336
}
324337

325338
@Override
@@ -358,12 +371,14 @@ public <S> T visit(ExistsExpression existsExpression, S context) {
358371

359372
@Override
360373
public <S> T visit(MemberOfExpression memberOfExpression, S context) {
361-
return memberOfExpression.getRightExpression().accept(this, context);
374+
return visitExpressions(memberOfExpression, context,
375+
memberOfExpression.getLeftExpression(), memberOfExpression.getRightExpression());
362376
}
363377

364378
@Override
365379
public <S> T visit(AnyComparisonExpression anyComparisonExpression, S context) {
366-
return applyExpression(anyComparisonExpression, context);
380+
return visitExpressions(anyComparisonExpression, context,
381+
anyComparisonExpression.getSelect());
367382
}
368383

369384
@Override
@@ -535,7 +550,10 @@ protected <S> T visitBinaryExpression(BinaryExpression binaryExpression, S conte
535550

536551
@Override
537552
public <S> T visit(JsonExpression jsonExpr, S context) {
538-
return jsonExpr.getExpression().accept(this, context);
553+
ArrayList<Expression> subExpressions = new ArrayList<>();
554+
subExpressions.add(jsonExpr.getExpression());
555+
subExpressions.addAll(jsonExpr.getIdents());
556+
return visitExpressions(jsonExpr, context, subExpressions);
539557
}
540558

541559
@Override
@@ -728,13 +746,28 @@ public <S> T visit(XMLSerializeExpr xmlSerializeExpr, S context) {
728746

729747
@Override
730748
public <S> T visit(TimezoneExpression timezoneExpression, S context) {
731-
return timezoneExpression.getLeftExpression().accept(this, context);
749+
ArrayList<Expression> subExpressions = new ArrayList<>();
750+
subExpressions.add(timezoneExpression.getLeftExpression());
751+
subExpressions.addAll(timezoneExpression.getTimezoneExpressions());
752+
return visitExpressions(timezoneExpression, context, subExpressions);
732753
}
733754

734755
@Override
735756
public <S> T visit(JsonAggregateFunction jsonAggregateFunction, S context) {
736-
return visitExpressions(jsonAggregateFunction, context,
737-
jsonAggregateFunction.getExpression(), jsonAggregateFunction.getFilterExpression());
757+
ArrayList<Expression> subExpressions = new ArrayList<>();
758+
subExpressions.add(jsonAggregateFunction.getExpression());
759+
if (jsonAggregateFunction.getKey() instanceof Expression) {
760+
subExpressions.add((Expression) jsonAggregateFunction.getKey());
761+
}
762+
if (jsonAggregateFunction.getValue() instanceof Expression) {
763+
subExpressions.add((Expression) jsonAggregateFunction.getValue());
764+
}
765+
addOrderByExpressions(subExpressions,
766+
jsonAggregateFunction.getExpressionOrderByElements());
767+
subExpressions.add(jsonAggregateFunction.getFilterExpression());
768+
subExpressions.addAll(jsonAggregateFunction.getPartitionExpressionList());
769+
addOrderByExpressions(subExpressions, jsonAggregateFunction.getOrderByElements());
770+
return visitExpressions(jsonAggregateFunction, context, subExpressions);
738771
}
739772

740773
@Override
@@ -834,7 +867,8 @@ public <S> T visit(TranscodingFunction transcodingFunction, S context) {
834867

835868
@Override
836869
public <S> T visit(TrimFunction trimFunction, S context) {
837-
return trimFunction.getExpression().accept(this, context);
870+
return visitExpressions(trimFunction, context, trimFunction.getExpression(),
871+
trimFunction.getFromExpression());
838872
}
839873

840874
@Override

‎src/main/java/net/sf/jsqlparser/statement/select/SelectVisitorAdapter.java‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,19 @@ public <S> T visit(ParenthesedSelect select, S context) {
135135
unpivot.accept(pivotVisitor, context);
136136
}
137137

138-
expressionVisitor.visitLimit(select.getLimit(), context);
138+
visitLimitOffsetFetch(select, context);
139+
return null;
140+
}
139141

142+
private <S> void visitLimitOffsetFetch(Select select, S context) {
143+
expressionVisitor.visitLimit(select.getLimitBy(), context);
144+
expressionVisitor.visitLimit(select.getLimit(), context);
140145
if (select.getOffset() != null) {
141-
expressionVisitor.visitExpression(select.getOffset().getOffset(), null);
146+
expressionVisitor.visitExpression(select.getOffset().getOffset(), context);
142147
}
143148
if (select.getFetch() != null) {
144-
expressionVisitor.visitExpression(select.getFetch().getExpression(), null);
149+
expressionVisitor.visitExpression(select.getFetch().getExpression(), context);
145150
}
146-
147-
return null;
148151
}
149152

150153
@Override
@@ -190,11 +193,7 @@ public <S> T visit(PlainSelect plainSelect, S context) {
190193

191194
expressionVisitor.visitExpression(plainSelect.getPreWhere(), context);
192195
expressionVisitor.visitExpression(plainSelect.getWhere(), context);
193-
194-
// if (plainSelect.getOracleHierarchical() != null) {
195-
// //@todo: implement
196-
// }
197-
//
196+
expressionVisitor.visitExpression(plainSelect.getOracleHierarchical(), context);
198197

199198
expressionVisitor.visitPreferringClause(plainSelect.getPreferringClause(), context);
200199
expressionVisitor.visit(plainSelect.getGroupBy(), context);
@@ -219,25 +218,14 @@ public <S> T visit(PlainSelect plainSelect, S context) {
219218
}
220219

221220
expressionVisitor.visitOrderBy(plainSelect.getOrderByElements(), context);
221+
visitLimitOffsetFetch(plainSelect, context);
222222

223-
// if (plainSelect.getLimitBy() != null) {
224-
// //@todo: implement
225-
// }
226-
// if (plainSelect.getLimit() != null) {
227-
// //@todo: implement
228-
// }
229-
if (plainSelect.getOffset() != null) {
230-
expressionVisitor.visitExpression(plainSelect.getOffset().getOffset(), context);
231-
}
232223
if (plainSelect.getMySqlProcedureAnalyse() != null) {
233224
expressionVisitor.visitExpression(
234225
plainSelect.getMySqlProcedureAnalyse().getMaxElements(), context);
235226
expressionVisitor.visitExpression(
236227
plainSelect.getMySqlProcedureAnalyse().getMaxMemory(), context);
237228
}
238-
if (plainSelect.getFetch() != null) {
239-
expressionVisitor.visitExpression(plainSelect.getFetch().getExpression(), context);
240-
}
241229
// if (plainSelect.getForMode() != null) {
242230
// //@todo: implement
243231
// }
@@ -289,9 +277,12 @@ public <S> T visit(FromQuery fromQuery, S context) {
289277

290278
@Override
291279
public <S> T visit(SetOperationList setOpList, S context) {
280+
visitWithItems(setOpList.getWithItemsList(), context);
292281
for (Select select : setOpList.getSelects()) {
293282
select.accept(this, context);
294283
}
284+
expressionVisitor.visitOrderBy(setOpList.getOrderByElements(), context);
285+
visitLimitOffsetFetch(setOpList, context);
295286
return null;
296287
}
297288

@@ -341,7 +332,8 @@ public <S> T visit(WithItem<?> withItem, S context) {
341332
}
342333

343334
@Override
344-
public <S> T visit(Values aThis, S context) {
335+
public <S> T visit(Values values, S context) {
336+
expressionVisitor.visitExpressions(values.getExpressions(), context);
345337
return null;
346338
}
347339

‎src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.junit.jupiter.api.DisplayName;
2222
import org.junit.jupiter.api.Nested;
2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
2426

2527
import java.util.Collections;
2628
import java.util.EnumSet;
@@ -498,4 +500,92 @@ void explainIsUncertain() throws JSQLParserException {
498500
assertThat(f.returnsResultSet()).isTrue();
499501
assertThat(f.mayModifyData()).isTrue();
500502
}
503+
504+
/**
505+
* Every expression position has to be traversed, otherwise a function call placed there escapes
506+
* the purity check and a subquery placed there escapes the lock and write checks.
507+
*/
508+
@Nested
509+
@DisplayName("every expression position is traversed")
510+
class ExpressionPositions {
511+
512+
private StatementFeatures analyseAllPureExcept(String sql, String impure)
513+
throws JSQLParserException {
514+
return StatementFeatureVisitor.analyse(CCJSqlParserUtil.parse(sql),
515+
name -> !name.equals(impure));
516+
}
517+
518+
@ParameterizedTest(name = "{0}")
519+
@ValueSource(strings = {
520+
"SELECT substring(pg_read_file('x') FROM 2 FOR 3)",
521+
"SELECT trim(LEADING 'x' FROM pg_read_file('x'))",
522+
"SELECT position(pg_read_file('x') IN a) FROM t",
523+
"SELECT overlay(a PLACING pg_read_file('x') FROM 2) FROM t",
524+
"SELECT a FROM t WHERE a = ANY (SELECT pg_read_file('x'))",
525+
"SELECT a FROM t WHERE a > ALL (SELECT pg_read_file('x'))",
526+
"SELECT a FROM t WHERE a = SOME (SELECT pg_read_file('x'))",
527+
"VALUES (pg_read_file('x'))",
528+
"SELECT a FROM t WHERE a IN (VALUES (pg_read_file('x')))",
529+
"SELECT a FROM t LIMIT length(pg_read_file('x'))",
530+
"SELECT a FROM t WHERE a LIKE 'a' ESCAPE pg_read_file('x')",
531+
"SELECT d AT TIME ZONE pg_read_file('x') FROM t",
532+
"SELECT j -> pg_read_file('x') FROM t",
533+
"SELECT json_objectagg(a : pg_read_file('x')) FROM t",
534+
"SELECT a FROM t WHERE pg_read_file('x') MEMBER OF (j)",
535+
"SELECT a FROM t START WITH a = pg_read_file('x') CONNECT BY PRIOR a = b",
536+
"SELECT a FROM t LIMIT 1 BY pg_read_file('x')",
537+
"WITH c AS (SELECT pg_read_file('x')) SELECT a FROM c UNION SELECT b FROM u",
538+
"SELECT a FROM t UNION SELECT b FROM u ORDER BY pg_read_file('x')",
539+
"SELECT a FROM t UNION SELECT b FROM u LIMIT length(pg_read_file('x'))",
540+
"SELECT j ->> pg_read_file('x') FROM t",
541+
"SELECT j #> ARRAY[pg_read_file('x')] FROM t",
542+
"SELECT j #>> ARRAY[pg_read_file('x')] FROM t",
543+
"SELECT a[length(pg_read_file('x'))] FROM t",
544+
"SELECT t.a[length(pg_read_file('x'))] FROM t",
545+
"SELECT a[1:length(pg_read_file('x'))] FROM t",
546+
"SELECT a FROM t WHERE a[length(pg_read_file('x'))] = 1",
547+
"SELECT 1 UNION SELECT 2 LIMIT length(pg_read_file('x'))",
548+
"SELECT 1 UNION SELECT 2 FETCH FIRST length(pg_read_file('x')) ROWS ONLY"})
549+
void functionIsChecked(String sql) throws JSQLParserException {
550+
StatementFeatures impure = analyseAllPureExcept(sql, "pg_read_file");
551+
assertThat(impure.getUnresolvedReferences()).containsExactly("pg_read_file");
552+
assertThat(impure.getUncertain()).contains(StmtFeature.MODIFIES_DATA,
553+
StmtFeature.MODIFIES_SCHEMA);
554+
555+
StatementFeatures pure = analyseAllPureExcept(sql, "");
556+
assertThat(pure.mayModifyData()).isFalse();
557+
assertThat(pure.getUnresolvedReferences()).isEmpty();
558+
}
559+
560+
@ParameterizedTest(name = "{0}")
561+
@ValueSource(strings = {
562+
"SELECT a FROM t WHERE a = ANY (SELECT b FROM u FOR UPDATE)",
563+
"SELECT a FROM t WHERE a > ALL (SELECT b FROM u FOR UPDATE)",
564+
"VALUES ((SELECT b FROM u FOR UPDATE))",
565+
"SELECT a FROM t WHERE a IN (VALUES ((SELECT b FROM u FOR UPDATE)))",
566+
"SELECT a FROM t LIMIT (SELECT b FROM u FOR UPDATE)"})
567+
void rowLockIsSeen(String sql) throws JSQLParserException {
568+
StatementFeatures f = analyse(sql);
569+
assertThat(f.is(StmtFeature.MODIFIES_TRANSACTION)).isTrue();
570+
assertThat(f.is(StmtFeature.READS_DATA)).isTrue();
571+
}
572+
573+
@ParameterizedTest(name = "{0}")
574+
@ValueSource(strings = {
575+
"SELECT a FROM t WHERE a = ANY (WITH c AS (DELETE FROM u RETURNING b) SELECT b FROM c)",
576+
"VALUES ((WITH c AS (DELETE FROM u RETURNING b) SELECT b FROM c))"})
577+
void nestedWriteIsSeen(String sql) throws JSQLParserException {
578+
StatementFeatures f = analyse(sql);
579+
assertThat(f.modifiesData()).isTrue();
580+
assertThat(f.returnsResultSet()).isTrue();
581+
}
582+
583+
@Test
584+
void topLevelValuesReturnsRowsWithoutReading() throws JSQLParserException {
585+
StatementFeatures f = analyse("VALUES (1, 2)");
586+
assertThat(f.returnsResultSet()).isTrue();
587+
assertThat(f.is(StmtFeature.READS_DATA)).isFalse();
588+
assertThat(f.mayModifyData()).isFalse();
589+
}
590+
}
501591
}

0 commit comments

Comments
 (0)