-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
65209: sql: use of constraint columns in FK can differ from order in PK definition r=otan a=samxsmith sql: use of constraint columns in FK can differ from order in PK definition ColumnIDs: Implement PermutationOf - This methods returns true if the content of the two sets of ColumnIDs is the same, even if they are not in the same order. Contrary to the existing Equal() method, which also requires order equality. descpb: IndexDescriptor uses PermutationOf for comparing columns - Previously when referring to an existing constraint, with a set of columns, the Equals() method was used. That method requires order equality to match. - Postgres recognises a constraint by reference even when the provided fields are not in the order they were given when creating it. - PermutationOf compares the column elements regardless of order. This means that the behaviour of a constraint reference matches that of Postgres. Fixes #63324 Release note (sql change): The use order of columns in a foreign key no longer needs to match the order the columns were defined for the reference table's unique constraint. Co-authored-by: Sam X Smith <[email protected]>
- Loading branch information
Showing
6 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,81 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package descpb | ||
|
||
import "testing" | ||
|
||
func TestColumnIDsPermutationOf(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
columnIDsOne, columnIDsTwo ColumnIDs | ||
expectedResult bool | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "different IDs", | ||
columnIDsOne: ColumnIDs{ColumnID(1), ColumnID(2)}, | ||
columnIDsTwo: ColumnIDs{ColumnID(3), ColumnID(4)}, | ||
expectedResult: false, | ||
}, | ||
{ | ||
name: "one element extra", | ||
columnIDsOne: ColumnIDs{ColumnID(1), ColumnID(2)}, | ||
columnIDsTwo: ColumnIDs{ColumnID(1), ColumnID(2), ColumnID(3)}, | ||
expectedResult: false, | ||
}, | ||
{ | ||
name: "one element less", | ||
columnIDsOne: ColumnIDs{ColumnID(1), ColumnID(2), ColumnID(3)}, | ||
columnIDsTwo: ColumnIDs{ColumnID(1), ColumnID(2)}, | ||
expectedResult: false, | ||
}, | ||
{ | ||
name: "same elements, in different order", | ||
columnIDsOne: ColumnIDs{ColumnID(1), ColumnID(2), ColumnID(3)}, | ||
columnIDsTwo: ColumnIDs{ColumnID(3), ColumnID(2), ColumnID(1)}, | ||
expectedResult: true, | ||
}, | ||
{ | ||
name: "when duplicate is in both, returns true", | ||
columnIDsOne: ColumnIDs{ColumnID(1), ColumnID(2), ColumnID(1), ColumnID(3)}, | ||
columnIDsTwo: ColumnIDs{ColumnID(3), ColumnID(1), ColumnID(2), ColumnID(1)}, | ||
expectedResult: true, | ||
}, | ||
{ | ||
name: "when duplicate in first, returns true", | ||
columnIDsOne: ColumnIDs{ColumnID(1), ColumnID(2), ColumnID(1), ColumnID(3)}, | ||
columnIDsTwo: ColumnIDs{ColumnID(3), ColumnID(2), ColumnID(1)}, | ||
expectedResult: true, | ||
}, | ||
{ | ||
name: "when duplicate in second, returns true", | ||
columnIDsOne: ColumnIDs{ColumnID(2), ColumnID(1), ColumnID(3)}, | ||
columnIDsTwo: ColumnIDs{ColumnID(3), ColumnID(1), ColumnID(2), ColumnID(1)}, | ||
expectedResult: true, | ||
}, | ||
{ | ||
name: "when each list has a different duplicate, same length, returns true", | ||
columnIDsOne: ColumnIDs{ColumnID(2), ColumnID(1), ColumnID(2)}, | ||
columnIDsTwo: ColumnIDs{ColumnID(1), ColumnID(2), ColumnID(1)}, | ||
expectedResult: true, | ||
}, | ||
} | ||
|
||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := tt.columnIDsOne.PermutationOf(tt.columnIDsTwo) | ||
if result != tt.expectedResult { | ||
t.Errorf("PermuationOf() %s: got %v, want %v", tt.name, result, tt.expectedResult) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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