Skip to content

Commit 3a15a0e

Browse files
committed
importer: update a few tests to use non-deprecated syntax
Tests were refactored to use IMPORT INTO ... CSV DATA syntax. A notable change is that since IMPORT INTO is rolled back differently, in TestFailedImport we no longer look for the GC job since the data is deleted via explicit `gcjob.DeleteAllTableData` and `sql.DeleteTableWithPredicate` calls. Release note: None
1 parent f596cbd commit 3a15a0e

18 files changed

+184
-128
lines changed

pkg/sql/importer/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ go_test(
205205
"//pkg/sql/sem/tree",
206206
"//pkg/sql/sessiondata",
207207
"//pkg/sql/stats",
208-
"//pkg/sql/tests",
209208
"//pkg/sql/types",
210209
"//pkg/storage",
211210
"//pkg/testutils",

pkg/sql/importer/import_csv_mark_redaction_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func TestMarkRedactionCCLStatement(t *testing.T) {
2525
expected string
2626
}{
2727
{
28-
"IMPORT CSV 'file' WITH delimiter = 'foo'",
29-
"IMPORT CSV ‹'*****'› WITH OPTIONS (delimiter = ‹'foo'›)",
28+
`IMPORT INTO foo CSV DATA ('file') WITH delimiter = 'foo'`,
29+
`IMPORT INTO ‹""›.‹""›.‹foo› CSV DATA (‹'*****'›) WITH OPTIONS (delimiter = ‹'foo'›)`,
3030
},
3131
}
3232

pkg/sql/importer/import_stmt_test.go

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import (
5858
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
5959
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
6060
"github.com/cockroachdb/cockroach/pkg/sql/stats"
61-
"github.com/cockroachdb/cockroach/pkg/sql/tests"
6261
"github.com/cockroachdb/cockroach/pkg/testutils"
6362
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
6463
"github.com/cockroachdb/cockroach/pkg/testutils/jobutils"
@@ -2006,35 +2005,36 @@ func TestImportRowLimit(t *testing.T) {
20062005
})
20072006
}
20082007

