Skip to content

Commit

Permalink
Merge pull request #8474 from dolthub/nicktobey/secondary
Browse files Browse the repository at this point in the history
[no-release-notes] Replace ConvertToSecondaryKeylessIndex with AddHashToSchema
  • Loading branch information
nicktobey authored Oct 23, 2024
2 parents 1290f6a + 010b7da commit efd6e18
Show file tree
Hide file tree
Showing 28 changed files with 48 additions and 66 deletions.
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/schcmds/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func putEmptyTableWithSchema(ctx context.Context, tblName string, root doltdb.Ro
return nil, errhand.BuildDError("error: failed to get table.").AddCause(err).Build()
}

empty, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), sch)
empty, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), sch, false)
if err != nil {
return nil, errhand.BuildDError("error: failed to get table.").AddCause(err).Build()
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/doltdb/commit_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestAsyncPushOnWrite(t *testing.T) {
assert.NoError(t, err)

tSchema := createTestSchema(t)
rowData, err := durable.NewEmptyIndex(ctx, ddb.vrw, ddb.ns, tSchema)
rowData, err := durable.NewEmptyIndex(ctx, ddb.vrw, ddb.ns, tSchema, false)
require.NoError(t, err)
tbl, err := CreateTestTable(ddb.vrw, ddb.ns, tSchema, rowData)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/doltdb/doltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func CreateTestTable(vrw types.ValueReadWriter, ns tree.NodeStore, tSchema schem

func createTestRowData(t *testing.T, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema) durable.Index {
if types.Format_Default == types.Format_DOLT {
idx, err := durable.NewEmptyIndex(context.Background(), vrw, ns, sch)
idx, err := durable.NewEmptyIndex(context.Background(), vrw, ns, sch, false)
require.NoError(t, err)
return idx
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestLDNoms(t *testing.T) {

ctx := context.Background()
tSchema := createTestSchema(t)
rowData, err := durable.NewEmptyIndex(ctx, ddb.vrw, ddb.ns, tSchema)
rowData, err := durable.NewEmptyIndex(ctx, ddb.vrw, ddb.ns, tSchema, false)
if err != nil {
t.Fatal("Failed to create new empty index")
}
Expand Down
16 changes: 10 additions & 6 deletions go/libraries/doltcore/doltdb/durable/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ func RefFromIndex(ctx context.Context, vrw types.ValueReadWriter, idx Index) (ty
}

// indexFromRef reads the types.Ref from storage and returns the Index it points to.
// This is only used by noms format and can be removed.
func indexFromRef(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema, r types.Ref) (Index, error) {
return indexFromAddr(ctx, vrw, ns, sch, r.TargetHash())
return indexFromAddr(ctx, vrw, ns, sch, r.TargetHash(), false)
}

func indexFromAddr(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema, addr hash.Hash) (Index, error) {
func indexFromAddr(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema, addr hash.Hash, isKeylessTable bool) (Index, error) {
v, err := vrw.ReadValue(ctx, addr)
if err != nil {
return nil, err
Expand All @@ -111,7 +112,7 @@ func indexFromAddr(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeS
return IndexFromNomsMap(v.(types.Map), vrw, ns), nil

case types.Format_DOLT:
pm, err := shim.MapFromValue(v, sch, ns)
pm, err := shim.MapFromValue(v, sch, ns, isKeylessTable)
if err != nil {
return nil, err
}
Expand All @@ -123,7 +124,7 @@ func indexFromAddr(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeS
}

// NewEmptyIndex returns an index with no rows.
func NewEmptyIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema) (Index, error) {
func NewEmptyIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema, isKeylessSecondary bool) (Index, error) {
switch vrw.Format() {
case types.Format_LD_1:
m, err := types.NewMap(ctx, vrw)
Expand All @@ -134,6 +135,9 @@ func NewEmptyIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeS

case types.Format_DOLT:
kd, vd := sch.GetMapDescriptors()
if isKeylessSecondary {
kd = prolly.AddHashToSchema(kd)
}
m, err := prolly.NewMapFromTuples(ctx, ns, kd, vd)
if err != nil {
return nil, err
Expand Down Expand Up @@ -370,7 +374,7 @@ func NewIndexSetWithEmptyIndexes(ctx context.Context, vrw types.ValueReadWriter,
return nil, err
}
for _, index := range sch.Indexes().AllIndexes() {
empty, err := NewEmptyIndex(ctx, vrw, ns, index.Schema())
empty, err := NewEmptyIndex(ctx, vrw, ns, index.Schema(), false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -507,7 +511,7 @@ func (is doltDevIndexSet) GetIndex(ctx context.Context, tableSch schema.Schema,
if idxSch == nil {
idxSch = idx.Schema()
}
return indexFromAddr(ctx, is.vrw, is.ns, idxSch, foundAddr)
return indexFromAddr(ctx, is.vrw, is.ns, idxSch, foundAddr, schema.IsKeyless(tableSch))
}

func (is doltDevIndexSet) PutIndex(ctx context.Context, name string, idx Index) (IndexSet, error) {
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/doltdb/durable/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ func (t doltDevTable) GetTableRows(ctx context.Context) (Index, error) {
if err != nil {
return nil, err
}
m, err := shim.MapFromValue(types.SerialMessage(rowbytes), sch, t.ns)
m, err := shim.MapFromValue(types.SerialMessage(rowbytes), sch, t.ns, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/doltdb/root_val.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ func (root *rootValue) putTable(ctx context.Context, tName TableName, ref types.
func CreateEmptyTable(ctx context.Context, root RootValue, tName TableName, sch schema.Schema) (RootValue, error) {
ns := root.NodeStore()
vrw := root.VRW()
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, sch)
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/doltdb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewTableFromDurable(table durable.Table) *Table {
}

func NewEmptyTable(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema) (*Table, error) {
rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch)
rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/dtestutils/testcommands/multienv.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func createTestTable(dEnv *env.DoltEnv, tableName string, sch schema.Schema) err
vrw := dEnv.DoltDB.ValueReadWriter()
ns := dEnv.DoltDB.NodeStore()

idx, err := durable.NewEmptyIndex(ctx, vrw, ns, sch)
idx, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/merge/fulltext_rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func purgeFulltextTableData(ctx *sql.Context, root doltdb.RootValue, tableNames
if err != nil {
return nil, err
}
rows, err := durable.NewEmptyIndex(ctx, tbl.ValueReadWriter(), tbl.NodeStore(), sch)
rows, err := durable.NewEmptyIndex(ctx, tbl.ValueReadWriter(), tbl.NodeStore(), sch, false)
if err != nil {
return nil, err
}
Expand Down
3 changes: 0 additions & 3 deletions go/libraries/doltcore/merge/merge_prolly_indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ func mergeProllySecondaryIndexes(
return prolly.Map{}, false, err
}
m := durable.ProllyMapFromIndex(idx)
if schema.IsKeyless(sch) {
m = prolly.ConvertToSecondaryKeylessIndex(m)
}
return m, true, nil
}
return prolly.Map{}, false, nil
Expand Down
3 changes: 0 additions & 3 deletions go/libraries/doltcore/merge/merge_prolly_rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,9 +715,6 @@ func newUniqIndex(ctx *sql.Context, sch schema.Schema, tableName string, def sch
return uniqIndex{}, err
}

if schema.IsKeyless(sch) { // todo(andy): sad panda
secondary = prolly.ConvertToSecondaryKeylessIndex(secondary)
}
p := clustered.Pool()

prefixDesc := secondary.KeyDesc().PrefixDesc(def.Count())
Expand Down
13 changes: 6 additions & 7 deletions go/libraries/doltcore/merge/mutable_secondary_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ func GetMutableSecondaryIdxs(ctx *sql.Context, ourSch, sch schema.Schema, tableN
return nil, err
}
m := durable.ProllyMapFromIndex(idx)
if schema.IsKeyless(sch) {
m = prolly.ConvertToSecondaryKeylessIndex(m)
}
mods[i], err = NewMutableSecondaryIdx(ctx, m, ourSch, sch, tableName, index)
if err != nil {
return nil, err
Expand Down Expand Up @@ -76,17 +73,19 @@ func GetMutableSecondaryIdxsWithPending(ctx *sql.Context, ourSch, sch schema.Sch
// TODO: This isn't technically required, but correctly handling updating secondary indexes when only some
// of the table's rows have been updated is difficult to get right.
// Dropping the index is potentially slower but guaranteed to be correct.
if !m.KeyDesc().Equals(index.Schema().GetKeyDescriptorWithNoConversion()) {
idxKeyDesc := m.KeyDesc()
if schema.IsKeyless(sch) {
idxKeyDesc = idxKeyDesc.PrefixDesc(idxKeyDesc.Count() - 1)
}

if !idxKeyDesc.Equals(index.Schema().GetKeyDescriptorWithNoConversion()) {
continue
}

if !m.ValDesc().Equals(index.Schema().GetValueDescriptor()) {
continue
}

if schema.IsKeyless(sch) {
m = prolly.ConvertToSecondaryKeylessIndex(m)
}
newMutableSecondaryIdx, err := NewMutableSecondaryIdx(ctx, m, ourSch, sch, tableName, index)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions go/libraries/doltcore/merge/violations_fk.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func GetForeignKeyViolations(ctx context.Context, newRoot, baseRoot doltdb.RootV
return err
}
// Parent does not exist in the ancestor so we use an empty map
emptyIdx, err := durable.NewEmptyIndex(ctx, postParent.Table.ValueReadWriter(), postParent.Table.NodeStore(), postParent.Schema)
emptyIdx, err := durable.NewEmptyIndex(ctx, postParent.Table.ValueReadWriter(), postParent.Table.NodeStore(), postParent.Schema, false)
if err != nil {
return err
}
Expand All @@ -129,7 +129,7 @@ func GetForeignKeyViolations(ctx context.Context, newRoot, baseRoot doltdb.RootV
return err
}
// Child does not exist in the ancestor so we use an empty map
emptyIdx, err := durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postChild.Table.NodeStore(), postChild.Schema)
emptyIdx, err := durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postChild.Table.NodeStore(), postChild.Schema, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -370,7 +370,7 @@ func parentFkConstraintViolations(
}
var idx durable.Index
if empty {
idx, err = durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postParent.Table.NodeStore(), postParent.Schema)
idx, err = durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postParent.Table.NodeStore(), postParent.Schema, false)
if err != nil {
return err
}
Expand Down Expand Up @@ -405,7 +405,7 @@ func childFkConstraintViolations(
}
var idx durable.Index
if empty {
idx, err = durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postChild.Table.NodeStore(), postChild.Schema)
idx, err = durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postChild.Table.NodeStore(), postChild.Schema, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/alterschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func makePeopleTable(ctx context.Context, dEnv *env.DoltEnv) (*env.DoltEnv, erro
if err != nil {
return nil, err
}
rows, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), sch)
rows, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), sch, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func CreateTestTable(t *testing.T, dEnv *env.DoltEnv, tableName string, sch sche
vrw := dEnv.DoltDB.ValueReadWriter()
ns := dEnv.DoltDB.NodeStore()

rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch)
rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
require.NoError(t, err)
tbl, err := doltdb.NewTable(ctx, vrw, ns, sch, rows, nil, nil)
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ func (itr *prollyConflictRowIter) loadTableMaps(ctx *sql.Context, baseHash, thei

var idx durable.Index
if !ok {
idx, err = durable.NewEmptyIndex(ctx, itr.vrw, itr.ns, itr.ourSch)
idx, err = durable.NewEmptyIndex(ctx, itr.vrw, itr.ns, itr.ourSch, false)
} else {
idx, err = baseTbl.GetRowData(ctx)
}
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 @@ -595,7 +595,7 @@ func tableData(ctx *sql.Context, tbl *doltdb.Table, ddb *doltdb.DoltDB) (durable
var err error

if tbl == nil {
data, err = durable.NewEmptyIndex(ctx, ddb.ValueReadWriter(), ddb.NodeStore(), schema.EmptySchema)
data, err = durable.NewEmptyIndex(ctx, ddb.ValueReadWriter(), ddb.NodeStore(), schema.EmptySchema, false)
if err != nil {
return nil, nil, err
}
Expand Down
1 change: 0 additions & 1 deletion go/libraries/doltcore/sqle/enginetest/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ func validateKeylessIndex(ctx context.Context, sch schema.Schema, def schema.Ind
return nil
}

secondary = prolly.ConvertToSecondaryKeylessIndex(secondary)
idxDesc, _ := secondary.Descriptors()
builder := val.NewTupleBuilder(idxDesc)
mapping := ordinalMappingsForSecondaryIndex(sch, def)
Expand Down
6 changes: 3 additions & 3 deletions go/libraries/doltcore/sqle/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ func emptyFulltextTable(
return nil, nil, err
}

empty, err := durable.NewEmptyIndex(ctx, dt.ValueReadWriter(), dt.NodeStore(), doltSchema)
empty, err := durable.NewEmptyIndex(ctx, dt.ValueReadWriter(), dt.NodeStore(), doltSchema, false)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -1036,7 +1036,7 @@ func (t *WritableDoltTable) truncate(
}

for _, idx := range sch.Indexes().AllIndexes() {
empty, err := durable.NewEmptyIndex(ctx, table.ValueReadWriter(), table.NodeStore(), idx.Schema())
empty, err := durable.NewEmptyIndex(ctx, table.ValueReadWriter(), table.NodeStore(), idx.Schema(), false)
if err != nil {
return nil, err
}
Expand All @@ -1060,7 +1060,7 @@ func (t *WritableDoltTable) truncate(
}
}

empty, err := durable.NewEmptyIndex(ctx, table.ValueReadWriter(), table.NodeStore(), sch)
empty, err := durable.NewEmptyIndex(ctx, table.ValueReadWriter(), table.NodeStore(), sch, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/temp_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func NewTempTable(
vrw := ddb.ValueReadWriter()
ns := ddb.NodeStore()

idx, err := durable.NewEmptyIndex(ctx, vrw, ns, sch)
idx, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func CreateEmptyTestTable(dEnv *env.DoltEnv, tableName string, sch schema.Schema
vrw := dEnv.DoltDB.ValueReadWriter()
ns := dEnv.DoltDB.NodeStore()

rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch)
rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
if err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions go/libraries/doltcore/sqle/writer/prolly_table_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/globalstate"
"github.com/dolthub/dolt/go/libraries/doltcore/sqle/index"
"github.com/dolthub/dolt/go/store/pool"
"github.com/dolthub/dolt/go/store/prolly"
"github.com/dolthub/dolt/go/store/val"
)

Expand Down Expand Up @@ -118,7 +117,6 @@ func getSecondaryKeylessProllyWriters(ctx context.Context, t *doltdb.Table, schS
return nil, err
}
m := durable.ProllyMapFromIndex(idxRows)
m = prolly.ConvertToSecondaryKeylessIndex(m)

keyDesc, _ := m.Descriptors()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ const (
// single prolly tree materialization by presorting the index keys in an
// intermediate file format.
func BuildProllyIndexExternal(ctx *sql.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema, tableName string, idx schema.Index, primary prolly.Map, uniqCb DupEntryCb) (durable.Index, error) {
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, idx.Schema())
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, idx.Schema(), schema.IsKeyless(sch))
if err != nil {
return nil, err
}
secondary := durable.ProllyMapFromIndex(empty)
if schema.IsKeyless(sch) {
secondary = prolly.ConvertToSecondaryKeylessIndex(secondary)
}

iter, err := primary.IterAll(ctx)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions go/libraries/doltcore/table/editor/creation/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,11 @@ func BuildUniqueProllyIndex(
primary prolly.Map,
cb DupEntryCb,
) (durable.Index, error) {
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, idx.Schema())
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, idx.Schema(), schema.IsKeyless(sch))
if err != nil {
return nil, err
}
secondary := durable.ProllyMapFromIndex(empty)
if schema.IsKeyless(sch) {
secondary = prolly.ConvertToSecondaryKeylessIndex(secondary)
}

iter, err := primary.IterAll(ctx)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestEndToEnd(t *testing.T) {
root, err := dEnv.WorkingRoot(ctx)
require.NoError(t, err)

empty, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), tt.sch)
empty, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), tt.sch, false)
require.NoError(t, err)

indexes, err := durable.NewIndexSet(ctx, root.VRW(), root.NodeStore())
Expand Down
2 changes: 1 addition & 1 deletion go/store/datas/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func parse_Statistics(ctx context.Context, bs []byte, ns tree.NodeStore, vr type
return nil, err
}

m, err := shim.MapFromValue(value, schema.StatsTableDoltSchema, ns)
m, err := shim.MapFromValue(value, schema.StatsTableDoltSchema, ns, false)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit efd6e18

Please sign in to comment.