github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/tuple.go (about) 1 // Copyright 2020-2021 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 "strings" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 types2 "github.com/dolthub/go-mysql-server/sql/types" 23 ) 24 25 // Tuple is a fixed-size collection of expressions. 26 // A tuple of size 1 is treated as the expression itself. 27 type Tuple []sql.Expression 28 29 // NewTuple creates a new Tuple expression. 30 func NewTuple(exprs ...sql.Expression) Tuple { 31 return Tuple(exprs) 32 } 33 34 // Eval implements the Expression interface. 35 func (t Tuple) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 36 if len(t) == 1 { 37 return t[0].Eval(ctx, row) 38 } 39 40 var result = make([]interface{}, len(t)) 41 for i, e := range t { 42 v, err := e.Eval(ctx, row) 43 if err != nil { 44 return nil, err 45 } 46 47 result[i] = v 48 } 49 50 return result, nil 51 } 52 53 // IsNullable implements the Expression interface. 54 func (t Tuple) IsNullable() bool { 55 if len(t) == 1 { 56 return t[0].IsNullable() 57 } 58 59 return false 60 } 61 62 func (t Tuple) String() string { 63 var exprs = make([]string, len(t)) 64 for i, e := range t { 65 exprs[i] = e.String() 66 } 67 return fmt.Sprintf("(%s)", strings.Join(exprs, ", ")) 68 } 69 70 func (t Tuple) DebugString() string { 71 var exprs = make([]string, len(t)) 72 for i, e := range t { 73 exprs[i] = sql.DebugString(e) 74 } 75 return fmt.Sprintf("TUPLE(%s)", strings.Join(exprs, ", ")) 76 } 77 78 // Resolved implements the Expression interface. 79 func (t Tuple) Resolved() bool { 80 for _, e := range t { 81 if !e.Resolved() { 82 return false 83 } 84 } 85 86 return true 87 } 88 89 // Type implements the Expression interface. 90 func (t Tuple) Type() sql.Type { 91 if len(t) == 1 { 92 return t[0].Type() 93 } 94 95 types := make([]sql.Type, len(t)) 96 for i, e := range t { 97 types[i] = e.Type() 98 } 99 100 return types2.CreateTuple(types...) 101 } 102 103 // WithChildren implements the Expression interface. 104 func (t Tuple) WithChildren(children ...sql.Expression) (sql.Expression, error) { 105 if len(children) != len(t) { 106 return nil, sql.ErrInvalidChildrenNumber.New(t, len(children), len(t)) 107 } 108 return NewTuple(children...), nil 109 } 110 111 // Children implements the Expression interface. 112 func (t Tuple) Children() []sql.Expression { 113 return t 114 }