Skip to content

Commit

Permalink
last few uses of Left/Right
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Aug 28, 2024
1 parent cf7430e commit b0c5192
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 99 deletions.
5 changes: 5 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,11 @@ type (

// IndexType is the type of index in a DDL statement
IndexType int8

WhereAble interface {
AddWhere(e Expr)
GetWherePredicate() Expr
}
)

var _ OrderAndLimit = (*Select)(nil)
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtctl/workflow/materializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ func (mz *materializer) generateBinlogSources(ctx context.Context, targetShard *
Name: sqlparser.NewIdentifierCI("in_keyrange"),
Exprs: subExprs,
}
addFilter(sel, inKeyRange)
sel.AddWhere(inKeyRange)
}
if tenantClause != nil {
addFilter(sel, *tenantClause)
sel.AddWhere(*tenantClause)
}
rule.Filter = sqlparser.String(sel)
bls.Filter.Rules = append(bls.Filter.Rules, rule)
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtctl/workflow/traffic_switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ func (ts *trafficSwitcher) addTenantFilter(ctx context.Context, filter string) (
if !ok {
return "", fmt.Errorf("unrecognized statement: %s", filter)
}
addFilter(sel, *tenantClause)
sel.AddWhere(*tenantClause)
filter = sqlparser.String(sel)
return filter, nil
}
Expand Down
17 changes: 0 additions & 17 deletions go/vt/vtctl/workflow/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,23 +793,6 @@ func LegacyBuildTargets(ctx context.Context, ts *topo.Server, tmc tmclient.Table
}, nil
}

func addFilter(sel *sqlparser.Select, filter sqlparser.Expr) {
if sel.Where != nil {
sel.Where = &sqlparser.Where{
Type: sqlparser.WhereClause,
Expr: &sqlparser.AndExpr{
Left: filter,
Right: sel.Where.Expr,
},
}
} else {
sel.Where = &sqlparser.Where{
Type: sqlparser.WhereClause,
Expr: filter,
}
}
}

func getTenantClause(vrOptions *vtctldatapb.WorkflowOptions,
targetVSchema *vindexes.KeyspaceSchema, parser *sqlparser.Parser) (*sqlparser.Expr, error) {
if vrOptions.TenantId == "" {
Expand Down
15 changes: 3 additions & 12 deletions go/vt/vtctl/workflow/vexec/query_planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,7 @@ func (planner *VReplicationLogQueryPlanner) planSelect(sel *sqlparser.Select) (Q
case nil:
targetWhere.Expr = expr
default:
targetWhere.Expr = &sqlparser.AndExpr{
Left: expr,
Right: where.Expr,
}
targetWhere.Expr = sqlparser.CreateAndExpr(expr, where.Expr)
}

sel.Where = targetWhere
Expand Down Expand Up @@ -408,10 +405,7 @@ func addDefaultWheres(planner QueryPlanner, where *sqlparser.Where) *sqlparser.W
Expr: expr,
}
default:
newWhere.Expr = &sqlparser.AndExpr{
Left: newWhere.Expr,
Right: expr,
}
newWhere.Expr = sqlparser.CreateAndExpr(newWhere.Expr, expr)
}
}

Expand All @@ -424,10 +418,7 @@ func addDefaultWheres(planner QueryPlanner, where *sqlparser.Where) *sqlparser.W
Right: sqlparser.NewStrLiteral(params.Workflow),
}

newWhere.Expr = &sqlparser.AndExpr{
Left: newWhere.Expr,
Right: expr,
}
newWhere.Expr = sqlparser.CreateAndExpr(newWhere.Expr, expr)
}

