|
| 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 expression |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/cockroachdb/errors" |
| 21 | + "github.com/dolthub/go-mysql-server/sql" |
| 22 | + vitess "github.com/dolthub/vitess/go/vt/sqlparser" |
| 23 | + |
| 24 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 25 | +) |
| 26 | + |
| 27 | +// ColumnAccess represents an ARRAY[...] expression. |
| 28 | +type ColumnAccess struct { |
| 29 | + colName string |
| 30 | + colNameIdx int |
| 31 | + colTyp *pgtypes.DoltgresType |
| 32 | + child sql.Expression |
| 33 | +} |
| 34 | + |
| 35 | +var _ vitess.Injectable = (*ColumnAccess)(nil) |
| 36 | +var _ sql.Expression = (*ColumnAccess)(nil) |
| 37 | + |
| 38 | +// NewColumnAccess returns a new *ColumnAccess. |
| 39 | +func NewColumnAccess(colName string, colIdx int) (*ColumnAccess, error) { |
| 40 | + if len(colName) > 0 { |
| 41 | + return &ColumnAccess{ |
| 42 | + colName: colName, |
| 43 | + colNameIdx: -1, |
| 44 | + colTyp: nil, |
| 45 | + child: nil, |
| 46 | + }, nil |
| 47 | + } else { |
| 48 | + return &ColumnAccess{ |
| 49 | + colName: "", |
| 50 | + colNameIdx: colIdx, |
| 51 | + colTyp: nil, |
| 52 | + child: nil, |
| 53 | + }, nil |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Children implements the sql.Expression interface. |
| 58 | +func (expr *ColumnAccess) Children() []sql.Expression { |
| 59 | + return []sql.Expression{expr.child} |
| 60 | +} |
| 61 | + |
| 62 | +// Eval implements the sql.Expression interface. |
| 63 | +func (expr *ColumnAccess) Eval(ctx *sql.Context, row sql.Row) (any, error) { |
| 64 | + field, err := expr.child.Eval(ctx, row) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + if field == nil { |
| 69 | + return nil, nil |
| 70 | + } |
| 71 | + recordVals, ok := field.([]pgtypes.RecordValue) |
| 72 | + if !ok { |
| 73 | + if len(expr.colName) > 0 { |
| 74 | + return nil, errors.Errorf("column notation .%s applied to type %s, which is not a composite type", |
| 75 | + expr.colName, expr.child.Type().String()) |
| 76 | + } else { |
| 77 | + return nil, errors.Errorf("column notation .@%d applied to type %s, which is not a composite type", |
| 78 | + expr.colNameIdx+1, expr.child.Type().String()) |
| 79 | + } |
| 80 | + } |
| 81 | + return recordVals[expr.colNameIdx].Value, nil |
| 82 | +} |
| 83 | + |
| 84 | +// IsNullable implements the sql.Expression interface. |
| 85 | +func (expr *ColumnAccess) IsNullable() bool { |
| 86 | + return true |
| 87 | +} |
| 88 | + |
| 89 | +// Resolved implements the sql.Expression interface. |
| 90 | +func (expr *ColumnAccess) Resolved() bool { |
| 91 | + return expr.child != nil && expr.child.Resolved() |
| 92 | +} |
| 93 | + |
| 94 | +// String implements the sql.Expression interface. |
| 95 | +func (expr *ColumnAccess) String() string { |
| 96 | + if expr.child == nil { |
| 97 | + return "COLUMN_ACCESS" |
| 98 | + } |
| 99 | + if len(expr.colName) > 0 { |
| 100 | + return fmt.Sprintf("(%s).%s", expr.child.String(), expr.colName) |
| 101 | + } else { |
| 102 | + return fmt.Sprintf("(%s).@%d", expr.child.String(), expr.colNameIdx+1) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// Type implements the sql.Expression interface. |
| 107 | +func (expr *ColumnAccess) Type() sql.Type { |
| 108 | + if expr.colTyp != nil { |
| 109 | + return expr.colTyp |
| 110 | + } |
| 111 | + if expr.child == nil { |
| 112 | + return nil |
| 113 | + } |
| 114 | + // We're technically returning a different type here since an unresolved type is not the same as a resolved one. |
| 115 | + // However, for many early analyzer steps, we only check the ID, so this at least lets us get past those cases. |
| 116 | + return pgtypes.NewUnresolvedDoltgresTypeFromID(expr.child.Type().(*pgtypes.DoltgresType).CompositeAttrs[expr.colNameIdx].TypeID) |
| 117 | +} |
| 118 | + |
| 119 | +// WithChildren implements the sql.Expression interface. |
| 120 | +func (expr *ColumnAccess) WithChildren(children ...sql.Expression) (sql.Expression, error) { |
| 121 | + if len(children) != 1 { |
| 122 | + return nil, sql.ErrInvalidChildrenNumber.New(expr, len(children), 1) |
| 123 | + } |
| 124 | + childType := children[0].Type() |
| 125 | + doltgresType, ok := childType.(*pgtypes.DoltgresType) |
| 126 | + if !ok { |
| 127 | + return nil, errors.New("column access is only valid for Doltgres types") |
| 128 | + } |
| 129 | + if !doltgresType.IsCompositeType() { |
| 130 | + if len(expr.colName) > 0 { |
| 131 | + return nil, errors.Errorf("column notation .%s applied to type %s, which is not a composite type", |
| 132 | + expr.colName, children[0].Type().String()) |
| 133 | + } else { |
| 134 | + return nil, errors.Errorf("column notation .@%d applied to type %s, which is not a composite type", |
| 135 | + expr.colNameIdx+1, children[0].Type().String()) |
| 136 | + } |
| 137 | + } |
| 138 | + var idx int |
| 139 | + if len(expr.colName) > 0 { |
| 140 | + idx = -1 |
| 141 | + for _, attr := range doltgresType.CompositeAttrs { |
| 142 | + if attr.Name == expr.colName { |
| 143 | + idx = int(attr.Num - 1) |
| 144 | + break |
| 145 | + } |
| 146 | + } |
| 147 | + if idx == -1 { |
| 148 | + return nil, errors.Errorf(`column "%s" not found in data type %s`, |
| 149 | + expr.colName, doltgresType.String()) |
| 150 | + } |
| 151 | + } else { |
| 152 | + if expr.colNameIdx < 0 || expr.colNameIdx >= len(doltgresType.CompositeAttrs) { |
| 153 | + return nil, errors.Errorf("column notation .@%d applied to type %s is out of bounds", |
| 154 | + expr.colNameIdx+1, children[0].Type().String()) |
| 155 | + } |
| 156 | + idx = expr.colNameIdx |
| 157 | + } |
| 158 | + return &ColumnAccess{ |
| 159 | + colName: expr.colName, |
| 160 | + colNameIdx: idx, |
| 161 | + colTyp: expr.colTyp, |
| 162 | + child: children[0], |
| 163 | + }, nil |
| 164 | +} |
| 165 | + |
| 166 | +// WithResolvedChildren implements the vitess.InjectableExpression interface. |
| 167 | +func (expr *ColumnAccess) WithResolvedChildren(children []any) (any, error) { |
| 168 | + newExpressions := make([]sql.Expression, len(children)) |
| 169 | + for i, resolvedChild := range children { |
| 170 | + resolvedExpression, ok := resolvedChild.(sql.Expression) |
| 171 | + if !ok { |
| 172 | + return nil, errors.Errorf("expected vitess child to be an expression but has type `%T`", resolvedChild) |
| 173 | + } |
| 174 | + newExpressions[i] = resolvedExpression |
| 175 | + } |
| 176 | + return expr.WithChildren(newExpressions...) |
| 177 | +} |
| 178 | + |
| 179 | +// WithType returns this expression with the given type set, as it must be set within the analyzer. |
| 180 | +func (expr *ColumnAccess) WithType(typ *pgtypes.DoltgresType) sql.Expression { |
| 181 | + ne := *expr |
| 182 | + ne.colTyp = typ |
| 183 | + return &ne |
| 184 | +} |
0 commit comments