Repository navigation
Translate the first branch of a UNION query - #1843
Open
agusaldasoro wants to merge 5 commits into
Open
agusaldasoro wants to merge 5 commits into
agusaldasoro wants to merge 5 commits into
Conversation
A UNION is parsed into a SetOperationList, which the generator cast to PlainSelect, so the query failed with a ClassCastException and never got data. The first branch of a UNION is now translated, since one branch returning rows is enough; INTERSECT and EXCEPT are rejected explicitly.
agusaldasoro
marked this pull request as ready for review
October 8, 2026 13:59
jgaleotti
requested changes
Oct 8, 2026
| ) { | ||
| if (sqlQuery is Select) { // TODO: Handle other queries | ||
| val plainSelect = sqlQuery.selectBody as PlainSelect | ||
| val plainSelect = translatedSelect(sqlQuery) |
Collaborator
There was a problem hiding this comment.
but this is no longer a plainSelect, right?
| when (sqlQuery) { | ||
| is Select -> { | ||
| val plainSelect = sqlQuery.selectBody as PlainSelect | ||
| val plainSelect = translatedSelect(sqlQuery) |
Collaborator
There was a problem hiding this comment.
is the output of translatedSelect a PlainSelect?
| // Add tables from JOINs and WHERE clause if they exist | ||
| if (sqlQuery is Select) { | ||
| val plainSelect = sqlQuery.selectBody as PlainSelect | ||
| val plainSelect = translatedSelect(sqlQuery) |
Collaborator
Author
There was a problem hiding this comment.
Yes, you are right, I renamed the method as it still is a plain select
jgaleotti
approved these changes
Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
JSqlParser 4.9 parses a query that combines SELECTs with
UNIONinto aSetOperationList, not aPlainSelect.SmtLibGeneratorcastsqlQuery.selectBody as PlainSelectin four places. For aUNION, the cast threwClassCastException, the query was recorded as untranslatable, and no data was ever generated for it. A parenthesised top-levelSELECT(ParenthesedSelect) failed the same way.Fix
The four casts are replaced by
plainSelectOf, which always returns the plainSELECTto translate:PlainSelectis used as is, and aParenthesedSelectis unwrapped;UNION(orUNION ALL), the first branch is translated as if it were the whole query. AUNIONreturns rows as soon as any one branch does, so data for the first branch is enough;INTERSECTandEXCEPTconstrain every branch, which the translation cannot express. They are rejected with an explicit message instead of aClassCastException, and still end up recorded as untranslatable.The default table for unqualified columns is now taken from the translated branch rather than from all the tables of the statement. Otherwise a
WHEREof the first branch could resolve to a table of another branch.Tests
New
SetOperationSolvingTest, end to end against Z3 and H2:unionGeneratesDataForItsFirstBranch: solvesSELECT … FROM users WHERE age > 30 UNION SELECT … FROM products WHERE stock < 0, inserts the generated rows and checks that theUNIONreturns them. Onmasterthe solver returns no actions.intersectIsRejectedWithoutCrashing: anINTERSECTreturns no actions and does not throw.All tests under
org.evomaster.core.database.sql.solverpass.This PR touches the same lines of
SmtLibGenerator.appendQueryConstraintsandappendJoinConstraintsas #1842 (the default table), so whichever is merged second will need a small rebase.