return newWhere
Expand Down
17 changes: 1 addition & 16 deletions go/vt/vttablet/tabletserver/vstreamer/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func (plan *Plan) analyzeWhere(vschema *localVSchema, where *sqlparser.Where) er
if where == nil {
return nil
}
exprs := splitAndExpression(nil, where.Expr)
exprs := sqlparser.SplitAndExpression(nil, where.Expr)
for _, expr := range exprs {
switch expr := expr.(type) {
case *sqlparser.ComparisonExpr:
Expand Down Expand Up @@ -595,21 +595,6 @@ func (plan *Plan) analyzeWhere(vschema *localVSchema, where *sqlparser.Where) er
return nil
}

// splitAndExpression breaks up the Expr into AND-separated conditions
// and appends them to filters, which can be shuffled and recombined
// as needed.
func splitAndExpression(filters []sqlparser.Expr, node sqlparser.Expr) []sqlparser.Expr {
if node == nil {
return filters
}
switch node := node.(type) {
case *sqlparser.AndExpr:
filters = splitAndExpression(filters, node.Left)
return splitAndExpression(filters, node.Right)
}
return append(filters, node)
}

func (plan *Plan) analyzeExprs(vschema *localVSchema, selExprs sqlparser.SelectExprs) error {
if _, ok := selExprs[0].(*sqlparser.StarExpr); !ok {
for _, expr := range selExprs {
Expand Down
16 changes: 1 addition & 15 deletions go/vt/wrangler/materializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,21 +1405,7 @@ func (mz *materializer) generateInserts(ctx context.Context, sourceShards []*top
Name: sqlparser.NewIdentifierCI("in_keyrange"),
Exprs: subExprs,
}
if sel.Where != nil {
sel.Where = &sqlparser.Where{
Type: sqlparser.WhereClause,
Expr: &sqlparser.AndExpr{
Left: inKeyRange,
Right: sel.Where.Expr,
},
}
} else {
sel.Where = &sqlparser.Where{
Type: sqlparser.WhereClause,
Expr: inKeyRange,
}
}

sel.AddWhere(inKeyRange)
filter = sqlparser.String(sel)
}

Expand Down
15 changes: 6 additions & 9 deletions go/vt/wrangler/vdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1439,16 +1439,13 @@ func removeKeyrange(where *sqlparser.Where) *sqlparser.Where {
func removeExprKeyrange(node sqlparser.Expr) sqlparser.Expr {
switch node := node.(type) {
case *sqlparser.AndExpr:
if isFuncKeyrange(node.Left) {
return removeExprKeyrange(node.Right)
}
if isFuncKeyrange(node.Right) {
return removeExprKeyrange(node.Left)
}
return &sqlparser.AndExpr{
Left: removeExprKeyrange(node.Left),
Right: removeExprKeyrange(node.Right),
var keep sqlparser.Exprs
for _, p := range node.Predicates {
if !isFuncKeyrange(p) {
keep = append(keep, removeExprKeyrange(p))
}
}
return sqlparser.CreateAndExpr(keep...)
}
return node
}
Expand Down
39 changes: 12 additions & 27 deletions go/vt/wrangler/vexec_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,20 @@ func (vx *vexec) buildPlan(ctx context.Context) (plan *vexecPlan, err error) {
case *sqlparser.Insert:
plan, err = vx.buildInsertPlan(ctx, vx.planner, stmt)
case *sqlparser.Select:
plan, err = vx.buildSelectPlan(ctx, vx.planner, stmt)
plan, err = vx.buildSelectPlan(vx.planner, stmt)
default:
return nil, fmt.Errorf("query not supported by vexec: %s", sqlparser.String(stmt))
}
return plan, err
}

// analyzeWhereEqualsColumns identifies column names in a WHERE clause that have a comparison expression
func (vx *vexec) analyzeWhereEqualsColumns(where *sqlparser.Where) []string {
func (vx *vexec) analyzeWhereEqualsColumns(expr sqlparser.Expr) []string {
var cols []string
if where == nil {
if expr == nil {
return cols
}
exprs := sqlparser.SplitAndExpression(nil, where.Expr)
exprs := sqlparser.SplitAndExpression(nil, expr)
for _, expr := range exprs {
switch expr := expr.(type) {
case *sqlparser.ComparisonExpr:
Expand All @@ -185,8 +185,8 @@ func (vx *vexec) analyzeWhereEqualsColumns(where *sqlparser.Where) []string {
}

// addDefaultWheres modifies the query to add, if appropriate, the workflow and DB-name column modifiers
func (vx *vexec) addDefaultWheres(planner vexecPlanner, where *sqlparser.Where) *sqlparser.Where {
cols := vx.analyzeWhereEqualsColumns(where)
func (vx *vexec) addDefaultWheres(planner vexecPlanner, stmt sqlparser.WhereAble) {
cols := vx.analyzeWhereEqualsColumns(stmt.GetWherePredicate())
var hasDBName, hasWorkflow bool
plannerParams := planner.params()
for _, col := range cols {
Expand All @@ -196,37 +196,22 @@ func (vx *vexec) addDefaultWheres(planner vexecPlanner, where *sqlparser.Where)
hasWorkflow = true
}
}
newWhere := where
if !hasDBName {
expr := &sqlparser.ComparisonExpr{
Left: &sqlparser.ColName{Name: sqlparser.NewIdentifierCI(plannerParams.dbNameColumn)},
Operator: sqlparser.EqualOp,
Right: sqlparser.NewStrLiteral(vx.primaries[0].DbName()),
}
if newWhere == nil {
newWhere = &sqlparser.Where{
Type: sqlparser.WhereClause,
Expr: expr,
}
} else {
newWhere.Expr = &sqlparser.AndExpr{
Left: newWhere.Expr,
Right: expr,
}
}
stmt.AddWhere(expr)
}
if !hasWorkflow && vx.workflow != "" {
expr := &sqlparser.ComparisonExpr{
Left: &sqlparser.ColName{Name: sqlparser.NewIdentifierCI(plannerParams.workflowColumn)},
Operator: sqlparser.EqualOp,
Right: sqlparser.NewStrLiteral(vx.workflow),
}
newWhere.Expr = &sqlparser.AndExpr{
Left: newWhere.Expr,
Right: expr,
}
stmt.AddWhere(expr)
}
return newWhere
}

// buildUpdatePlan builds a plan for an UPDATE query
Expand Down Expand Up @@ -262,7 +247,7 @@ func (vx *vexec) buildUpdatePlan(ctx context.Context, planner vexecPlanner, upd
return nil, fmt.Errorf("Query must match one of these templates: %s", strings.Join(templates, "; "))
}
}
upd.Where = vx.addDefaultWheres(planner, upd.Where)
vx.addDefaultWheres(planner, upd)

buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("%v", upd)
Expand All @@ -285,7 +270,7 @@ func (vx *vexec) buildDeletePlan(ctx context.Context, planner vexecPlanner, del
return nil, fmt.Errorf("unsupported construct: %v", sqlparser.String(del))
}

del.Where = vx.addDefaultWheres(planner, del.Where)
vx.addDefaultWheres(planner, del)

buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("%v", del)
Expand Down Expand Up @@ -325,8 +310,8 @@ func (vx *vexec) buildInsertPlan(ctx context.Context, planner vexecPlanner, ins
}

// buildSelectPlan builds a plan for a SELECT query
func (vx *vexec) buildSelectPlan(ctx context.Context, planner vexecPlanner, sel *sqlparser.Select) (*vexecPlan, error) {
sel.Where = vx.addDefaultWheres(planner, sel.Where)
func (vx *vexec) buildSelectPlan(planner vexecPlanner, sel *sqlparser.Select) (*vexecPlan, error) {
vx.addDefaultWheres(planner, sel)
buf := sqlparser.NewTrackedBuffer(nil)
buf.Myprintf("%v", sel)

Expand Down

0 comments on commit b0c5192

Please sign in to comment.