Skip to content

Commit

Permalink
add any_value as an accepted aggregation function
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 19, 2023
1 parent f2e5dbd commit ac02f1b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
22 changes: 17 additions & 5 deletions go/vt/vtgate/engine/opcode/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,27 @@ var SupportedAggregates = map[string]AggregateOpcode{
"vgtid": AggregateGtid,
"count_star": AggregateCountStar,
"random": AggregateRandom,
"any_value": AggregateRandom,
}

var AggregateName = map[AggregateOpcode]string{
AggregateCount: "count",
AggregateSum: "sum",
AggregateMin: "min",
AggregateMax: "max",
AggregateCountDistinct: "count_distinct",
AggregateSumDistinct: "sum_distinct",
AggregateGtid: "vgtid",
AggregateCountStar: "count_star",
AggregateRandom: "random",
}

func (code AggregateOpcode) String() string {
for k, v := range SupportedAggregates {
if v == code {
return k
}
name := AggregateName[code]
if name == "" {
name = "ERROR"
}
return "ERROR"
return name
}

// MarshalJSON serializes the AggregateOpcode as a JSON string.
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/aggregation_pushing.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func isMinOrMax(in popcode.AggregateOpcode) bool {
}
}

func isRandom(in popcode.AggregateOpcode) bool {
func isAnyValue(in popcode.AggregateOpcode) bool {
return in == popcode.AggregateRandom
}

Expand All @@ -444,7 +444,7 @@ func splitAggregationsToLeftAndRight(
deps := ctx.SemTable.RecursiveDeps(aggr.Original.Expr)
var other *operators.Aggr
// if we are sending down min/max/random, we don't have to multiply the results with anything
if !isMinOrMax(aggr.OpCode) && !isRandom(aggr.OpCode) {
if !isMinOrMax(aggr.OpCode) && !isAnyValue(aggr.OpCode) {
other = countStarAggr()
}
switch {
Expand Down
63 changes: 63 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -6734,5 +6734,68 @@
"user.user_extra"
]
}
},
{
"comment": "plan a query with any_value()",
"query": "select count(*), any_value(u.name), any_value(ue.title) from user u join user_extra ue on u.bar = ue.foo ",
"v3-plan": "VT12001: unsupported: cross-shard query with aggregates",
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select count(*), any_value(u.name), any_value(ue.title) from user u join user_extra ue on u.bar = ue.foo ",
"Instructions": {
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "sum_count_star(0) AS count(*), random(1) AS any_value(u.`name`), random(2) AS any_value(ue.title)",
"Inputs": [
{
"OperatorType": "Projection",
"Expressions": [
"[COLUMN 0] * [COLUMN 1] as count(*)",
"[COLUMN 2] as any_value(u.`name`)",
"[COLUMN 3] as any_value(ue.title)"
],
"Inputs": [
{
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "L:0,R:0,L:1,R:1",
"JoinVars": {
"u_bar": 2
},
"TableName": "`user`_user_extra",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*), any_value(u.`name`), u.bar from `user` as u where 1 != 1 group by u.bar",
"Query": "select count(*), any_value(u.`name`), u.bar from `user` as u group by u.bar",
"Table": "`user`"
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*), any_value(ue.title) from user_extra as ue where 1 != 1 group by .0",
"Query": "select count(*), any_value(ue.title) from user_extra as ue where ue.foo = :u_bar group by .0",
"Table": "user_extra"
}
]
}
]
}
]
},
"TablesUsed": [
"user.user",
"user.user_extra"
]
}
}
]

0 comments on commit ac02f1b

Please sign in to comment.