Skip to content

Commit 6eb398e

Browse files
committed
workload_generator: fix constraint prefix check
While extracting the table schemas from the create_statements file, we use a prefix-based check to identify whether a line has a column information or a constraint. This check fails in case the prefix of the column name matches. e.g. a line beginning with "CHECK" is a constraint. But, if the column name is "checksum", this matches and causes an issue to identify the column as a constraint. To fix this, the check is changed to be "CHECK ". A TODO is added to use sql parser instead of this string match and regex logics. Release Note: None Epic: None
1 parent 59c110c commit 6eb398e

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

pkg/workload/workload_generator/schema_generator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ func generateDDLFromCSV(
352352
// It parses the table name, columns, and constraints (primary keys, unique constraints,
353353
// foreign keys, and check constraints) from the DDL statement and returns a structured
354354
// representation of the table schema.
355+
// TODO: (@nameisbhaskar) use sql parser instead of regexes - https://github.com/cockroachdb/cockroach/issues/155173
355356
func ParseDDL(ddl string) (*TableSchema, error) {
356357
tableMatch := tablePattern.FindStringSubmatch(ddl)
357358
if tableMatch == nil {
@@ -439,7 +440,8 @@ func hasConstrainingPrefix(up string) bool {
439440
sqlFamily,
440441
}
441442
for _, p := range prefixes {
442-
if strings.HasPrefix(up, p) {
443+
// a space is added after the prefix to avoid matches like "checksum" as a field name.
444+
if strings.HasPrefix(up, p+" ") {
443445
return true
444446
}
445447
}

pkg/workload/workload_generator/schema_generator_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestSplitColumnDefsAndTableConstraints(t *testing.T) {
2626
id INT PRIMARY KEY,
2727
name TEXT NOT NULL,
2828
age INT DEFAULT 30,
29+
checksum BYTES DEFAULT 30,
2930
CONSTRAINT user_pk PRIMARY KEY (id),
3031
UNIQUE (name),
3132
FOREIGN KEY (age) REFERENCES other(age),
@@ -36,6 +37,7 @@ func TestSplitColumnDefsAndTableConstraints(t *testing.T) {
3637
"id INT PRIMARY KEY",
3738
"name TEXT NOT NULL",
3839
"age INT DEFAULT 30",
40+
"checksum BYTES DEFAULT 30",
3941
}
4042
wantConstraints := []string{
4143
"CONSTRAINT user_pk PRIMARY KEY (id)",

0 commit comments

Comments
 (0)