|
| 1 | +// Copyright 2024 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 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/dolthub/go-mysql-server/sql" |
| 22 | + "github.com/dolthub/go-mysql-server/sql/analyzer" |
| 23 | + "github.com/dolthub/go-mysql-server/sql/plan" |
| 24 | + "github.com/dolthub/go-mysql-server/sql/transform" |
| 25 | + |
| 26 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 27 | +) |
| 28 | + |
| 29 | +// defaultIndexPrefixLength is the index prefix length that this analyzer rule applies automatically to TEXT columns |
| 30 | +// in secondary indexes. 768 is the limit for the prefix length in MySQL and is also enforced in Dolt/GMS, so this |
| 31 | +// is currently the largest size we can support. |
| 32 | +const defaultIndexPrefixLength = 768 |
| 33 | + |
| 34 | +// AddImplicitPrefixLengths searches the |node| tree for any nodes creating an index, and plugs in a default index |
| 35 | +// prefix length for any TEXT columns in those new indexes. This rule is intended to be used for Postgres compatibility, |
| 36 | +// since Postgres does not require specifying prefix lengths for TEXT columns. |
| 37 | +func AddImplicitPrefixLengths(_ *sql.Context, _ *analyzer.Analyzer, node sql.Node, _ *plan.Scope, _ analyzer.RuleSelector, _ *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) { |
| 38 | + var targetSchema sql.Schema |
| 39 | + transform.Inspect(node, func(node sql.Node) bool { |
| 40 | + if st, ok := node.(sql.SchemaTarget); ok { |
| 41 | + targetSchema = st.TargetSchema().Copy() |
| 42 | + return false |
| 43 | + } |
| 44 | + return true |
| 45 | + }) |
| 46 | + |
| 47 | + // Recurse through the node tree to fill in prefix lengths. Note that some statements come in as Block nodes |
| 48 | + // that contain multiple nodes, so we need to recurse through and handle all of them. |
| 49 | + return transform.Node(node, func(node sql.Node) (sql.Node, transform.TreeIdentity, error) { |
| 50 | + switch node := node.(type) { |
| 51 | + case *plan.AddColumn: |
| 52 | + // For any AddColumn nodes, we need to update the target schema with the column being added, otherwise |
| 53 | + // we won't be able to find those columns if they are also being added to a secondary index. |
| 54 | + var err error |
| 55 | + targetSchema, err = analyzer.ValidateAddColumn(targetSchema, node) |
| 56 | + if err != nil { |
| 57 | + return nil, transform.SameTree, err |
| 58 | + } |
| 59 | + |
| 60 | + case *plan.CreateTable: |
| 61 | + newIndexes := make([]*sql.IndexDef, len(node.Indexes())) |
| 62 | + for i := range node.Indexes() { |
| 63 | + copy := *node.Indexes()[i] |
| 64 | + newIndexes[i] = © |
| 65 | + } |
| 66 | + indexModified := false |
| 67 | + for _, index := range newIndexes { |
| 68 | + targetSchema := node.TargetSchema() |
| 69 | + colMap := schToColMap(targetSchema) |
| 70 | + for i := range index.Columns { |
| 71 | + col, ok := colMap[strings.ToLower(index.Columns[i].Name)] |
| 72 | + if !ok { |
| 73 | + return nil, false, fmt.Errorf("indexed column %s not found in schema", index.Columns[i].Name) |
| 74 | + } |
| 75 | + if _, ok := col.Type.(pgtypes.TextType); ok && index.Columns[i].Length == 0 { |
| 76 | + index.Columns[i].Length = defaultIndexPrefixLength |
| 77 | + indexModified = true |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + if indexModified { |
| 82 | + newNode, err := node.WithIndexDefs(newIndexes) |
| 83 | + return newNode, transform.NewTree, err |
| 84 | + } |
| 85 | + |
| 86 | + case *plan.AlterIndex: |
| 87 | + if node.Action == plan.IndexAction_Create { |
| 88 | + colMap := schToColMap(targetSchema) |
| 89 | + newColumns := make([]sql.IndexColumn, len(node.Columns)) |
| 90 | + for i := range node.Columns { |
| 91 | + copy := node.Columns[i] |
| 92 | + newColumns[i] = copy |
| 93 | + } |
| 94 | + indexModified := false |
| 95 | + for i := range newColumns { |
| 96 | + col, ok := colMap[strings.ToLower(newColumns[i].Name)] |
| 97 | + if !ok { |
| 98 | + return nil, false, fmt.Errorf("indexed column %s not found in schema", newColumns[i].Name) |
| 99 | + } |
| 100 | + if _, ok := col.Type.(pgtypes.TextType); ok && newColumns[i].Length == 0 { |
| 101 | + newColumns[i].Length = defaultIndexPrefixLength |
| 102 | + indexModified = true |
| 103 | + } |
| 104 | + } |
| 105 | + if indexModified { |
| 106 | + newNode, err := node.WithColumns(newColumns) |
| 107 | + return newNode, transform.NewTree, err |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return node, transform.SameTree, nil |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func schToColMap(sch sql.Schema) map[string]*sql.Column { |
| 116 | + colMap := make(map[string]*sql.Column, len(sch)) |
| 117 | + for _, col := range sch { |
| 118 | + colMap[strings.ToLower(col.Name)] = col |
| 119 | + } |
| 120 | + return colMap |
| 121 | +} |
0 commit comments