github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/table/table_iterator_test.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package table 16 17 import ( 18 "context" 19 "io" 20 "math/rand" 21 "testing" 22 23 "github.com/dolthub/go-mysql-server/sql" 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 27 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb/durable" 28 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 29 "github.com/dolthub/dolt/go/store/prolly" 30 "github.com/dolthub/dolt/go/store/prolly/message" 31 "github.com/dolthub/dolt/go/store/prolly/tree" 32 "github.com/dolthub/dolt/go/store/types" 33 "github.com/dolthub/dolt/go/store/val" 34 ) 35 36 var testRand = rand.New(rand.NewSource(1)) 37 38 func TestTableIteratorProlly(t *testing.T) { 39 n := 100 40 41 for i := 0; i < 10; i++ { 42 offset := testRand.Intn(n) 43 m, tups := mustMakeProllyMap(t, n) 44 idx := durable.IndexFromProllyMap(m) 45 itr, err := NewTableIterator(context.Background(), sch, idx, uint64(offset)) 46 require.NoError(t, err) 47 expectedRows := tuplesToRows(t, tups[offset:]) 48 testIterator(t, itr, expectedRows) 49 } 50 } 51 52 func testIterator(t *testing.T, iter RowIter, expected []sql.Row) { 53 ctx := context.Background() 54 for _, eR := range expected { 55 r, err := iter.Next(ctx) 56 require.NoError(t, err) 57 assert.Equal(t, eR, r) 58 } 59 _, err := iter.Next(ctx) 60 require.Equal(t, io.EOF, err) 61 } 62 63 var sch = schema.MustSchemaFromCols(schema.NewColCollection( 64 schema.NewColumn("pk", 0, types.UintKind, true), 65 schema.NewColumn("col1", 1, types.UintKind, false))) 66 var kd = val.NewTupleDescriptor( 67 val.Type{Enc: val.Uint32Enc, Nullable: false}, 68 ) 69 var vd = val.NewTupleDescriptor( 70 val.Type{Enc: val.Uint32Enc, Nullable: true}, 71 ) 72 73 func mustMakeProllyMap(t *testing.T, count int) (prolly.Map, [][2]val.Tuple) { 74 75 ns := tree.NewTestNodeStore() 76 77 tuples := tree.RandomTuplePairs(count, kd, vd, ns) 78 om := mustProllyMapFromTuples(t, kd, vd, tuples) 79 80 return om, tuples 81 } 82 83 func mustProllyMapFromTuples(t *testing.T, kd, vd val.TupleDesc, tuples [][2]val.Tuple) prolly.Map { 84 ctx := context.Background() 85 ns := tree.NewTestNodeStore() 86 87 serializer := message.NewProllyMapSerializer(vd, ns.Pool()) 88 chunker, err := tree.NewEmptyChunker(ctx, ns, serializer) 89 require.NoError(t, err) 90 91 for _, pair := range tuples { 92 err := chunker.AddPair(ctx, tree.Item(pair[0]), tree.Item(pair[1])) 93 require.NoError(t, err) 94 } 95 root, err := chunker.Done(ctx) 96 require.NoError(t, err) 97 98 return prolly.NewMap(root, ns, kd, vd) 99 } 100 101 func tuplesToRows(t *testing.T, kvs [][2]val.Tuple) (rows []sql.Row) { 102 103 rows = make([]sql.Row, len(kvs)) 104 for i, kv := range kvs { 105 v1, err := tree.GetField(context.Background(), kd, 0, kv[0], nil) 106 require.NoError(t, err) 107 v2, err := tree.GetField(context.Background(), kd, 0, kv[1], nil) 108 require.NoError(t, err) 109 rows[i] = sql.Row{v1, v2} 110 } 111 112 return 113 }