Skip to content

Commit

Permalink
Merge #118425
Browse files Browse the repository at this point in the history
118425: changefeedccl: make changefeed random expressions test deterministic r=jayshrivastava a=rharding6373

Before this change, it was difficult to reproduce errors from TestChangefeedRandomExpressions because the test setup involved non-deterministic queries. This PR both improves the test's determinism, logs setup information for easier reproducibility, and reduces the number of initial inserts to populate the test table.

Epic: None
Informs: #118061

Release note: none

Co-authored-by: rharding6373 <[email protected]>
  • Loading branch information
craig[bot] and rharding6373 committed Jan 30, 2024
2 parents 70150a6 + da9c96b commit 1d2534b
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pkg/ccl/backupccl/backuprand/backup_rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ database_name = 'rand' AND schema_name = 'public'`)
// Note: we do not care how many rows successfully populate
// the given table
if _, err := randgen.PopulateTableWithRandData(rng, tc.Conns[0], tableName,
numInserts); err != nil {
numInserts, nil); err != nil {
t.Fatal(err)
}
tableNames = append(tableNames, tableName)
Expand Down
29 changes: 23 additions & 6 deletions pkg/ccl/changefeedccl/changefeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,25 +1039,42 @@ func TestChangefeedRandomExpressions(t *testing.T) {
tblName := "seed"
defer s.DB.Close()

sqlDB.ExecMultiple(t, sqlsmith.Setups[tblName](rng)...)
setup := sqlsmith.Setups[tblName](rng)
sqlDB.ExecMultiple(t, setup...)

// TODO: PopulateTableWithRandData doesn't work with enums
sqlDB.Exec(t, "ALTER TABLE seed DROP COLUMN _enum")
dropEnumQry := "ALTER TABLE seed DROP COLUMN _enum;"
sqlDB.Exec(t, dropEnumQry)

// Attempt to insert a few more than our target 100 values, since there's a
// small chance we may not succeed that many inserts.
numInserts := 110
inserts := make([]string, 0, numInserts)
for rows := 0; rows < 100; {
var err error
var newRows int
if newRows, err = randgen.PopulateTableWithRandData(rng, s.DB, tblName, 200); err != nil {
if newRows, err = randgen.PopulateTableWithRandData(rng, s.DB, tblName, numInserts, &inserts); err != nil {
t.Fatal(err)
}
rows += newRows
}

sqlDB.Exec(t, `DELETE FROM seed WHERE rowid NOT IN (SELECT rowid FROM seed LIMIT 100)`)
limitQry := "DELETE FROM seed WHERE rowid NOT IN (SELECT rowid FROM seed ORDER BY rowid LIMIT 100);"
sqlDB.Exec(t, limitQry)

// Put the enums back. enum_range('hi'::greeting)[rowid%7] will give nulls when rowid%7=0 or 6.
sqlDB.Exec(t, `ALTER TABLE seed ADD COLUMN _enum greeting`)
sqlDB.Exec(t, `UPDATE seed SET _enum = enum_range('hi'::greeting)[rowid%7]`)
addEnumQry := "ALTER TABLE seed ADD COLUMN _enum greeting;"
sqlDB.Exec(t, addEnumQry)
populateEnumQry := "UPDATE seed SET _enum = enum_range('hi'::greeting)[rowid%7];"
sqlDB.Exec(t, populateEnumQry)
// Get values to log setup.
t.Logf("setup:\n%s\n%s\n%s\n%s\n%s\n%s",
strings.Join(setup, "\n"),
dropEnumQry,
strings.Join(inserts, "\n"),
limitQry,
addEnumQry,
populateEnumQry)

queryGen, err := sqlsmith.NewSmither(s.DB, rng,
sqlsmith.DisableWith(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/importer/exportparquet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestRandomParquetExports(t *testing.T) {

for i = 0; i < numTables; i++ {
tableName = string(stmts[i].(*tree.CreateTable).Table.ObjectName)
numRows, err := randgen.PopulateTableWithRandData(rng, db, tableName, 20)
numRows, err := randgen.PopulateTableWithRandData(rng, db, tableName, 20, nil)
require.NoError(t, err)
if numRows > 5 {
// Ensure the table only contains columns supported by EXPORT Parquet. If an
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/importer/read_import_avro_logical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func TestImportAvroLogicalTypes(t *testing.T) {
rng, _ := randutil.NewTestRand()
success := false
for i := 1; i <= 5; i++ {
numRowsInserted, err := randgen.PopulateTableWithRandData(rng, db, origTableName, 30)
numRowsInserted, err := randgen.PopulateTableWithRandData(rng, db, origTableName, 30, nil)
require.NoError(t, err)
if numRowsInserted > 5 {
success = true
Expand Down
8 changes: 7 additions & 1 deletion pkg/sql/randgen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,14 @@ func generateInsertStmtVals(rng *rand.Rand, colTypes []*types.T, nullable []bool
// - UNIQUE or CHECK constraint violation. RandDatum is naive to these constraints.
// - Out of range error for a computed INT2 or INT4 column.
//
// If a non-nil inserts is provided, it will be populated with the successful
// insert statements.
//
// If numRowsInserted == 0, PopulateTableWithRandomData or RandDatum couldn't
// handle this table's schema. Consider increasing numInserts or filing a bug.
// TODO(harding): Populate data in partitions.
func PopulateTableWithRandData(
rng *rand.Rand, db *gosql.DB, tableName string, numInserts int,
rng *rand.Rand, db *gosql.DB, tableName string, numInserts int, inserts *[]string,
) (numRowsInserted int, err error) {
var createStmtSQL string
res := db.QueryRow(fmt.Sprintf("SELECT create_statement FROM [SHOW CREATE TABLE %s]", tree.NameString(tableName)))
Expand Down Expand Up @@ -396,6 +399,9 @@ func PopulateTableWithRandData(
_, err := db.Exec(insertStmt)
if err == nil {
numRowsInserted++
if inserts != nil {
*inserts = append(*inserts, insertStmt)
}
}
}
return numRowsInserted, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/randgen/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestPopulateTableWithRandData(t *testing.T) {
for i := 0; i < numTables; i++ {
tableName := string(stmts[i].(*tree.CreateTable).Table.ObjectName)
numRows := 30
numRowsInserted, err := randgen.PopulateTableWithRandData(rng, dbConn, tableName, numRows)
numRowsInserted, err := randgen.PopulateTableWithRandData(rng, dbConn, tableName, numRows, nil)
require.NoError(t, err)
res := sqlDB.QueryStr(t, fmt.Sprintf("SELECT count(*) FROM %s", tree.NameString(tableName)))
require.Equal(t, fmt.Sprint(numRowsInserted), res[0][0])
Expand Down

0 comments on commit 1d2534b

Please sign in to comment.