Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@
public <S> T visit(JsonExpression jsonExpr, S context) {
ArrayList<Expression> subExpressions = new ArrayList<>();
subExpressions.add(jsonExpr.getExpression());
subExpressions.addAll(jsonExpr.getIdents());

Check warning on line 567 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (windows-latest)

[deprecation] getIdents() in JsonExpression has been deprecated

Check warning on line 567 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (ubuntu-latest)

[deprecation] getIdents() in JsonExpression has been deprecated

Check warning on line 567 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (macos-latest)

[deprecation] getIdents() in JsonExpression has been deprecated
return visitExpressions(jsonExpr, context, subExpressions);
}

Expand Down Expand Up @@ -829,12 +829,12 @@

@Override
public <S> T visit(ConnectByRootOperator connectByRootOperator, S context) {
return connectByRootOperator.getColumn().accept(this, context);

Check warning on line 832 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (windows-latest)

[deprecation] getColumn() in ConnectByRootOperator has been deprecated

Check warning on line 832 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (ubuntu-latest)

[deprecation] getColumn() in ConnectByRootOperator has been deprecated

Check warning on line 832 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (macos-latest)

[deprecation] getColumn() in ConnectByRootOperator has been deprecated
}

@Override
public <S> T visit(ConnectByPriorOperator connectByPriorOperator, S context) {
return connectByPriorOperator.getColumn().accept(this, context);

Check warning on line 837 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (windows-latest)

[deprecation] getColumn() in ConnectByPriorOperator has been deprecated

Check warning on line 837 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (ubuntu-latest)

[deprecation] getColumn() in ConnectByPriorOperator has been deprecated

Check warning on line 837 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (macos-latest)

[deprecation] getColumn() in ConnectByPriorOperator has been deprecated
}

@Override
Expand Down Expand Up @@ -867,7 +867,7 @@
if (selectVisitor != null) {
if (select.getWithItemsList() != null) {
for (WithItem<?> item : select.getWithItemsList()) {
item.accept(selectVisitor, context);

Check warning on line 870 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (windows-latest)

[deprecation] <T,S>accept(SelectVisitor<T>,S) in WithItem has been deprecated

Check warning on line 870 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (ubuntu-latest)

[deprecation] <T,S>accept(SelectVisitor<T>,S) in WithItem has been deprecated

Check warning on line 870 in src/main/java/net/sf/jsqlparser/expression/ExpressionVisitorAdapter.java

View workflow job for this annotation

GitHub Actions / Maven Verify (macos-latest)

[deprecation] <T,S>accept(SelectVisitor<T>,S) in WithItem has been deprecated
}
}
select.accept(selectVisitor, context);
Expand Down Expand Up @@ -969,7 +969,7 @@

@Override
public <S> T visit(FromQuery fromQuery, S context) {
return null;
return visit((Select) fromQuery, context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1336,6 +1337,13 @@ public <S> 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 <S> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<T> implements SelectVisitor<T> {
Expand Down Expand Up @@ -272,9 +292,121 @@ public <S> T visit(UnPivotQuery unPivotQuery, S context) {

@Override
public <S> 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<S> 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<S> implements PipeOperatorVisitor<T, S> {

@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 <S> T visit(SetOperationList setOpList, S context) {
visitWithItems(setOpList.getWithItemsList(), context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down
Loading