Skip to content

Commit

Permalink
join table functions (#1405)
Browse files Browse the repository at this point in the history
* join table functions

* fix todo
  • Loading branch information
max-hoffman authored Nov 15, 2022
1 parent 637d6f7 commit bff1004
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 8 deletions.
4 changes: 2 additions & 2 deletions enginetest/engine_only_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ func (s SimpleTableFunction) WithDatabase(_ sql.Database) (sql.Node, error) {
return s, nil
}

func (s SimpleTableFunction) FunctionName() string {
func (s SimpleTableFunction) Name() string {
return "simple_table_function"
}

Expand Down Expand Up @@ -758,7 +758,7 @@ type TestProvider struct {
func NewTestProvider(dbProvider *sql.MutableDatabaseProvider, tf sql.TableFunction) *TestProvider {
return &TestProvider{
*dbProvider,
map[string]sql.TableFunction{strings.ToLower(tf.FunctionName()): tf},
map[string]sql.TableFunction{strings.ToLower(tf.Name()): tf},
}
}

Expand Down
9 changes: 8 additions & 1 deletion sql/analyzer/coster.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func (c *coster) costRel(n relExpr) (float64, error) {
return c.costAntiJoin(n)
case *subqueryAlias:
return c.costSubqueryAlias(n)
case *tableFunc:
return c.costTableFunc(n)
case *fullOuterJoin:
return c.costFullOuterJoin(n)
case *concatJoin:
Expand Down Expand Up @@ -190,7 +192,12 @@ func (c *coster) costSemiJoin(n *semiJoin) (float64, error) {
return l, nil
}

func (c *coster) costSubqueryAlias(n *subqueryAlias) (float64, error) {
func (c *coster) costSubqueryAlias(_ *subqueryAlias) (float64, error) {
// TODO: if the whole plan was memo, we would have accurate costs for subqueries
return 10000, nil
}

func (c *coster) costTableFunc(_ *tableFunc) (float64, error) {
// TODO: sql.TableFunction should expose RowCount()
return 10, nil
}
4 changes: 4 additions & 0 deletions sql/analyzer/exec_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ func (b *ExecBuilder) buildSubqueryAlias(r *subqueryAlias, input sql.Schema, chi
return r.table, nil
}

func (b *ExecBuilder) buildTableFunc(r *tableFunc, input sql.Schema, children ...sql.Node) (sql.Node, error) {
return r.table, nil
}

func (b *ExecBuilder) buildRecursiveCte(r *recursiveCte, input sql.Schema, children ...sql.Node) (sql.Node, error) {
return r.table, nil
}
Expand Down
2 changes: 2 additions & 0 deletions sql/analyzer/join_order_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ func (j *joinOrderBuilder) buildJoinLeaf(n sql.Nameable) *exprGroup {
rel = &tableScan{relBase: b, table: n.ResolvedTable}
case *plan.ValueDerivedTable:
rel = &values{relBase: b, table: n}
case sql.TableFunction:
rel = &tableFunc{relBase: b, table: n}
default:
panic(fmt.Sprintf("unrecognized join leaf: %T", n))
}
Expand Down
32 changes: 32 additions & 0 deletions sql/analyzer/memo.og.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sql/analyzer/resolve_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func resolveTableFunctions(ctx *sql.Context, a *Analyzer, n sql.Node, _ *Scope,
return n, transform.SameTree, nil
}

tableFunction, err := a.Catalog.TableFunction(ctx, utf.FunctionName())
tableFunction, err := a.Catalog.TableFunction(ctx, utf.Name())
if err != nil {
return nil, transform.SameTree, err
}
Expand Down
3 changes: 1 addition & 2 deletions sql/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,10 @@ type TableFunction interface {
Node
Expressioner
Databaser
Nameable

// NewInstance returns a new instance of the table function
NewInstance(ctx *Context, db Database, expressions []Expression) (Node, error)
// FunctionName returns the name of this table function
FunctionName() string
}

// Table represents the backend of a SQL table.
Expand Down
4 changes: 2 additions & 2 deletions sql/expression/unresolved.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ func (utf *UnresolvedTableFunction) WithDatabase(database sql.Database) (sql.Nod
return utf, nil
}

// FunctionName implements the TableFunction interface
func (utf *UnresolvedTableFunction) FunctionName() string {
// Name implements the TableFunction interface
func (utf *UnresolvedTableFunction) Name() string {
return utf.name
}

Expand Down

0 comments on commit bff1004

Please sign in to comment.