-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sql: multi-use common table expressions #21084
Comments
Note that the decision on whether to run a read-only statement once (and cache the results) or more than once is cost-based. Also, if a read-only statement is used more than once, it may be useful to "unnest" the statement in order to push-down filters or merge projections. I don't have examples on hand, but this is discussed in academic literature regarding optimizing CTEs. |
Would I be correct in saying this would allow the introspection query used in Postgraphile to run on CockroachDB? The expressiveness of GraphQL for API development has seen the Postgraphile project gain considerable traction. Being able to use it with a database such as CockroachDB for its cloud native features would be very compelling. I understand that this alone may not allow Postgraphile to run. Maybe a seperate issue could be created and tracked within #25468? |
@stevenross I added PostGraphile to #25470. |
This commit introduces a With operator to opt, which allows us to reference CTEs multiple times. This enables some missing functionality, where we can reference CTEs multiple times, or not at all, even if they contain mutations. We lose some optimizations because CTEs now present an optimization fence. This might be fixable with a rule something like: ``` (With $value:* $input:(WithRef) & (References $input $value) ) => $value ``` subject to certain side-effect restrictions, along with some rule props to get the Withs down further in the tree. Additionally, as they are now, WithExprs present another optimization fence, since we don't have any rules for pushing them further down the tree. Ideally, we would get all WithExprs that *can* be inlined down to their point of use, and all those that cannot up to the very root of the tree. This is future work. Fixes cockroachdb#24307. Fixes cockroachdb#21084. Release note (sql change): Common Table Expressions (CTEs) may now be referenced from multiple locations in a query.
This commit introduces a With operator to opt, which allows us to reference CTEs multiple times. This enables some missing functionality, where we can reference CTEs multiple times, or not at all, even if they contain mutations. We lose some optimizations because CTEs now present an optimization fence. This might be fixable with a rule something like: ``` (With $value:* $input:(WithRef) & (References $input $value) ) => $value ``` subject to certain side-effect restrictions, along with some rule props to get the Withs down further in the tree. Additionally, as they are now, WithExprs present another optimization fence, since we don't have any rules for pushing them further down the tree. Ideally, we would get all WithExprs that *can* be inlined down to their point of use, and all those that cannot up to the very root of the tree. This is future work. Fixes cockroachdb#24307. Fixes cockroachdb#21084. Release note (sql change): Common Table Expressions (CTEs) may now be referenced from multiple locations in a query.
This commit introduces a With operator to opt, which allows us to reference CTEs multiple times. This enables some missing functionality, where we can reference CTEs multiple times, or not at all, even if they contain mutations. We lose some optimizations because CTEs now present an optimization fence. This might be fixable with a rule something like: ``` (With $value:* $input:(WithRef) & (References $input $value) ) => $value ``` subject to certain side-effect restrictions, along with some rule props to get the Withs down further in the tree. Additionally, as they are now, WithExprs present another optimization fence, since we don't have any rules for pushing them further down the tree. Ideally, we would get all WithExprs that *can* be inlined down to their point of use, and all those that cannot up to the very root of the tree. This is future work. Fixes cockroachdb#24307. Fixes cockroachdb#21084. Release note (sql change): Common Table Expressions (CTEs) may now be referenced from multiple locations in a query.
This commit introduces a With operator to opt, which allows us to reference CTEs multiple times. This enables some missing functionality, where we can reference CTEs multiple times, or not at all, even if they contain mutations. We lose some optimizations because CTEs now present an optimization fence. This might be fixable with a rule something like: ``` (With $value:* $input:(WithRef) & (References $input $value) ) => $value ``` subject to certain side-effect restrictions, along with some rule props to get the Withs down further in the tree. Additionally, as they are now, WithExprs present another optimization fence, since we don't have any rules for pushing them further down the tree. Ideally, we would get all WithExprs that *can* be inlined down to their point of use, and all those that cannot up to the very root of the tree. This is future work. Fixes cockroachdb#24307. Fixes cockroachdb#21084. Release note (sql change): Common Table Expressions (CTEs) may now be referenced from multiple locations in a query.
This commit introduces a With operator to opt, which allows us to reference CTEs multiple times. This enables some missing functionality, where we can reference CTEs multiple times, or not at all, even if they contain mutations. We lose some optimizations because CTEs now present an optimization fence. This might be fixable with a rule something like: ``` (With $value:* $input:(WithRef) & (References $input $value) ) => $value ``` subject to certain side-effect restrictions, along with some rule props to get the Withs down further in the tree. Additionally, as they are now, WithExprs present another optimization fence, since we don't have any rules for pushing them further down the tree. Ideally, we would get all WithExprs that *can* be inlined down to their point of use, and all those that cannot up to the very root of the tree. This is future work. Fixes cockroachdb#24307. Fixes cockroachdb#21084. Release note (sql change): Common Table Expressions (CTEs) may now be referenced from multiple locations in a query.
This commit introduces a With operator to opt, which allows us to reference CTEs multiple times. This enables some missing functionality, where we can reference CTEs multiple times, or not at all, even if they contain mutations. We lose some optimizations because CTEs now present an optimization fence. This might be fixable with a rule something like: ``` (With $value:* $input:(WithRef) & (References $input $value) ) => $value ``` subject to certain side-effect restrictions, along with some rule props to get the Withs down further in the tree. Additionally, as they are now, WithExprs present another optimization fence, since we don't have any rules for pushing them further down the tree. Ideally, we would get all WithExprs that *can* be inlined down to their point of use, and all those that cannot up to the very root of the tree. This is future work. Fixes cockroachdb#24307. Fixes cockroachdb#21084. Release note (sql change): Common Table Expressions (CTEs) may now be referenced from multiple locations in a query.
38670: opt: add WITH operator r=justinj a=justinj This commit introduces a With operator to opt, which allows us to reference CTEs multiple times. This enables some missing functionality, where we can reference CTEs multiple times, or not at all, even if they contain mutations. We lose some optimizations because CTEs now present an optimization fence. This might be fixable with a rule something like: ``` (With $value:* $input:(WithRef) & (References $input $value) ) => $value ``` subject to certain side-effect restrictions, along with some rule props to get the Withs down further in the tree. Additionally, as they are now, WithExprs present another optimization fence, since we don't have any rules for pushing them further down the tree. Ideally, we would get all WithExprs that *can* be inlined down to their point of use, and all those that cannot up to the very root of the tree. This is future work. Fixes #24307. Fixes #21084. Release note (sql change): Common Table Expressions (CTEs) may now be referenced from multiple locations in a query. Co-authored-by: Justin Jaffray <justin@cockroachlabs.com>
I'm trying to get the total count of matched rows, but return only a subset of them for pagination, like described in: https://stackoverflow.com/a/28888696/926620 : WITH cte AS (
SELECT *
FROM tbl
WHERE /* whatever */
)
SELECT *
FROM (
TABLE cte
ORDER BY col1
LIMIT ?
OFFSET ?
) sub
RIGHT JOIN (SELECT count(*) FROM cte) c(full_count) ON true; but I get:
Is there any way to efficiently do that in CockroachDB? BTW AFAIK |
@Bessonov what version of CRDB are you using? In 19.2 (we have just released a release candidate https://www.cockroachlabs.com/docs/releases/#testing-releases) this should be supported but it isn't in early versions. For example: WITH r AS (SELECT * FROM rides WHERE revenue > 98),
results AS (SELECT * FROM users AS u, r WHERE r.rider_id = u.id)
SELECT * FROM results;
id | city | name | address | credit_card | id | city | vehicle_city | rider_id | vehicle_id | start_address | end_address | start_time | end_time | revenue
+--------------------------------------+---------------+------------------+--------------------------------+-------------+--------------------------------------+---------------+---------------+--------------------------------------+--------------------------------------+-----------------------------------+---------------------------+---------------------------+---------------------------+---------+
ae147ae1-47ae-4800-8000-000000000022 | amsterdam | Tyler Dalton | 88194 Angela Gardens Suite 94 | 4443538758 | bbe76c8b-4395-4000-8000-00000000016f | amsterdam | amsterdam | ae147ae1-47ae-4800-8000-000000000022 | aaaaaaaa-aaaa-4800-8000-00000000000a | 45295 Brewer View Suite 52 | 62188 Jade Causeway | 2018-12-17 03:04:05+00:00 | 2018-12-17 13:04:05+00:00 | 99.00 We have some documentation in our 19.2 version of the docs https://www.cockroachlabs.com/docs/v19.2/common-table-expressions.html#overview |
@awoods187 thank you for your response! I was not able to build cockroachdb from sources. But now tested with 19.2 image and it does work as you stated! Do you have any thoughts on |
We haven't yet written up docs but good information can be found here cockroachdb/docs#3743 |
Split from #7029 which was closed when we merged support for single-use common table expressions in #20359.
Support common table expressions whose clauses are used more than once in the statement. This may require temporary tables or other novel infrastructure, since each referenced statement should be run at most once in the case of DML statements, and in the case of read-only statements, should probably be run at most once anyway for efficiency.
For example,
In this statement, the
x
clause is referenced more than once, which is not currently supported.The text was updated successfully, but these errors were encountered: