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

Minimal index searchable interface #2125

Merged
merged 2 commits into from
Nov 16, 2023
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
16 changes: 14 additions & 2 deletions sql/analyzer/costed_index_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,20 @@ func costedIndexScans(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sco
return n, transform.SameTree, nil
}

//var statistics []sql.Statistic
//var err error
if is, ok := rt.UnderlyingTable().(sql.IndexSearchableTable); ok && is.SkipIndexCosting() {
lookup, err := is.LookupForExpressions(ctx, expression.SplitConjunction(filter.Expression))
if err != nil {
return n, transform.SameTree, err
}
if lookup.IsEmpty() {
return n, transform.SameTree, nil
}
ret, err := plan.NewStaticIndexedAccessForTableNode(rt, lookup)
if err != nil {
return n, transform.SameTree, err
}
return plan.NewFilter(filter.Expression, ret), transform.NewTree, nil
}
statistics, err := a.Catalog.StatsProvider.GetTableStats(ctx, strings.ToLower(rt.Database().Name()), strings.ToLower(rt.Name()))
if err != nil {
return n, transform.SameTree, err
Expand Down
97 changes: 97 additions & 0 deletions sql/analyzer/costed_index_scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/transform"
"github.com/dolthub/go-mysql-server/sql/types"
)
Expand Down Expand Up @@ -831,3 +832,99 @@ func and2(expressions ...sql.Expression) sql.Expression {
}
return expression.NewAnd(expressions[0], and2(expressions[1:]...))
}

func TestIndexSearchable(t *testing.T) {
// we want to run costed index scan rule with the indexSearchableTable as input
input := plan.NewFilter(
expression.NewEquals(
expression.NewGetFieldWithTable(0, types.Int64, "mydb", "xy", "x", false),
expression.NewLiteral(1, types.Int64),
),
plan.NewResolvedTable(&indexSearchableTable{underlying: plan.NewDualSqlTable()}, nil, nil),
)

exp := `
Filter
├─ (xy.x = 1)
└─ IndexedTableAccess()
├─ index: [xy.x]
└─ filters: [{[1, 1]}]
`
res, same, err := costedIndexScans(nil, nil, input, nil, nil)
require.NoError(t, err)
require.False(t, bool(same))
require.Equal(t, strings.TrimSpace(exp), strings.TrimSpace(res.String()), "expected:\n%s,\nfound:\n%s\n", exp, res.String())
}

var xIdx = &dummyIdx{id: "primary", database: "mydb", table: "xy", expr: []sql.Expression{expression.NewGetFieldWithTable(0, types.Int64, "mydb", "xy", "x", false)}}

type indexSearchableTable struct {
underlying sql.Table
}

var _ sql.IndexSearchableTable = (*indexSearchableTable)(nil)
var _ sql.IndexAddressable = (*indexSearchableTable)(nil)
var _ sql.IndexedTable = (*indexSearchableTable)(nil)

func (i *indexSearchableTable) Name() string {
return i.underlying.Name()
}

func (i *indexSearchableTable) String() string {
return i.underlying.String()
}

func (i *indexSearchableTable) Schema() sql.Schema {
return i.underlying.Schema()
}

func (i *indexSearchableTable) Collation() sql.CollationID {
return i.underlying.Collation()
}

func (i *indexSearchableTable) Partitions(context *sql.Context) (sql.PartitionIter, error) {
//TODO implement me
panic("implement me")
}

func (i *indexSearchableTable) PartitionRows(context *sql.Context, partition sql.Partition) (sql.RowIter, error) {
//TODO implement me
panic("implement me")
}

func (i *indexSearchableTable) SkipIndexCosting() bool {
return true
}

func (i *indexSearchableTable) IndexWithPrefix(ctx *sql.Context, expressions []string) (sql.Index, error) {
//TODO implement me
panic("implement me")
}

func (i *indexSearchableTable) LookupForExpressions(ctx *sql.Context, exprs []sql.Expression) (sql.IndexLookup, error) {
if eq, ok := exprs[0].(*expression.Equals); ok {
if gf, ok := eq.Left().(*expression.GetField); ok && strings.EqualFold(gf.Name(), "x") {
if lit, ok := eq.Right().(*expression.Literal); ok {
ranges := sql.RangeCollection{{sql.ClosedRangeColumnExpr(lit.Value(), lit.Value(), lit.Type())}}
return sql.IndexLookup{Index: xIdx, Ranges: ranges}, nil
}
}
}
return sql.IndexLookup{}, nil
}

func (i *indexSearchableTable) IndexedAccess(lookup sql.IndexLookup) sql.IndexedTable {
return i
}

func (i *indexSearchableTable) GetIndexes(ctx *sql.Context) ([]sql.Index, error) {
return nil, nil
}

func (i *indexSearchableTable) PreciseMatch() bool {
return false
}
func (i *indexSearchableTable) LookupPartitions(context *sql.Context, lookup sql.IndexLookup) (sql.PartitionIter, error) {
//TODO implement me
panic("implement me")
}
2 changes: 1 addition & 1 deletion sql/analyzer/index_analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ type dummyIdx struct {
var _ sql.Index = (*dummyIdx)(nil)

func (i dummyIdx) CanSupport(r ...sql.Range) bool {
return false
return true
}

func (i dummyIdx) Expressions() []string {
Expand Down
17 changes: 17 additions & 0 deletions sql/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,20 @@ type MutableTableNode interface {
// WrappedTable returns the Table this node wraps, without unwinding any additional layers of wrapped tables.
WrappedTable() Table
}

// IndexSearchable lets a node use custom logic to create
// *plan.IndexedTableAccess
type IndexSearchable interface {
// SkipIndexCosting defers to an integrator for provide a suitable
// index lookup.
SkipIndexCosting() bool
// LookupForExpressions returns an sql.IndexLookup for an expression
// set.
LookupForExpressions(*Context, []Expression) (IndexLookup, error)
}

// IndexSearchableTable is a Table supports custom index generation.
type IndexSearchableTable interface {
IndexAddressableTable
IndexSearchable
}