From 2b625583468a87cf13d1178e5bc5fa7b910b49f0 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Mon, 3 Mar 2025 17:54:14 -0800 Subject: [PATCH 1/6] Loosened foreign key validation --- server/analyzer/init.go | 47 +++++++++++++++++++++++++++++++++ testing/go/foreign_keys_test.go | 20 ++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/server/analyzer/init.go b/server/analyzer/init.go index 2e1d605c8d..11da17c984 100644 --- a/server/analyzer/init.go +++ b/server/analyzer/init.go @@ -15,7 +15,12 @@ package analyzer import ( + "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/vitess/go/sqltypes" ) // IDs are basically arbitrary, we just need to ensure that they do not conflict with existing IDs @@ -86,6 +91,48 @@ func Init() { analyzer.Rule{Id: ruleId_AddDomainConstraintsToCasts, Apply: AddDomainConstraintsToCasts}, analyzer.Rule{Id: ruleId_ReplaceNode, Apply: ReplaceNode}, analyzer.Rule{Id: ruleId_InsertContextRootFinalizer, Apply: InsertContextRootFinalizer}) + + initEngine() +} + +func initEngine() { + plan.ValidateForeignKeyDefinition = validateForeignKeyDefinition +} + +// 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(ctx, col.Type, parentCol.Type) { + return sql.ErrForeignKeyColumnTypeMismatch.New(fkDef.Columns[i], fkDef.ParentColumns[i]) + } + } + return nil +} + +// foreignKeyComparableTypes returns whether the two given types are able to be used as parent/child columns in a +// foreign key. +func foreignKeyComparableTypes(ctx *sql.Context, type1 sql.Type, type2 sql.Type) bool { + if !type1.Equals(type2) { + // There seems to be a special case where CHAR/VARCHAR/BINARY/VARBINARY can have unequal lengths. + // Have not tested every type nor combination, but this seems specific to those 4 types. + if type1.Type() == type2.Type() { + switch type1.Type() { + case sqltypes.Char, sqltypes.VarChar, sqltypes.Binary, sqltypes.VarBinary: + type1String := type1.(sql.StringType) + type2String := type2.(sql.StringType) + if type1String.Collation().CharacterSet() != type2String.Collation().CharacterSet() { + return false + } + default: + return false + } + } else { + return false + } + } + return true } // insertAnalyzerRules inserts the given rule(s) before or after the given analyzer.RuleId, returning an updated slice. diff --git a/testing/go/foreign_keys_test.go b/testing/go/foreign_keys_test.go index aee7ca707a..7b659ca21b 100755 --- a/testing/go/foreign_keys_test.go +++ b/testing/go/foreign_keys_test.go @@ -44,6 +44,26 @@ func TestForeignKeys(t *testing.T) { }, }, }, + { + Name: "text foreign key", + SetUpScript: []string{ + `CREATE TABLE parent (a text PRIMARY KEY, b int)`, + `CREATE TABLE child (a INT PRIMARY KEY, b text, FOREIGN KEY (b) REFERENCES parent(a))`, + `INSERT INTO parent VALUES ('a', 1)`, + }, + Assertions: []ScriptTestAssertion{ + { + Query: "INSERT INTO child VALUES (1, 'a')", + }, + { + Query: "INSERT INTO child VALUES (2, 'a')", + }, + { + Query: "INSERT INTO child VALUES (3, 'b')", + ExpectedErr: "Foreign key violation", + }, + }, + }, { Name: "foreign key with dolt_add, dolt_commit", SetUpScript: []string{ From 2713aecd520e9bebc7c43c00bcbd7191be824ba4 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Thu, 6 Mar 2025 12:52:59 -0800 Subject: [PATCH 2/6] Added a comment --- server/analyzer/init.go | 1 + 1 file changed, 1 insertion(+) diff --git a/server/analyzer/init.go b/server/analyzer/init.go index 11da17c984..dadee96424 100644 --- a/server/analyzer/init.go +++ b/server/analyzer/init.go @@ -101,6 +101,7 @@ func initEngine() { // 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 { + // TODO: this check is too permissive, we should be doing some type checks here for i := range fkDef.Columns { col := cols[strings.ToLower(fkDef.Columns[i])] parentCol := parentCols[strings.ToLower(fkDef.ParentColumns[i])] From 6d61577afb8ae9e5907ca993a658ad57154a88a6 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Thu, 6 Mar 2025 12:56:05 -0800 Subject: [PATCH 3/6] new gms --- go.mod | 2 +- go.sum | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 630c85f845..7c7678e769 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 github.com/dolthub/go-icu-regex v0.0.0-20250303123116-549b8d7cad00 - github.com/dolthub/go-mysql-server v0.19.1-0.20250306014046-f73a318f7731 + github.com/dolthub/go-mysql-server v0.19.1-0.20250306201826-505a850324ae github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216 github.com/dolthub/vitess v0.0.0-20250304211657-920ca9ec2b9a github.com/fatih/color v1.13.0 diff --git a/go.sum b/go.sum index 3e784c444d..fcc3a746f3 100644 --- a/go.sum +++ b/go.sum @@ -214,8 +214,6 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= -github.com/dolthub/dolt/go v0.40.5-0.20250306002834-757a7c6016fc h1:LgMQ7j9Ivm1+2PNPnCvl6aAqzJgjwiZRNYj2Qb4v/5c= -github.com/dolthub/dolt/go v0.40.5-0.20250306002834-757a7c6016fc/go.mod h1:+hDo/Xc/euo+w6aV1fwxCkrke6XiwZET73aWNM8I1WA= github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d h1:gO9+wrmNHXukPNCO1tpfCcXIdMlW/qppbUStfLvqz/U= github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d/go.mod h1:L5RDYZbC9BBWmoU2+TjTekeqqhFXX5EqH9ln00O0stY= github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 h1:u3PMzfF8RkKd3lB9pZ2bfn0qEG+1Gms9599cr0REMww= @@ -224,8 +222,6 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U= github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0= github.com/dolthub/go-icu-regex v0.0.0-20250303123116-549b8d7cad00 h1:rh2ij2yTYKJWlX+c8XRg4H5OzqPewbU1lPK8pcfVmx8= github.com/dolthub/go-icu-regex v0.0.0-20250303123116-549b8d7cad00/go.mod h1:ylU4XjUpsMcvl/BKeRRMXSH7e7WBrPXdSLvnRJYrxEA= -github.com/dolthub/go-mysql-server v0.19.1-0.20250306014046-f73a318f7731 h1:flDUXUqKRo4u5gdoQZBeO3jESUnCNkv01GDmiZbgAA4= -github.com/dolthub/go-mysql-server v0.19.1-0.20250306014046-f73a318f7731/go.mod h1:yr+Vv47/YLOKMgiEY+QxHTlbIVpTuiVtkEZ5l+xruY4= github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI= github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q= github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE= @@ -234,8 +230,6 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI= github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216 h1:JWkKRE4EHUcEVQCMRBej8DYxjYjRz/9MdF/NNQh0o70= github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216/go.mod h1:e/FIZVvT2IR53HBCAo41NjqgtEnjMJGKca3Y/dAmZaA= -github.com/dolthub/vitess v0.0.0-20250304211657-920ca9ec2b9a h1:HIH9g4z+yXr4DIFyT6L5qOIEGJ1zVtlj6baPyHAG4Yw= -github.com/dolthub/vitess v0.0.0-20250304211657-920ca9ec2b9a/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= From 509be1159c7123ff976f02b20223e680b74575c4 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Thu, 6 Mar 2025 12:58:58 -0800 Subject: [PATCH 4/6] updated go.sum --- go.sum | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/go.sum b/go.sum index fcc3a746f3..7ac8ec1107 100644 --- a/go.sum +++ b/go.sum @@ -214,6 +214,8 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/dolthub/dolt/go v0.40.5-0.20250306002834-757a7c6016fc h1:LgMQ7j9Ivm1+2PNPnCvl6aAqzJgjwiZRNYj2Qb4v/5c= +github.com/dolthub/dolt/go v0.40.5-0.20250306002834-757a7c6016fc/go.mod h1:+hDo/Xc/euo+w6aV1fwxCkrke6XiwZET73aWNM8I1WA= github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d h1:gO9+wrmNHXukPNCO1tpfCcXIdMlW/qppbUStfLvqz/U= github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d/go.mod h1:L5RDYZbC9BBWmoU2+TjTekeqqhFXX5EqH9ln00O0stY= github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 h1:u3PMzfF8RkKd3lB9pZ2bfn0qEG+1Gms9599cr0REMww= @@ -222,6 +224,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U= github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0= github.com/dolthub/go-icu-regex v0.0.0-20250303123116-549b8d7cad00 h1:rh2ij2yTYKJWlX+c8XRg4H5OzqPewbU1lPK8pcfVmx8= github.com/dolthub/go-icu-regex v0.0.0-20250303123116-549b8d7cad00/go.mod h1:ylU4XjUpsMcvl/BKeRRMXSH7e7WBrPXdSLvnRJYrxEA= +github.com/dolthub/go-mysql-server v0.19.1-0.20250306201826-505a850324ae h1:BrTbZcJfiRPH+F5/aqsaL06nm5IYnPirJBjUQJkRL0A= +github.com/dolthub/go-mysql-server v0.19.1-0.20250306201826-505a850324ae/go.mod h1:yr+Vv47/YLOKMgiEY+QxHTlbIVpTuiVtkEZ5l+xruY4= github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI= github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q= github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE= @@ -230,6 +234,8 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI= github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216 h1:JWkKRE4EHUcEVQCMRBej8DYxjYjRz/9MdF/NNQh0o70= github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216/go.mod h1:e/FIZVvT2IR53HBCAo41NjqgtEnjMJGKca3Y/dAmZaA= +github.com/dolthub/vitess v0.0.0-20250304211657-920ca9ec2b9a h1:HIH9g4z+yXr4DIFyT6L5qOIEGJ1zVtlj6baPyHAG4Yw= +github.com/dolthub/vitess v0.0.0-20250304211657-920ca9ec2b9a/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= From 38c5b5555742e435f273c8b69d1c4457f4644a81 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Thu, 6 Mar 2025 12:59:54 -0800 Subject: [PATCH 5/6] formatting --- server/analyzer/init.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/analyzer/init.go b/server/analyzer/init.go index dadee96424..27a65c4428 100644 --- a/server/analyzer/init.go +++ b/server/analyzer/init.go @@ -91,7 +91,7 @@ func Init() { analyzer.Rule{Id: ruleId_AddDomainConstraintsToCasts, Apply: AddDomainConstraintsToCasts}, analyzer.Rule{Id: ruleId_ReplaceNode, Apply: ReplaceNode}, analyzer.Rule{Id: ruleId_InsertContextRootFinalizer, Apply: InsertContextRootFinalizer}) - + initEngine() } From 24130f3d919f5838e3a09f31e98a89fa2759357d Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Fri, 7 Mar 2025 08:22:19 -0800 Subject: [PATCH 6/6] New gms --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 7c7678e769..f4d415ae45 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/dolthub/dolt/go/gen/proto/dolt/services/eventsapi v0.0.0-20241119094239-f4e529af734d github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 github.com/dolthub/go-icu-regex v0.0.0-20250303123116-549b8d7cad00 - github.com/dolthub/go-mysql-server v0.19.1-0.20250306201826-505a850324ae + github.com/dolthub/go-mysql-server v0.19.1-0.20250307161823-e8ce0df0d8f2 github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216 github.com/dolthub/vitess v0.0.0-20250304211657-920ca9ec2b9a github.com/fatih/color v1.13.0 diff --git a/go.sum b/go.sum index 7ac8ec1107..80e35540b7 100644 --- a/go.sum +++ b/go.sum @@ -224,8 +224,8 @@ github.com/dolthub/fslock v0.0.3 h1:iLMpUIvJKMKm92+N1fmHVdxJP5NdyDK5bK7z7Ba2s2U= github.com/dolthub/fslock v0.0.3/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0= github.com/dolthub/go-icu-regex v0.0.0-20250303123116-549b8d7cad00 h1:rh2ij2yTYKJWlX+c8XRg4H5OzqPewbU1lPK8pcfVmx8= github.com/dolthub/go-icu-regex v0.0.0-20250303123116-549b8d7cad00/go.mod h1:ylU4XjUpsMcvl/BKeRRMXSH7e7WBrPXdSLvnRJYrxEA= -github.com/dolthub/go-mysql-server v0.19.1-0.20250306201826-505a850324ae h1:BrTbZcJfiRPH+F5/aqsaL06nm5IYnPirJBjUQJkRL0A= -github.com/dolthub/go-mysql-server v0.19.1-0.20250306201826-505a850324ae/go.mod h1:yr+Vv47/YLOKMgiEY+QxHTlbIVpTuiVtkEZ5l+xruY4= +github.com/dolthub/go-mysql-server v0.19.1-0.20250307161823-e8ce0df0d8f2 h1:5AAJTJWaiYO1ut8TkEKbeBTp3x0UIOd1yV9nC1s3vjg= +github.com/dolthub/go-mysql-server v0.19.1-0.20250307161823-e8ce0df0d8f2/go.mod h1:yr+Vv47/YLOKMgiEY+QxHTlbIVpTuiVtkEZ5l+xruY4= github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI= github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q= github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=