Skip to content

Commit

Permalink
Merge pull request #8527 from dolthub/taylor/pref-dtables
Browse files Browse the repository at this point in the history
Support multiple schema for system tables with $tablename for doltgres
  • Loading branch information
tbantle22 authored Nov 7, 2024
2 parents 1785ef9 + eba40a9 commit 5fb8fb1
Show file tree
Hide file tree
Showing 14 changed files with 213 additions and 136 deletions.
72 changes: 60 additions & 12 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,20 +330,38 @@ 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)
baseTableName := tblName[len(doltdb.DoltDiffTablePrefix):]
tname := doltdb.TableName{Name: baseTableName, Schema: db.schemaName}
if resolve.UseSearchPath && db.schemaName == "" {
var err error
tname, _, _, err = resolve.Table(ctx, root, baseTableName)
if err != nil {
return nil, false, err
}
}

dt, err := dtables.NewDiffTable(ctx, db.Name(), tname, db.ddb, root, head)
if err != nil {
return nil, false, err
}
return dt, true, nil

case strings.HasPrefix(lwrName, doltdb.DoltCommitDiffTablePrefix):
suffix := tblName[len(doltdb.DoltCommitDiffTablePrefix):]
baseTableName := tblName[len(doltdb.DoltCommitDiffTablePrefix):]
tname := doltdb.TableName{Name: baseTableName, Schema: db.schemaName}
if resolve.UseSearchPath && db.schemaName == "" {
var err error
tname, _, _, err = resolve.Table(ctx, root, baseTableName)
if err != nil {
return nil, false, err
}
}

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(), tname, db.ddb, root, ws.StagedRoot())
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -377,22 +395,40 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
}

case strings.HasPrefix(lwrName, doltdb.DoltConfTablePrefix):
suffix := tblName[len(doltdb.DoltConfTablePrefix):]
srcTable, ok, err := db.getTableInsensitive(ctx, head, ds, root, suffix, asOf)
baseTableName := tblName[len(doltdb.DoltConfTablePrefix):]
tname := doltdb.TableName{Name: baseTableName, Schema: db.schemaName}
if resolve.UseSearchPath && db.schemaName == "" {
var err error
tname, _, _, err = resolve.Table(ctx, root, baseTableName)
if err != nil {
return nil, false, err
}
}

srcTable, ok, err := db.getTableInsensitive(ctx, head, ds, root, tname.Name, 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, tname, srcTable, root, dtables.RootSetter(db))
if err != nil {
return nil, false, err
}
return dt, true, nil

case strings.HasPrefix(lwrName, doltdb.DoltConstViolTablePrefix):
suffix := tblName[len(doltdb.DoltConstViolTablePrefix):]
dt, err := dtables.NewConstraintViolationsTable(ctx, suffix, root, dtables.RootSetter(db))
baseTableName := tblName[len(doltdb.DoltConstViolTablePrefix):]
tname := doltdb.TableName{Name: baseTableName, Schema: db.schemaName}
if resolve.UseSearchPath && db.schemaName == "" {
var err error
tname, _, _, err = resolve.Table(ctx, root, baseTableName)
if err != nil {
return nil, false, err
}
}

dt, err := dtables.NewConstraintViolationsTable(ctx, tname, root, dtables.RootSetter(db))
if err != nil {
return nil, false, err
}
Expand All @@ -408,9 +444,21 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
roots, _ := sess.GetRoots(ctx, db.RevisionQualifiedName())
head := roots.Head

userTable := tblName[len(doltdb.DoltWorkspaceTablePrefix):]
baseTableName := tblName[len(doltdb.DoltWorkspaceTablePrefix):]
tname := doltdb.TableName{Name: baseTableName, Schema: db.schemaName}
if resolve.UseSearchPath && db.schemaName == "" {
var err error
baseName, _, exists, err := resolve.Table(ctx, root, baseTableName)
if err != nil {
return nil, false, err
}
// Only set tname if table exists so that emptyWorkspaceTable is used if the table does not exist
if exists {
tname = baseName
}
}

