Repository navigation
Add subqueries in expressions - #159
Merged
Merged
Conversation
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.
This PR adds subqueries in expressions for milestone M2 (#3). It is a part of the item "
SELECTwith joins, subqueries, CASE, regclass/regtype casts and pg_get_* functions". The casts and thepg_get_*functions will come in a later PR.Analyzer
crates/rupg-analyze/src/sublink.rsistransformSubLink. It runs the subquery first and then the left side, as PostgreSQL does. It takesEXISTS,IN,NOT IN,op ANY,op ALL,ARRAY(SELECT ...)and a scalar subquery.transformSubLink:subquery must return only one column,subquery has too many columnsandsubquery has too few columns, at the same positions. An operator ofANYorALLthat does not givebooleangives42804.ARRAY()of a type with no array type givescould not find array type for data type.Varnow haslevels_up, asvarlevelsup. The analyzer keeps the scopes of the outer queries. A column name and a table name search from the subquery up to the outer queries, and stop at the first level that has the name, ascolNameToVarandrefnameNamespaceItemdo.errorMissingColumnanderrorMissingRTEsearch the relations of all levels. So the hint of a close name can name a column of an outer relation, as in PostgreSQL.substitute_grouped_columns_mutatordoes. A column of a grouped outer query that is not inGROUP BYgivessubquery uses ungrouped column "c.amname" from outer query.LIMITandOFFSETrefuse only the columns of their own query, also inside a subquery. A column of an outer query is a constant there.0A000for it for now.Executor
InitPlandoes. Other subqueries run again for each row, with the rows of the outer queries in a stack of frames.WHEREpush down and the column marks count only the columns of their own level. A condition such aso.opcmethod = a.oidin a subquery goes down to the scan ofo.EXISTSfollowssimplify_EXISTS_query. With no aggregate, noHAVING, noOFFSET, and aLIMITthat is a constant more than 0 or null, it does not compute the target list. SoEXISTS (SELECT 1/0 FROM pg_am WHERE false)is false andEXISTS (SELECT 1/0)is true with no error.OFFSET 0and an aggregate keep the target list, and the error comes, as in PostgreSQL.21000 more than one row returned by a subquery used as an expression. With no row it gives null.ANYandALLuse the logic of three values ofExecScanSubPlan.Not in this PR
INorANY, such as(a, b) IN (SELECT ...).ARRAY()of an array, which makes an array with one more dimension.SELECT (SELECT max(a.oid) FROM pg_type LIMIT 1) FROM pg_am a.FROM.EXISTS (SELECT 1/0 FROM pg_am LIMIT 0), so it givesdivision by zero. rupg gives false. The constant folding item will fix this.Tests
subquery_errorsin rupg-analyze checks the errors and the positions, the hints of close names in outer relations, and an outer column inLIMIT.subqueriesin rupg-exec runs 42 queries on the catalog. They include correlated subqueries at two levels,IN,NOT INwith a null,ANYandALLwith no rows,ARRAY()with nulls, subqueries inJOIN ... ON,HAVING,GROUP BY,ORDER BYandLIMIT, the cases ofsimplify_EXISTS_query, and the error of a scalar subquery with many rows.cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspaceand thecargo xtaskchecks pass on server1.