Skip to content

fix: check functions in PIVOT clauses of FROM items - #2945

Open
tanjiroK-coder wants to merge 1 commit into
JSQLParser:masterfrom
tanjiroK-coder:pivot-clause-traversal
Open

tanjiroK-coder wants to merge 1 commit into
JSQLParser:masterfrom
tanjiroK-coder:pivot-clause-traversal

Conversation

@tanjiroK-coder

Copy link
Copy Markdown
Contributor

FromItemVisitorAdapter never walks the PIVOT / UNPIVOT clause the grammar attaches to a FROM item: visit(Table) returns immediately, and the derived table, LATERAL, table function, parenthesized join and VALUES visits ignore getPivot() / getUnPivot(), so only MATCH_RECOGNIZE walked its pivot (SelectVisitorAdapter checks PlainSelect.getPivot(), which the parser does not set for this syntax). StatementFeatureVisitor is built on that adapter, so the aggregate calls of an Oracle PIVOT never reach the pure-function check: SELECT * FROM t PIVOT (sum(purge_audit()) FOR a IN (1)), the same clause on a derived or joined table, and a PIVOT XML IN subquery that calls a function come back with mayModifyData() == false and no unresolved reference and pass the read-only guard in usage.rst, while SELECT purge_audit() FROM t leaves MODIFIES_DATA possible with unresolved=[purge_audit]. Oracle takes any expression as the aggregate argument, and a user-defined aggregate in that position was not reported either. Validation had the matching gap: SelectValidator.visit(Pivot) only validated the FOR columns while visit(PivotXml) also validated the aggregate calls, so with Feature.pivot allowed and Feature.function removed, SELECT * FROM t PIVOT (max(upper(a)) FOR b IN (1)) validated with no errors and the PIVOT XML form did not. I found it while comparing the FROM-item clauses TablesNamesFinder walks with the ones the adapter walks. The adapter now walks the pivot of every FROM item through the PivotVisitorAdapter it already used for MATCH_RECOGNIZE, and visit(Pivot) validates the aggregate calls as visit(PivotXml) does; the IN values are left out there, since SQL Server writes them as bracketed identifiers that are not columns. Four new functionIsChecked cases and one testValidateFunctionOutsideWhere case fail before (5 failures) and pass after; spotless, Checkstyle, PMD and SpotBugs pass, and the rest of ./gradlew test is unchanged (the only local failures are 33 Mockito inline-mock-maker init errors and 2 temp-dir access errors, which fail the same way on master on this Windows machine).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant