forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: check unsupported types during schema changes and type/function …
…creation It is necessary to check the cluster version when building an expression that references a newly-added type, in order to ensure that all nodes support that type. Previously, these checks were omitted for casts in the vectorized engine, expressions for partial index predicates and check constraints, function parameters and return types, and user-defined composite types. This patch adds version-checking for function/procedure parameters and return types, as well as for user-defined types. It also augments the type-checking logic for casts and type annotations with a version check; this handles the remaining cases. The execution-time checks for cast expressions are left untouched, just in case the new type-checking logic misses important cases. For now, these changes only apply to the `PG_LSN` type, which will be new in 23.2. A future commit will add support for `REFCURSOR`, and will need to use the same checks. Informs cockroachdb#111560 Release note: None
- Loading branch information
1 parent
969d1c1
commit efc6267
Showing
12 changed files
with
276 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,163 @@ | ||
# LogicTest: local-mixed-22.2-23.1 | ||
# TODO(otan): add tests for mixed 23.1-23.2. | ||
# LogicTest: cockroach-go-testserver-upgrade-to-master | ||
|
||
statement error must be finalized to use pg_lsn | ||
SELECT '1010F/AAAA'::text::pg_lsn | ||
statement ok | ||
CREATE TABLE xy (x INT, y INT); | ||
|
||
statement error pg_lsn not supported until version 23.2 | ||
CREATE TABLE pg_lsn_table(id pg_lsn, val pg_lsn) | ||
# ---------------------------------------------------------------------- | ||
# Test PG_LSN references with all nodes running old binaries. | ||
# ---------------------------------------------------------------------- | ||
|
||
subtest all_old_cast | ||
|
||
# Cast to PG_LSN. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
SELECT 'A01F0/1AAA'::PG_LSN; | ||
|
||
# Cast to PG_LSN using the vectorized engine. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
SELECT 'a01f0/1aaa'::PG_LSN FROM generate_series(1, 100) LIMIT 1; | ||
|
||
subtest all_old_table | ||
|
||
# Table that references PG_LSN. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
CREATE TABLE t (x PG_LSN); | ||
|
||
# Add a PG_LSN column. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
ALTER TABLE xy ADD COLUMN curs PG_LSN; | ||
|
||
# Alter a column type to PG_LSN. | ||
statement ok | ||
SET enable_experimental_alter_column_type_general=true; | ||
|
||
statement error pgcode 0A000 unimplemented: this syntax | ||
ALTER TABLE xy ALTER COLUMN y TYPE PG_LSN; | ||
|
||
# Create a partial index that uses the PG_LSN type. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
CREATE INDEX part ON xy (x) WHERE y::PG_LSN < 'a01f0/1aaa'; | ||
|
||
# Add a check constraint that uses the PG_LSN type. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
ALTER TABLE xy ADD CONSTRAINT bar CHECK (y::PG_LSN < 'fffff100/100'); | ||
|
||
subtest all_old_type | ||
|
||
# UDT that references PG_LSN. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
CREATE TYPE typ AS (x INT, y PG_LSN); | ||
|
||
subtest all_old_function | ||
|
||
# Function that returns PG_LSN. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
CREATE OR REPLACE FUNCTION f() RETURNS PG_LSN AS $$ | ||
SELECT 'a01f0/1aaa'; | ||
$$ LANGUAGE SQL; | ||
|
||
# Function that takes PG_LSN argument. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
CREATE OR REPLACE FUNCTION f(curs PG_LSN) RETURNS STRING AS $$ | ||
SELECT curs; | ||
$$ LANGUAGE SQL; | ||
|
||
# Function that references PG_LSN internally. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$ | ||
SELECT 'a01f0/1aaa'::PG_LSN; | ||
SELECT 0; | ||
$$ LANGUAGE SQL; | ||
|
||
# Function that returns a composite type with PG_LSN component. | ||
statement error pgcode 0A000 unimplemented: this syntax | ||
CREATE FUNCTION f() RETURNS RECORD AS $$ | ||
SELECT (1, 'a01f0/1aaa'::PG_LSN, true); | ||
$$ LANGUAGE SQL; | ||
|
||
subtest end | ||
|
||
# ---------------------------------------------------------------------- | ||
# Verify that PG_LSN is not allowed after upgrading the gateway. | ||
# ---------------------------------------------------------------------- | ||
|
||
upgrade 0 | ||
|
||
user root nodeidx=0 | ||
|
||
subtest upgrade_one_cast | ||
|
||
# Cast to PG_LSN. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
SELECT 'a01f0/1aaa'::PG_LSN; | ||
|
||
# Cast to PG_LSN using the vectorized engine. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
SELECT 'a01f0/1aaa'::PG_LSN FROM generate_series(1, 100) LIMIT 1; | ||
|
||
subtest upgrade_one_table | ||
|
||
# Table that references PG_LSN. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
CREATE TABLE t (x PG_LSN); | ||
|
||
# Add a PG_LSN column. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
ALTER TABLE xy ADD COLUMN curs PG_LSN; | ||
|
||
# Alter a column type to PG_LSN. | ||
statement ok | ||
SET enable_experimental_alter_column_type_general=true; | ||
|
||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
ALTER TABLE xy ALTER COLUMN y TYPE PG_LSN; | ||
|
||
# Create a partial index that uses the PG_LSN type. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
CREATE INDEX part ON xy (x) WHERE y::PG_LSN < 'a01f0/1aaa'; | ||
|
||
# Add a check constraint that uses the PG_LSN type. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
ALTER TABLE xy ADD CONSTRAINT bar CHECK (y::PG_LSN < 'fffff100/100'); | ||
|
||
subtest upgrade_one_type | ||
|
||
# UDT that references PG_LSN. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
CREATE TYPE typ AS (x INT, y PG_LSN); | ||
|
||
subtest upgrade_one_function | ||
|
||
# Function that returns PG_LSN. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
CREATE OR REPLACE FUNCTION f() RETURNS PG_LSN AS $$ | ||
SELECT 'a01f0/1aaa'; | ||
$$ LANGUAGE SQL; | ||
|
||
# Function that takes PG_LSN argument. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
CREATE OR REPLACE FUNCTION f(curs PG_LSN) RETURNS INT AS $$ | ||
SELECT 0; | ||
$$ LANGUAGE SQL; | ||
|
||
# Function that references PG_LSN internally. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$ | ||
SELECT 'a01f0/1aaa'::PG_LSN; | ||
SELECT 0; | ||
$$ LANGUAGE SQL; | ||
|
||
# Function that returns a composite type with PG_LSN component. | ||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
CREATE FUNCTION f() RETURNS RECORD AS $$ | ||
SELECT (1, 'a01f0/1aaa'::PG_LSN, true); | ||
$$ LANGUAGE SQL; | ||
|
||
statement error pgcode 0A000 pq: pg_lsn not supported until version 23.2 | ||
CREATE FUNCTION f() RETURNS RECORD AS $$ | ||
BEGIN | ||
RETURN (1, 'a01f0/1aaa'::PG_LSN, true); | ||
END | ||
$$ LANGUAGE PLpgSQL; | ||
|
||
subtest end |
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
7 changes: 7 additions & 0 deletions
7
pkg/sql/logictest/tests/cockroach-go-testserver-upgrade-to-master/generated_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
pkg/sql/logictest/tests/local-mixed-22.2-23.1/generated_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,47 @@ | ||
// Copyright 2023 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 eval | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/clusterversion" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" | ||
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree" | ||
"github.com/cockroachdb/cockroach/pkg/sql/types" | ||
) | ||
|
||
type unsupportedTypeChecker struct { | ||
version clusterversion.Handle | ||
} | ||
|
||
// NewUnsupportedTypeChecker returns a new tree.UnsupportedTypeChecker that can | ||
// be used to check whether a type is allowed by the current cluster version. | ||
func NewUnsupportedTypeChecker(handle clusterversion.Handle) tree.UnsupportedTypeChecker { | ||
return &unsupportedTypeChecker{version: handle} | ||
} | ||
|
||
var _ tree.UnsupportedTypeChecker = &unsupportedTypeChecker{} | ||
|
||
// CheckType implements the tree.UnsupportedTypeChecker interface. | ||
func (tc *unsupportedTypeChecker) CheckType(ctx context.Context, typ *types.T) error { | ||
var errorTypeString string | ||
if typ.Family() == types.PGLSNFamily { | ||
errorTypeString = "pg_lsn" | ||
} | ||
if errorTypeString != "" && !tc.version.IsActive(ctx, clusterversion.V23_2) { | ||
return pgerror.Newf(pgcode.FeatureNotSupported, | ||
"%s not supported until version 23.2", errorTypeString, | ||
) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.