|
| 1 | +// Copyright 2026 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package node |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/dolthub/go-mysql-server/sql" |
| 21 | + vitess "github.com/dolthub/vitess/go/vt/sqlparser" |
| 22 | +) |
| 23 | + |
| 24 | +// Return represents the statement RETURN statement. |
| 25 | +type Return struct { |
| 26 | + Expr sql.Expression |
| 27 | + exprStmt string |
| 28 | +} |
| 29 | + |
| 30 | +var _ sql.ExecSourceRel = (*Return)(nil) |
| 31 | +var _ vitess.Injectable = (*Return)(nil) |
| 32 | + |
| 33 | +// NewReturn creates a new *Return node. |
| 34 | +func NewReturn(exprStmt string) *Return { |
| 35 | + return &Return{ |
| 36 | + Expr: nil, |
| 37 | + exprStmt: exprStmt, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// Children implements the interface sql.ExecSourceRel. |
| 42 | +func (r *Return) Children() []sql.Node { |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// IsReadOnly implements the interface sql.ExecSourceRel. |
| 47 | +func (r *Return) IsReadOnly() bool { |
| 48 | + return true |
| 49 | +} |
| 50 | + |
| 51 | +// Resolved implements the interface sql.ExecSourceRel. |
| 52 | +func (r *Return) Resolved() bool { |
| 53 | + if r.Expr == nil { |
| 54 | + return false |
| 55 | + } |
| 56 | + return !r.Expr.Resolved() |
| 57 | +} |
| 58 | + |
| 59 | +// RowIter implements the interface sql.ExecSourceRel. |
| 60 | +func (r *Return) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) { |
| 61 | + // TODO: this cannot be called as we replace RETURN with SELECT to be able to parse the expression |
| 62 | + //val, err := r.Expr.Eval(ctx, row) |
| 63 | + //if err != nil { |
| 64 | + // return nil, err |
| 65 | + //} |
| 66 | + return sql.RowsToRowIter(), nil |
| 67 | +} |
| 68 | + |
| 69 | +// String implements the interface sql.ExecSourceRel. |
| 70 | +func (r *Return) String() string { |
| 71 | + if r.Expr == nil { |
| 72 | + return fmt.Sprintf("RETURN %s", r.exprStmt) |
| 73 | + } |
| 74 | + return fmt.Sprintf("RETURN %s", r.Expr.String()) |
| 75 | +} |
| 76 | + |
| 77 | +// Schema implements the interface sql.ExecSourceRel. |
| 78 | +func (r *Return) Schema() sql.Schema { |
| 79 | + return sql.Schema{ |
| 80 | + {Name: r.Expr.String(), Type: r.Expr.Type(), Source: ""}, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// WithChildren implements the interface sql.ExecSourceRel. |
| 85 | +func (r *Return) WithChildren(children ...sql.Node) (sql.Node, error) { |
| 86 | + if len(children) != 0 { |
| 87 | + return nil, sql.ErrInvalidChildrenNumber.New(r, len(children), 0) |
| 88 | + } |
| 89 | + return r, nil |
| 90 | +} |
| 91 | + |
| 92 | +// WithResolvedChildren implements the interface sql.ExecSourceRel. |
| 93 | +func (r *Return) WithResolvedChildren(children []any) (any, error) { |
| 94 | + if len(children) != 1 { |
| 95 | + return nil, sql.ErrInvalidChildrenNumber.New(r, len(children), 1) |
| 96 | + } |
| 97 | + |
| 98 | + nr := *r |
| 99 | + nr.Expr = children[0].(sql.Expression) |
| 100 | + return &nr, nil |
| 101 | +} |
0 commit comments