Skip to content

Commit

Permalink
Merge pull request #8634 from dolthub/taylor/dg-types
Browse files Browse the repository at this point in the history
Add pointers for schemas for `dolt_workspace_*` and `dolt_diff` system tables for doltgres
  • Loading branch information
tbantle22 authored Dec 3, 2024
2 parents bc96dda + c5206e5 commit 884ee1e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 26 deletions.
28 changes: 18 additions & 10 deletions go/libraries/doltcore/sqle/dtables/unscoped_diff_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,28 @@ func (dt *UnscopedDiffTable) String() string {
return dt.tableName
}

// Schema is a sql.Table interface function that returns the sql.Schema for this system table.
func (dt *UnscopedDiffTable) Schema() sql.Schema {
func getUnscopedDoltDiffSchema(dbName, tableName string) sql.Schema {
return []*sql.Column{
{Name: "commit_hash", Type: types.Text, Source: dt.tableName, PrimaryKey: true, DatabaseSource: dt.dbName},
{Name: "table_name", Type: types.Text, Source: dt.tableName, PrimaryKey: true, DatabaseSource: dt.dbName},
{Name: "committer", Type: types.Text, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
{Name: "email", Type: types.Text, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
{Name: "date", Type: types.Datetime, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
{Name: "message", Type: types.Text, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
{Name: "data_change", Type: types.Boolean, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
{Name: "schema_change", Type: types.Boolean, Source: dt.tableName, PrimaryKey: false, DatabaseSource: dt.dbName},
{Name: "commit_hash", Type: types.Text, Source: tableName, PrimaryKey: true, DatabaseSource: dbName},
{Name: "table_name", Type: types.Text, Source: tableName, PrimaryKey: true, DatabaseSource: dbName},
{Name: "committer", Type: types.Text, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
{Name: "email", Type: types.Text, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
{Name: "date", Type: types.Datetime, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
{Name: "message", Type: types.Text, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
{Name: "data_change", Type: types.Boolean, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
{Name: "schema_change", Type: types.Boolean, Source: tableName, PrimaryKey: false, DatabaseSource: dbName},
}
}

// GetUnscopedDoltDiffSchema returns the schema of the dolt_diff system table. This is used
// by Doltgres to update the dolt_diff schema using Doltgres types.
var GetUnscopedDoltDiffSchema = getUnscopedDoltDiffSchema

// Schema is a sql.Table interface function that returns the sql.Schema for this system table.
func (dt *UnscopedDiffTable) Schema() sql.Schema {
return GetUnscopedDoltDiffSchema(dt.dbName, dt.tableName)
}

// Collation implements the sql.Table interface.
func (dt *UnscopedDiffTable) Collation() sql.CollationID {
return sql.Collation_Default
Expand Down
44 changes: 28 additions & 16 deletions go/libraries/doltcore/sqle/dtables/workspace_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
"io"

"github.com/dolthub/go-mysql-server/sql"
sqltypes "github.com/dolthub/go-mysql-server/sql/types"
gmstypes "github.com/dolthub/go-mysql-server/sql/types"
"github.com/dolthub/vitess/go/sqltypes"

"github.com/dolthub/dolt/go/libraries/doltcore/diff"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
Expand Down Expand Up @@ -406,7 +407,10 @@ func NewWorkspaceTable(ctx *sql.Context, workspaceTableName string, tableName do
fromSch = toSch
}

totalSch, err := workspaceSchema(fromSch, toSch)
sch := sql.NewPrimaryKeySchema(GetDoltWorkspaceBaseSqlSchema())
baseDoltSch, err := sqlutil.ToDoltSchema(ctx, head, tableName, sch, head, sql.Collation_Default)

totalSch, err := workspaceSchema(fromSch, toSch, baseDoltSch)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -438,9 +442,21 @@ func (wt *WorkspaceTable) Schema() sql.Schema {
return wt.sqlSchema
}

// CalculateDiffSchema returns the schema for the dolt_diff table based on the schemas from the from and to tables.
// GetDoltWorkspaceBaseSqlSchema returns the base schema for the dolt_workspace_* system table.
// This is used by Doltgres to update the dolt_workspace_* schema using Doltgres types.
var GetDoltWorkspaceBaseSqlSchema = getDoltWorkspaceBaseSqlSchema

func getDoltWorkspaceBaseSqlSchema() sql.Schema {
return []*sql.Column{
{Name: "id", Type: gmstypes.Uint64, PrimaryKey: true, Nullable: false},
{Name: "staged", Type: gmstypes.Boolean, Nullable: false},
{Name: "diff_type", Type: gmstypes.MustCreateStringWithDefaults(sqltypes.VarChar, 1023), Nullable: false},
}
}

// workspaceSchema returns the schema for the dolt_workspace table based on the schemas from the from and to tables.
// Either may be nil, in which case the nil argument will use the schema of the non-nil argument
func workspaceSchema(fromSch, toSch schema.Schema) (schema.Schema, error) {
func workspaceSchema(fromSch, toSch, baseSch schema.Schema) (schema.Schema, error) {
if fromSch == nil && toSch == nil {
return nil, errors.New("Runtime error:non-nil argument required to CalculateDiffSchema")
} else if fromSch == nil {
Expand All @@ -449,13 +465,10 @@ func workspaceSchema(fromSch, toSch schema.Schema) (schema.Schema, error) {
toSch = fromSch
}

cols := make([]schema.Column, 0, 3+toSch.GetAllCols().Size()+fromSch.GetAllCols().Size())

cols = append(cols,
schema.NewColumn("id", 0, types.UintKind, true, schema.NotNullConstraint{}),
schema.NewColumn("staged", 0, types.BoolKind, false, schema.NotNullConstraint{}),
schema.NewColumn("diff_type", 0, types.StringKind, false, schema.NotNullConstraint{}),
)
baseColColl := baseSch.GetAllCols()
baseCols := baseColColl.GetColumns()
cols := make([]schema.Column, 0, baseColColl.Size()+toSch.GetAllCols().Size()+fromSch.GetAllCols().Size())
cols = append(cols, baseCols...)

transformer := func(sch schema.Schema, namer func(string) string) error {
return sch.GetAllCols().Iter(func(tag uint64, col schema.Column) (stop bool, err error) {
Expand Down Expand Up @@ -643,7 +656,7 @@ func getWorkspaceTableRow(

row = make(sql.Row, 3+tLen+fLen)

row[0] = rowId
row[0] = int64(rowId)
row[1] = staged
row[2] = diffTypeString(dif)

Expand Down Expand Up @@ -868,10 +881,9 @@ func (e emptyWorkspaceTable) String() string {
}

func (e emptyWorkspaceTable) Schema() sql.Schema {
return []*sql.Column{
{Name: "id", Type: sqltypes.Int32, Nullable: false},
{Name: "staged", Type: sqltypes.Boolean, Nullable: false},
}
sch := GetDoltWorkspaceBaseSqlSchema()
// Only return the "id" and "staged" columns.
return sch[0:2]
}

func (e emptyWorkspaceTable) Collation() sql.CollationID { return sql.Collation_Default }
Expand Down
32 changes: 32 additions & 0 deletions go/libraries/doltcore/sqle/enginetest/dolt_queries_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ import (
)

var DoltWorkspaceScriptTests = []queries.ScriptTest{
{
Name: "dolt_workspace_* empty table",
SetUpScript: []string{
"create table tbl (pk int primary key, val int);",
"call dolt_commit('-Am', 'creating table t');",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "select * from dolt_workspace_tbl",
Expected: []sql.Row{},
},
{
Query: "describe dolt_workspace_tbl",
Expected: []sql.Row{
{"id", "bigint unsigned", "NO", "PRI", nil, ""},
{"staged", "tinyint(1)", "NO", "", nil, ""},
},
},
},
},
{
Name: "dolt_workspace_* multiple edits of a single row",
SetUpScript: []string{
Expand All @@ -40,6 +60,18 @@ var DoltWorkspaceScriptTests = []queries.ScriptTest{
{0, true, "modified", 42, 51, 42, 42},
},
},
{
Query: "describe dolt_workspace_tbl",
Expected: []sql.Row{
{"id", "bigint unsigned", "NO", "PRI", nil, ""},
{"staged", "tinyint(1)", "NO", "", nil, ""},
{"diff_type", "varchar(1023)", "NO", "", nil, ""},
{"to_pk", "int", "YES", "", nil, ""},
{"to_val", "int", "YES", "", nil, ""},
{"from_pk", "int", "YES", "", nil, ""},
{"from_val", "int", "YES", "", nil, ""},
},
},
{
// Test case-insensitive table name
Query: "select * from dolt_workspace_TBL",
Expand Down

0 comments on commit 884ee1e

Please sign in to comment.