|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package colfetcher_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "regexp" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/cockroachdb/cockroach/pkg/base" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/sql/sem/eval" |
| 15 | + "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" |
| 16 | + "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" |
| 17 | + "github.com/cockroachdb/cockroach/pkg/util/leaktest" |
| 18 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | +) |
| 21 | + |
| 22 | +// TestScanLimitKVPairsRead is a regression test for fetching an extra KV batch |
| 23 | +// beyond the limit when the index has more than one column family and doesn't |
| 24 | +// include the max column family. It verifies that the correct number of KV |
| 25 | +// pairs are read as reported in EXPLAIN ANALYZE output. |
| 26 | +func TestScanLimitKVPairsRead(t *testing.T) { |
| 27 | + defer leaktest.AfterTest(t)() |
| 28 | + defer log.Scope(t).Close(t) |
| 29 | + |
| 30 | + ctx := context.Background() |
| 31 | + s, conn, _ := serverutils.StartServer(t, base.TestServerArgs{ |
| 32 | + Knobs: base.TestingKnobs{SQLEvalContext: &eval.TestingKnobs{ForceProductionValues: true}}, |
| 33 | + }) |
| 34 | + defer s.Stopper().Stop(ctx) |
| 35 | + sqlDB := sqlutils.MakeSQLRunner(conn) |
| 36 | + |
| 37 | + checkRows := func(rows [][]string) { |
| 38 | + // Look for "KV pairs read" in the output. |
| 39 | + kvPairsReadRE := regexp.MustCompile(`KV pairs read: ([\d,]+)`) |
| 40 | + var kvPairsCount string |
| 41 | + for _, row := range rows { |
| 42 | + if len(row) != 1 { |
| 43 | + t.Fatalf("expected one column in EXPLAIN ANALYZE output") |
| 44 | + } |
| 45 | + if matches := kvPairsReadRE.FindStringSubmatch(row[0]); len(matches) > 0 { |
| 46 | + kvPairsCount = matches[1] |
| 47 | + break |
| 48 | + } |
| 49 | + } |
| 50 | + require.Equal(t, "15", kvPairsCount, |
| 51 | + "expected exactly 15 KV pairs to be read for LIMIT 5 with 3 column families", |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + sqlDB.Exec(t, ` |
| 56 | + CREATE TABLE t_multi_cf ( |
| 57 | + k INT, a INT, b INT, c INT, |
| 58 | + INDEX (k) STORING (a, b), |
| 59 | + FAMILY (k), FAMILY (a), FAMILY (b), FAMILY (c) |
| 60 | + )`) |
| 61 | + |
| 62 | + // Case with no NULL values. |
| 63 | + sqlDB.Exec(t, `INSERT INTO t_multi_cf (SELECT t, t%19, t*7, t//3 FROM generate_series(1, 100) g(t))`) |
| 64 | + rows := sqlDB.QueryStr(t, `EXPLAIN ANALYZE SELECT a, b FROM t_multi_cf LIMIT 5`) |
| 65 | + checkRows(rows) |
| 66 | + |
| 67 | + // Case with some NULL values in the stored columns. |
| 68 | + sqlDB.Exec(t, `UPDATE t_multi_cf SET a = NULL WHERE k % 10 = 0`) |
| 69 | + rows = sqlDB.QueryStr(t, `EXPLAIN ANALYZE SELECT a, b FROM t_multi_cf LIMIT 5`) |
| 70 | + checkRows(rows) |
| 71 | +} |
0 commit comments