From 96debea33e3b3b462be50b1630debdbe073a49da Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Mon, 6 Nov 2023 14:29:18 +0100 Subject: [PATCH] wip --- go/vt/vtgate/engine/opcode/constants.go | 6 ++++- go/vt/vtgate/planbuilder/operators/phases.go | 24 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/go/vt/vtgate/engine/opcode/constants.go b/go/vt/vtgate/engine/opcode/constants.go index 07a39020f8b..af0959eef53 100644 --- a/go/vt/vtgate/engine/opcode/constants.go +++ b/go/vt/vtgate/engine/opcode/constants.go @@ -74,6 +74,7 @@ const ( AggregateAnyValue AggregateCountStar AggregateGroupConcat + AggregateAvg _NumOfOpCodes // This line must be last of the opcodes! ) @@ -85,6 +86,7 @@ var ( AggregateCountStar: sqltypes.Int64, AggregateSumDistinct: sqltypes.Decimal, AggregateSum: sqltypes.Decimal, + AggregateAvg: sqltypes.Decimal, AggregateGtid: sqltypes.VarChar, } ) @@ -96,6 +98,7 @@ var SupportedAggregates = map[string]AggregateOpcode{ "sum": AggregateSum, "min": AggregateMin, "max": AggregateMax, + "avg": AggregateAvg, // These functions don't exist in mysql, but are used // to display the plan. "count_distinct": AggregateCountDistinct, @@ -117,6 +120,7 @@ var AggregateName = map[AggregateOpcode]string{ AggregateCountStar: "count_star", AggregateGroupConcat: "group_concat", AggregateAnyValue: "any_value", + AggregateAvg: "avg", } func (code AggregateOpcode) String() string { @@ -145,7 +149,7 @@ func (code AggregateOpcode) Type(typ querypb.Type) querypb.Type { return sqltypes.Text case AggregateMax, AggregateMin, AggregateAnyValue: return typ - case AggregateSumDistinct, AggregateSum: + case AggregateSumDistinct, AggregateSum, AggregateAvg: if sqltypes.IsIntegral(typ) || sqltypes.IsDecimal(typ) { return sqltypes.Decimal } diff --git a/go/vt/vtgate/planbuilder/operators/phases.go b/go/vt/vtgate/planbuilder/operators/phases.go index 80f5ce71512..a09e09f973b 100644 --- a/go/vt/vtgate/planbuilder/operators/phases.go +++ b/go/vt/vtgate/planbuilder/operators/phases.go @@ -19,6 +19,7 @@ package operators import ( "vitess.io/vitess/go/slice" "vitess.io/vitess/go/vt/sqlparser" + "vitess.io/vitess/go/vt/vtgate/engine/opcode" "vitess.io/vitess/go/vt/vtgate/planbuilder/operators/ops" "vitess.io/vitess/go/vt/vtgate/planbuilder/operators/rewrite" "vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext" @@ -140,12 +141,35 @@ func addOrderingForAllAggregations(ctx *plancontext.PlanningContext, root ops.Op res = rewrite.NewTree("added ordering before aggregation", in) } + if needAvgBreaking(aggrOp.Aggregations) { + return splitAvgAggregations() + } + + for _, aggr := range aggrOp.Aggregations { + if aggr.OpCode == opcode.AggregateAvg { + + } + } + return in, res, nil } return rewrite.BottomUp(root, TableID, visitor, stopAtRoute) } +func needAvgBreaking(aggrs []Aggr) bool { + for _, aggr := range aggrs { + if aggr.OpCode == opcode.AggregateAvg { + return true + } + } + return false +} + +func splitAvgAggregations() { + +} + func addOrderingFor(aggrOp *Aggregator) { orderBys := slice.Map(aggrOp.Grouping, func(from GroupBy) ops.OrderBy { return from.AsOrderBy()