Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
systay committed Feb 5, 2025
1 parent 6eada31 commit 1c01d73
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 14 deletions.
4 changes: 4 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,10 @@ func (node *Select) AddOrder(order *Order) {

// SetOrderBy sets the order by clause
func (node *Select) SetOrderBy(orderBy []*Order) {
if len(orderBy) == 0 {
node.OrderBy = nil
return
}
node.OrderBy = &OrderBy{Ordering: orderBy}
}

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operator_transformers.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ func buildUpdatePrimitive(
upd.OwnedVindexQuery.Where = stmt.Where
vQuery = sqlparser.String(upd.OwnedVindexQuery)
vindexes = upd.Target.VTable.ColumnVindexes
if upd.OwnedVindexQuery.Limit != nil && len(upd.OwnedVindexQuery.OrderBy) == 0 {
if upd.OwnedVindexQuery.Limit != nil && len(upd.OwnedVindexQuery.GetOrderBy()) == 0 {
return nil, vterrors.VT12001("Vindex update should have ORDER BY clause when using LIMIT")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (ab *aggBuilder) handleAggr(ctx *plancontext.PlanningContext, aggr Aggr) er
return ab.handlePushThroughAggregation(ctx, aggr)
case opcode.AggregateGroupConcat:
f := aggr.Func.(*sqlparser.GroupConcatExpr)
if f.Distinct || len(f.OrderBy) > 0 {
if f.Distinct || len(f.OrderBy.GetOrdering()) > 0 {
panic(vterrors.VT12001("cannot evaluate group concat with distinct or order by"))
}
// this needs special handling, currently aborting the push of function
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/ast_to_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,18 +459,18 @@ func createSelectionOp(
selectExprs []sqlparser.SelectExpr,
tableExprs sqlparser.TableExprs,
where *sqlparser.Where,
orderBy sqlparser.OrderBy,
orderBy []*sqlparser.Order,
limit *sqlparser.Limit,
lock sqlparser.Lock,
) Operator {
selectionStmt := &sqlparser.Select{
SelectExprs: &sqlparser.SelectExprs{Exprs: selectExprs},
From: tableExprs,
Where: where,
OrderBy: orderBy,
Limit: limit,
Lock: lock,
}
selectionStmt.SetOrderBy(orderBy)
// There are no foreign keys to check for a select query, so we can pass anything for verifyAllFKs and fkToIgnore.
return createOpFromStmt(ctx, selectionStmt, false /* verifyAllFKs */, "" /* fkToIgnore */)
}
7 changes: 5 additions & 2 deletions go/vt/vtgate/planbuilder/operators/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,13 @@ func makeColName(col sqlparser.IdentifierCI, table TargetTable, isMultiTbl bool)
return sqlparser.NewColName(col.String())
}

func addOrdering(ctx *plancontext.PlanningContext, op Operator, orderBy sqlparser.OrderBy) Operator {
func addOrdering(ctx *plancontext.PlanningContext, op Operator, orderBy *sqlparser.OrderBy) Operator {
if orderBy == nil {
return op
}
es := &expressionSet{}
var order []OrderBy
for _, ord := range orderBy {
for _, ord := range orderBy.Ordering {
if sqlparser.IsNull(ord.Expr) || !es.add(ctx, ord.Expr) {
// ORDER BY null, or expression repeated can safely be ignored
continue
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/operators/dml_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func shortDesc(target TargetTable, ovq *sqlparser.Select) string {
if ovq != nil {
var cols, orderby, limit string
cols = fmt.Sprintf("COLUMNS: [%s]", sqlparser.String(ovq.SelectExprs))
if len(ovq.OrderBy) > 0 {
if len(ovq.GetOrderBy()) > 0 {
orderby = fmt.Sprintf(" ORDERBY: [%s]", sqlparser.String(ovq.OrderBy))
}
if ovq.Limit != nil {
Expand Down
7 changes: 5 additions & 2 deletions go/vt/vtgate/planbuilder/operators/queryprojection.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,13 @@ func (es *expressionSet) add(ctx *plancontext.PlanningContext, e sqlparser.Expr)
return true
}

func (qp *QueryProjection) addOrderBy(ctx *plancontext.PlanningContext, orderBy sqlparser.OrderBy) {
func (qp *QueryProjection) addOrderBy(ctx *plancontext.PlanningContext, orderBy *sqlparser.OrderBy) {
if orderBy == nil {
return
}
canPushSorting := true
es := &expressionSet{}
for _, order := range orderBy {
for _, order := range orderBy.Ordering {
if canIgnoreOrdering(ctx, order.Expr) {
continue
}
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/operators/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func createUpdateOperator(ctx *plancontext.PlanningContext, updStmt *sqlparser.U
VerifyAll: ctx.VerifyAllFKs,
}

if len(updStmt.OrderBy) > 0 {
if len(updStmt.GetOrderBy()) > 0 {
updOp.Source = addOrdering(ctx, op, updStmt.OrderBy)
} else {
updOp.Source = op
Expand Down Expand Up @@ -497,7 +497,7 @@ func createFKCascadeOp(ctx *plancontext.PlanningContext, parentOp Operator, updS
fkChildren = append(fkChildren, fkChild)
}

selectionOp := createSelectionOp(ctx, selectExprs, updStmt.TableExprs, updStmt.Where, updStmt.OrderBy, nil, getUpdateLock(targetTbl.VTable))
selectionOp := createSelectionOp(ctx, selectExprs, updStmt.TableExprs, updStmt.Where, updStmt.GetOrderBy(), nil, getUpdateLock(targetTbl.VTable))

return &FkCascade{
Selection: selectionOp,
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestBindingSubquery(t *testing.T) {
query: "select (select col from tabl limit 1) as a from foo join tabl order by a + 1",
requiredTableSet: semantics.EmptyTableSet(),
extractor: func(sel *sqlparser.Select) sqlparser.Expr {
return sel.OrderBy[0].Expr
return sel.GetOrderBy()[0].Expr
},
rewrite: true,
}, {
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 @@ -242,7 +242,7 @@ func createSelectOperator(ctx *plancontext.PlanningContext, selStmt sqlparser.Se
}

func isOnlyDual(sel *sqlparser.Select) bool {
if sel.Where != nil || sel.GroupBy != nil || sel.Having != nil || sel.OrderBy != nil {
if sel.Where != nil || sel.GroupBy != nil || sel.Having != nil || len(sel.GetOrderBy()) > 0 {
// we can only deal with queries without any other subclauses - just SELECT and FROM, nothing else is allowed
return false
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/testdata/onecase.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[
{
"comment": "Add your test case here for debugging and run go test -run=One.",
"query": "",
"query": "select *, last_insert_id() from music where user_id = 1 union select * from user where id = 1",
"plan": {
}
}
Expand Down

0 comments on commit 1c01d73

Please sign in to comment.