dt, err := dtables.NewWorkspaceTable(ctx, tblName, userTable, head, ws)
dt, err := dtables.NewWorkspaceTable(ctx, tblName, tname, head, ws)
if err != nil {
return nil, false, err
}
Expand Down Expand Up @@ -1621,7 +1669,7 @@ func (db Database) GetViewDefinition(ctx *sql.Context, viewName string) (sql.Vie
case strings.HasPrefix(lwrViewName, doltdb.DoltBlameViewPrefix):
tableName := lwrViewName[len(doltdb.DoltBlameViewPrefix):]

blameViewTextDef, err := dtables.NewBlameView(ctx, tableName, root)
blameViewTextDef, err := dtables.NewBlameView(ctx, doltdb.TableName{Name: tableName, Schema: db.schemaName}, root)
if err != nil {
return sql.ViewDefinition{}, false, err
}
Expand Down
11 changes: 5 additions & 6 deletions go/libraries/doltcore/sqle/dtables/blame_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,20 @@ const (
// NewBlameView returns a view expression for the DOLT_BLAME system view for the specified table.
// The DOLT_BLAME system view is a view on the DOLT_DIFF system table that shows the latest commit
// for each primary key in the specified table.
func NewBlameView(ctx *sql.Context, tableName string, root doltdb.RootValue) (string, error) {
table, _, ok, err := doltdb.GetTableInsensitive(ctx, root, doltdb.TableName{Name: tableName})
func NewBlameView(ctx *sql.Context, tableName doltdb.TableName, root doltdb.RootValue) (string, error) {
var table *doltdb.Table
var err error
table, tableName, err = getTableInsensitiveOrError(ctx, root, tableName)
if err != nil {
return "", err
}
if !ok {
return "", doltdb.ErrTableNotFound
}

sch, err := table.GetSchema(ctx)
if err != nil {
return "", nil
}

blameViewExpression, err := createDoltBlameViewExpression(tableName, sch.GetPKCols().GetColumns())
blameViewExpression, err := createDoltBlameViewExpression(tableName.Name, sch.GetPKCols().GetColumns())
if err != nil {
return "", err
}
Expand Down
26 changes: 12 additions & 14 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,16 +57,15 @@ 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)
var table *doltdb.Table
var err error
table, tblName, err = getTableInsensitiveOrError(ctx, wRoot, tblName)
if err != nil {
return nil, err
}
if !tableExists {
return nil, sql.ErrTableNotFound.New(diffTblName)
}

sch, err := table.GetSchema(ctx)
if err != nil {
Expand All @@ -86,7 +84,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 +108,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 +126,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 +195,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
32 changes: 15 additions & 17 deletions go/libraries/doltcore/sqle/dtables/conflicts_tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,38 @@ 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, srcTable sql.Table, root doltdb.RootValue, rs RootSetter) (sql.Table, error) {
var tbl *doltdb.Table
var err error
tbl, tblName, err = getTableInsensitiveOrError(ctx, root, tblName)
if err != nil {
return nil, err
} else if !ok {
return nil, sql.ErrTableNotFound.New(tblName)
}

if types.IsFormat_DOLT(tbl.Format()) {
upd, ok := srcTbl.(sql.UpdatableTable)
upd, ok := srcTable.(sql.UpdatableTable)
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
10 changes: 5 additions & 5 deletions go/libraries/doltcore/sqle/dtables/conflicts_tables_prolly.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func (ct ProllyConflictsTable) PartitionRows(ctx *sql.Context, part sql.Partitio
}

func (ct ProllyConflictsTable) Updater(ctx *sql.Context) sql.RowUpdater {
ourUpdater := ct.sqlTable.Updater(ctx)
return newProllyConflictOurTableUpdater(ourUpdater, ct.versionMappings, ct.baseSch, ct.ourSch, ct.theirSch)
return newProllyConflictOurTableUpdater(ctx, ct)
}

func (ct ProllyConflictsTable) Deleter(ctx *sql.Context) sql.RowDeleter {
Expand Down Expand Up @@ -469,11 +468,12 @@ type prollyConflictOurTableUpdater struct {
schemaOK bool
}

func newProllyConflictOurTableUpdater(ourUpdater sql.RowUpdater, versionMappings *versionMappings, baseSch, ourSch, theirSch schema.Schema) *prollyConflictOurTableUpdater {
func newProllyConflictOurTableUpdater(ctx *sql.Context, ct ProllyConflictsTable) *prollyConflictOurTableUpdater {
ourUpdater := ct.sqlTable.Updater(ctx)
return &prollyConflictOurTableUpdater{
srcUpdater: ourUpdater,
versionMappings: versionMappings,
pkOrdinals: ourSch.GetPkOrdinals(),
versionMappings: ct.versionMappings,
pkOrdinals: ct.ourSch.GetPkOrdinals(),
}
}

Expand Down
Loading

0 comments on commit 5fb8fb1

Please sign in to comment.