Skip to content

Commit 8359b35

Browse files
fix: check functions in PIVOT clauses of FROM items (#2945)
Co-authored-by: manticore-projects <andreas@manticore-projects.com>
1 parent 3b6040d commit 8359b35

4 files changed

Lines changed: 35 additions & 12 deletions

File tree

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,39 @@ public <S> T visit(MatchRecognize matchRecognize, S context) {
9191
matchRecognize.getInput().accept(this, context);
9292
matchRecognize
9393
.forEachExpression(expression -> expression.accept(expressionVisitor, context));
94+
visitPivots(matchRecognize, context);
95+
return null;
96+
}
97+
98+
/** Walks the PIVOT / UNPIVOT clause the grammar may attach to any FROM item. */
99+
private <S> void visitPivots(FromItem fromItem, S context) {
94100
PivotVisitorAdapter<T> pivots = new PivotVisitorAdapter<>(expressionVisitor);
95-
if (matchRecognize.getPivot() != null) {
96-
matchRecognize.getPivot().accept(pivots, context);
101+
if (fromItem.getPivot() != null) {
102+
fromItem.getPivot().accept(pivots, context);
97103
}
98-
if (matchRecognize.getUnPivot() != null) {
99-
matchRecognize.getUnPivot().accept(pivots, context);
104+
if (fromItem.getUnPivot() != null) {
105+
fromItem.getUnPivot().accept(pivots, context);
100106
}
101-
return null;
102107
}
103108

104109
@Override
105110
public <S> T visit(Table table, S context) {
111+
visitPivots(table, context);
106112
return null;
107113
}
108114

109115
@Override
110116
public <S> T visit(ParenthesedSelect select, S context) {
111-
return select.getSelect().accept(selectVisitor, context);
117+
T result = select.getSelect().accept(selectVisitor, context);
118+
visitPivots(select, context);
119+
return result;
112120
}
113121

114122
@Override
115123
public <S> T visit(LateralSubSelect lateralSubSelect, S context) {
116-
return lateralSubSelect.getSelect().accept(selectVisitor, context);
124+
T result = lateralSubSelect.getSelect().accept(selectVisitor, context);
125+
visitPivots(lateralSubSelect, context);
126+
return result;
117127
}
118128

119129
@Override
@@ -123,13 +133,15 @@ public <S> T visit(TableFunction tableFunction, S context) {
123133
function.accept(expressionVisitor, context);
124134
}
125135
}
136+
visitPivots(tableFunction, context);
126137
return null;
127138
}
128139

129140
@Override
130141
public <S> T visit(ParenthesedFromItem fromItem, S context) {
131142
T result = fromItem.getFromItem().accept(this, context);
132143
visitJoins(fromItem.getJoins(), context);
144+
visitPivots(fromItem, context);
133145
return result;
134146
}
135147

@@ -138,6 +150,7 @@ public <S> T visit(Values values, S context) {
138150
for (Expression expression : values.getExpressions()) {
139151
expression.accept(expressionVisitor, context);
140152
}
153+
visitPivots(values, context);
141154
return null;
142155
}
143156

‎src/main/java/net/sf/jsqlparser/util/validation/validator/SelectValidator.java‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,17 @@ public <S> Void visit(Table table, S context) {
342342
public <S> Void visit(Pivot pivot, S context) {
343343
validateFeature(Feature.pivot);
344344
validateOptionalExpressions(pivot.getForColumns());
345+
validatePivotFunctions(pivot, context);
345346
return null;
346347
}
347348

349+
private <S> void validatePivotFunctions(Pivot pivot, S context) {
350+
if (isNotEmpty(pivot.getFunctionItems())) {
351+
ExpressionValidator v = getValidator(ExpressionValidator.class);
352+
pivot.getFunctionItems().forEach(f -> f.getExpression().accept(v, context));
353+
}
354+
}
355+
348356
@Override
349357
public <S> Void visit(UnPivot unpivot, S context) {
350358
validateFeature(Feature.unpivot);
@@ -358,10 +366,7 @@ public <S> Void visit(UnPivot unpivot, S context) {
358366
public <S> Void visit(PivotXml pivot, S context) {
359367
validateFeature(Feature.pivotXml);
360368
validateOptionalExpressions(pivot.getForColumns());
361-
if (isNotEmpty(pivot.getFunctionItems())) {
362-
ExpressionValidator v = getValidator(ExpressionValidator.class);
363-
pivot.getFunctionItems().forEach(f -> f.getExpression().accept(v, context));
364-
}
369+
validatePivotFunctions(pivot, context);
365370
if (pivot.getInSelect() != null) {
366371
pivot.getInSelect().accept((SelectVisitor<Void>) this, context);
367372
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,10 @@ private StatementFeatures analyseAllPureExcept(String sql, String impure)
714714
"SET x = pg_read_file('x')",
715715
"DECLARE @x INT = length(pg_read_file('x'))",
716716
"ASSERT length(pg_read_file('x')) > 0",
717+
"SELECT * FROM t PIVOT (sum(pg_read_file('x')) FOR a IN (1))",
718+
"SELECT * FROM (SELECT a, b FROM t) PIVOT (sum(pg_read_file('x')) FOR a IN (1))",
719+
"SELECT * FROM u JOIN t PIVOT (sum(pg_read_file('x')) FOR a IN (1)) ON 1 = 1",
720+
"SELECT * FROM t PIVOT XML (sum(b) FOR a IN (SELECT pg_read_file('x') FROM u))"})
717721
"SHOW TABLES WHERE length(pg_read_file('x')) > 0",
718722
"SHOW TABLE STATUS WHERE length(pg_read_file('x')) > 0",
719723
"SHOW COLUMNS FROM t WHERE length(pg_read_file('x')) > 0",

‎src/test/java/net/sf/jsqlparser/util/validation/validator/SelectValidatorTest.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ public void testValidateTableFunctionArguments(String sql) {
267267
"SELECT a FROM t LIMIT 1 OFFSET upper(a)",
268268
"SELECT a FROM t OFFSET upper(a) ROWS",
269269
"SELECT a FROM t FETCH FIRST upper(a) ROWS ONLY",
270-
"SELECT a FROM t UNION SELECT b FROM u LIMIT upper(a)"})
270+
"SELECT a FROM t UNION SELECT b FROM u LIMIT upper(a)",
271+
"SELECT * FROM t PIVOT (max(upper(a)) FOR b IN (1))"})
271272
public void testValidateFunctionOutsideWhere(String sql) {
272273
validateNotAllowed(sql, 1, 1,
273274
new FeaturesAllowed(Feature.values()).remove(Feature.function), Feature.function);

0 commit comments

Comments
 (0)