-
-
Notifications
You must be signed in to change notification settings - Fork 39
Support for using TEXT fields in secondary indexes and UNIQUE constraints
#829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3ba7109
Adding support for using TEXT fields in UNIQUE indexes
fulghum e5f1eec
Update GMS and dolt dependencies to latest dev builds from fulghum/te…
fulghum a10d7b3
Changing approach to add in a separate analyzer rule
fulghum fdc5dad
Merge branch 'main' into fulghum/text
fulghum 3a9378e
Bumping GMS dep to latest dev build from fulghum/implicit-prefix-leng…
fulghum 9d3690b
Moving new analyzer rule into Doltgres package
fulghum e5f05fc
Bumping GMS dep to latest dev build from fulghum/implicit-prefix-leng…
fulghum 7bc5fe7
Limiting to only TEXT types
fulghum e23351a
go mod tidy
fulghum d7f8bc7
bug fix for casing in column map
fulghum 9126c8e
removing blank assignment to keep linter happy
fulghum 08b7ce7
Fixing incorrect coalesce test value
fulghum fa1bcd7
Comparing type directly to types.TextType, instead of relying on Type…
fulghum 28de1b6
Merge branch 'main' into fulghum/text
fulghum 0b33a4f
Bumping GMS dep to latest dev build from fulghum/implicit-prefix-leng…
fulghum 8830c99
PR Feedback
fulghum f862079
Merge branch 'main' into fulghum/text
fulghum c413409
PR Feedback: Increasing the implicit prefix size
fulghum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| // Copyright 2024 Dolthub, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package analyzer | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/analyzer" | ||
| "github.com/dolthub/go-mysql-server/sql/plan" | ||
| "github.com/dolthub/go-mysql-server/sql/transform" | ||
|
|
||
| "github.com/dolthub/doltgresql/server/types" | ||
| ) | ||
|
|
||
| // defaultIndexPrefixLength is the index prefix length that this analyzer rule applies automatically to TEXT columns | ||
| // in secondary indexes. | ||
| const defaultIndexPrefixLength = 255 | ||
|
|
||
| // AddImplicitPrefixLengths searches the |node| tree for any nodes creating an index, and plugs in a default index | ||
| // prefix length for any TEXT columns in those new indexes. This rule is intended to be used for Postgres compatibility, | ||
| // since Postgres does not require specifying prefix lengths for TEXT columns. | ||
| func AddImplicitPrefixLengths(_ *sql.Context, _ *analyzer.Analyzer, node sql.Node, _ *plan.Scope, _ analyzer.RuleSelector, _ *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) { | ||
| var targetSchema sql.Schema | ||
| transform.Inspect(node, func(node sql.Node) bool { | ||
| if st, ok := node.(sql.SchemaTarget); ok { | ||
| targetSchema = st.TargetSchema().Copy() | ||
| return false | ||
| } | ||
| return true | ||
| }) | ||
|
|
||
| // Recurse through the node tree to fill in prefix lengths. Note that some statements come in as Block nodes | ||
| // that contain multiple nodes, so we need to recurse through and handle all of them. | ||
| return transform.Node(node, func(node sql.Node) (sql.Node, transform.TreeIdentity, error) { | ||
| switch node := node.(type) { | ||
| case *plan.AddColumn: | ||
| // For any AddColumn nodes, we need to update the target schema with the column being added, otherwise | ||
| // we won't be able to find those columns if they are also being added to a secondary index. | ||
| var err error | ||
| targetSchema, err = analyzer.ValidateAddColumn(targetSchema, node) | ||
| if err != nil { | ||
| return nil, transform.SameTree, err | ||
| } | ||
|
|
||
| case *plan.CreateTable: | ||
| newIndexes := make([]*sql.IndexDef, len(node.Indexes())) | ||
| for i := range node.Indexes() { | ||
| copy := *node.Indexes()[i] | ||
| newIndexes[i] = © | ||
| } | ||
| indexModified := false | ||
| for _, index := range newIndexes { | ||
| targetSchema := node.TargetSchema() | ||
| colMap := schToColMap(targetSchema) | ||
|
|
||
| for i := range index.Columns { | ||
| col, ok := colMap[strings.ToLower(index.Columns[i].Name)] | ||
| if !ok { | ||
| return nil, false, fmt.Errorf("indexed column %s not found in schema", index.Columns[i].Name) | ||
| } | ||
| if _, ok := col.Type.(types.TextType); ok && index.Columns[i].Length == 0 { | ||
fulghum marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| index.Columns[i].Length = defaultIndexPrefixLength | ||
| indexModified = true | ||
| } | ||
| } | ||
| } | ||
| if indexModified { | ||
| newNode, err := node.WithIndexDefs(newIndexes) | ||
| return newNode, transform.NewTree, err | ||
| } | ||
|
|
||
| case *plan.AlterIndex: | ||
| if node.Action == plan.IndexAction_Create { | ||
| colMap := schToColMap(targetSchema) | ||
| newColumns := make([]sql.IndexColumn, len(node.Columns)) | ||
| for i := range node.Columns { | ||
| copy := node.Columns[i] | ||
| newColumns[i] = copy | ||
| } | ||
| indexModified := false | ||
| for i := range newColumns { | ||
| col, ok := colMap[strings.ToLower(newColumns[i].Name)] | ||
| if !ok { | ||
| return nil, false, fmt.Errorf("indexed column %s not found in schema", newColumns[i].Name) | ||
| } | ||
| if _, ok := col.Type.(types.TextType); ok && newColumns[i].Length == 0 { | ||
| newColumns[i].Length = defaultIndexPrefixLength | ||
| indexModified = true | ||
| } | ||
| } | ||
| if indexModified { | ||
| newNode, err := node.WithColumns(newColumns) | ||
| return newNode, transform.NewTree, err | ||
| } | ||
| } | ||
| } | ||
| return node, transform.SameTree, nil | ||
| }) | ||
| } | ||
|
|
||
| func schToColMap(sch sql.Schema) map[string]*sql.Column { | ||
| colMap := make(map[string]*sql.Column, len(sch)) | ||
| for _, col := range sch { | ||
| colMap[strings.ToLower(col.Name)] = col | ||
| } | ||
| return colMap | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.