Skip to content
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

Refactor: go/vt/vtgate/engine/opcode to reduce semantics package dependencies #12663

Merged
merged 3 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions go/vt/vtgate/engine/concatenate.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func formatTwoOptionsNicely(a, b string) string {
return a + "_" + b
}

// ErrWrongNumberOfColumnsInSelect is an error
var ErrWrongNumberOfColumnsInSelect = vterrors.NewErrorf(vtrpcpb.Code_FAILED_PRECONDITION, vterrors.WrongNumberOfColumnsInSelect, "The used SELECT statements have a different number of columns")
// errWrongNumberOfColumnsInSelect is an error
var errWrongNumberOfColumnsInSelect = vterrors.NewErrorf(vtrpcpb.Code_FAILED_PRECONDITION, vterrors.WrongNumberOfColumnsInSelect, "The used SELECT statements have a different number of columns")

// TryExecute performs a non-streaming exec.
func (c *Concatenate) TryExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
Expand All @@ -106,7 +106,7 @@ func (c *Concatenate) TryExecute(ctx context.Context, vcursor VCursor, bindVars
if len(rows) > 0 &&
len(r.Rows) > 0 &&
len(rows[0]) != len(r.Rows[0]) {
return nil, ErrWrongNumberOfColumnsInSelect
return nil, errWrongNumberOfColumnsInSelect
}

rows = append(rows, r.Rows...)
Expand Down Expand Up @@ -350,7 +350,7 @@ func (c *Concatenate) description() PrimitiveDescription {

func (c *Concatenate) compareFields(fields1 []*querypb.Field, fields2 []*querypb.Field) error {
if len(fields1) != len(fields2) {
return ErrWrongNumberOfColumnsInSelect
return errWrongNumberOfColumnsInSelect
}
for i, field1 := range fields1 {
if _, found := c.NoNeedToTypeCheck[i]; found {
Expand Down
113 changes: 113 additions & 0 deletions go/vt/vtgate/engine/opcode/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright 2023 The Vitess Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package opcode

import (
"fmt"

"vitess.io/vitess/go/sqltypes"
querypb "vitess.io/vitess/go/vt/proto/query"
)

// PulloutOpcode is a number representing the opcode
// for the PulloutSubquery primitive.
type PulloutOpcode int

// This is the list of PulloutOpcode values.
const (
PulloutValue = PulloutOpcode(iota)
PulloutIn
PulloutNotIn
PulloutExists
)

var pulloutName = map[PulloutOpcode]string{
PulloutValue: "PulloutValue",
PulloutIn: "PulloutIn",
PulloutNotIn: "PulloutNotIn",
PulloutExists: "PulloutExists",
}

func (code PulloutOpcode) String() string {
return pulloutName[code]
}

// MarshalJSON serializes the PulloutOpcode as a JSON string.
// It's used for testing and diagnostics.
func (code PulloutOpcode) MarshalJSON() ([]byte, error) {
return ([]byte)(fmt.Sprintf("\"%s\"", code.String())), nil
}

// AggregateOpcode is the aggregation Opcode.
type AggregateOpcode int

// These constants list the possible aggregate opcodes.
const (
AggregateUnassigned = AggregateOpcode(iota)
AggregateCount
AggregateSum
AggregateMin
AggregateMax
AggregateCountDistinct
AggregateSumDistinct
AggregateGtid
AggregateRandom
AggregateCountStar
)

var (
// OpcodeType keeps track of the known output types for different aggregate functions
OpcodeType = map[AggregateOpcode]querypb.Type{
AggregateCountDistinct: sqltypes.Int64,
AggregateCount: sqltypes.Int64,
AggregateCountStar: sqltypes.Int64,
AggregateSumDistinct: sqltypes.Decimal,
AggregateSum: sqltypes.Decimal,
AggregateGtid: sqltypes.VarChar,
}
)

// SupportedAggregates maps the list of supported aggregate
// functions to their opcodes.
var SupportedAggregates = map[string]AggregateOpcode{
"count": AggregateCount,
"sum": AggregateSum,
"min": AggregateMin,
"max": AggregateMax,
// These functions don't exist in mysql, but are used
// to display the plan.
"count_distinct": AggregateCountDistinct,
"sum_distinct": AggregateSumDistinct,
"vgtid": AggregateGtid,
"count_star": AggregateCountStar,
"random": AggregateRandom,
}

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

// MarshalJSON serializes the AggregateOpcode as a JSON string.
// It's used for testing and diagnostics.
func (code AggregateOpcode) MarshalJSON() ([]byte, error) {
return ([]byte)(fmt.Sprintf("\"%s\"", code.String())), nil
}
72 changes: 8 additions & 64 deletions go/vt/vtgate/engine/ordered_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,20 @@ import (
"vitess.io/vitess/go/vt/sqlparser"

"vitess.io/vitess/go/sqltypes"
. "vitess.io/vitess/go/vt/vtgate/engine/opcode"
"vitess.io/vitess/go/vt/vtgate/evalengine"

binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
querypb "vitess.io/vitess/go/vt/proto/query"
)

var (
// Some predefined values
countZero = sqltypes.MakeTrusted(sqltypes.Int64, []byte("0"))
countOne = sqltypes.MakeTrusted(sqltypes.Int64, []byte("1"))
sumZero = sqltypes.MakeTrusted(sqltypes.Decimal, []byte("0"))
)

var _ Primitive = (*OrderedAggregate)(nil)

// OrderedAggregate is a primitive that expects the underlying primitive
Expand Down Expand Up @@ -139,70 +147,6 @@ func (ap *AggregateParams) String() string {
return fmt.Sprintf("%s%s(%s)", ap.Opcode.String(), dispOrigOp, keyCol)
}

// AggregateOpcode is the aggregation Opcode.
type AggregateOpcode int

// These constants list the possible aggregate opcodes.
const (
AggregateUnassigned = AggregateOpcode(iota)
AggregateCount
AggregateSum
AggregateMin
AggregateMax
AggregateCountDistinct
AggregateSumDistinct
AggregateGtid
AggregateRandom
AggregateCountStar
)

var (
// OpcodeType keeps track of the known output types for different aggregate functions
OpcodeType = map[AggregateOpcode]querypb.Type{
AggregateCountDistinct: sqltypes.Int64,
AggregateCount: sqltypes.Int64,
AggregateCountStar: sqltypes.Int64,
AggregateSumDistinct: sqltypes.Decimal,
AggregateSum: sqltypes.Decimal,
AggregateGtid: sqltypes.VarChar,
}
// Some predefined values
countZero = sqltypes.MakeTrusted(sqltypes.Int64, []byte("0"))
countOne = sqltypes.MakeTrusted(sqltypes.Int64, []byte("1"))
sumZero = sqltypes.MakeTrusted(sqltypes.Decimal, []byte("0"))
)

// SupportedAggregates maps the list of supported aggregate
// functions to their opcodes.
var SupportedAggregates = map[string]AggregateOpcode{
"count": AggregateCount,
"sum": AggregateSum,
"min": AggregateMin,
"max": AggregateMax,
// These functions don't exist in mysql, but are used
// to display the plan.
"count_distinct": AggregateCountDistinct,
"sum_distinct": AggregateSumDistinct,
"vgtid": AggregateGtid,
"count_star": AggregateCountStar,
"random": AggregateRandom,
}

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

// MarshalJSON serializes the AggregateOpcode as a JSON string.
// It's used for testing and diagnostics.
func (code AggregateOpcode) MarshalJSON() ([]byte, error) {
return ([]byte)(fmt.Sprintf("\"%s\"", code.String())), nil
}

// RouteType returns a description of the query routing type used by the primitive
func (oa *OrderedAggregate) RouteType() string {
return oa.Input.RouteType()
Expand Down
1 change: 1 addition & 0 deletions go/vt/vtgate/engine/ordered_aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
binlogdatapb "vitess.io/vitess/go/vt/proto/binlogdata"
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/servenv"
. "vitess.io/vitess/go/vt/vtgate/engine/opcode"
)

var collationEnv *collations.Environment
Expand Down
31 changes: 1 addition & 30 deletions go/vt/vtgate/engine/pullout_subquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package engine

import (
"context"
"fmt"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/vterrors"
. "vitess.io/vitess/go/vt/vtgate/engine/opcode"

querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
Expand Down Expand Up @@ -189,32 +189,3 @@ func (ps *PulloutSubquery) description() PrimitiveDescription {
Other: other,
}
}

// PulloutOpcode is a number representing the opcode
// for the PulloutSubquery primitive.
type PulloutOpcode int

// This is the list of PulloutOpcode values.
const (
PulloutValue = PulloutOpcode(iota)
PulloutIn
PulloutNotIn
PulloutExists
)

var pulloutName = map[PulloutOpcode]string{
PulloutValue: "PulloutValue",
PulloutIn: "PulloutIn",
PulloutNotIn: "PulloutNotIn",
PulloutExists: "PulloutExists",
}

func (code PulloutOpcode) String() string {
return pulloutName[code]
}

// MarshalJSON serializes the PulloutOpcode as a JSON string.
// It's used for testing and diagnostics.
func (code PulloutOpcode) MarshalJSON() ([]byte, error) {
return ([]byte)(fmt.Sprintf("\"%s\"", code.String())), nil
}
2 changes: 1 addition & 1 deletion go/vt/vtgate/engine/pullout_subquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/sqltypes"

querypb "vitess.io/vitess/go/vt/proto/query"
. "vitess.io/vitess/go/vt/vtgate/engine/opcode"
)

func TestPulloutSubqueryValueGood(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions go/vt/vtgate/engine/scalar_aggregation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
querypb "vitess.io/vitess/go/vt/proto/query"
"vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/vterrors"
. "vitess.io/vitess/go/vt/vtgate/engine/opcode"
)

var _ Primitive = (*ScalarAggregate)(nil)
Expand Down
1 change: 1 addition & 0 deletions go/vt/vtgate/engine/scalar_aggregation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/require"

"vitess.io/vitess/go/sqltypes"
. "vitess.io/vitess/go/vt/vtgate/engine/opcode"
)

func TestEmptyRows(outer *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions go/vt/vtgate/planbuilder/aggregation_pushing.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
popcode "vitess.io/vitess/go/vt/vtgate/engine/opcode"
"vitess.io/vitess/go/vt/vtgate/planbuilder/operators"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"
)
Expand Down Expand Up @@ -231,7 +231,7 @@ func countStarAggr() *operators.Aggr {

return &operators.Aggr{
Original: &sqlparser.AliasedExpr{Expr: f},
OpCode: engine.AggregateCountStar,
OpCode: popcode.AggregateCountStar,
Alias: "count(*)",
}
}
Expand Down Expand Up @@ -420,17 +420,17 @@ func (hp *horizonPlanning) filteredPushAggregation(
return newplan, groupingOffsets, outputAggrs, pushed, nil
}

func isMinOrMax(in engine.AggregateOpcode) bool {
func isMinOrMax(in popcode.AggregateOpcode) bool {
switch in {
case engine.AggregateMin, engine.AggregateMax:
case popcode.AggregateMin, popcode.AggregateMax:
return true
default:
return false
}
}

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

func splitAggregationsToLeftAndRight(
Expand Down
13 changes: 6 additions & 7 deletions go/vt/vtgate/planbuilder/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
"bytes"
"fmt"

"vitess.io/vitess/go/vt/vterrors"

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vterrors"
popcode "vitess.io/vitess/go/vt/vtgate/engine/opcode"
systay marked this conversation as resolved.
Show resolved Hide resolved
"vitess.io/vitess/go/vt/vtgate/vindexes"
)

Expand Down Expand Up @@ -144,7 +143,7 @@ func (pb *primitiveBuilder) findOrigin(expr sqlparser.Expr, reservedVars *sqlpar
if !ok {
// (subquery) -> :_sq
expr = sqlparser.ReplaceExpr(expr, sqi.ast, sqlparser.NewArgument(sqName))
pullouts = append(pullouts, newPulloutSubquery(engine.PulloutValue, sqName, hasValues, sqi.plan))
pullouts = append(pullouts, newPulloutSubquery(popcode.PulloutValue, sqName, hasValues, sqi.plan))
continue
}
switch construct := construct.(type) {
Expand All @@ -166,7 +165,7 @@ func (pb *primitiveBuilder) findOrigin(expr sqlparser.Expr, reservedVars *sqlpar
Right: right,
}
expr = sqlparser.ReplaceExpr(expr, construct, newExpr)
pullouts = append(pullouts, newPulloutSubquery(engine.PulloutIn, sqName, hasValues, sqi.plan))
pullouts = append(pullouts, newPulloutSubquery(popcode.PulloutIn, sqName, hasValues, sqi.plan))
} else {
// a not in (subquery) -> (:__sq_has_values = 0 or (a not in ::__sq))
left := &sqlparser.ComparisonExpr{
Expand All @@ -184,12 +183,12 @@ func (pb *primitiveBuilder) findOrigin(expr sqlparser.Expr, reservedVars *sqlpar
Right: right,
}
expr = sqlparser.ReplaceExpr(expr, construct, newExpr)
pullouts = append(pullouts, newPulloutSubquery(engine.PulloutNotIn, sqName, hasValues, sqi.plan))
pullouts = append(pullouts, newPulloutSubquery(popcode.PulloutNotIn, sqName, hasValues, sqi.plan))
}
case *sqlparser.ExistsExpr:
// exists (subquery) -> :__sq_has_values
expr = sqlparser.ReplaceExpr(expr, construct, sqlparser.NewArgument(hasValues))
pullouts = append(pullouts, newPulloutSubquery(engine.PulloutExists, sqName, hasValues, sqi.plan))
pullouts = append(pullouts, newPulloutSubquery(popcode.PulloutExists, sqName, hasValues, sqi.plan))
}
}
return pullouts, highestOrigin, expr, nil
Expand Down
Loading