Skip to content

SubPlan Pushdown#186

Closed
serprex wants to merge 7 commits into
mainfrom
subquery-pushdown-v2
Closed

SubPlan Pushdown#186
serprex wants to merge 7 commits into
mainfrom
subquery-pushdown-v2

Conversation

@serprex
Copy link
Copy Markdown
Member

@serprex serprex commented Apr 4, 2026

rebased #126 & got working with tests

@serprex serprex requested review from iskakaushik and theory April 4, 2026 20:01
@serprex serprex force-pushed the subquery-pushdown-v2 branch 4 times, most recently from 59c6eda to 92be51e Compare April 4, 2026 21:30
@serprex serprex marked this pull request as draft April 4, 2026 21:48
@serprex serprex changed the title Subquery Pushdown SubPlan Pushdown Apr 4, 2026
@serprex serprex force-pushed the subquery-pushdown-v2 branch 4 times, most recently from 837e202 to 689716a Compare April 5, 2026 01:20
@serprex serprex marked this pull request as ready for review April 5, 2026 02:13
@serprex serprex force-pushed the subquery-pushdown-v2 branch 4 times, most recently from 6d9cdbc to 6e4eeaf Compare April 5, 2026 19:24
Comment thread src/deparse.c Outdated
}
}
if (!found)
elog(ERROR, "no ANY_SUBLINK alternative for pushdown");
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be ereport, would like to get this cleaned up across codebase after http streaming lands

@serprex serprex force-pushed the subquery-pushdown-v2 branch 2 times, most recently from 73bc318 to e30acbb Compare April 5, 2026 19:37
@serprex serprex added the pushdown Improvements to query pushdown label Apr 6, 2026
@serprex serprex mentioned this pull request Apr 6, 2026
Copy link
Copy Markdown
Collaborator

@theory theory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a commit that executes the fully pushed-down query. It fails. I suspect we need to do some sort of jiggerpokery to generate new aliases in the subquery and then to reference tables in the outer query.

Comment on lines +262 to +268
Foreign Scan on subquery_test.spd_orders
Output: spd_orders.id, spd_orders.status
Remote SQL: SELECT id, status FROM subquery_test.spd_orders WHERE ((id IN (SELECT order_id FROM subquery_test.items) OR (status = 'closed'))) ORDER BY id ASC NULLS LAST
SubPlan 2
-> Foreign Scan on subquery_test.items
Output: items.order_id
Remote SQL: SELECT order_id FROM subquery_test.items
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this and the other queries in this file not be pushed down as a single ClickHouse query?

Comment thread src/deparse.c
Comment on lines +398 to +400
if (subplan->subLinkType != ANY_SUBLINK &&
subplan->subLinkType != EXISTS_SUBLINK &&
subplan->subLinkType != EXPR_SUBLINK)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, no support for CTEs (yet)? What about the others?

Comment thread src/deparse.c Outdated
return false;
}

/* Reject nested SubPlans (correlated or InitPlan) */
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Comment thread src/deparse.c
if (first)
appendStringInfoChar(buf, '1');

/* FROM clause */
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we unable to use the existing deparsers for these clauses? deparseFromExprForRel() or deparseFromExpr(), deparseSortGroupClause(), etc.?

@serprex serprex marked this pull request as draft April 7, 2026 00:49
serprex and others added 2 commits May 1, 2026 03:57
Demonstrate that it fails due to improper table aliasing.
@serprex serprex force-pushed the subquery-pushdown-v2 branch from 3155369 to 73bbcf8 Compare May 1, 2026 03:58
Comment thread src/deparse.c
Comment on lines +427 to +428
* CTE_SUBLINK Recursive/materialised CTE survives
* past inline_cte_walker; CH cannot host.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean? Possible to support in the future? What would it take?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're operating on AST after some optimizations, CTE_SUBLINK at this point is complicated enough to not be optimized

CH does not support recursive CTE: https://clickhouse.com/docs/sql-reference/statements/select/with

Recursion is prevented by hiding the current CTE from the identifier resolution process.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, would be nice to support non-recursive CTE in the future.

Comment thread src/deparse.c
* ALL_SUBLINK CH lacks ALL; would need NOT EXISTS
* rewrite. NOT IN already arrives as
* NOT(ANY_SUBLINK).
* ROWCOMPARE_SUBLINK Multi-column compare not deparsed.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could they be? What would need to change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they could be, but will require more intrusive deparsing logic, on CH I think it could work by converting to using tuples. but seems best to have initial PR manage dealing with pg subplan logic where syntax is pretty 1:1, then followup can expand

@theory theory mentioned this pull request May 11, 2026
@serprex
Copy link
Copy Markdown
Member Author

serprex commented May 26, 2026

failed experiment, may be used as a reference by some future attempt

@serprex serprex closed this May 26, 2026
@theory theory deleted the subquery-pushdown-v2 branch May 27, 2026 09:25
@serprex serprex restored the subquery-pushdown-v2 branch May 27, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pushdown Improvements to query pushdown

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants