-
Notifications
You must be signed in to change notification settings - Fork 31
Grammar for plan constraints #20
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# **RFC007 for Presto** | ||
|
||
## Grammar for Plan Constraints | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would call this feature Plan Hints rather than Plan Constraints. The optimizer may not follow the hints (though we should issue a warning if we don't) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sure, will rename |
||
|
||
Proposers | ||
|
||
* @aaneja | ||
* @ClarenceThreepwood | ||
|
||
## Related Issues | ||
|
||
* [Overview doc](https://prestodb.io/wp-content/uploads/Search-Space-Improvements-Plan-Constraints.pdf) on Plan Constraints as a tool to control search space | ||
* PrestoDB blog on - [Elevating Presto Query Optimization](https://prestodb.io/blog/2024/03/21/elevating-presto-query-optimization/) | ||
|
||
## Summary | ||
|
||
This document proposes a grammar for specifying plan constraints | ||
|
||
## Background | ||
|
||
Plan constraints can be used to lock down critical aspects of an execution plan, such as access method, join method, and join order. | ||
|
||
Most commercially available cost-based optimizers support some form by which a user could express plan constraints that enable these two use-cases. Common terms used by DB vendors to describe this set of features include "plan constraints", "optimization guidelines" or "plan hints". | ||
|
||
There are two broad approaches as to how these plan constraints are stored and represented. Vendors such as [Oracle](https://docs.oracle.com/cd/B10500_01/server.920/a96533/hintsref.htm) & [Vertica](https://www.vertica.com/docs/10.0.x/HTML/Content/Authoring/SQLReferenceManual/LanguageElements/Hints/Hints.htm) provide a grammar/syntax where the plan constraint may be expressed inline with the sql query. The parser then picks up the constraint, validates it, and then passes it on to the query optimizer. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would helpful to describe the syntax of other systems and how this one compares. like if we're doing something different from everyone else we need a very clear reason why. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can add a summary section describing the hint syntax, but IMO we can be opinionated about the syntax and make the right tradeoffs for what works best for us. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree we can be opinionated and choose what works best for us, but we should still learn from what others do and where we vary, we should understand why we are going a different route/ are consciously choosing it. Being more similar to other systems people may have encountered can also make it more user friendly. |
||
|
||
Other systems such as [DB2](https://www.ibm.com/docs/en/db2/11.1?topic=guidelines-creating-statement-level-optimization) and [SqlServer](https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-query?view=sql-server-ver16), provide mechanisms to specify and store these optimization guidelines independent of a sql query, allow the database to map these stored plan constraints to an incoming query and then pass these constraints on to the optimizer as applicable. | ||
|
||
Let us call these two approaches – inline plan constraints vs independent plan constraints. | ||
|
||
|
||
## Proposed Implementation | ||
|
||
We propose a grammar for specifying independent plan constraints, which take the form of a SQL comment block that would build an object graph of the constraints. Multiple constraints can be specified in a single place. The grammar is open for extension as we develop more mechanisms to lock down plans. | ||
|
||
In this first cut, users can build constraints to control | ||
- Join orders and distributions for INNER JOIN's | ||
- Cardinality (row counts) for base relations and join sub-plans | ||
|
||
### Grammar (ANTLR 4) | ||
|
||
``` | ||
grammar PlanConstraints; | ||
|
||
// Lexer rules | ||
WS : [ \t\r\n]+ -> skip ; | ||
NUMBER : [0-9]+ ; | ||
LOJ : 'LOJ' ; | ||
ROJ : 'ROJ' ; | ||
IJ : 'IJ' ; | ||
LPAREN : '(' ; | ||
RPAREN : ')' ; | ||
JOIN_DIST_PARTITIONED : '[P]' ; | ||
JOIN_DIST_REPLICATED : '[R]' ; | ||
CARD : 'CARD' ; | ||
JOIN : 'JOIN' ; | ||
CONSTRAINTS_START_MARKER :'/*!'; | ||
aaneja marked this conversation as resolved.
Show resolved
Hide resolved
aaneja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
CONSTRAINTS_END_MARKER :'*/'; | ||
|
||
fragment DIGIT | ||
: [0-9] | ||
; | ||
|
||
fragment LETTER | ||
: [A-Z] | ||
| [a-z] | ||
; | ||
|
||
IDENTIFIER | ||
: (LETTER | '_') (LETTER | DIGIT | '_' | '@' | ':')* | ||
; | ||
|
||
identifier | ||
: IDENTIFIER | ||
; | ||
|
||
standAloneRelation | ||
: identifier | ||
; | ||
|
||
groupedRelation | ||
: LPAREN joinedRelation RPAREN | ||
; | ||
|
||
joinTypeOrDefault | ||
: joinType | ||
| { "IJ" } // Default value | ||
; | ||
|
||
joinType | ||
: LOJ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Had mentioned this in an earlier comment, we plan to support join hints for outer joins as well. This provides support for the same. I am OK removing it for now, since v1 will not have support for using this hint There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but the type of join is determined by the query, so even if we support join hints, we shouldn't need to specify if it's an inner or outer join. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If/when we support outer join reordering, we would need these to specify hints for outer-join reordering. Of course, if the planner never evaluates these hint choices, these hints are ignored and there is zero chance of a correctness issue w.r.t what type of join to choose |
||
| ROJ | ||
| IJ | ||
; | ||
|
||
joinAttribute | ||
: JOIN_DIST_PARTITIONED | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just call this PARTITIONED. JOIN_DIST_PARTITIONED is clunky to write and to remember There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's some confusion, the user will use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, thanks. missed this. Still would recommend using broadcast/ B vs. R because that's more consistent with the user facing language we use elsewhere (e.g. session property join_distribution_type=BROADCAST). |
||
| JOIN_DIST_REPLICATED | ||
aaneja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
; | ||
|
||
joinedRelation | ||
: standAloneRelation joinTypeOrDefault standAloneRelation (joinAttribute)? #ss | ||
| standAloneRelation joinTypeOrDefault groupedRelation (joinAttribute)? #sg | ||
| groupedRelation joinTypeOrDefault standAloneRelation (joinAttribute)? #gs | ||
| groupedRelation joinTypeOrDefault groupedRelation (joinAttribute)? #gg | ||
; | ||
|
||
|
||
cardinalityConstraint | ||
: CARD LPAREN joinedRelation NUMBER RPAREN | ||
| CARD LPAREN standAloneRelation NUMBER RPAREN | ||
; | ||
|
||
joinConstraint : JOIN LPAREN joinedRelation RPAREN; | ||
|
||
planConstraint | ||
: joinConstraint | ||
| cardinalityConstraint; | ||
|
||
planConstraintString : CONSTRAINTS_START_MARKER (planConstraint)* CONSTRAINTS_END_MARKER; | ||
|
||
|
||
``` | ||
|
||
|
||
### Examples of constraints | ||
aaneja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
1. Inner Join constraints - | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't we just use replicated or paritioned syntax? Imo the brackets and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate on what this would look like for the cited example ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like dremio or databricks. Current way also is fine but suggestion is to use explicit names for broadcast/partition etc since the goal of this is to allow users to explictly set the types There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not opposed to this; my only gripe with it is that it that the plan constraint string can get quite verbose, e.g for a 4-table join order - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this fine grained kind of control. I like the simplicity of just specifying the partitioning of the table, and every other example I found seems to take that approach.
You can specify to use syntactic join order if you want to control the join ordering in a more complex way. I find the existing syntax complex and hard to use. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DB2 supports 'join requests' similar to these join hints. It has variants that specify the join method too
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. I see the appeal of specifying a partial join order for auto generated queries, but also would like it to be easy to specify join hints for the common case where people just want to mark some table for broadcast or similar. I wonder if there's an alternative approach we can use to achieve both of these goals There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add Spark style independent hints -
These will be complementary to the join-order syntax. So the below join hints are equivalent but provide flexibility to the user -
Will think of more examples and incorporate them |
||
1. `join (a (b c))` - Join the relations a,b,c as a right deep tree (denoted by the brackets). Use regular rules for determining join distribution | ||
2. `join ((a c [R]) b [P])` - In addition to the join order, use a REPLICATED `[R]` join for sub-plan `(a c)` and PARTITIONED `[P]` for `(a c) b` | ||
3. If an inner join condition does not exist between nodes a CrossJoin is automatically inferred | ||
2. Cardinality constraints - | ||
aaneja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1. `card (c 10)` - Set the output row count estimate of `c` to `10` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rather not expose this kind of control. Better to specify what you want to have happen with this table, and otherwise leave it be. Seems pretty risky to encode cardinality estimates in the query text. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate on the risk ? I see this
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The risk I see is that if you are specifying a cardinality of x, you are probably doing it because you want the optimizer to do some particular thing about it. But you don't really know what the optimizer will do with that information, and it could do one thing for a while, and then in a new release there's an optimizer change and it does something else. Because you aren't directly controlling what happens when you specify the cardinality, it's hard to say how it might affect the query, and could be hard to debug if the performance degrades (vs. if you e.g. specify broadcast join and your data gets bigger, it's very clear what happens) It can also get out of sync with the data (there is always a risk with hand tuning that the optimization will no longer be relevan or will perform worse as the data changes, but specifying a specific cardinality estimate can have more varied and unknown effects). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that this is a powerful knob to give to the users, one that we can document as such (with caveats) To aid debugging, we can add Additional safeguards like warnings & metrics can be incorporated too if the stats estimate differ widely from actual runtime observed cardinality |
||
2. `card (c o 10)` - When/If considering a join node of shape `(c o)` set the output row count estimate to `10` | ||
3. Both type of constraints - | ||
`join (c o) card (c o 10)` - Force a join-sub-graph of nodes `c InnerJoin o`. For this join node, set the output row count estimate to `10` | ||
|
||
#### Full SQL examples of queries with constraints | ||
|
||
Force the join of `c` and `cte` which is otherwise ignored, see [19354](https://github.com/prestodb/presto/issues/19354) | ||
``` | ||
/*! join ((c cte) n) */ | ||
aaneja marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- | ||
with cte as ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will have an option for constraint within cte also I presume. I won't push on it, but it would be good to add that example as well. |
||
select min(orderkey) as min | ||
from orders | ||
) | ||
select count(*) | ||
from customer c, | ||
nation n, | ||
cte | ||
where c.custkey = cte.min | ||
and n.nationkey = c.nationkey | ||
``` | ||
|
||
Force the inner join of `l` and `o`, which is otherwise ignored, see [19894](https://github.com/prestodb/presto/issues/19894) | ||
``` | ||
/*! join (s (l o)) */ | ||
select 1 | ||
from supplier s, | ||
lineitem l, | ||
orders o | ||
where l.orderkey = o.orderkey | ||
``` | ||
|
||
|
||
### Other points of note | ||
- Relation names loosely resolve to WITH query aliases (CTE definitions), table names and aliases. A detailed description of the name resolution is out of scope of this RFC (this will be covered in the description of the implementation PR) |
Uh oh!
There was an error while loading. Please reload this page.