Skip to content

Commit

Permalink
Merge pull request #8479 from dolthub/zachmu/fk-debug
Browse files Browse the repository at this point in the history
cross-schema foreign key support for doltgres tables
  • Loading branch information
zachmu authored Oct 25, 2024
2 parents 486e50f + 45fcccd commit 1bf5606
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ require (
github.com/cespare/xxhash/v2 v2.2.0
github.com/creasty/defaults v1.6.0
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
github.com/dolthub/go-mysql-server v0.18.2-0.20241024211034-824fa1df0200
github.com/dolthub/go-mysql-server v0.18.2-0.20241024233927-96a484368bea
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63
github.com/dolthub/swiss v0.1.0
github.com/goccy/go-json v0.10.2
Expand Down
4 changes: 2 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,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-20240916130659-0118adc6b662 h1:aC17hZD6iwzBwwfO5M+3oBT5E5gGRiQPdn+vzpDXqIA=
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662/go.mod h1:KPUcpx070QOfJK1gNe0zx4pA5sicIK1GMikIGLKC168=
github.com/dolthub/go-mysql-server v0.18.2-0.20241024211034-824fa1df0200 h1:sNx8JlWmUb3bPEa3mMAIVrShAwTpxxc1wxT245JVJx8=
github.com/dolthub/go-mysql-server v0.18.2-0.20241024211034-824fa1df0200/go.mod h1:z/GGuH2asedC+lkJA4sx+C3oyRH1HRx8ET6N9AGBVms=
github.com/dolthub/go-mysql-server v0.18.2-0.20241024233927-96a484368bea h1:bqHfelpj4LTC4RQnnFeC8KiXda90E0YfG0Elg5AqV0w=
github.com/dolthub/go-mysql-server v0.18.2-0.20241024233927-96a484368bea/go.mod h1:z/GGuH2asedC+lkJA4sx+C3oyRH1HRx8ET6N9AGBVms=
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=
Expand Down
17 changes: 17 additions & 0 deletions go/libraries/doltcore/sqle/clusterdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package clusterdb
import (
"context"
"errors"
"fmt"
"strings"

"github.com/dolthub/go-mysql-server/sql"
Expand Down Expand Up @@ -192,3 +193,19 @@ func (n noopRepoStateWriter) TempTableFilesDir() (string, error) {
func (n noopRepoStateWriter) UpdateBranch(name string, new env.BranchConfig) error {
return nil
}

func (db database) GetSchema(ctx *sql.Context, schemaName string) (sql.DatabaseSchema, bool, error) {
panic(fmt.Sprintf("GetSchema is not implemented for database %T", db))
}

func (db database) CreateSchema(ctx *sql.Context, schemaName string) error {
panic(fmt.Sprintf("CreateSchema is not implemented for database %T", db))
}

func (db database) AllSchemas(ctx *sql.Context) ([]sql.DatabaseSchema, error) {
panic(fmt.Sprintf("AllSchemas is not implemented for database %T", db))
}

func (db database) SchemaName() string {
return ""
}
2 changes: 2 additions & 0 deletions go/libraries/doltcore/sqle/dsess/session_db_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ type SessionDatabaseBranchSpec struct {

type SqlDatabase interface {
sql.Database
sql.SchemaDatabase
sql.DatabaseSchema
SessionDatabase
RevisionDatabase

Expand Down
26 changes: 15 additions & 11 deletions go/libraries/doltcore/sqle/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (t *DoltTable) TableName() doltdb.TableName {
return doltdb.TableName{Name: t.tableName, Schema: t.db.Schema()}
}

func (t *DoltTable) DatabaseSchema() sql.DatabaseSchema {
return t.db
}

func (t *DoltTable) SkipIndexCosting() bool {
return false
}
Expand Down Expand Up @@ -291,6 +295,7 @@ type doltReadOnlyTableInterface interface {
sql.CheckTable
sql.PrimaryKeyTable
sql.CommentedTable
sql.DatabaseSchemaTable
}

var _ doltReadOnlyTableInterface = (*DoltTable)(nil)
Expand Down Expand Up @@ -1258,10 +1263,12 @@ func (t *DoltTable) GetDeclaredForeignKeys(ctx *sql.Context) ([]sql.ForeignKeyCo
toReturn[i] = sql.ForeignKeyConstraint{
Name: fk.Name,
Database: t.db.Name(),
Table: fk.TableName.Name, // TODO: schema name
Table: fk.TableName.Name,
SchemaName: fk.TableName.Schema,
Columns: fk.UnresolvedFKDetails.TableColumns,
ParentDatabase: t.db.Name(),
ParentTable: fk.ReferencedTableName.Name, // TODO: schema name
ParentTable: fk.ReferencedTableName.Name,
ParentSchema: fk.ReferencedTableName.Schema,
ParentColumns: fk.UnresolvedFKDetails.ReferencedTableColumns,
OnUpdate: toReferentialAction(fk.OnUpdate),
OnDelete: toReferentialAction(fk.OnDelete),
Expand Down Expand Up @@ -2632,13 +2639,12 @@ func (t *WritableDoltTable) createForeignKey(
onUpdateRefAction, onDeleteRefAction doltdb.ForeignKeyReferentialAction) (doltdb.ForeignKey, error) {

if !sqlFk.IsResolved {
// TODO: schema should be part of the foreign key defn
return doltdb.ForeignKey{
Name: sqlFk.Name,
TableName: doltdb.TableName{Name: sqlFk.Table, Schema: t.db.SchemaName()},
TableName: doltdb.TableName{Name: sqlFk.Table, Schema: sqlFk.SchemaName},
TableIndex: "",
TableColumns: nil,
ReferencedTableName: doltdb.TableName{Name: sqlFk.ParentTable, Schema: t.db.SchemaName()},
ReferencedTableName: doltdb.TableName{Name: sqlFk.ParentTable, Schema: sqlFk.ParentSchema},
ReferencedTableIndex: "",
ReferencedTableColumns: nil,
OnUpdate: onUpdateRefAction,
Expand Down Expand Up @@ -2666,9 +2672,7 @@ func (t *WritableDoltTable) createForeignKey(
} else {
var ok bool
var err error
// TODO: the parent table can be in another schema

refTbl, _, ok, err = doltdb.GetTableInsensitive(ctx, root, doltdb.TableName{Name: sqlFk.ParentTable, Schema: t.db.schemaName})
refTbl, _, ok, err = doltdb.GetTableInsensitive(ctx, root, doltdb.TableName{Name: sqlFk.ParentTable, Schema: sqlFk.ParentSchema})
if err != nil {
return doltdb.ForeignKey{}, err
}
Expand Down Expand Up @@ -2708,13 +2712,12 @@ func (t *WritableDoltTable) createForeignKey(
refTableIndexName = refTableIndex.Name()
}

// TODO: foreign key defn should include schema names
return doltdb.ForeignKey{
Name: sqlFk.Name,
TableName: doltdb.TableName{Name: sqlFk.Table, Schema: t.db.SchemaName()},
TableIndex: tableIndexName,
TableColumns: colTags,
ReferencedTableName: doltdb.TableName{Name: sqlFk.ParentTable, Schema: t.db.SchemaName()},
ReferencedTableName: doltdb.TableName{Name: sqlFk.ParentTable, Schema: sqlFk.ParentSchema},
ReferencedTableIndex: refTableIndexName,
ReferencedTableColumns: refColTags,
OnUpdate: onUpdateRefAction,
Expand Down Expand Up @@ -2917,13 +2920,14 @@ func (t *AlterableDoltTable) CreateIndexForForeignKey(ctx *sql.Context, idx sql.
// toForeignKeyConstraint converts a Dolt resolved foreign key to a GMS foreign key. If the key is unresolved, then this
// function should not be used.
func toForeignKeyConstraint(fk doltdb.ForeignKey, dbName string, childSch, parentSch schema.Schema) (cst sql.ForeignKeyConstraint, err error) {
// TODO: foreign key defn should include schema name
cst = sql.ForeignKeyConstraint{
Name: fk.Name,
Database: dbName,
SchemaName: fk.TableName.Schema,
Table: fk.TableName.Name,
Columns: make([]string, len(fk.TableColumns)),
ParentDatabase: dbName,
ParentSchema: fk.ReferencedTableName.Schema,
ParentTable: fk.ReferencedTableName.Name,
ParentColumns: make([]string, len(fk.ReferencedTableColumns)),
OnUpdate: toReferentialAction(fk.OnUpdate),
Expand Down
18 changes: 18 additions & 0 deletions go/libraries/doltcore/sqle/user_space_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package sqle

import (
"fmt"

"github.com/dolthub/go-mysql-server/sql"

"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
Expand Down Expand Up @@ -137,3 +139,19 @@ func (db *UserSpaceDatabase) RevisionQualifiedName() string {
func (db *UserSpaceDatabase) RequestedName() string {
return db.Name()
}

func (db *UserSpaceDatabase) GetSchema(ctx *sql.Context, schemaName string) (sql.DatabaseSchema, bool, error) {
panic(fmt.Sprintf("GetSchema is not implemented for database %T", db))
}

func (db *UserSpaceDatabase) CreateSchema(ctx *sql.Context, schemaName string) error {
panic(fmt.Sprintf("CreateSchema is not implemented for database %T", db))
}

func (db *UserSpaceDatabase) AllSchemas(ctx *sql.Context) ([]sql.DatabaseSchema, error) {
panic(fmt.Sprintf("AllSchemas is not implemented for database %T", db))
}

func (db *UserSpaceDatabase) SchemaName() string {
return ""
}

0 comments on commit 1bf5606

Please sign in to comment.