Added doc comments to schema.Schema, integration test

This commit is contained in:
Andy Arthur
2022-10-20 12:17:01 -07:00
parent a30a620196
commit a90f64a1a5
2 changed files with 70 additions and 5 deletions

View File

@@ -64,6 +64,52 @@ func TestSqlIntegration(t *testing.T) {
}
}
func TestSchemaOrdering(t *testing.T) {
tests := []struct {
name string
query string
pkCols []string
otherCols []string
allCols []string
ordinals []int
}{
{
name: "primary key",
query: "CREATE TABLE t (a int, b int, pk2 int, c int, pk1 int, PRIMARY KEY (pk1, pk2));",
pkCols: []string{"pk1", "pk2"},
otherCols: []string{"a", "b", "c"},
allCols: []string{"a", "b", "pk2", "c", "pk1"},
ordinals: []int{4, 2},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
root := runTestSql(t, ctx, []string{test.query})
tbl, ok, err := root.GetTable(ctx, "t")
require.NoError(t, err)
require.True(t, ok)
sch, err := tbl.GetSchema(ctx)
require.NoError(t, err)
for i, col := range sch.GetPKCols().GetColumns() {
assert.Equal(t, test.pkCols[i], col.Name)
}
for i, col := range sch.GetNonPKCols().GetColumns() {
assert.Equal(t, test.otherCols[i], col.Name)
}
for i, col := range sch.GetAllCols().GetColumns() {
assert.Equal(t, test.allCols[i], col.Name)
}
for i, ord := range sch.GetPkOrdinals() {
assert.Equal(t, test.ordinals[i], ord)
}
})
}
}
func TestGetKeyTags(t *testing.T) {
const tblName = "test"
tests := []struct {

View File

@@ -26,16 +26,34 @@ import (
"github.com/dolthub/dolt/go/store/val"
)
// Schema is an interface for retrieving the columns that make up a schema
// Schema defines the schema of a table and describes both its SQL schema and storage layout.
//
// For example, a SQL table defined as:
//
// `CREATE TABLE t (a int, b int, pk2 int, c int, pk1 int, PRIMARY KEY (pk1, pk2));`
//
// Has a corresponding Schema of:
//
// Schema {
// PkCols: [pk1, pk2],
// NonPkCols: [a, b, c],
// AllCols: [a, b, pk2, c, pk1],
// PkOrdinals: [4, 2],
// }
type Schema interface {
// GetPKCols gets the collection of columns which make the primary key. They
// are always returned in ordinal order.
// GetPKCols gets the collection of columns which make the primary key.
// Columns in this collection are ordered by storage order, which is
// defined in the 'PRIMARY KEY(...)' clause of a CREATE TABLE statement.
GetPKCols() *ColCollection
// GetNonPKCols gets the collection of columns which are not part of the primary key.
// Columns in this collection are ordered by schema order (display order), which is
// defined by the order of first occurrence in a CREATE TABLE statement.
GetNonPKCols() *ColCollection
// GetAllCols gets the collection of all columns (pk and non-pk)
// Columns in this collection are ordered by schema order (display order), which is
// defined by the order of first occurrence in a CREATE TABLE statement.
GetAllCols() *ColCollection
// Indexes returns a collection of all indexes on the table that this schema belongs to.
@@ -44,10 +62,11 @@ type Schema interface {
// Checks returns a collection of all check constraints on the table that this schema belongs to.
Checks() CheckCollection
// GetPkOrdinals returns a slice of the primary key ordering indexes relative to the schema column ordering
// GetPkOrdinals returns a slice of schema order positions for the primary key columns. These ith
// value of this slice contains schema position for the ith column in the PK ColCollection.
GetPkOrdinals() []int
// SetPkOrdinals specifies a primary key column ordering
// SetPkOrdinals specifies a primary key column ordering. See GetPkOrdinals.
SetPkOrdinals([]int) error
// AddColumn adds a column to this schema in the order given and returns the resulting Schema.