-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: fix UPSERT fast path regression
And add a test to prevent future regressions. A second bug was unearthed which caused the upsert fast path to be incorrect during schema changes. This was caught by the schema change tests when the regression was fixed. Closes #13437.
- Loading branch information
Showing
4 changed files
with
96 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2017 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
// | ||
// Author: Daniel Harrison (dan@cockroachlabs.com) | ||
|
||
package sql_test | ||
|
||
import ( | ||
"bytes" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/keys" | ||
"github.com/cockroachdb/cockroach/pkg/roachpb" | ||
"github.com/cockroachdb/cockroach/pkg/storage" | ||
"github.com/cockroachdb/cockroach/pkg/storage/storagebase" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
) | ||
|
||
func TestUpsertFastPath(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
// This filter increments scans for every ScanRequest that hits user table | ||
// data. | ||
var scans uint64 | ||
filter := func(filterArgs storagebase.FilterArgs) *roachpb.Error { | ||
if filterArgs.Req.Method() == roachpb.Scan && | ||
bytes.Compare(filterArgs.Req.Header().Key, keys.UserTableDataMin) >= 0 { | ||
atomic.AddUint64(&scans, 1) | ||
} | ||
return nil | ||
} | ||
|
||
s, conn, _ := serverutils.StartServer(t, base.TestServerArgs{ | ||
Knobs: base.TestingKnobs{Store: &storage.StoreTestingKnobs{ | ||
TestingCommandFilter: filter, | ||
}}, | ||
}) | ||
defer s.Stopper().Stop() | ||
sqlDB := sqlutils.MakeSQLRunner(t, conn) | ||
sqlDB.Exec(`CREATE DATABASE d`) | ||
sqlDB.Exec(`CREATE TABLE d.kv (k INT PRIMARY KEY, v INT)`) | ||
|
||
// This should hit the fast path. | ||
atomic.StoreUint64(&scans, 0) | ||
sqlDB.Exec(`UPSERT INTO d.kv VALUES (1, 1)`) | ||
if s := atomic.LoadUint64(&scans); s != 0 { | ||
t.Errorf("expected no scans (the upsert fast path) but got %d", s) | ||
} | ||
|
||
// This should hit the fast path. | ||
atomic.StoreUint64(&scans, 0) | ||
sqlDB.Exec(`INSERT INTO d.kv VALUES (1, 1) ON CONFLICT (k) DO UPDATE SET v=excluded.v`) | ||
if s := atomic.LoadUint64(&scans); s != 0 { | ||
t.Errorf("expected no scans (the upsert fast path) but got %d", s) | ||
} | ||
|
||
// This should not hit the fast path because it doesn't set every column. | ||
atomic.StoreUint64(&scans, 0) | ||
sqlDB.Exec(`UPSERT INTO d.kv (k) VALUES (1)`) | ||
if s := atomic.LoadUint64(&scans); s != 1 { | ||
t.Errorf("expected 1 scans (no upsert fast path) but got %d", s) | ||
} | ||
|
||
// This should not hit the fast path because kv has a secondary index. | ||
sqlDB.Exec(`CREATE INDEX vidx ON d.kv (v)`) | ||
atomic.StoreUint64(&scans, 0) | ||
sqlDB.Exec(`UPSERT INTO d.kv VALUES (1, 1)`) | ||
if s := atomic.LoadUint64(&scans); s != 1 { | ||
t.Errorf("expected 1 scans (no upsert fast path) but got %d", s) | ||
} | ||
} |