Skip to content

Commit

Permalink
Support multiple schema for the rest of the system tables with for do…
Browse files Browse the repository at this point in the history
…ltgres
  • Loading branch information
tbantle22 committed Nov 5, 2024
1 parent de1a434 commit acdeb8b
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 99 deletions.
49 changes: 44 additions & 5 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
// at runtime
switch {
case strings.HasPrefix(lwrName, doltdb.DoltDiffTablePrefix):
if resolve.UseSearchPath && db.schemaName == "" {
schemaName, err := resolve.FirstExistingSchemaOnSearchPath(ctx, root)
if err != nil {
return nil, false, err
}
db.schemaName = schemaName
}

if head == nil {
var err error
head, err = ds.GetHeadCommit(ctx, db.RevisionQualifiedName())
Expand All @@ -331,19 +339,27 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
}

tableName := tblName[len(doltdb.DoltDiffTablePrefix):]
dt, err := dtables.NewDiffTable(ctx, db.Name(), tableName, db.ddb, root, head)
dt, err := dtables.NewDiffTable(ctx, db.Name(), doltdb.TableName{Name: tableName, Schema: db.schemaName}, db.ddb, root, head)
if err != nil {
return nil, false, err
}
return dt, true, nil

case strings.HasPrefix(lwrName, doltdb.DoltCommitDiffTablePrefix):
if resolve.UseSearchPath && db.schemaName == "" {
schemaName, err := resolve.FirstExistingSchemaOnSearchPath(ctx, root)
if err != nil {
return nil, false, err
}
db.schemaName = schemaName
}

suffix := tblName[len(doltdb.DoltCommitDiffTablePrefix):]
ws, err := ds.WorkingSet(ctx, db.RevisionQualifiedName())
if err != nil {
return nil, false, err
}
dt, err := dtables.NewCommitDiffTable(ctx, db.Name(), suffix, db.ddb, root, ws.StagedRoot())
dt, err := dtables.NewCommitDiffTable(ctx, db.Name(), doltdb.TableName{Name: suffix, Schema: db.schemaName}, db.ddb, root, ws.StagedRoot())
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -385,27 +401,50 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
}

case strings.HasPrefix(lwrName, doltdb.DoltConfTablePrefix):
if resolve.UseSearchPath && db.schemaName == "" {
schemaName, err := resolve.FirstExistingSchemaOnSearchPath(ctx, root)
if err != nil {
return nil, false, err
}
db.schemaName = schemaName
}

suffix := tblName[len(doltdb.DoltConfTablePrefix):]
srcTable, ok, err := db.getTableInsensitive(ctx, head, ds, root, suffix, asOf)
if err != nil {
return nil, false, err
} else if !ok {
return nil, false, nil
}
dt, err := dtables.NewConflictsTable(ctx, suffix, srcTable, root, dtables.RootSetter(db))
dt, err := dtables.NewConflictsTable(ctx, doltdb.TableName{Name: suffix, Schema: db.schemaName}, srcTable, root, dtables.RootSetter(db))
if err != nil {
return nil, false, err
}
return dt, true, nil

case strings.HasPrefix(lwrName, doltdb.DoltConstViolTablePrefix):
if resolve.UseSearchPath && db.schemaName == "" {
schemaName, err := resolve.FirstExistingSchemaOnSearchPath(ctx, root)
if err != nil {
return nil, false, err
}
db.schemaName = schemaName
}

suffix := tblName[len(doltdb.DoltConstViolTablePrefix):]
dt, err := dtables.NewConstraintViolationsTable(ctx, suffix, root, dtables.RootSetter(db))
dt, err := dtables.NewConstraintViolationsTable(ctx, doltdb.TableName{Name: suffix, Schema: db.schemaName}, root, dtables.RootSetter(db))
if err != nil {
return nil, false, err
}
return dt, true, nil
case strings.HasPrefix(lwrName, doltdb.DoltWorkspaceTablePrefix):
if resolve.UseSearchPath && db.schemaName == "" {
schemaName, err := resolve.FirstExistingSchemaOnSearchPath(ctx, root)
if err != nil {
return nil, false, err
}
db.schemaName = schemaName
}
sess := dsess.DSessFromSess(ctx.Session)

ws, err := sess.WorkingSet(ctx, db.RevisionQualifiedName())
Expand All @@ -418,7 +457,7 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds

