@@ -8,7 +8,12 @@ package replicationtestutils
8
8
import (
9
9
"context"
10
10
"fmt"
11
+ "math/rand"
11
12
13
+ "github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
14
+ "github.com/cockroachdb/cockroach/pkg/sql/randgen"
15
+ "github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
16
+ "github.com/cockroachdb/cockroach/pkg/sql/types"
12
17
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
13
18
"github.com/cockroachdb/errors"
14
19
)
@@ -39,3 +44,75 @@ func CheckEmptyDLQs(ctx context.Context, db sqlutils.DBHandle, dbName string) er
39
44
}
40
45
return nil
41
46
}
47
+
48
+ func GenerateLDRTable (
49
+ ctx context.Context , rng * rand.Rand , tableName string , supportKVWriter bool ,
50
+ ) string {
51
+ columnByName := func (name tree.Name , columnDefs []* tree.ColumnTableDef ) * tree.ColumnTableDef {
52
+ for _ , col := range columnDefs {
53
+ if col .Name == name {
54
+ return col
55
+ }
56
+ }
57
+ return nil
58
+ }
59
+
60
+ tableDef := randgen .RandCreateTableWithName (ctx , rng , tableName , 0 , []randgen.TableOption {
61
+ randgen .WithPrimaryIndexRequired (),
62
+ randgen .WithSkipColumnFamilyMutations (),
63
+ randgen .WithPrimaryIndexFilter (func (indexDef * tree.IndexTableDef , columnDefs []* tree.ColumnTableDef ) bool {
64
+ for _ , col := range indexDef .Columns {
65
+ columnDef := columnByName (col .Column , columnDefs )
66
+ // TODO(127315): types with composite encoding are not supported in the
67
+ // primary key by LDR.
68
+ if colinfo .CanHaveCompositeKeyEncoding (columnDef .Type .(* types.T )) {
69
+ return false
70
+ }
71
+ // Do not allow computed columns in the primary key. Non-virtual computed columns in the primary key is
72
+ // allowed by LDR in general, but its not compatible with the conflict workload.
73
+ // TODO(jeffswenson): support computed columns in the primary key in the conflict workload.
74
+ if columnDef .IsComputed () {
75
+ return false
76
+ }
77
+ }
78
+ if supportKVWriter && indexDef .Sharded != nil {
79
+ // The KV writer does not support hash sharded indexes.
80
+ return false
81
+ }
82
+ return true
83
+ }),
84
+ randgen .WithIndexFilter (func (indexDef tree.TableDef , columnDefs []* tree.ColumnTableDef ) bool {
85
+ switch indexDef := indexDef .(type ) {
86
+ case * tree.UniqueConstraintTableDef :
87
+ // Do not allow unique indexes. The random data may cause
88
+ // spurious unique constraint violations.
89
+ // TODO(jeffswenson): extend the conflict workload to support unique indexes on fields
90
+ // that can randomly generate exclusively unique values for each row. E.g. UUIDs could be unique, but
91
+ // BOOLs are too limiting.
92
+ return false
93
+ case * tree.IndexTableDef :
94
+ for _ , col := range indexDef .Columns {
95
+ if supportKVWriter && col .Expr != nil {
96
+ // Do not allow expression indexes. These cause SQL to generate a hidden computed column, which is not
97
+ // supported by the kv writer.
98
+ if col .Expr != nil {
99
+ return false
100
+ }
101
+ }
102
+ columnDef := columnByName (col .Column , columnDefs )
103
+ if columnDef .IsVirtual () {
104
+ // Virtual computed columns are not supported in indexes by the classic sql writer or the kv writer.
105
+ // TODO(jeffswenson): remove this restriction once the crud writer is the only writer.
106
+ return false
107
+ }
108
+ }
109
+ if supportKVWriter && indexDef .Sharded != nil {
110
+ // The KV writer does not support hash sharded indexes.
111
+ return false
112
+ }
113
+ }
114
+ return true
115
+ }),
116
+ })
117
+ return tree .AsStringWithFlags (tableDef , tree .FmtParsable )
118
+ }
0 commit comments