|
| 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 analyzer |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/dolthub/go-mysql-server/sql" |
| 19 | + "github.com/dolthub/go-mysql-server/sql/analyzer" |
| 20 | + "github.com/dolthub/go-mysql-server/sql/expression" |
| 21 | + "github.com/dolthub/go-mysql-server/sql/plan" |
| 22 | + "github.com/dolthub/go-mysql-server/sql/transform" |
| 23 | + |
| 24 | + pgnode "github.com/dolthub/doltgresql/server/node" |
| 25 | +) |
| 26 | + |
| 27 | +// validateColumnDefaults ensures that newly created column defaults from a DDL statement are legal for the type of |
| 28 | +// column, various other business logic checks to match MySQL's logic. |
| 29 | +func ValidateColumnDefaults(ctx *sql.Context, _ *analyzer.Analyzer, n sql.Node, _ *plan.Scope, _ analyzer.RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) { |
| 30 | + span, ctx := ctx.Span("validateColumnDefaults") |
| 31 | + defer span.End() |
| 32 | + |
| 33 | + return transform.Node(n, func(n sql.Node) (sql.Node, transform.TreeIdentity, error) { |
| 34 | + switch node := n.(type) { |
| 35 | + case *plan.AlterDefaultSet: |
| 36 | + table := getResolvedTable(node) |
| 37 | + sch := table.Schema() |
| 38 | + index := sch.IndexOfColName(node.ColumnName) |
| 39 | + if index == -1 { |
| 40 | + return nil, transform.SameTree, sql.ErrColumnNotFound.New(node.ColumnName) |
| 41 | + } |
| 42 | + col := sch[index] |
| 43 | + err := validateColumnDefault(ctx, col, node.Default) |
| 44 | + if err != nil { |
| 45 | + return node, transform.SameTree, err |
| 46 | + } |
| 47 | + |
| 48 | + return node, transform.SameTree, nil |
| 49 | + |
| 50 | + case sql.SchemaTarget: |
| 51 | + switch node.(type) { |
| 52 | + case *plan.AlterPK, *plan.AddColumn, *plan.ModifyColumn, *plan.AlterDefaultDrop, *plan.CreateTable, *plan.DropColumn, *pgnode.CreateTable: |
| 53 | + // DDL nodes must validate any new column defaults, continue to logic below |
| 54 | + default: |
| 55 | + // other node types are not altering the schema and therefore don't need validation of column defaults |
| 56 | + return n, transform.SameTree, nil |
| 57 | + } |
| 58 | + |
| 59 | + // There may be multiple DDL nodes in the plan (ALTER TABLE statements can have many clauses), and for each of them |
| 60 | + // we need to count the column indexes in the very hacky way outlined above. |
| 61 | + i := 0 |
| 62 | + return transform.NodeExprs(n, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) { |
| 63 | + eWrapper, ok := e.(*expression.Wrapper) |
| 64 | + if !ok { |
| 65 | + return e, transform.SameTree, nil |
| 66 | + } |
| 67 | + |
| 68 | + defer func() { |
| 69 | + i++ |
| 70 | + }() |
| 71 | + |
| 72 | + eVal := eWrapper.Unwrap() |
| 73 | + if eVal == nil { |
| 74 | + return e, transform.SameTree, nil |
| 75 | + } |
| 76 | + colDefault, ok := eVal.(*sql.ColumnDefaultValue) |
| 77 | + if !ok { |
| 78 | + return e, transform.SameTree, nil |
| 79 | + } |
| 80 | + |
| 81 | + col, err := lookupColumnForTargetSchema(ctx, node, i) |
| 82 | + if err != nil { |
| 83 | + return nil, transform.SameTree, err |
| 84 | + } |
| 85 | + |
| 86 | + err = validateColumnDefault(ctx, col, colDefault) |
| 87 | + if err != nil { |
| 88 | + return nil, transform.SameTree, err |
| 89 | + } |
| 90 | + |
| 91 | + return e, transform.SameTree, nil |
| 92 | + }) |
| 93 | + default: |
| 94 | + return node, transform.SameTree, nil |
| 95 | + } |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +// lookupColumnForTargetSchema looks at the target schema for the specified SchemaTarget node and returns |
| 100 | +// the column based on the specified index. For most node types, this is simply indexing into the target |
| 101 | +// schema but a few types require special handling. |
| 102 | +func lookupColumnForTargetSchema(_ *sql.Context, node sql.SchemaTarget, colIndex int) (*sql.Column, error) { |
| 103 | + schema := node.TargetSchema() |
| 104 | + |
| 105 | + switch n := node.(type) { |
| 106 | + case *plan.ModifyColumn: |
| 107 | + if colIndex < len(schema) { |
| 108 | + return schema[colIndex], nil |
| 109 | + } else { |
| 110 | + return n.NewColumn(), nil |
| 111 | + } |
| 112 | + case *plan.AddColumn: |
| 113 | + if colIndex < len(schema) { |
| 114 | + return schema[colIndex], nil |
| 115 | + } else { |
| 116 | + return n.Column(), nil |
| 117 | + } |
| 118 | + case *plan.AlterDefaultSet: |
| 119 | + index := schema.IndexOfColName(n.ColumnName) |
| 120 | + if index == -1 { |
| 121 | + return nil, sql.ErrTableColumnNotFound.New(n.Table, n.ColumnName) |
| 122 | + } |
| 123 | + return schema[index], nil |
| 124 | + default: |
| 125 | + if colIndex < len(schema) { |
| 126 | + return schema[colIndex], nil |
| 127 | + } else { |
| 128 | + // TODO: sql.ErrColumnNotFound would be a better error here, but we need to add all the different node types to |
| 129 | + // the switch to get it |
| 130 | + return nil, expression.ErrIndexOutOfBounds.New(colIndex, len(schema)) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// validateColumnDefault validates that the column default expression is valid for the column type and returns an error |
| 136 | +// if not |
| 137 | +func validateColumnDefault(ctx *sql.Context, col *sql.Column, colDefault *sql.ColumnDefaultValue) error { |
| 138 | + if colDefault == nil { |
| 139 | + return nil |
| 140 | + } |
| 141 | + |
| 142 | + var err error |
| 143 | + sql.Inspect(colDefault.Expr, func(e sql.Expression) bool { |
| 144 | + switch e.(type) { |
| 145 | + case sql.FunctionExpression, *expression.UnresolvedFunction: |
| 146 | + // TODO: functions must be deterministic to be used in column defaults |
| 147 | + return true |
| 148 | + case *plan.Subquery: |
| 149 | + err = sql.ErrColumnDefaultSubquery.New(col.Name) |
| 150 | + return false |
| 151 | + case *expression.GetField: |
| 152 | + if !colDefault.IsParenthesized() { |
| 153 | + err = sql.ErrInvalidColumnDefaultValue.New(col.Name) |
| 154 | + return false |
| 155 | + } |
| 156 | + return true |
| 157 | + default: |
| 158 | + return true |
| 159 | + } |
| 160 | + }) |
| 161 | + |
| 162 | + if err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + |
| 166 | + // validate type of default expression |
| 167 | + if err = colDefault.CheckType(ctx); err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +// Finds first ResolvedTable node that is a descendant of the node given |
| 175 | +// This function will not look inside SubqueryAliases |
| 176 | +func getResolvedTable(node sql.Node) *plan.ResolvedTable { |
| 177 | + var table *plan.ResolvedTable |
| 178 | + transform.Inspect(node, func(n sql.Node) bool { |
| 179 | + // Inspect is called on all children of a node even if an earlier child's call returns false. |
| 180 | + // We only want the first TableNode match. |
| 181 | + if table != nil { |
| 182 | + return false |
| 183 | + } |
| 184 | + switch nn := n.(type) { |
| 185 | + case *plan.SubqueryAlias: |
| 186 | + // We should not be matching with ResolvedTables inside SubqueryAliases |
| 187 | + return false |
| 188 | + case *plan.ResolvedTable: |
| 189 | + if !plan.IsDualTable(nn) { |
| 190 | + table = nn |
| 191 | + return false |
| 192 | + } |
| 193 | + case *plan.IndexedTableAccess: |
| 194 | + if rt, ok := nn.TableNode.(*plan.ResolvedTable); ok { |
| 195 | + table = rt |
| 196 | + return false |
| 197 | + } |
| 198 | + } |
| 199 | + return true |
| 200 | + }) |
| 201 | + return table |
| 202 | +} |
0 commit comments