-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: NFC-normalization on double-quoted identifiers
There was a lack of NFC-normalization on double-quoted identifiers. This allowed for ambiguous table/db/column names. This change adds normalization and prevents this case. Release note: None
- Loading branch information
Showing
3 changed files
with
77 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2021 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package sql_test | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNFCNormalization(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
defer log.Scope(t).Close(t) | ||
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{Insecure: true}) | ||
defer s.Stopper().Stop(context.Background()) | ||
defer db.Close() | ||
|
||
_, err := db.Exec("CREATE TABLE café (a INT)") | ||
require.NoError(t, err) | ||
|
||
_, err = db.Exec(`CREATE TABLE "Café" (a INT)`) | ||
require.NoError(t, err) | ||
|
||
_, err = db.Exec("CREATE TABLE Cafe\u0301 (a INT)") | ||
require.Errorf(t, err, "The tables should be considered duplicates when normalized") | ||
require.True(t, strings.Contains(err.Error(), "already exists")) | ||
|
||
_, err = db.Exec("CREATE TABLE cafe\u0301 (a INT)") | ||
require.Errorf(t, err, "The tables should be considered duplicates when normalized") | ||
require.True(t, strings.Contains(err.Error(), "already exists")) | ||
|
||
_, err = db.Exec("CREATE TABLE caf\u00E9 (a INT)") | ||
require.Errorf(t, err, "The tables should be considered duplicates when normalized") | ||
require.True(t, strings.Contains(err.Error(), "already exists")) | ||
|
||
_, err = db.Exec("CREATE TABLE \"caf\u00E9\" (a INT)") | ||
require.Errorf(t, err, "The tables should be considered duplicates when normalized") | ||
require.True(t, strings.Contains(err.Error(), "already exists")) | ||
|
||
_, err = db.Exec("CREATE TABLE \"cafe\u0301\" (a INT)") | ||
require.Errorf(t, err, "The tables should be considered duplicates when normalized") | ||
require.True(t, strings.Contains(err.Error(), "already exists")) | ||
|
||
//Ensure normal strings are not normalized like double quoted strings | ||
var b bool | ||
err = db.QueryRow("SELECT 'caf\u00E9' = 'cafe\u0301'").Scan(&b) | ||
require.NoError(t, err) | ||
require.False(t, b) | ||
|
||
} |
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