|
| 1 | +// Copyright 2025 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 expression |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/cockroachdb/errors" |
| 19 | + "github.com/dolthub/go-mysql-server/sql" |
| 20 | + vitess "github.com/dolthub/vitess/go/vt/sqlparser" |
| 21 | + |
| 22 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 23 | +) |
| 24 | + |
| 25 | +// NewRecordExpr creates a new record expression. |
| 26 | +func NewRecordExpr() *RecordExpr { |
| 27 | + // Initialize the record expression with the generic Record type. When the analyzer |
| 28 | + // resolves the children of the InjectedExpr and sets them by calling |
| 29 | + // WithResolvedChildren(), the type will be updated with the exact field types. |
| 30 | + return &RecordExpr{ |
| 31 | + typ: pgtypes.Record, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// RecordExpr is a set of sql.Expressions wrapped together in a single value. |
| 36 | +type RecordExpr struct { |
| 37 | + exprs []sql.Expression |
| 38 | + typ *pgtypes.DoltgresType |
| 39 | +} |
| 40 | + |
| 41 | +var _ sql.Expression = (*RecordExpr)(nil) |
| 42 | +var _ vitess.Injectable = (*RecordExpr)(nil) |
| 43 | + |
| 44 | +// Resolved implements the sql.Expression interface. |
| 45 | +func (t *RecordExpr) Resolved() bool { |
| 46 | + for _, expr := range t.exprs { |
| 47 | + if !expr.Resolved() { |
| 48 | + return false |
| 49 | + } |
| 50 | + } |
| 51 | + return true |
| 52 | +} |
| 53 | + |
| 54 | +// String implements the sql.Expression interface. |
| 55 | +func (t *RecordExpr) String() string { |
| 56 | + return "RECORD EXPR" |
| 57 | +} |
| 58 | + |
| 59 | +// Type implements the sql.Expression interface. |
| 60 | +func (t *RecordExpr) Type() sql.Type { |
| 61 | + return t.typ |
| 62 | +} |
| 63 | + |
| 64 | +// IsNullable implements the sql.Expression interface. |
| 65 | +func (t *RecordExpr) IsNullable() bool { |
| 66 | + return false |
| 67 | +} |
| 68 | + |
| 69 | +// Eval implements the sql.Expression interface. |
| 70 | +func (t *RecordExpr) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { |
| 71 | + vals := make([]interface{}, len(t.exprs)) |
| 72 | + for i, expr := range t.exprs { |
| 73 | + val, err := expr.Eval(ctx, row) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + vals[i] = val |
| 78 | + } |
| 79 | + |
| 80 | + return vals, nil |
| 81 | +} |
| 82 | + |
| 83 | +// Children implements the sql.Expression interface. |
| 84 | +func (t *RecordExpr) Children() []sql.Expression { |
| 85 | + return t.exprs |
| 86 | +} |
| 87 | + |
| 88 | +// WithChildren implements the sql.Expression interface. |
| 89 | +func (t *RecordExpr) WithChildren(children ...sql.Expression) (sql.Expression, error) { |
| 90 | + tCopy := *t |
| 91 | + tCopy.exprs = children |
| 92 | + return &tCopy, nil |
| 93 | +} |
| 94 | + |
| 95 | +// WithResolvedChildren implements the vitess.Injectable interface |
| 96 | +func (t *RecordExpr) WithResolvedChildren(children []any) (any, error) { |
| 97 | + newExpressions := make([]sql.Expression, len(children)) |
| 98 | + for i, resolvedChild := range children { |
| 99 | + resolvedExpression, ok := resolvedChild.(sql.Expression) |
| 100 | + if !ok { |
| 101 | + return nil, errors.Errorf("expected vitess child to be an expression but has type `%T`", resolvedChild) |
| 102 | + } |
| 103 | + newExpressions[i] = resolvedExpression |
| 104 | + } |
| 105 | + newExpr, err := t.WithChildren(newExpressions...) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + fieldTypes := make([]sql.Type, len(newExpressions)) |
| 111 | + for i, expr := range newExpressions { |
| 112 | + fieldTypes[i] = expr.Type() |
| 113 | + } |
| 114 | + |
| 115 | + newExpr.(*RecordExpr).typ = pgtypes.CreateRecordTypeFromFieldTypes(fieldTypes) |
| 116 | + return newExpr, err |
| 117 | +} |
0 commit comments