Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changefeedccl: ensure that spans planned from cdc queries have end keys #143102

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/ccl/changefeedccl/cdceval/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@ func SpansForExpression(
return nil, withErrorHint(err, d.FamilyName, d.HasOtherFamilies)
}

return plan.Spans, nil
// Make sure any single-key spans are expanded to have end keys.
spans := plan.Spans
for i := range spans {
if len(spans[i].EndKey) == 0 {
spans[i].EndKey = spans[i].Key.Clone().Next()
}
}

return spans, nil
}

// withErrorHint wraps error with error hints.
Expand Down
52 changes: 52 additions & 0 deletions pkg/ccl/changefeedccl/changefeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10794,6 +10794,58 @@ func TestChangefeedProtectedTimestampUpdate(t *testing.T) {
cdcTest(t, testFn, feedTestForceSink("kafka"), withTxnRetries)
}

func TestCDCQuerySelectSingleRow(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

errCh := make(chan error, 1)
knobsFn := func(knobs *base.TestingKnobs) {
if knobs.DistSQL == nil {
knobs.DistSQL = &execinfra.TestingKnobs{}
}
if knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed == nil {
knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed = &TestingKnobs{}
}
cfKnobs := knobs.DistSQL.(*execinfra.TestingKnobs).Changefeed.(*TestingKnobs)
cfKnobs.HandleDistChangefeedError = func(err error) error {
errCh <- err
return err
}
}

testFn := func(t *testing.T, s TestServer, f cdctest.TestFeedFactory) {
db := sqlutils.MakeSQLRunner(s.DB)
db.Exec(t, `CREATE TABLE foo (key INT PRIMARY KEY);`)
db.Exec(t, `INSERT INTO foo VALUES (1), (2), (3);`)

// initial_scan='only' is not required, but it makes testing this easier.
foo := feed(t, f, `CREATE CHANGEFEED WITH initial_scan='only' AS SELECT * FROM foo WHERE key = 1`)
defer closeFeed(t, foo)

done := make(chan struct{})
go func() {
defer close(done)
assertPayloads(t, foo, []string{`foo: [1]->{"key": 1}`})
}()

select {
case err := <-errCh:
// Ignore any error after the above assertion completed, because
// it's likely just due to feed shutdown.
select {
case <-done:
default:
t.Fatalf("unexpected error: %v", err)
}
case <-time.After(30 * time.Second):
t.Fatal("timed out")
case <-done:
return
}
}
cdcTest(t, testFn, withKnobsFn(knobsFn))
}

func assertReasonableMVCCTimestamp(t *testing.T, ts string) {
epochNanos := parseTimeToHLC(t, ts).WallTime
now := timeutil.Now()
Expand Down