userTable := tblName[len(doltdb.DoltWorkspaceTablePrefix):]

dt, err := dtables.NewWorkspaceTable(ctx, tblName, userTable, head, ws)
dt, err := dtables.NewWorkspaceTable(ctx, tblName, doltdb.TableName{Name: userTable, Schema: db.schemaName}, head, ws)
if err != nil {
return nil, false, err
}
Expand Down
21 changes: 10 additions & 11 deletions go/libraries/doltcore/sqle/dtables/commit_diff_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/rowconv"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
"github.com/dolthub/dolt/go/store/types"
)
Expand All @@ -39,7 +38,7 @@ var ErrExactlyOneFromCommit = errors.New("dolt_commit_diff_* tables must be filt
var ErrInvalidCommitDiffTableArgs = errors.New("commit_diff_<table> requires one 'to_commit' and one 'from_commit'")

type CommitDiffTable struct {
name string
tableName doltdb.TableName
dbName string
ddb *doltdb.DoltDB
joiner *rowconv.Joiner
Expand All @@ -58,10 +57,10 @@ var _ sql.Table = (*CommitDiffTable)(nil)
var _ sql.IndexAddressable = (*CommitDiffTable)(nil)
var _ sql.StatisticsTable = (*CommitDiffTable)(nil)

func NewCommitDiffTable(ctx *sql.Context, dbName, tblName string, ddb *doltdb.DoltDB, wRoot, sRoot doltdb.RootValue) (sql.Table, error) {
diffTblName := doltdb.DoltCommitDiffTablePrefix + tblName
func NewCommitDiffTable(ctx *sql.Context, dbName string, tblName doltdb.TableName, ddb *doltdb.DoltDB, wRoot, sRoot doltdb.RootValue) (sql.Table, error) {
diffTblName := doltdb.DoltCommitDiffTablePrefix + tblName.Name

_, table, tableExists, err := resolve.Table(ctx, wRoot, tblName)
table, tableExists, err := wRoot.GetTable(ctx, tblName)
if err != nil {
return nil, err
}
Expand All @@ -86,7 +85,7 @@ func NewCommitDiffTable(ctx *sql.Context, dbName, tblName string, ddb *doltdb.Do

return &CommitDiffTable{
dbName: dbName,
name: tblName,
tableName: tblName,
ddb: ddb,
workingRoot: wRoot,
stagedRoot: sRoot,
Expand All @@ -110,11 +109,11 @@ func (dt *CommitDiffTable) RowCount(_ *sql.Context) (uint64, bool, error) {
}

func (dt *CommitDiffTable) Name() string {
return doltdb.DoltCommitDiffTablePrefix + dt.name
return doltdb.DoltCommitDiffTablePrefix + dt.tableName.Name
}

func (dt *CommitDiffTable) String() string {
return doltdb.DoltCommitDiffTablePrefix + dt.name
return doltdb.DoltCommitDiffTablePrefix + dt.tableName.Name
}

func (dt *CommitDiffTable) Schema() sql.Schema {
Expand All @@ -128,7 +127,7 @@ func (dt *CommitDiffTable) Collation() sql.CollationID {

// GetIndexes implements sql.IndexAddressable
func (dt *CommitDiffTable) GetIndexes(ctx *sql.Context) ([]sql.Index, error) {
return []sql.Index{index.DoltToFromCommitIndex(dt.name)}, nil
return []sql.Index{index.DoltToFromCommitIndex(dt.tableName.Name)}, nil
}

// IndexedAccess implements sql.IndexAddressable
Expand Down Expand Up @@ -197,12 +196,12 @@ func (dt *CommitDiffTable) LookupPartitions(ctx *sql.Context, i sql.IndexLookup)
return nil, err
}

toTable, _, _, err := doltdb.GetTableInsensitive(ctx, toRoot, doltdb.TableName{Name: dt.name})
toTable, _, _, err := doltdb.GetTableInsensitive(ctx, toRoot, dt.tableName)
if err != nil {
return nil, err
}

fromTable, _, _, err := doltdb.GetTableInsensitive(ctx, fromRoot, doltdb.TableName{Name: dt.name})
fromTable, _, _, err := doltdb.GetTableInsensitive(ctx, fromRoot, dt.tableName)
if err != nil {
return nil, err
}
Expand Down
26 changes: 12 additions & 14 deletions go/libraries/doltcore/sqle/dtables/conflicts_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/merge"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
"github.com/dolthub/dolt/go/store/types"
)

// NewConflictsTable returns a new ConflictsTable instance
func NewConflictsTable(ctx *sql.Context, tblName string, srcTbl sql.Table, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
resolvedTableName, tbl, ok, err := resolve.Table(ctx, root, tblName)
func NewConflictsTable(ctx *sql.Context, tblName doltdb.TableName, srcTbl sql.Table, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
tbl, ok, err := root.GetTable(ctx, tblName)
if err != nil {
return nil, err
} else if !ok {
Expand All @@ -42,20 +41,20 @@ func NewConflictsTable(ctx *sql.Context, tblName string, srcTbl sql.Table, root
if !ok {
return nil, fmt.Errorf("%s can not have conflicts because it is not updateable", tblName)
}
return newProllyConflictsTable(ctx, tbl, upd, resolvedTableName, root, rs)
return newProllyConflictsTable(ctx, tbl, upd, tblName, root, rs)
}

return newNomsConflictsTable(ctx, tbl, resolvedTableName.Name, root, rs)
return newNomsConflictsTable(ctx, tbl, tblName, root, rs)
}

func newNomsConflictsTable(ctx *sql.Context, tbl *doltdb.Table, tblName string, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
rd, err := merge.NewConflictReader(ctx, tbl, doltdb.TableName{Name: tblName})
func newNomsConflictsTable(ctx *sql.Context, tbl *doltdb.Table, tblName doltdb.TableName, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
rd, err := merge.NewConflictReader(ctx, tbl, tblName)
if err != nil {
return nil, err
}
confSch := rd.GetSchema()

sqlSch, err := sqlutil.FromDoltSchema("", doltdb.DoltConfTablePrefix+tblName, confSch)
sqlSch, err := sqlutil.FromDoltSchema("", doltdb.DoltConfTablePrefix+tblName.Name, confSch)
if err != nil {
return nil, err
}
Expand All @@ -75,7 +74,7 @@ var _ sql.DeletableTable = ConflictsTable{}

// ConflictsTable is a sql.Table implementation that provides access to the conflicts that exist for a user table
type ConflictsTable struct {
tblName string
tblName doltdb.TableName
sqlSch sql.PrimaryKeySchema
root doltdb.RootValue
tbl *doltdb.Table
Expand All @@ -89,12 +88,12 @@ type RootSetter interface {

// Name returns the name of the table
func (ct ConflictsTable) Name() string {
return doltdb.DoltConfTablePrefix + ct.tblName
return doltdb.DoltConfTablePrefix + ct.tblName.Name
}

// String returns a string identifying the table
func (ct ConflictsTable) String() string {
return doltdb.DoltConfTablePrefix + ct.tblName
return doltdb.DoltConfTablePrefix + ct.tblName.Name
}

// Schema returns the sql.Schema of the table
Expand All @@ -115,8 +114,7 @@ func (ct ConflictsTable) Partitions(ctx *sql.Context) (sql.PartitionIter, error)
// PartitionRows returns a RowIter for the given partition
func (ct ConflictsTable) PartitionRows(ctx *sql.Context, part sql.Partition) (sql.RowIter, error) {
// conflict reader must be reset each time partitionRows is called.
// TODO: schema name
rd, err := merge.NewConflictReader(ctx, ct.tbl, doltdb.TableName{Name: ct.tblName})
rd, err := merge.NewConflictReader(ctx, ct.tbl, ct.tblName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -205,7 +203,7 @@ func (cd *conflictDeleter) Close(ctx *sql.Context) error {
return err
}

updatedRoot, err := cd.ct.root.PutTable(ctx, doltdb.TableName{Name: cd.ct.tblName}, updatedTbl)
updatedRoot, err := cd.ct.root.PutTable(ctx, cd.ct.tblName, updatedTbl)

if err != nil {
return err
Expand Down
16 changes: 8 additions & 8 deletions go/libraries/doltcore/sqle/dtables/constraint_violations.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ import (
)

// NewConstraintViolationsTable returns a sql.Table that lists constraint violations.
func NewConstraintViolationsTable(ctx *sql.Context, tblName string, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
func NewConstraintViolationsTable(ctx *sql.Context, tblName doltdb.TableName, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
if root.VRW().Format() == types.Format_DOLT {
return newProllyCVTable(ctx, tblName, root, rs)
}

return newNomsCVTable(ctx, tblName, root, rs)
}

func newNomsCVTable(ctx *sql.Context, tblName string, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
tbl, tblName, ok, err := doltdb.GetTableInsensitive(ctx, root, doltdb.TableName{Name: tblName})
func newNomsCVTable(ctx *sql.Context, tblName doltdb.TableName, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
tbl, _, ok, err := doltdb.GetTableInsensitive(ctx, root, tblName)
if err != nil {
return nil, err
} else if !ok {
Expand All @@ -46,7 +46,7 @@ func newNomsCVTable(ctx *sql.Context, tblName string, root doltdb.RootValue, rs
if err != nil {
return nil, err
}
sqlSch, err := sqlutil.FromDoltSchema("", doltdb.DoltConstViolTablePrefix+tblName, cvSch)
sqlSch, err := sqlutil.FromDoltSchema("", doltdb.DoltConstViolTablePrefix+tblName.Name, cvSch)
if err != nil {
return nil, err
}
Expand All @@ -64,7 +64,7 @@ func newNomsCVTable(ctx *sql.Context, tblName string, root doltdb.RootValue, rs
// constraintViolationsTable is a sql.Table implementation that provides access to the constraint violations that exist
// for a user table for the old format.
type constraintViolationsTable struct {
tblName string
tblName doltdb.TableName
root doltdb.RootValue
cvSch schema.Schema
sqlSch sql.PrimaryKeySchema
Expand All @@ -77,12 +77,12 @@ var _ sql.DeletableTable = (*constraintViolationsTable)(nil)

// Name implements the interface sql.Table.
func (cvt *constraintViolationsTable) Name() string {
return doltdb.DoltConstViolTablePrefix + cvt.tblName
return doltdb.DoltConstViolTablePrefix + cvt.tblName.Name
}

// String implements the interface sql.Table.
func (cvt *constraintViolationsTable) String() string {
return doltdb.DoltConstViolTablePrefix + cvt.tblName
return doltdb.DoltConstViolTablePrefix + cvt.tblName.Name
}

// Schema implements the interface sql.Table.
Expand Down Expand Up @@ -193,7 +193,7 @@ func (cvd *constraintViolationsDeleter) Close(ctx *sql.Context) error {
if err != nil {
return err
}
updatedRoot, err := cvd.cvt.root.PutTable(ctx, doltdb.TableName{Name: cvd.cvt.tblName}, updatedTbl)
updatedRoot, err := cvd.cvt.root.PutTable(ctx, cvd.cvt.tblName, updatedTbl)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/merge"
"github.com/dolthub/dolt/go/libraries/doltcore/schema"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/resolve"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/sqlutil"
"github.com/dolthub/dolt/go/store/hash"
"github.com/dolthub/dolt/go/store/pool"
Expand All @@ -33,8 +32,8 @@ import (
"github.com/dolthub/dolt/go/store/val"
)

func newProllyCVTable(ctx *sql.Context, tblName string, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
resolvedName, tbl, ok, err := resolve.Table(ctx, root, tblName)
func newProllyCVTable(ctx *sql.Context, tblName doltdb.TableName, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
tbl, ok, err := root.GetTable(ctx, tblName)
if err != nil {
return nil, err
} else if !ok {
Expand All @@ -44,7 +43,7 @@ func newProllyCVTable(ctx *sql.Context, tblName string, root doltdb.RootValue, r
if err != nil {
return nil, err
}
sqlSch, err := sqlutil.FromDoltSchema("", doltdb.DoltConstViolTablePrefix+resolvedName.Name, cvSch)
sqlSch, err := sqlutil.FromDoltSchema("", doltdb.DoltConstViolTablePrefix+tblName.Name, cvSch)
if err != nil {
return nil, err
}
Expand All @@ -55,7 +54,7 @@ func newProllyCVTable(ctx *sql.Context, tblName string, root doltdb.RootValue, r
}
m := durable.ProllyMapFromArtifactIndex(arts)
return &prollyConstraintViolationsTable{
tblName: resolvedName,
tblName: tblName,
root: root,
sqlSch: sqlSch,
tbl: tbl,
Expand Down
Loading

0 comments on commit acdeb8b

Please sign in to comment.