Skip to content
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
4 changes: 4 additions & 0 deletions enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,10 @@ var QueryTests = []QueryTest{
// Query: "select y as z from xy group by (y) having AVG(z) > 0",
// Expected: []sql.Row{{1}, {2}, {3}},
//},
{
Query: "SELECT * FROM mytable t0 INNER JOIN mytable t1 ON (t1.i IN (((true)%(''))));",
Expected: []sql.Row{},
},
{
Query: "select x from xy where y in (select xy.x from xy join (select t2.y from xy t2 where exists (select t3.y from xy t3 where t3.y = xy.x)) t1) order by 1;",
Expected: []sql.Row{{0}, {1}, {2}, {3}},
Expand Down
20 changes: 11 additions & 9 deletions sql/analyzer/costed_index_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func getCostedIndexScan(ctx *sql.Context, statsProv sql.StatsProvider, rt sql.Ta
}

// flatten expression tree for costing
c := newIndexCoster(rt.Name())
c := newIndexCoster(ctx, rt.Name())
root, leftover, imprecise := c.flatten(expression.JoinAnd(filters...))
if root == nil {
return nil, nil, nil, err
Expand Down Expand Up @@ -337,16 +337,18 @@ func addIndexScans(m *memo.Memo) error {
})
}

func newIndexCoster(underlyingName string) *indexCoster {
func newIndexCoster(ctx *sql.Context, underlyingName string) *indexCoster {
return &indexCoster{
ctx: ctx,
i: 1,
idToExpr: make(map[indexScanId]sql.Expression),
underlyingName: underlyingName,
}
}

type indexCoster struct {
i indexScanId
ctx *sql.Context
i indexScanId
// idToExpr is a record of conj decomposition so we can remove duplicates later
idToExpr map[indexScanId]sql.Expression
// bestStat is the lowest cardinality indexScan option
Expand Down Expand Up @@ -558,7 +560,7 @@ func (c *indexCoster) flatten(e sql.Expression) (indexFilter, sql.Expression, sq

default:
c.idToExpr[c.i] = e
leaf, ok := newLeaf(c.i, e, c.underlyingName)
leaf, ok := newLeaf(c.ctx, c.i, e, c.underlyingName)
c.i++
if !ok {
return nil, e, sql.FastIntSet{}
Expand Down Expand Up @@ -599,7 +601,7 @@ func (c *indexCoster) flattenAnd(e *expression.And, and *iScanAnd) (sql.FastIntS
}
default:
c.idToExpr[c.i] = e
leaf, ok := newLeaf(c.i, e, c.underlyingName)
leaf, ok := newLeaf(c.ctx, c.i, e, c.underlyingName)
if !ok {
invalid.Add(int(c.i))
} else {
Expand Down Expand Up @@ -639,7 +641,7 @@ func (c *indexCoster) flattenOr(e *expression.Or, or *iScanOr) (bool, bool) {
imprecise = imprecise || imp
default:
c.idToExpr[c.i] = e
leaf, ok := newLeaf(c.i, e, c.underlyingName)
leaf, ok := newLeaf(c.ctx, c.i, e, c.underlyingName)
if !ok {
return false, false
} else {
Expand Down Expand Up @@ -1260,7 +1262,7 @@ func (o indexScanOp) swap() indexScanOp {
}
}

func newLeaf(id indexScanId, e sql.Expression, underlying string) (*iScanLeaf, bool) {
func newLeaf(ctx *sql.Context, id indexScanId, e sql.Expression, underlying string) (*iScanLeaf, bool) {
var op indexScanOp
var left sql.Expression
var right sql.Expression
Expand Down Expand Up @@ -1354,7 +1356,7 @@ func newLeaf(id indexScanId, e sql.Expression, underlying string) (*iScanLeaf, b
tup := right.(expression.Tuple)
var litSet []interface{}
for _, lit := range tup {
value, err := lit.Eval(nil, nil)
value, err := lit.Eval(ctx, nil)
if err != nil {
return nil, false
}
Expand All @@ -1363,7 +1365,7 @@ func newLeaf(id indexScanId, e sql.Expression, underlying string) (*iScanLeaf, b
return &iScanLeaf{id: id, gf: gf, op: op, setValues: litSet, underlying: underlying}, true
}

value, err := right.Eval(nil, nil)
value, err := right.Eval(ctx, nil)
if err != nil {
return nil, false
}
Expand Down
7 changes: 4 additions & 3 deletions sql/analyzer/costed_index_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ Or
},
}

ctx := sql.NewEmptyContext()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newIndexCoster("xyz")
c := newIndexCoster(ctx, "xyz")
root, leftover, _ := c.flatten(tt.in)
costTree := formatIndexFilter(root)
require.Equal(t, strings.TrimSpace(tt.exp), strings.TrimSpace(costTree), costTree)
Expand Down Expand Up @@ -569,7 +570,7 @@ func TestRangeBuilder(t *testing.T) {
for _, tt := range tests {
t.Run(fmt.Sprintf("Expr: %s\nRange: %s", tt.filter.String(), tt.exp.DebugString()), func(t *testing.T) {

c := newIndexCoster(testTable)
c := newIndexCoster(ctx, testTable)
root, _, _ := c.flatten(tt.filter)

var idx sql.Index
Expand Down Expand Up @@ -680,7 +681,7 @@ func TestRangeBuilderInclude(t *testing.T) {
//t.Skip("todo add tests and implement")

// TODO make index
c := newIndexCoster("xyz")
c := newIndexCoster(ctx, "xyz")
root, _, _ := c.flatten(tt.in)
b := newIndexScanRangeBuilder(ctx, dummy1, tt.include, sql.FastIntSet{}, c.idToExpr)
cmpRanges, err := b.buildRangeCollection(root)
Expand Down
2 changes: 1 addition & 1 deletion sql/analyzer/resolve_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func validateDropTables(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.S
return nil, transform.SameTree, sql.ErrDropTableNotSupported.New(t.Database().Name())
}
case *plan.UnresolvedTable:
if dt.IfExists() {
if dt.IfExists() && ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: mysql.ERBadTable,
Expand Down
12 changes: 7 additions & 5 deletions sql/expression/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ var (
)

func arithmeticWarning(ctx *sql.Context, errCode int, errMsg string) {
ctx.Session.Warn(&sql.Warning{
Level: "Warning",
Code: errCode,
Message: errMsg,
})
if ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Warning",
Code: errCode,
Message: errMsg,
})
}
}

// ArithmeticOp implements an arithmetic expression. Since we had separate expressions
Expand Down
4 changes: 2 additions & 2 deletions sql/plan/alter_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func (a *AlterEvent) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error)
}
if a.AlterStatus {
// TODO: support DISABLE ON SLAVE event status
if a.Status == sql.EventStatus_DisableOnSlave {
if a.Status == sql.EventStatus_DisableOnSlave && ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Warning",
Code: mysql.ERNotSupportedYet,
Expand Down Expand Up @@ -441,7 +441,7 @@ func (a *alterEventIter) Next(ctx *sql.Context) (sql.Row, error) {
if (a.event.HasExecuteAt || a.event.HasEnds) && eventEndingTime.Sub(a.event.LastAltered).Seconds() < 0 {
// If the event execution/end time is altered and in the past.
if a.alterSchedule {
if a.event.OnCompletionPreserve {
if a.event.OnCompletionPreserve && ctx != nil && ctx.Session != nil {
// If ON COMPLETION PRESERVE is defined, the event is disabled.
a.event.Status = sql.EventStatus_Disable.String()
ctx.Session.Warn(&sql.Warning{
Expand Down
6 changes: 3 additions & 3 deletions sql/plan/ddl_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (c *CreateEvent) WithEventScheduler(scheduler sql.EventScheduler) sql.Node
// of this CREATE EVENT statement.
func (c *CreateEvent) GetEventDefinition(ctx *sql.Context, eventCreationTime, lastAltered, lastExecuted time.Time, tz string) (sql.EventDefinition, error) {
// TODO: support DISABLE ON SLAVE event status
if c.Status == sql.EventStatus_DisableOnSlave {
if c.Status == sql.EventStatus_DisableOnSlave && ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Warning",
Code: mysql.ERNotSupportedYet,
Expand Down Expand Up @@ -394,7 +394,7 @@ func (c *createEventIter) Next(ctx *sql.Context) (sql.Row, error) {
if c.event.HasExecuteAt {
// If the event execution time is in the past and is set.
if c.event.ExecuteAt.Sub(c.event.CreatedAt).Seconds() <= -1 {
if c.event.OnCompletionPreserve {
if c.event.OnCompletionPreserve && ctx != nil && ctx.Session != nil {
// If ON COMPLETION PRESERVE is defined, the event is disabled.
c.event.Status = sql.EventStatus_Disable.String()
_, err = c.eventDb.UpdateEvent(ctx, c.event.Name, c.event)
Expand Down Expand Up @@ -634,7 +634,7 @@ func (d *DropEvent) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error)
}

err := eventDb.DropEvent(ctx, d.EventName)
if d.IfExists && sql.ErrEventDoesNotExist.Is(err) {
if d.IfExists && sql.ErrEventDoesNotExist.Is(err) && ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: 1305,
Expand Down
4 changes: 2 additions & 2 deletions sql/planbuilder/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (b *Builder) buildDropTable(inScope *scope, c *ast.DDL) (outScope *scope) {
tableName := strings.ToLower(t.Name.String())
if c.IfExists {
_, _, err := b.cat.Table(b.ctx, dbName, tableName)
if sql.ErrTableNotFound.Is(err) {
if sql.ErrTableNotFound.Is(err) && b.ctx != nil && b.ctx.Session != nil {
b.ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: mysql.ERBadTable,
Expand Down Expand Up @@ -1436,7 +1436,7 @@ func (b *Builder) buildDBDDL(inScope *scope, c *ast.DBDDL) (outScope *scope) {
} else if ccType == "collate" {
val := cc.Value
collationStr = &val
} else {
} else if b.ctx != nil && b.ctx.Session != nil {
b.ctx.Session.Warn(&sql.Warning{
Level: "Warning",
Code: mysql.ERNotSupportedYet,
Expand Down
4 changes: 2 additions & 2 deletions sql/rowexec/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (b *BaseBuilder) buildCreateDB(ctx *sql.Context, n *plan.CreateDB, row sql.
rows := []sql.Row{{types.OkResult{RowsAffected: 1}}}

if exists {
if n.IfNotExists {
if n.IfNotExists && ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: mysql.ERDbCreateExists,
Expand Down Expand Up @@ -641,7 +641,7 @@ func (b *BaseBuilder) buildDropProcedure(ctx *sql.Context, n *plan.DropProcedure
func (b *BaseBuilder) buildDropDB(ctx *sql.Context, n *plan.DropDB, row sql.Row) (sql.RowIter, error) {
exists := n.Catalog.HasDatabase(ctx, n.DbName)
if !exists {
if n.IfExists {
if n.IfExists && ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: mysql.ERDbDropExists,
Expand Down
12 changes: 7 additions & 5 deletions sql/rowexec/ddl_iters.go
Original file line number Diff line number Diff line change
Expand Up @@ -2012,11 +2012,13 @@ func (b *BaseBuilder) executeAlterIndex(ctx *sql.Context, n *plan.AlterIndex) er
case plan.IndexAction_Rename:
return indexable.RenameIndex(ctx, n.PreviousIndexName, n.IndexName)
case plan.IndexAction_DisableEnableKeys:
ctx.Session.Warn(&sql.Warning{
Level: "Warning",
Code: mysql.ERNotSupportedYet,
Message: "'disable/enable keys' feature is not supported yet",
})
if ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Warning",
Code: mysql.ERNotSupportedYet,
Message: "'disable/enable keys' feature is not supported yet",
})
}
return nil
default:
return plan.ErrIndexActionNotImplemented.New(n.Action)
Expand Down
24 changes: 14 additions & 10 deletions sql/rowexec/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,13 @@ func convertDataAndWarn(ctx *sql.Context, tableSchema sql.Schema, row sql.Row, c
sqlerr := sql.CastSQLError(err)

// Add a warning instead
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: sqlerr.Num,
Message: err.Error(),
})
if ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: sqlerr.Num,
Message: err.Error(),
})
}

return row
}
Expand All @@ -348,11 +350,13 @@ func warnOnIgnorableError(ctx *sql.Context, row sql.Row, err error) error {
sqlerr := sql.CastSQLError(err)

// Add a warning instead
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: sqlerr.Num,
Message: err.Error(),
})
if ctx != nil && ctx.Session != nil {
ctx.Session.Warn(&sql.Warning{
Level: "Note",
Code: sqlerr.Num,
Message: err.Error(),
})
}

// In this case the default value gets updated so return nil
if sql.ErrInsertIntoNonNullableDefaultNullColumn.Is(err) {
Expand Down