-
-
Notifications
You must be signed in to change notification settings - Fork 58
Allow different but compatible types in foreign key constraints #1277
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 all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
2b62558
Loosened foreign key validation
zachmu dc944fe
Merge branch 'zachmu/text-primary-keys' into zachmu/foreign-key-text-…
zachmu 2713aec
Added a comment
zachmu 6d61577
new gms
zachmu 509be11
updated go.sum
zachmu 38c5b55
formatting
zachmu 1d662c2
New test for type compatibility on foreign key
zachmu ecc9da4
checkpoint
zachmu 03b3206
more tests
zachmu 9f21414
Closer to correct semantics for foreign key opertions
zachmu 145480f
More insert tests to rule out insert problems in foreign key tests
zachmu c683c29
Stub for foreign key type conversions
zachmu 55024ab
bug fix for ignoring the constraint name in foreign key creation, mor…
zachmu 7b072e5
Another bug fix for missing index / constraint name, more tests
zachmu 8357d38
Bug fixes in test defns
zachmu 3524892
Tests for out of bounds type conversions
zachmu 291a495
new test, fixed typos
zachmu 7996632
Added a comment about current logic
zachmu 0a1eddc
formatting
zachmu 9698ee9
Merge branch 'main' into zachmu/foreign-key-types
zachmu 903b23b
new gms
zachmu 20f5f87
PR feedback and bug fix for type conversion
zachmu 5df4f2c
Bug fix for key name generation
zachmu 1a7d4e1
Another name generation bug fix
zachmu 6e9876b
new gms
zachmu e7c4b49
formatting
zachmu b7a1215
PR feedback
zachmu 987b557
formatting
zachmu 211d4d9
merge main, fix a couple tests
zachmu 73dde67
new gms
zachmu 96b1809
new gms
zachmu 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,74 @@ | ||
| // Copyright 2025 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 ( | ||
| "strings" | ||
|
|
||
| "github.com/cockroachdb/errors" | ||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/expression" | ||
|
|
||
| "github.com/dolthub/doltgresql/server/functions/framework" | ||
| "github.com/dolthub/doltgresql/server/types" | ||
| ) | ||
|
|
||
| // validateForeignKeyDefinition validates that the given foreign key definition is valid for creation | ||
| func validateForeignKeyDefinition(ctx *sql.Context, fkDef sql.ForeignKeyConstraint, cols map[string]*sql.Column, parentCols map[string]*sql.Column) error { | ||
| for i := range fkDef.Columns { | ||
| col := cols[strings.ToLower(fkDef.Columns[i])] | ||
| parentCol := parentCols[strings.ToLower(fkDef.ParentColumns[i])] | ||
| if !foreignKeyComparableTypes(col.Type, parentCol.Type) { | ||
| return errors.Errorf("Key columns %q and %q are of incompatible types: %s and %s", col.Name, parentCol.Name, col.Type.String(), parentCol.Type.String()) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // foreignKeyComparableTypes returns whether the two given types are able to be used as parent/child columns in a | ||
| // foreign key. | ||
| func foreignKeyComparableTypes(from sql.Type, to sql.Type) bool { | ||
| dtFrom, ok := from.(*types.DoltgresType) | ||
| if !ok { | ||
| return false // should never be possible | ||
| } | ||
|
|
||
| dtTo, ok := to.(*types.DoltgresType) | ||
| if !ok { | ||
| return false // should never be possible | ||
| } | ||
|
|
||
| if dtFrom.Equals(dtTo) { | ||
| return true | ||
| } | ||
|
|
||
| fromLiteral := expression.NewLiteral(dtFrom.Zero(), from) | ||
| toLiteral := expression.NewLiteral(dtTo.Zero(), to) | ||
|
|
||
| // a foreign key between two different types is valid if there is an equality operator on the two types | ||
| // TODO: there are some subtleties in postgres not captured by this logic, e.g. a foreign key from double -> int | ||
| // is valid, but the reverse is not. This works fine, but is more permissive than postgres is. | ||
| eq := framework.GetBinaryFunction(framework.Operator_BinaryEqual).Compile("=", fromLiteral, toLiteral) | ||
| if eq == nil || eq.StashedError() != nil { | ||
| return false | ||
| } | ||
|
|
||
| // Additionally, we need to be able to convert freely between the two types in both directions, since we do this | ||
| // during the process of enforcing the constraints | ||
| forwardConversion := types.GetAssignmentCast(dtFrom, dtTo) | ||
| reverseConversion := types.GetAssignmentCast(dtTo, dtFrom) | ||
|
|
||
| return forwardConversion != nil && reverseConversion != nil | ||
| } |
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
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
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.
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.