2009-
func TestFailedImportGC(t *testing.T) {
2008+
// TestFailedImport verifies that a failed import will clean up after itself
2009+
// (meaning that the table doesn't contain garbage data that was partially
2010+
// imported and that the table is brought online).
2011+
func TestFailedImport(t *testing.T) {
20102012
defer leaktest.AfterTest(t)()
20112013
defer log.Scope(t).Close(t)
20122014

20132015
skip.UnderRace(t)
20142016

2017+
rng, _ := randutil.NewTestRand()
20152018
const nodes = 3
2016-
2017-
var forceFailure bool
2018-
blockGC := make(chan struct{})
2019+
numFiles := nodes
2020+
rowsPerFile := 1000
2021+
rowsPerRaceFile := 16
2022+
testFiles := makeCSVData(t, numFiles, rowsPerFile, nodes, rowsPerRaceFile)
20192023

20202024
ctx := context.Background()
2021-
baseDir := datapathutils.TestDataPath(t, "pgdump")
2025+
baseDir := datapathutils.TestDataPath(t, "csv")
20222026
tc := serverutils.StartCluster(t, nodes, base.TestClusterArgs{ServerArgs: base.TestServerArgs{
20232027
// Test fails within a test tenant. This may be because we're trying
20242028
// to access files in nodelocal://1, which is off node. More
20252029
// investigation is required. Tracked with #76378.
20262030
DefaultTestTenant: base.TODOTestTenantDisabled,
20272031
SQLMemoryPoolSize: 256 << 20,
20282032
ExternalIODir: baseDir,
2029-
Knobs: base.TestingKnobs{
2030-
GCJob: &sql.GCJobTestingKnobs{
2031-
RunBeforeResume: func(_ jobspb.JobID) error { <-blockGC; return nil },
2032-
},
2033-
},
20342033
}})
20352034
defer tc.Stopper().Stop(ctx)
20362035
conn := tc.ServerConn(0)
20372036

2037+
var forceFailure bool
20382038
for i := 0; i < tc.NumServers(); i++ {
20392039
tc.Server(i).JobRegistry().(*jobs.Registry).TestingWrapResumerConstructor(
20402040
jobspb.TypeImport,
@@ -2051,56 +2051,25 @@ func TestFailedImportGC(t *testing.T) {
20512051
}
20522052

20532053
sqlDB := sqlutils.MakeSQLRunner(conn)
2054-
kvDB := tc.Server(0).DB()
2055-
20562054
sqlDB.Exec(t, `SET CLUSTER SETTING kv.bulk_ingest.batch_size = '10KB'`)
2055+
sqlDB.Exec(t, "CREATE DATABASE failedimport; USE failedimport;")
2056+
sqlDB.Exec(t, `CREATE TABLE t (a INT PRIMARY KEY, b STRING)`)
20572057

2058-
forceFailure = true
2059-
defer func() { forceFailure = false }()
2060-
defer gcjob.SetSmallMaxGCIntervalForTest()()
2061-
beforeImport, err := tree.MakeDTimestampTZ(tc.Server(0).Clock().Now().GoTime(), time.Millisecond)
2062-
if err != nil {
2063-
t.Fatal(err)
2058+
expectedRows := "0"
2059+
if rng.Float64() < 0.5 {
2060+
sqlDB.Exec(t, `INSERT INTO t VALUES (-1, 'a'), (-2, 'b')`)
2061+
expectedRows = "2"
20642062
}
20652063

2066-
sqlDB.Exec(t, "CREATE DATABASE failedimport; USE failedimport;")
2064+
forceFailure = true
2065+
defer func() { forceFailure = false }()
20672066
// Hit a failure during import.
20682067
sqlDB.ExpectErr(
20692068
t, `testing injected failure`,
2070-
fmt.Sprintf(`IMPORT TABLE simple FROM PGDUMP ('%s') WITH ignore_unsupported_statements`, "nodelocal://1/simple.sql"),
2069+
fmt.Sprintf("IMPORT INTO t (a, b) CSV DATA (%s)", strings.Join(testFiles.files, ",")),
20712070
)
2072-
// Nudge the registry to quickly adopt the job.
2073-
tc.Server(0).JobRegistry().(*jobs.Registry).TestingNudgeAdoptionQueue()
2074-
2075-
// In the case of the test, the ID of the table that will be cleaned up due
2076-
// to the failed import will be two higher than the ID of the empty database
2077-
// it was created in.
2078-
// We increment the id once for the public schema and a second time for the
2079-
// "MakeSimpleTableDescriptor".
2080-
dbID := sqlutils.QueryDatabaseID(t, sqlDB.DB, "failedimport")
2081-
tableID := descpb.ID(dbID + 2)
2082-
var td catalog.TableDescriptor
2083-
execCfg := tc.Server(0).ExecutorConfig().(sql.ExecutorConfig)
2084-
if err := sql.DescsTxn(ctx, &execCfg, func(ctx context.Context, txn isql.Txn, col *descs.Collection) (err error) {
2085-
td, err = col.ByIDWithoutLeased(txn.KV()).Get().Table(ctx, tableID)
2086-
return err
2087-
}); err != nil {
2088-
t.Fatal(err)
2089-
}
2090-
// Ensure that we have garbage written to the descriptor that we want to
2091-
// clean up.
2092-
tests.CheckKeyCount(t, kvDB, td.TableSpan(keys.SystemSQLCodec), 87)
2093-
2094-
// Allow GC to progress.
2095-
close(blockGC)
2096-
// Ensure that a GC job was created, and wait for it to finish.
2097-
doneGCQuery := fmt.Sprintf(
2098-
"SELECT count(*) FROM crdb_internal.jobs WHERE job_type = '%s' AND running_status = '%s' AND created > %s",
2099-
"SCHEMA CHANGE GC", sql.StatusWaitingForMVCCGC, beforeImport.String(),
2100-
)
2101-
sqlDB.CheckQueryResultsRetry(t, doneGCQuery, [][]string{{"1"}})
2102-
// Expect there are no more KVs for this span.
2103-
tests.CheckKeyCount(t, kvDB, td.TableSpan(keys.SystemSQLCodec), 0)
2071+
// Ensure that the table is online and is reverted properly.
2072+
sqlDB.CheckQueryResultsRetry(t, "SELECT count(*) FROM t", [][]string{{expectedRows}})
21042073
}
21052074

21062075
// TestImportIntoCSVCancel cancels a distributed import. This test
@@ -2163,10 +2132,6 @@ func TestImportIntoCSVCancel(t *testing.T) {
21632132
sqlDB.CheckQueryResults(t, "SELECT count(*) FROM t", [][]string{{"0"}})
21642133
}
21652134

2166-
// Verify that a failed import will clean up after itself. This means:
2167-
// - Delete the garbage data that it partially imported.
2168-
// - Delete the table descriptor for the table that was created during the
2169-
// import.
21702135
func TestImportCSVStmt(t *testing.T) {
21712136
defer leaktest.AfterTest(t)()
21722137
defer log.Scope(t).Close(t)
@@ -6538,8 +6503,12 @@ func TestCreateStatsAfterImport(t *testing.T) {
65386503
stats.DefaultAsOfTime = time.Microsecond
65396504

65406505
const nodes = 1
6506+
numFiles := nodes
6507+
rowsPerFile := 1000
6508+
rowsPerRaceFile := 16
6509+
testFiles := makeCSVData(t, numFiles, rowsPerFile, nodes, rowsPerRaceFile)
65416510
ctx := context.Background()
6542-
baseDir := datapathutils.TestDataPath(t)
6511+
baseDir := datapathutils.TestDataPath(t, "csv")
65436512

65446513
// Disable stats collection on system tables before the cluster is started,
65456514
// otherwise there is a race condition where stats may be collected before we
@@ -6560,22 +6529,39 @@ func TestCreateStatsAfterImport(t *testing.T) {
65606529

65616530
sqlDB.Exec(t, `SET CLUSTER SETTING sql.stats.automatic_collection.enabled=true`)
65626531

6563-
sqlDB.Exec(t, "IMPORT PGDUMP ($1) WITH ignore_unsupported_statements", "nodelocal://1/cockroachdump/dump.sql")
6532+
sqlDB.Exec(t, `CREATE TABLE t (a INT PRIMARY KEY, b STRING)`)
65646533

6565-
// Verify that statistics have been created.
6534+
emptyTableStats := [][]string{
6535+
{"__auto__", "{a}", "0", "0", "0"},
6536+
{"__auto__", "{b}", "0", "0", "0"},
6537+
}
6538+
// Wait until stats are collected on the empty table (to make the test more
6539+
// deterministic).
65666540
sqlDB.CheckQueryResultsRetry(t,
65676541
`SELECT statistics_name, column_names, row_count, distinct_count, null_count
65686542
FROM [SHOW STATISTICS FOR TABLE t]`,
6569-
[][]string{
6570-
{"__auto__", "{i}", "2", "2", "0"},
6571-
{"__auto__", "{t}", "2", "2", "0"},
6572-
})
6543+
emptyTableStats)
6544+
6545+
sqlDB.Exec(
6546+
t, fmt.Sprintf("IMPORT INTO t (a, b) CSV DATA (%s)", strings.Join(testFiles.files, ",")),
6547+
)
6548+
6549+
expectedStats := [][]string{
6550+
{"__auto__", "{a}", "1000", "1000", "0"},
6551+
{"__auto__", "{b}", "1000", "26", "0"},
6552+
}
6553+
if util.RaceEnabled {
6554+
expectedStats = [][]string{
6555+
{"__auto__", "{a}", "16", "16", "0"},
6556+
{"__auto__", "{b}", "16", "16", "0"},
6557+
}
6558+
}
6559+
6560+
// Verify that statistics have been created.
65736561
sqlDB.CheckQueryResultsRetry(t,
65746562
`SELECT statistics_name, column_names, row_count, distinct_count, null_count
6575-
FROM [SHOW STATISTICS FOR TABLE a]`,
6576-
[][]string{
6577-
{"__auto__", "{i}", "1", "1", "0"},
6578-
})
6563+
FROM [SHOW STATISTICS FOR TABLE t]`,
6564+
append(emptyTableStats, expectedStats...))
65796565
}
65806566

65816567
func TestImportAvro(t *testing.T) {

pkg/sql/plpgsql/parser/testdata/stmt_exec_sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
parse
22
DECLARE
33
BEGIN
4-
IMPORT TABLE foo
5-
FROM PGDUMP 'userfile://defaultdb.public.userfiles_root/db.sql'
4+
IMPORT INTO foo
5+
CSV DATA ('userfile://defaultdb.public.userfiles_root/db.sql')
66
WITH max_row_size='524288';
77
END
88
----
99
DECLARE
1010
BEGIN
11-
IMPORT TABLE foo FROM PGDUMP '*****' WITH OPTIONS (max_row_size = '524288');
11+
IMPORT INTO foo CSV DATA ('*****') WITH OPTIONS (max_row_size = '524288');
1212
END;
1313
-- normalized!
1414
DECLARE
1515
BEGIN
16-
IMPORT TABLE foo FROM PGDUMP ('*****') WITH OPTIONS (max_row_size = ('524288'));
16+
IMPORT INTO foo CSV DATA (('*****')) WITH OPTIONS (max_row_size = ('524288'));
1717
END;
1818
-- fully parenthesized
1919
DECLARE
2020
BEGIN
21-
IMPORT TABLE foo FROM PGDUMP '_' WITH OPTIONS (max_row_size = '_');
21+
IMPORT INTO foo CSV DATA ('_') WITH OPTIONS (max_row_size = '_');
2222
END;
2323
-- literals removed
2424
DECLARE
2525
BEGIN
26-
IMPORT TABLE _ FROM PGDUMP '*****' WITH OPTIONS (_ = '524288');
26+
IMPORT INTO _ CSV DATA ('*****') WITH OPTIONS (_ = '524288');
2727
END;
2828
-- identifiers removed
2929
DECLARE
3030
BEGIN
31-
IMPORT TABLE foo FROM PGDUMP 'userfile://defaultdb.public.userfiles_root/db.sql' WITH OPTIONS (max_row_size = '524288');
31+
IMPORT INTO foo CSV DATA ('userfile://defaultdb.public.userfiles_root/db.sql') WITH OPTIONS (max_row_size = '524288');
3232
END;
3333
-- passwords exposed
3434

pkg/sql/sem/tree/testdata/pretty/import1.align-deindent.golden

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@
33
1:
44
-
55
IMPORT
6-
TABLE
6+
INTO
77
t
8-
FROM
98
CSV
10-
$1
9+
DATA (
10+
$1
11+
)
1112

1213
6:
1314
------
1415
IMPORT
15-
TABLE t
16-
FROM
17-
CSV $1
16+
INTO t
17+
CSV DATA (
18+
$1
19+
)
1820

19-
26:
20-
--------------------------
21-
IMPORT TABLE t FROM CSV $1
21+
16:
22+
----------------
23+
IMPORT
24+
INTO t
25+
CSV DATA ($1)
26+
27+
27:
28+
---------------------------
29+
IMPORT INTO t CSV DATA ($1)
2230

2331

pkg/sql/sem/tree/testdata/pretty/import1.align-deindent.golden.short

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// GENERATED FILE DO NOT EDIT
33
1:
44
-
5-
IMPORT TABLE t FROM CSV $1
5+
IMPORT INTO t CSV DATA ($1)
66

77

pkg/sql/sem/tree/testdata/pretty/import1.align-only.golden

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@
33
1:
44
-
55
IMPORT
6-
TABLE
6+
INTO
77
t
8-
FROM
98
CSV
10-
$1
9+
DATA (
10+
$1
11+
)
1112

1213
6:
1314
------
1415
IMPORT
15-
TABLE t
16-
FROM
17-
CSV $1
16+
INTO t
17+
CSV DATA (
18+
$1
19+
)
1820

19-
26:
20-
--------------------------
21-
IMPORT TABLE t FROM CSV $1
21+
16:
22+
----------------
23+
IMPORT
24+
INTO t
25+
CSV DATA ($1)
26+
27+
27:
28+
---------------------------
29+
IMPORT INTO t CSV DATA ($1)
2230

2331

pkg/sql/sem/tree/testdata/pretty/import1.align-only.golden.short

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// GENERATED FILE DO NOT EDIT
33
1:
44
-
5-
IMPORT TABLE t FROM CSV $1
5+
IMPORT INTO t CSV DATA ($1)
66

77

pkg/sql/sem/tree/testdata/pretty/import1.ref.golden

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
1:
44
-
55
IMPORT
6-
TABLE
6+
INTO
77
t
8-
FROM
98
CSV
10-
$1
9+
DATA (
10+
$1
11+
)
1112

12-
26:
13-
--------------------------
14-
IMPORT TABLE t FROM CSV $1
13+
13:
14+
-------------
15+
IMPORT
16+
INTO
17+
t
18+
CSV
19+
DATA ($1)
20+
21+
27:
22+
---------------------------
23+
IMPORT INTO t CSV DATA ($1)
1524

1625

pkg/sql/sem/tree/testdata/pretty/import1.ref.golden.short

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
// GENERATED FILE DO NOT EDIT
33
1:
44
-
5-
IMPORT TABLE t FROM CSV $1
5+
IMPORT INTO t CSV DATA ($1)
66

77

0 commit comments

Comments
 (0)