Skip to content

Commit 611dd73

Browse files
author
James Cor
committed
merge with main
2 parents 523207e + b02d6d5 commit 611dd73

File tree

18 files changed

+1304
-1093
lines changed

18 files changed

+1304
-1093
lines changed

enginetest/queries/foreign_key_queries.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ var ForeignKeyTests = []ScriptTest{
134134
},
135135
},
136136
{
137+
// MySQL parses the SET DEFAULT referential action, but most engines, e.g. InnoDB, don't actually support it
137138
Name: "SET DEFAULT not supported",
138139
Assertions: []ScriptTestAssertion{
139140
{

enginetest/queries/queries.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6439,6 +6439,10 @@ SELECT * FROM cte WHERE d = 2;`,
64396439
Query: "SELECT FROM_BASE64('YmFy')",
64406440
Expected: []sql.Row{{[]byte("bar")}},
64416441
},
6442+
{
6443+
Query: "SELECT TIMESTAMPADD(DAY, 1, '2018-05-02')",
6444+
Expected: []sql.Row{{"2018-05-03"}},
6445+
},
64426446
{
64436447
Query: "SELECT DATE_ADD('2018-05-02', INTERVAL 1 day)",
64446448
Expected: []sql.Row{{"2018-05-03"}},

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/dolthub/go-icu-regex v0.0.0-20250327004329-6799764f2dad
77
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71
88
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
9-
github.com/dolthub/vitess v0.0.0-20250414213151-810e7add1b8e
9+
github.com/dolthub/vitess v0.0.0-20250417230335-b8d80bc39341
1010
github.com/go-kit/kit v0.10.0
1111
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
1212
github.com/gocraft/dbr/v2 v2.7.2

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE
5858
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI=
5959
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE=
6060
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
61-
github.com/dolthub/vitess v0.0.0-20250414213151-810e7add1b8e h1:EASQ7Rjk4lbQjwqQaBmjjGxpbRRQqlnudEdmJZGjk/A=
62-
github.com/dolthub/vitess v0.0.0-20250414213151-810e7add1b8e/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
61+
github.com/dolthub/vitess v0.0.0-20250410233614-8d8c7a5b3d6b h1:2wE+qJwJ5SRIzz+dJQT8XbkpK+g8/pFt34AU/iJ5K+Y=
62+
github.com/dolthub/vitess v0.0.0-20250410233614-8d8c7a5b3d6b/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
63+
github.com/dolthub/vitess v0.0.0-20250414165810-f0031a6472b7 h1:4Y043kZgAH1WhOER0nk+02KPKxJX8Ir6yK7cGzY04c4=
64+
github.com/dolthub/vitess v0.0.0-20250414165810-f0031a6472b7/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
65+
github.com/dolthub/vitess v0.0.0-20250417230335-b8d80bc39341 h1:qebIGlJEgi/mSXVZ39P77cklPuuIl8gApyTVMnKm79s=
66+
github.com/dolthub/vitess v0.0.0-20250417230335-b8d80bc39341/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
6367
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
6468
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
6569
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=

sql/analyzer/apply_foreign_keys.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import (
2121
"github.com/dolthub/go-mysql-server/sql/transform"
2222

2323
"github.com/dolthub/go-mysql-server/sql"
24+
"github.com/dolthub/go-mysql-server/sql/expression"
2425
"github.com/dolthub/go-mysql-server/sql/plan"
26+
"github.com/dolthub/go-mysql-server/sql/planbuilder"
2527
)
2628

2729
// applyForeignKeys handles the application and resolution of foreign keys and their tables.
@@ -380,7 +382,19 @@ func getForeignKeyRefActions(ctx *sql.Context, a *Analyzer, tbl sql.ForeignKeyTa
380382
if err != nil {
381383
return nil, err
382384
}
383-
childTblSch := childTbl.Schema()
385+
386+
// TODO: Foreign key information is not fully added to the plan node until these FK rules in the analyzer,
387+
// but it should be added during the binding phase, in planbuilder. Because this information is added
388+
// late, we have to do extra work here to resolve the schema defaults, which normally would happen
389+
// during binding. This extra FK information also doesn't get its exec indexes fixed up, so we have to
390+
// manually do that here. Moving all the FK information into the binding, planbuilder, phase would
391+
// clean this up. We should also fix assignExecIndexes to find all these FK schema references and fix
392+
// their exec indexes.
393+
childTblSch, err := resolveSchemaDefaults(ctx, a.Catalog, childTbl)
394+
if err != nil {
395+
return nil, err
396+
}
397+
384398
childParentMapping, err := plan.GetChildParentMapping(tblSch, childTblSch, fk)
385399
if err != nil {
386400
return nil, err
@@ -429,6 +443,40 @@ func getForeignKeyRefActions(ctx *sql.Context, a *Analyzer, tbl sql.ForeignKeyTa
429443
return fkEditor, nil
430444
}
431445

446+
// resolveSchemaDefaults resolves the default values for the schema of |table|. This is primarily needed for column
447+
// default value expressions, since those don't get resolved during the planbuilder phase and assignExecIndexes
448+
// doesn't traverse through the ForeignKeyEditors and referential actions to find all of them. In addition to resolving
449+
// the expressions, this also ensures their GetField indexes are correct, knowing that those expressions will only
450+
// be evaluated in the context of a single table.
451+
func resolveSchemaDefaults(ctx *sql.Context, catalog *Catalog, table sql.Table) (sql.Schema, error) {
452+
// Resolve any column default expressions in tblSch
453+
builder := planbuilder.New(ctx, catalog, nil, sql.GlobalParser)
454+
childTblSch := builder.ResolveSchemaDefaults(ctx.GetCurrentDatabase(), table.Name(), table.Schema())
455+
456+
// Field Indexes are off by one initially and don't fixed by assignExecIndexes because it doesn't traverse through
457+
// the ForeignKeyEditors and referential actions, so we correct them here. This is safe because we know these fields
458+
// will only ever be accessed within the scope of a single table, so all we have to do is decrement the index by 1.
459+
for i, col := range childTblSch {
460+
if col.Default != nil {
461+
expr := col.Default.Expr
462+
expr, identity, err := transform.Expr(expr, func(e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
463+
if gf, ok := e.(*expression.GetField); ok {
464+
return gf.WithIndex(gf.Index() - 1), transform.NewTree, nil
465+
}
466+
return e, transform.SameTree, nil
467+
})
468+
if err != nil {
469+
return nil, err
470+
}
471+
if identity == transform.NewTree {
472+
childTblSch[i].Default.Expr = expr
473+
}
474+
}
475+
}
476+
477+
return childTblSch, nil
478+
}
479+
432480
// foreignKeyTableName is the combination of a table's database along with their name, both lowercased.
433481
type foreignKeyTableName struct {
434482
dbName string

sql/constraints.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const (
3636
// a number of referential actions, the majority of them are functionally ignored and default to RESTRICT.
3737
func (f ForeignKeyReferentialAction) IsEquivalentToRestrict() bool {
3838
switch f {
39-
case ForeignKeyReferentialAction_Cascade, ForeignKeyReferentialAction_SetNull:
39+
case ForeignKeyReferentialAction_Cascade, ForeignKeyReferentialAction_SetNull, ForeignKeyReferentialAction_SetDefault:
4040
return false
4141
default:
4242
return true

0 commit comments

Comments
 (0)