Skip to content

Commit

Permalink
add comment directive to fall back on the old horizon planner
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Jun 3, 2023
1 parent a07fd54 commit 9fca4f9
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 60 deletions.
12 changes: 12 additions & 0 deletions go/vt/sqlparser/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const (
// where 0 is the highest priority, and MaxPriorityValue is the lowest one.
DirectivePriority = "PRIORITY"

// DirectiveOldHorizonPlanner forces the planner to fall back on the v16 horizon planner
DirectiveOldHorizonPlanner = "OLD_HP"

// MaxPriorityValue specifies the maximum value allowed for the priority query directive. Valid priority values are
// between zero and MaxPriorityValue.
MaxPriorityValue = 100
Expand Down Expand Up @@ -449,3 +452,12 @@ func GetWorkloadNameFromStatement(statement Statement) string {

return workloadName
}

func UseOldHorizonPlanner(statement Statement) bool {
sel, ok := statement.(SelectStatement)
if !ok {
return false
}

return sel.GetParsedComments().Directives().IsSet(DirectiveOldHorizonPlanner)
}
4 changes: 4 additions & 0 deletions go/vt/vtgate/planbuilder/gen4_planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ func newBuildSelectPlan(
// record any warning as planner warning.
vschema.PlannerWarning(semTable.Warning)

if sqlparser.UseOldHorizonPlanner(selStmt) {
oldHorizonPlanner = true
}

ctx := plancontext.NewPlanningContext(reservedVars, semTable, vschema, version, oldHorizonPlanner)

if ks, _ := semTable.SingleUnshardedKeyspace(); ks != nil {
Expand Down
183 changes: 123 additions & 60 deletions go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,67 @@
]
}
},
{
"comment": "count(*) spread across join - use the old horizon planner",
"query": "select /*vt+ OLD_HP */ count(*) from user join user_extra on user.foo = user_extra.bar",
"v3-plan": "VT12001: unsupported: cross-shard query with aggregates",
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select /*vt+ OLD_HP */ count(*) from user join user_extra on user.foo = user_extra.bar",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "sum_count_star(0) AS count(*)",
"Inputs": [
{
"OperatorType": "Projection",
"Expressions": [
"[COLUMN 0] * COALESCE([COLUMN 1], INT64(1)) as count(*)"
],
"Inputs": [
{
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "L:1,R:1",
"JoinVars": {
"user_foo": 0
},
"TableName": "`user`_user_extra",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.foo, count(*), weight_string(`user`.foo) from `user` where 1 != 1 group by `user`.foo, weight_string(`user`.foo)",
"Query": "select /*vt+ OLD_HP */ `user`.foo, count(*), weight_string(`user`.foo) from `user` group by `user`.foo, weight_string(`user`.foo)",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 1, count(*) from user_extra where 1 != 1 group by 1",
"Query": "select /*vt+ OLD_HP */ 1, count(*) from user_extra where user_extra.bar = :user_foo group by 1",
"Table": "user_extra"
}
]
}
]
}
]
},
"TablesUsed": [
"user.user",
"user.user_extra"
]
}
},
{
"comment": "sum spread across join",
"query": "select sum(user.col) from user join user_extra on user.foo = user_extra.bar",
Expand Down Expand Up @@ -6160,67 +6221,69 @@
"user.user"
]
}
}, {
"comment": "scatter aggregate with ambiguous aliases",
"query": "select distinct a, b as a from user",
"v3-plan": "generating ORDER BY clause: VT03021: ambiguous column reference: a",
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select distinct a, b as a from user",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "(0|2), (1|3)",
"ResultColumns": 2,
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select a, b as a, weight_string(a), weight_string(b) from `user` where 1 != 1 group by a, b, weight_string(a), weight_string(b)",
"OrderBy": "(0|2) ASC, (1|3) ASC",
"Query": "select a, b as a, weight_string(a), weight_string(b) from `user` group by a, b, weight_string(a), weight_string(b) order by a asc, b asc",
"Table": "`user`"
}
},
{
"comment": "scatter aggregate with ambiguous aliases",
"query": "select distinct a, b as a from user",
"v3-plan": "generating ORDER BY clause: VT03021: ambiguous column reference: a",
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select distinct a, b as a from user",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "(0|2), (1|3)",
"ResultColumns": 2,
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select a, b as a, weight_string(a), weight_string(b) from `user` where 1 != 1 group by a, b, weight_string(a), weight_string(b)",
"OrderBy": "(0|2) ASC, (1|3) ASC",
"Query": "select a, b as a, weight_string(a), weight_string(b) from `user` group by a, b, weight_string(a), weight_string(b) order by a asc, b asc",
"Table": "`user`"
}
]
},
"TablesUsed": [
"user.user"
]
},
"TablesUsed": [
"user.user"
]
}
}, {
"comment": "scatter aggregate with complex select list (can't build order by)",
"query": "select distinct a+1 from user",
"v3-plan": "generating ORDER BY clause: VT12001: unsupported: reference a complex expression",
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select distinct a+1 from user",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "(0|1)",
"ResultColumns": 1,
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select a + 1, weight_string(a + 1) from `user` where 1 != 1 group by a + 1, weight_string(a + 1)",
"OrderBy": "(0|1) ASC",
"Query": "select a + 1, weight_string(a + 1) from `user` group by a + 1, weight_string(a + 1) order by a + 1 asc",
"Table": "`user`"
}
}
},
{
"comment": "scatter aggregate with complex select list (can't build order by)",
"query": "select distinct a+1 from user",
"v3-plan": "generating ORDER BY clause: VT12001: unsupported: reference a complex expression",
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select distinct a+1 from user",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Ordered",
"GroupBy": "(0|1)",
"ResultColumns": 1,
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select a + 1, weight_string(a + 1) from `user` where 1 != 1 group by a + 1, weight_string(a + 1)",
"OrderBy": "(0|1) ASC",
"Query": "select a + 1, weight_string(a + 1) from `user` group by a + 1, weight_string(a + 1) order by a + 1 asc",
"Table": "`user`"
}
]
},
"TablesUsed": [
"user.user"
]
},
"TablesUsed": [
"user.user"
]
}
}
}
]

0 comments on commit 9fca4f9

Please sign in to comment.