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

test: add warnings to planner tests #16987

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions go/mysql/sqlerror/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ const (
ERWrongFKDef = ErrorCode(1239)
ERKeyRefDoNotMatchTableRef = ErrorCode(1240)
ERCyclicReference = ErrorCode(1245)
ERAutoConvert = ErrorCode(1246)
ERIllegalReference = ErrorCode(1247)
ERDerivedMustHaveAlias = ErrorCode(1248)
ERTableNameNotAllowedHere = ErrorCode(1250)
Expand Down
4 changes: 3 additions & 1 deletion go/test/vschemawrapper/vschema_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type VSchemaWrapper struct {
EnableViews bool
TestBuilder func(query string, vschema plancontext.VSchema, keyspace string) (*engine.Plan, error)
Env *vtenv.Environment
Warnings []*querypb.QueryWarning
}

func (vw *VSchemaWrapper) GetPrepareData(stmtName string) *vtgatepb.PrepareData {
Expand Down Expand Up @@ -132,7 +133,8 @@ func (vw *VSchemaWrapper) Environment() *vtenv.Environment {
return vw.Env
}

func (vw *VSchemaWrapper) PlannerWarning(_ string) {
func (vw *VSchemaWrapper) PlannerWarnings(warnings ...*querypb.QueryWarning) {
vw.Warnings = warnings
}

func (vw *VSchemaWrapper) ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error) {
Expand Down
20 changes: 16 additions & 4 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"vitess.io/vitess/go/slice"
querypb "vitess.io/vitess/go/vt/proto/query"

"github.com/nsf/jsondiff"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -661,10 +666,11 @@ func createFkDefinition(childCols []string, parentTableName string, parentCols [

type (
planTest struct {
Comment string `json:"comment,omitempty"`
Query string `json:"query,omitempty"`
Plan json.RawMessage `json:"plan,omitempty"`
Skip bool `json:"skip,omitempty"`
Comment string `json:"comment,omitempty"`
Query string `json:"query,omitempty"`
Plan json.RawMessage `json:"plan,omitempty"`
Skip bool `json:"skip,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
)

Expand All @@ -688,6 +694,9 @@ func (s *planTestSuite) testFile(filename string, vschema *vschemawrapper.VSchem
}
vschema.Version = Gen4
out := getPlanOutput(tcase, vschema, render)
current.Warnings = slice.Map(vschema.Warnings, func(q *querypb.QueryWarning) string {
return q.String()
})

// our expectation for the planner on the query is one of three
// - produces same plan as expected
Expand All @@ -710,6 +719,9 @@ func (s *planTestSuite) testFile(filename string, vschema *vschemawrapper.VSchem
} else if tcase.Skip {
t.Errorf("query is correct even though it is skipped:\n %s", tcase.Query)
}
if !tcase.Skip {
assert.Equal(t, tcase.Warnings, current.Warnings)
}
current.Plan = []byte(out)
})
expected = append(expected, current)
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/plancontext/planning_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func CreatePlanningContext(stmt sqlparser.Statement,
}

// record any warning as planner warning.
vschema.PlannerWarning(semTable.Warning)
vschema.PlannerWarnings(semTable.Warnings...)

return &PlanningContext{
ReservedVars: reservedVars,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ func (v *vschema) WarnUnshardedOnly(format string, params ...any) {
panic("implement me")
}

func (v *vschema) PlannerWarning(message string) {
// TODO implement me
func (v *vschema) PlannerWarnings(...*querypb.QueryWarning) {
panic("implement me")
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/plancontext/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type VSchema interface {
WarnUnshardedOnly(format string, params ...any)

// PlannerWarning records warning created during planning.
PlannerWarning(message string)
PlannerWarnings(warnings ...*querypb.QueryWarning)

// ForeignKeyMode returns the foreign_key flag value
ForeignKeyMode(keyspace string) (vschemapb.Keyspace_ForeignKeyMode, error)
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func gen4planSQLCalcFoundRows(vschema plancontext.VSchema, sel *sqlparser.Select
return nil, err
}
// record any warning as planner warning.
vschema.PlannerWarning(semTable.Warning)
vschema.PlannerWarnings(semTable.Warnings...)

plan, tablesUsed, err := buildSQLCalcFoundRowsPlan(query, sel, reservedVars, vschema)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion go/vt/vtgate/planbuilder/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"strconv"
"strings"

"vitess.io/vitess/go/mysql/sqlerror"
querypb "vitess.io/vitess/go/vt/proto/query"

"vitess.io/vitess/go/vt/key"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/sysvars"
Expand Down Expand Up @@ -92,7 +95,10 @@ func buildSetPlan(stmt *sqlparser.Set, vschema plancontext.VSchema) (*planResult
// This is to keep the backward compatibility.
// 'transaction_isolation' was added as a reserved connection system variable, so it used to change the setting at session level already.
// logging warning now to
vschema.PlannerWarning("converted 'next transaction' scope to 'session' scope")
vschema.PlannerWarnings(&querypb.QueryWarning{
Code: uint32(sqlerror.ERAutoConvert),
Message: "converted 'next transaction' scope to 'session' scope",
})
}
case sqlparser.VitessMetadataScope:
value, err := getValueFor(expr)
Expand Down
Loading
Loading