diff --git a/sql/planbuilder/from.go b/sql/planbuilder/from.go index cd931971eb..6bb6698410 100644 --- a/sql/planbuilder/from.go +++ b/sql/planbuilder/from.go @@ -514,6 +514,13 @@ func (b *Builder) buildTableFunc(inScope *scope, t *ast.TableFuncExpr) (outScope b.handleErr(err) } + if ctf, isCTF := newInstance.(sql.CatalogTableFunction); isCTF { + newInstance, err = ctf.WithCatalog(b.cat) + if err != nil { + b.handleErr(err) + } + } + // Table Function must always have an alias, pick function name as alias if none is provided var newAlias *plan.TableAlias if t.Alias.IsEmpty() { diff --git a/sql/tables.go b/sql/tables.go index 8bd36529b4..0d9f6f5a76 100644 --- a/sql/tables.go +++ b/sql/tables.go @@ -14,7 +14,9 @@ package sql -import "fmt" +import ( + "fmt" +) // Table is a SQL table. type Table interface { @@ -41,6 +43,14 @@ type TableFunction interface { NewInstance(ctx *Context, db Database, args []Expression) (Node, error) } +// CatalogTableFunction is a table function that can be used as a table factor in many SQL queries. +type CatalogTableFunction interface { + TableFunction + + // WithCatalog returns a new instance of the table function with the given catalog + WithCatalog(c Catalog) (TableFunction, error) +} + // TemporaryTable allows tables to declare that they are temporary (created by CREATE TEMPORARY TABLE). // Only used for validation of certain DDL operations -- in almost all respects TemporaryTables are indistinguishable // from persisted tables to the engine.