Skip to content

Commit

Permalink
Merge pull request #8253 from dolthub/macneale4/workspace-read
Browse files Browse the repository at this point in the history
Add dolt_workspace_* system tables
  • Loading branch information
macneale4 authored Aug 13, 2024
2 parents c540b7f + 48f6bc2 commit a3dc825
Show file tree
Hide file tree
Showing 13 changed files with 979 additions and 41 deletions.
7 changes: 3 additions & 4 deletions go/libraries/doltcore/doltdb/system_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,13 @@ var generatedSystemTables = []string{
RemotesTableName,
}

var generatedSystemViewPrefixes = []string{
DoltBlameViewPrefix,
}

var generatedSystemTablePrefixes = []string{
DoltDiffTablePrefix,
DoltCommitDiffTablePrefix,
DoltHistoryTablePrefix,
DoltConfTablePrefix,
DoltConstViolTablePrefix,
DoltWorkspaceTablePrefix,
}

const (
Expand Down Expand Up @@ -264,6 +261,8 @@ const (
DoltConfTablePrefix = "dolt_conflicts_"
// DoltConstViolTablePrefix is the prefix assigned to all the generated constraint violation tables
DoltConstViolTablePrefix = "dolt_constraint_violations_"
// DoltWorkspaceTablePrefix is the prefix assigned to all the generated workspace tables
DoltWorkspaceTablePrefix = "dolt_workspace_"
)

const (
Expand Down
11 changes: 11 additions & 0 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,17 @@ func (db Database) getTableInsensitive(ctx *sql.Context, head *doltdb.Commit, ds
return nil, false, err
}
return dt, true, nil
case strings.HasPrefix(lwrName, doltdb.DoltWorkspaceTablePrefix):
sess := dsess.DSessFromSess(ctx.Session)
roots, _ := sess.GetRoots(ctx, db.RevisionQualifiedName())

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

dt, err := dtables.NewWorkspaceTable(ctx, userTable, roots)
if err != nil {
return nil, false, err
}
return dt, true, nil
}

var dt sql.Table
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dolt_diff_table_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (dtf *DiffTableFunction) RowIter(ctx *sql.Context, _ sql.Row) (sql.RowIter,
ddb := sqledb.DbData().Ddb
dp := dtables.NewDiffPartition(dtf.tableDelta.ToTable, dtf.tableDelta.FromTable, toCommitStr, fromCommitStr, dtf.toDate, dtf.fromDate, dtf.tableDelta.ToSch, dtf.tableDelta.FromSch)

return dtables.NewDiffPartitionRowIter(*dp, ddb, dtf.joiner), nil
return dtables.NewDiffPartitionRowIter(dp, ddb, dtf.joiner), nil
}

// findMatchingDelta returns the best matching table delta for the table name
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dolt_patch_table_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ func getDiffQuery(ctx *sql.Context, dbData env.DbData, td diff.TableDelta, fromR
diffQuerySqlSch, projections := getDiffQuerySqlSchemaAndProjections(diffPKSch.Schema, columnsWithDiff)

dp := dtables.NewDiffPartition(td.ToTable, td.FromTable, toRefDetails.hashStr, fromRefDetails.hashStr, toRefDetails.commitTime, fromRefDetails.commitTime, td.ToSch, td.FromSch)
ri := dtables.NewDiffPartitionRowIter(*dp, dbData.Ddb, j)
ri := dtables.NewDiffPartitionRowIter(dp, dbData.Ddb, j)

return diffQuerySqlSch, projections, ri, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dtables/column_diff_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ func calculateColDelta(ctx *sql.Context, ddb *doltdb.DoltDB, delta *diff.TableDe
now := time.Now() // accurate commit time returned elsewhere
// TODO: schema name?
dp := NewDiffPartition(delta.ToTable, delta.FromTable, delta.ToName.Name, delta.FromName.Name, (*dtypes.Timestamp)(&now), (*dtypes.Timestamp)(&now), delta.ToSch, delta.FromSch)
ri := NewDiffPartitionRowIter(*dp, ddb, j)
ri := NewDiffPartitionRowIter(dp, ddb, j)

var resultColNames []string
var resultDiffTypes []string
Expand Down
51 changes: 19 additions & 32 deletions go/libraries/doltcore/sqle/dtables/diff_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import (
"github.com/dolthub/dolt/go/store/val"
)

type diffRowItr struct {
// ldDiffRowItr is a sql.RowIter implementation which iterates over an LD formated DB in order to generate the
// dolt_diff_{table} results. This is legacy code at this point, as the DOLT format is what we'll support going forward.
type ldDiffRowItr struct {
ad diff.RowDiffer
diffSrc *diff.RowDiffSource
joiner *rowconv.Joiner
Expand All @@ -43,7 +45,7 @@ type diffRowItr struct {
toCommitInfo commitInfo
}

var _ sql.RowIter = &diffRowItr{}
var _ sql.RowIter = &ldDiffRowItr{}

type commitInfo struct {
name types.String
Expand All @@ -52,7 +54,7 @@ type commitInfo struct {
dateTag uint64
}

func newNomsDiffIter(ctx *sql.Context, ddb *doltdb.DoltDB, joiner *rowconv.Joiner, dp DiffPartition, lookup sql.IndexLookup) (*diffRowItr, error) {
func newLdDiffIter(ctx *sql.Context, ddb *doltdb.DoltDB, joiner *rowconv.Joiner, dp DiffPartition, lookup sql.IndexLookup) (*ldDiffRowItr, error) {
fromData, fromSch, err := tableData(ctx, dp.from, ddb)

if err != nil {
Expand Down Expand Up @@ -110,7 +112,7 @@ func newNomsDiffIter(ctx *sql.Context, ddb *doltdb.DoltDB, joiner *rowconv.Joine
src := diff.NewRowDiffSource(rd, joiner, ctx.Warn)
src.AddInputRowConversion(fromConv, toConv)

return &diffRowItr{
return &ldDiffRowItr{
ad: rd,
diffSrc: src,
joiner: joiner,
Expand All @@ -121,7 +123,7 @@ func newNomsDiffIter(ctx *sql.Context, ddb *doltdb.DoltDB, joiner *rowconv.Joine
}

// Next returns the next row
func (itr *diffRowItr) Next(ctx *sql.Context) (sql.Row, error) {
func (itr *ldDiffRowItr) Next(ctx *sql.Context) (sql.Row, error) {
r, err := itr.diffSrc.NextDiff()

if err != nil {
Expand Down Expand Up @@ -180,7 +182,7 @@ func (itr *diffRowItr) Next(ctx *sql.Context) (sql.Row, error) {
}

// Close closes the iterator
func (itr *diffRowItr) Close(*sql.Context) (err error) {
func (itr *ldDiffRowItr) Close(*sql.Context) (err error) {
defer itr.ad.Close()
defer func() {
closeErr := itr.diffSrc.Close()
Expand All @@ -203,7 +205,6 @@ type prollyDiffIter struct {
fromSch, toSch schema.Schema
targetFromSch, targetToSch schema.Schema
fromConverter, toConverter ProllyRowConverter
fromVD, toVD val.TupleDesc
keyless bool

fromCm commitInfo2
Expand Down Expand Up @@ -285,8 +286,6 @@ func newProllyDiffIter(ctx *sql.Context, dp DiffPartition, targetFromSchema, tar
return prollyDiffIter{}, err
}

fromVD := fsch.GetValueDescriptor()
toVD := tsch.GetValueDescriptor()
keyless := schema.IsKeyless(targetFromSchema) && schema.IsKeyless(targetToSchema)
child, cancel := context.WithCancel(ctx)
iter := prollyDiffIter{
Expand All @@ -298,8 +297,6 @@ func newProllyDiffIter(ctx *sql.Context, dp DiffPartition, targetFromSchema, tar
targetToSch: targetToSchema,
fromConverter: fromConverter,
toConverter: toConverter,
fromVD: fromVD,
toVD: toVD,
keyless: keyless,
fromCm: fromCm,
toCm: toCm,
Expand Down Expand Up @@ -372,7 +369,7 @@ func (itr prollyDiffIter) queueRows(ctx context.Context) {
// todo(andy): copy string fields
func (itr prollyDiffIter) makeDiffRowItr(ctx context.Context, d tree.Diff) (*repeatingRowIter, error) {
if !itr.keyless {
r, err := itr.getDiffRow(ctx, d)
r, err := itr.getDiffTableRow(ctx, d)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -404,15 +401,17 @@ func (itr prollyDiffIter) getDiffRowAndCardinality(ctx context.Context, d tree.D
}
}

r, err = itr.getDiffRow(ctx, d)
r, err = itr.getDiffTableRow(ctx, d)
if err != nil {
return nil, 0, err
}

return r, n, nil
}

func (itr prollyDiffIter) getDiffRow(ctx context.Context, dif tree.Diff) (row sql.Row, err error) {
// getDiffTableRow returns a row for the diff table given the diff type and the row from the source and target tables. The
// output schema is intended for dolt_diff_* tables and dolt_diff function.
func (itr prollyDiffIter) getDiffTableRow(ctx context.Context, dif tree.Diff) (row sql.Row, err error) {
tLen := schemaSize(itr.targetToSch)
fLen := schemaSize(itr.targetFromSch)

Expand Down Expand Up @@ -500,16 +499,15 @@ func maybeTime(t *time.Time) interface{} {
var _ sql.RowIter = (*diffPartitionRowIter)(nil)

type diffPartitionRowIter struct {
diffPartitions *DiffPartitions
ddb *doltdb.DoltDB
joiner *rowconv.Joiner
currentPartition *sql.Partition
currentPartition *DiffPartition
currentRowIter *sql.RowIter
}

func NewDiffPartitionRowIter(partition sql.Partition, ddb *doltdb.DoltDB, joiner *rowconv.Joiner) *diffPartitionRowIter {
func NewDiffPartitionRowIter(partition *DiffPartition, ddb *doltdb.DoltDB, joiner *rowconv.Joiner) *diffPartitionRowIter {
return &diffPartitionRowIter{
currentPartition: &partition,
currentPartition: partition,
ddb: ddb,
joiner: joiner,
}
Expand All @@ -518,16 +516,10 @@ func NewDiffPartitionRowIter(partition sql.Partition, ddb *doltdb.DoltDB, joiner
func (itr *diffPartitionRowIter) Next(ctx *sql.Context) (sql.Row, error) {
for {
if itr.currentPartition == nil {
nextPartition, err := itr.diffPartitions.Next(ctx)
if err != nil {
return nil, err
}
itr.currentPartition = &nextPartition
return nil, io.EOF
}

if itr.currentRowIter == nil {
dp := (*itr.currentPartition).(DiffPartition)
rowIter, err := dp.GetRowIter(ctx, itr.ddb, itr.joiner, sql.IndexLookup{})
rowIter, err := itr.currentPartition.GetRowIter(ctx, itr.ddb, itr.joiner, sql.IndexLookup{})
if err != nil {
return nil, err
}
Expand All @@ -538,12 +530,7 @@ func (itr *diffPartitionRowIter) Next(ctx *sql.Context) (sql.Row, error) {
if err == io.EOF {
itr.currentPartition = nil
itr.currentRowIter = nil

if itr.diffPartitions == nil {
return nil, err
}

continue
return nil, err
} else if err != nil {
return nil, err
} else {
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dtables/diff_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ func (dp DiffPartition) GetRowIter(ctx *sql.Context, ddb *doltdb.DoltDB, joiner
if types.IsFormat_DOLT(ddb.Format()) {
return newProllyDiffIter(ctx, dp, dp.fromSch, dp.toSch)
} else {
return newNomsDiffIter(ctx, ddb, joiner, dp, lookup)
return newLdDiffIter(ctx, ddb, joiner, dp, lookup)
}
}

Expand Down
Loading

0 comments on commit a3dc825

Please sign in to comment.