diff --git a/src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java b/src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java index 11b052239..628cb5b70 100644 --- a/src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java +++ b/src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java @@ -969,7 +969,7 @@ public T visit(CosineSimilarity cosineSimilarity, S context) { @Override public T visit(FromQuery fromQuery, S context) { - return null; + return visit((Select) fromQuery, context); } @Override diff --git a/src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java b/src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java index 2c66d8729..55c8128fe 100644 --- a/src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java +++ b/src/main/java/net/sf/jsqlparser/statement/StatementFeatureVisitor.java @@ -148,6 +148,7 @@ import net.sf.jsqlparser.statement.oracle.OracleAssignment; import net.sf.jsqlparser.statement.oracle.OracleBlock; import net.sf.jsqlparser.statement.oracle.OracleNullStatement; +import net.sf.jsqlparser.statement.piped.FromQuery; import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement; import net.sf.jsqlparser.statement.reindex.Reindex; import net.sf.jsqlparser.statement.resourcegroup.AlterResourceGroup; @@ -1336,6 +1337,13 @@ public Void visit(TableStatement tableStatement, S context) { return super.visit(tableStatement, context); } + /** A pipe query always starts from a FROM item, as in FROM t |> WHERE a = 1. */ + @Override + public Void visit(FromQuery fromQuery, S context) { + analysis.certain(StmtFeature.READS_DATA); + return super.visit(fromQuery, context); + } + /** * Replaces the inherited implementation, which casts the CTE body to ParenthesedSelect and * therefore throws on {@code WITH c AS (DELETE .. RETURNING *)}. Routing to the statement diff --git a/src/main/java/net/sf/jsqlparser/statement/select/SelectVisitorAdapter.java b/src/main/java/net/sf/jsqlparser/statement/select/SelectVisitorAdapter.java index db2ad7594..41c5b7ff0 100644 --- a/src/main/java/net/sf/jsqlparser/statement/select/SelectVisitorAdapter.java +++ b/src/main/java/net/sf/jsqlparser/statement/select/SelectVisitorAdapter.java @@ -9,6 +9,7 @@ */ package net.sf.jsqlparser.statement.select; +import java.util.Collections; import java.util.List; import net.sf.jsqlparser.expression.ExpressionVisitor; import net.sf.jsqlparser.expression.ExpressionVisitorAdapter; @@ -19,7 +20,26 @@ import net.sf.jsqlparser.statement.ParenthesedStatement; import net.sf.jsqlparser.statement.Statement; import net.sf.jsqlparser.statement.StatementVisitor; +import net.sf.jsqlparser.statement.piped.AggregatePipeOperator; +import net.sf.jsqlparser.statement.piped.AsPipeOperator; +import net.sf.jsqlparser.statement.piped.CallPipeOperator; +import net.sf.jsqlparser.statement.piped.DropPipeOperator; +import net.sf.jsqlparser.statement.piped.ExtendPipeOperator; import net.sf.jsqlparser.statement.piped.FromQuery; +import net.sf.jsqlparser.statement.piped.JoinPipeOperator; +import net.sf.jsqlparser.statement.piped.LimitPipeOperator; +import net.sf.jsqlparser.statement.piped.OrderByPipeOperator; +import net.sf.jsqlparser.statement.piped.PipeOperator; +import net.sf.jsqlparser.statement.piped.PipeOperatorVisitor; +import net.sf.jsqlparser.statement.piped.PivotPipeOperator; +import net.sf.jsqlparser.statement.piped.RenamePipeOperator; +import net.sf.jsqlparser.statement.piped.SelectPipeOperator; +import net.sf.jsqlparser.statement.piped.SetOperationPipeOperator; +import net.sf.jsqlparser.statement.piped.SetPipeOperator; +import net.sf.jsqlparser.statement.piped.TableSamplePipeOperator; +import net.sf.jsqlparser.statement.piped.UnPivotPipeOperator; +import net.sf.jsqlparser.statement.piped.WherePipeOperator; +import net.sf.jsqlparser.statement.piped.WindowPipeOperator; @SuppressWarnings({"PMD.UncommentedEmptyMethodBody"}) public class SelectVisitorAdapter implements SelectVisitor { @@ -272,9 +292,121 @@ public T visit(UnPivotQuery unPivotQuery, S context) { @Override public T visit(FromQuery fromQuery, S context) { + visitWithItems(fromQuery.getWithItemsList(), context); + fromItemVisitor.visitFromItem(fromQuery.getFromItem(), context); + if (fromQuery.getLateralViews() != null) { + for (LateralView lateralView : fromQuery.getLateralViews()) { + expressionVisitor.visitExpression(lateralView.getGeneratorFunction(), context); + } + } + fromItemVisitor.visitJoins(fromQuery.getJoins(), context); + + PipeOperatorWalker pipeOperators = new PipeOperatorWalker<>(); + for (PipeOperator pipeOperator : fromQuery.getPipeOperators()) { + pipeOperator.accept(pipeOperators, context); + } return null; } + /** Walks the expressions, joins and nested queries of the pipe operators of a FromQuery. */ + private final class PipeOperatorWalker implements PipeOperatorVisitor { + + @Override + public T visit(AggregatePipeOperator aggregate, S context) { + visitSelectItems(aggregate.getSelectItems(), context); + visitSelectItems(aggregate.getGroupItems(), context); + return null; + } + + @Override + public T visit(AsPipeOperator as, S context) { + return null; + } + + @Override + public T visit(CallPipeOperator call, S context) { + return fromItemVisitor.visitFromItem(call.getTableFunction(), context); + } + + @Override + public T visit(DropPipeOperator drop, S context) { + return expressionVisitor.visitExpressions(drop.getColumns(), context); + } + + @Override + public T visit(ExtendPipeOperator extend, S context) { + return visit((SelectPipeOperator) extend, context); + } + + @Override + public T visit(JoinPipeOperator join, S context) { + return fromItemVisitor.visitJoins(Collections.singletonList(join.getJoin()), context); + } + + @Override + public T visit(LimitPipeOperator limit, S context) { + expressionVisitor.visitExpression(limit.getLimitExpression(), context); + return expressionVisitor.visitExpression(limit.getOffsetExpression(), context); + } + + @Override + public T visit(OrderByPipeOperator orderBy, S context) { + return expressionVisitor.visitOrderBy(orderBy.getOrderByElements(), context); + } + + @Override + public T visit(PivotPipeOperator pivot, S context) { + expressionVisitor.visitExpression(pivot.getAggregateExpression(), context); + visitSelectItems(pivot.getPivotColumns(), context); + return null; + } + + @Override + public T visit(RenamePipeOperator rename, S context) { + return visit((SelectPipeOperator) rename, context); + } + + @Override + public T visit(SelectPipeOperator select, S context) { + visitSelectItems(select.getSelectItems(), context); + return null; + } + + @Override + public T visit(SetPipeOperator set, S context) { + return expressionVisitor.visitUpdateSets(set.getUpdateSets(), context); + } + + @Override + public T visit(TableSamplePipeOperator tableSample, S context) { + return null; + } + + @Override + public T visit(SetOperationPipeOperator setOperation, S context) { + for (ParenthesedSelect select : setOperation.getSelects()) { + select.accept(SelectVisitorAdapter.this, context); + } + return null; + } + + @Override + public T visit(UnPivotPipeOperator unPivot, S context) { + visitSelectItems(unPivot.getPivotColumns(), context); + return null; + } + + @Override + public T visit(WherePipeOperator where, S context) { + return expressionVisitor.visitExpression(where.getExpression(), context); + } + + @Override + public T visit(WindowPipeOperator window, S context) { + return visit((SelectPipeOperator) window, context); + } + } + @Override public T visit(SetOperationList setOpList, S context) { visitWithItems(setOpList.getWithItemsList(), context); diff --git a/src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java b/src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java index 0d61f23d6..4f288e6c8 100644 --- a/src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java +++ b/src/test/java/net/sf/jsqlparser/statement/StatementFeatureVisitorTest.java @@ -347,6 +347,17 @@ void dataModifyingCteBeforeTable(String sql) throws JSQLParserException { assertThat(f.modifiesData()).isTrue(); } + @ParameterizedTest(name = "{0}") + @ValueSource(strings = {"WITH c AS (DELETE FROM t RETURNING *) FROM c |> SELECT a", + "WITH c AS (UPDATE t SET a = 1 RETURNING *) FROM u |> WHERE a = 1"}) + @DisplayName("data-modifying CTE in front of a pipe query: the CTE still mutates") + void dataModifyingCteBeforePipeQuery(String sql) throws JSQLParserException { + StatementFeatures f = analyse(sql); + assertThat(f.returnsResultSet()).isTrue(); + assertThat(f.is(StmtFeature.READS_DATA)).isTrue(); + assertThat(f.modifiesData()).isTrue(); + } + @Test @DisplayName("RETURNING inside a nested CTE of an INSERT returns nothing to the client") void nestedReturningIsNotAResultSet() throws JSQLParserException { @@ -742,7 +753,21 @@ private StatementFeatures analyseAllPureExcept(String sql, String impure) "SHOW TABLE STATUS WHERE length(pg_read_file('x')) > 0", "SHOW COLUMNS FROM t WHERE length(pg_read_file('x')) > 0", "SHOW INDEX FROM t WHERE length(pg_read_file('x')) > 0", - "SHOW VARIABLES WHERE length(pg_read_file('x')) > 0"}) + "SHOW VARIABLES WHERE length(pg_read_file('x')) > 0", + "FROM t |> WHERE length(pg_read_file('x')) > 0", + "FROM t |> SELECT pg_read_file('x')", + "FROM t |> EXTEND pg_read_file('x') AS y", + "FROM t |> SET a = pg_read_file('x')", + "FROM t |> AGGREGATE max(a) GROUP BY pg_read_file('x')", + "FROM t |> ORDER BY pg_read_file('x')", + "FROM t |> LIMIT length(pg_read_file('x'))", + "FROM t |> JOIN u ON u.a = pg_read_file('x')", + "FROM t JOIN u ON u.a = pg_read_file('x')", + "FROM t |> CALL pg_read_file('x')", + "FROM t |> UNION ALL (SELECT pg_read_file('x'))", + "FROM t |> PIVOT (sum(pg_read_file('x')) FOR a IN (1))", + "FROM t |> WINDOW sum(pg_read_file('x')) OVER () AS w", + "SELECT * FROM (FROM t |> WHERE length(pg_read_file('x')) > 0)"}) void functionIsChecked(String sql) throws JSQLParserException { StatementFeatures impure = analyseAllPureExcept(sql, "pg_read_file"); assertThat(impure.getUnresolvedReferences()).containsExactly("pg_read_file");