-
-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow using function and table function
- Loading branch information
1 parent
09a7e80
commit eca5bf7
Showing
8 changed files
with
154 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2024 Dolthub, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package dtablefunctions | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/dolthub/go-mysql-server/sql" | ||
) | ||
|
||
var _ sql.TableFunction = &TableFunction{} | ||
var _ sql.ExecSourceRel = &TableFunction{} | ||
|
||
type TableFunction struct { | ||
underlyingFunc sql.Function | ||
|
||
args []sql.Expression | ||
database sql.Database | ||
funcExpr sql.Expression | ||
} | ||
|
||
func NewTableFunction(f sql.Function) sql.TableFunction { | ||
return &TableFunction{ | ||
underlyingFunc: f, | ||
} | ||
} | ||
|
||
func (t *TableFunction) NewInstance(ctx *sql.Context, db sql.Database, args []sql.Expression) (sql.Node, error) { | ||
nt := *t | ||
nt.database = db | ||
nt.args = args | ||
f, err := nt.underlyingFunc.NewInstance(args) | ||
if err != nil { | ||
return nil, err | ||
} | ||
nt.funcExpr = f | ||
return &nt, nil | ||
} | ||
|
||
func (t *TableFunction) Children() []sql.Node { | ||
return nil | ||
} | ||
|
||
func (t *TableFunction) Database() sql.Database { | ||
return t.database | ||
} | ||
|
||
func (t *TableFunction) Expressions() []sql.Expression { | ||
return t.funcExpr.Children() | ||
} | ||
|
||
func (t *TableFunction) IsReadOnly() bool { | ||
return true | ||
} | ||
|
||
func (t *TableFunction) Name() string { | ||
return t.underlyingFunc.FunctionName() | ||
} | ||
|
||
func (t *TableFunction) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) { | ||
v, err := t.funcExpr.Eval(ctx, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return sql.RowsToRowIter(sql.Row{v}), nil | ||
} | ||
|
||
func (t *TableFunction) Resolved() bool { | ||
for _, expr := range t.args { | ||
return expr.Resolved() | ||
} | ||
return true | ||
} | ||
|
||
func (t *TableFunction) Schema() sql.Schema { | ||
return sql.Schema{&sql.Column{Name: t.underlyingFunc.FunctionName(), Type: t.funcExpr.Type()}} | ||
} | ||
|
||
func (t *TableFunction) String() string { | ||
var args []string | ||
for _, expr := range t.args { | ||
args = append(args, expr.String()) | ||
} | ||
return fmt.Sprintf("%s(%s)", t.underlyingFunc.FunctionName(), strings.Join(args, ", ")) | ||
} | ||
|
||
func (t *TableFunction) WithChildren(children ...sql.Node) (sql.Node, error) { | ||
if len(children) != 0 { | ||
return nil, fmt.Errorf("unexpected children") | ||
} | ||
return t, nil | ||
} | ||
|
||
func (t *TableFunction) WithDatabase(database sql.Database) (sql.Node, error) { | ||
nt := *t | ||
nt.database = database | ||
return &nt, nil | ||
} | ||
|
||
func (t *TableFunction) WithExpressions(exprs ...sql.Expression) (sql.Node, error) { | ||
l := len(t.funcExpr.Children()) | ||
if len(exprs) != l { | ||
return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), l) | ||
} | ||
nt := *t | ||
nf, err := nt.funcExpr.WithChildren(exprs...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
nt.funcExpr = nf | ||
return &nt, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters