Skip to content

Commit

Permalink
convert straight_join to join and log as planner warning
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Oct 15, 2021
1 parent 2c3adcf commit 91917fa
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 6 deletions.
2 changes: 0 additions & 2 deletions go/vt/vtgate/planbuilder/abstract/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ func getOperatorFromTableExpr(tableExpr sqlparser.TableExpr, semTable *semantics
lhs, rhs = rhs, lhs
}
return &Join{LHS: lhs, RHS: rhs, LeftJoin: true, Predicate: tableExpr.Condition.On}, nil
case sqlparser.StraightJoinType:
return nil, semantics.Gen4NotSupportedF(tableExpr.Join.ToString())
default:
return nil, vterrors.Errorf(vtrpcpb.Code_UNIMPLEMENTED, "unsupported: %s", tableExpr.Join.ToString())
}
Expand Down
3 changes: 3 additions & 0 deletions go/vt/vtgate/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type ContextVSchema interface {
// that could become a problem if they move to a sharded keyspace
WarnUnshardedOnly(format string, params ...interface{})

// PlannerWarning records warning created during planning.
PlannerWarning(message string)

// ForeignKeyMode returns the foreign_key flag value
ForeignKeyMode() string
}
Expand Down
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 @@ -78,6 +78,10 @@ func gen4planSQLCalcFoundRows(vschema ContextVSchema, sel *sqlparser.Select, que
if err != nil {
return nil, err
}
if semTable.Warning != "" {
// log it as planning warning.
vschema.PlannerWarning(semTable.Warning)
}
plan, err := buildSQLCalcFoundRowsPlan(query, sel, reservedVars, vschema, planSelectGen4)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ type vschemaWrapper struct {
version PlannerVersion
}

func (vw *vschemaWrapper) PlannerWarning(_ string) {
}

func (vw *vschemaWrapper) ForeignKeyMode() string {
return "allow"
}
Expand Down
17 changes: 16 additions & 1 deletion go/vt/vtgate/planbuilder/testdata/from_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ Gen4 plan same as above
}
}

# Straight-join
# Straight-join (Gen4 ignores the straight_join hint)
"select m1.col from unsharded as m1 straight_join unsharded as m2"
{
"QueryType": "SELECT",
Expand All @@ -801,6 +801,21 @@ Gen4 plan same as above
"Table": "unsharded"
}
}
{
"QueryType": "SELECT",
"Original": "select m1.col from unsharded as m1 straight_join unsharded as m2",
"Instructions": {
"OperatorType": "Route",
"Variant": "SelectUnsharded",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select m1.col from unsharded as m1, unsharded as m2 where 1 != 1",
"Query": "select m1.col from unsharded as m1, unsharded as m2",
"Table": "unsharded"
}
}

# Three-way join
"select user.col from user join unsharded as m1 join unsharded as m2"
Expand Down
4 changes: 4 additions & 0 deletions go/vt/vtgate/semantics/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type analyzer struct {
inProjection int

projErr error
warning string
}

// newAnalyzer create the semantic analyzer
Expand Down Expand Up @@ -75,6 +76,7 @@ func Analyze(statement sqlparser.SelectStatement, currentDb string, si SchemaInf
semTable := analyzer.newSemTable(statement)

semTable.ProjectionErr = analyzer.projErr
semTable.Warning = analyzer.warning
return semTable, nil
}

Expand Down Expand Up @@ -126,6 +128,8 @@ func (a *analyzer) analyzeDown(cursor *sqlparser.Cursor) bool {
a.setError(err)
return true
}
// log any warn in rewriting.
a.warning = a.rewriter.warning

a.enterProjection(cursor)
// this is the visitor going down the tree. Returning false here would just not visit the children
Expand Down
10 changes: 8 additions & 2 deletions go/vt/vtgate/semantics/early_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
)

type earlyRewriter struct {
scoper *scoper
clause string
scoper *scoper
clause string
warning string
}

func (r *earlyRewriter) down(cursor *sqlparser.Cursor) error {
Expand Down Expand Up @@ -59,6 +60,11 @@ func (r *earlyRewriter) down(cursor *sqlparser.Cursor) error {
if changed {
cursor.ReplaceAndRevisit(selExprs)
}
case *sqlparser.JoinTableExpr:
if node.Join == sqlparser.StraightJoinType {
node.Join = sqlparser.NormalJoinType
r.warning = "straight join is converted to normal join"
}
case *sqlparser.Order:
r.clause = "order clause"
case sqlparser.GroupBy:
Expand Down
2 changes: 2 additions & 0 deletions go/vt/vtgate/semantics/semantic_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ type (
// ColumnEqualities is used to enable transitive closures
// if a == b and b == c then a == c
ColumnEqualities map[columnName][]sqlparser.Expr

Warning string
}

columnName struct {
Expand Down
14 changes: 13 additions & 1 deletion go/vt/vtgate/vcursor_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ func (vc *vcursorImpl) TargetString() string {
return vc.safeSession.TargetString
}

// MaxBufferingRetries is to represent max retries on buffering.
const MaxBufferingRetries = 3

func (vc *vcursorImpl) ExecutePrimitive(primitive engine.Primitive, bindVars map[string]*querypb.BindVariable, wantfields bool) (*sqltypes.Result, error) {
Expand Down Expand Up @@ -746,7 +747,18 @@ func (vc *vcursorImpl) WarnUnshardedOnly(format string, params ...interface{}) {
}
}

// ForeignKey implements the VCursor interface
// PlannerWarning implements the VCursor interface
func (vc *vcursorImpl) PlannerWarning(message string) {
if message == "" {
return
}
vc.warnings = append(vc.warnings, &querypb.QueryWarning{
Code: mysql.ERNotSupportedYet,
Message: message,
})
}

// ForeignKeyMode implements the VCursor interface
func (vc *vcursorImpl) ForeignKeyMode() string {
if foreignKeyMode == nil {
return ""
Expand Down

0 comments on commit 91917fa

Please sign in to comment.