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

types: remove no longer used version gate #77320

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions pkg/gen/protobuf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ PROTOBUF_SRCS = [
"//pkg/sql/contentionpb:contentionpb_go_proto",
"//pkg/sql/execinfrapb:execinfrapb_go_proto",
"//pkg/sql/inverted:inverted_go_proto",
"//pkg/sql/lex:lex_go_proto",
"//pkg/sql/pgwire/pgerror:pgerror_go_proto",
"//pkg/sql/protoreflect/test:protoreflecttest_go_proto",
"//pkg/sql/rowenc/rowencpb:rowencpb_go_proto",
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ go_library(
"//pkg/sql/gcjob/gcjobnotifier",
"//pkg/sql/idxusage",
"//pkg/sql/inverted",
"//pkg/sql/lex",
"//pkg/sql/lexbase",
"//pkg/sql/memsize",
"//pkg/sql/mutations",
Expand Down
4 changes: 0 additions & 4 deletions pkg/sql/add_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ func (p *planner) addColumnImpl(
)
}

if err := checkTypeIsSupported(params.ctx, params.ExecCfg().Settings, toType); err != nil {
return err
}

var colOwnedSeqDesc *tabledesc.Mutable
newDef, seqPrefix, seqName, seqOpts, err := params.p.processSerialLikeInColumnDef(params.ctx, d, tn)
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions pkg/sql/alter_column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ func AlterColumnType(
return err
}

if err := checkTypeIsSupported(ctx, params.ExecCfg().Settings, typ); err != nil {
return err
}

// Special handling for STRING COLLATE xy to verify that we recognize the language.
if t.Collation != "" {
if types.IsStringType(typ) {
Expand Down
15 changes: 0 additions & 15 deletions pkg/sql/create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1529,9 +1529,6 @@ func NewTableDesc(
)
}
}
if err := checkTypeIsSupported(ctx, st, defType); err != nil {
return nil, err
}
if d.PrimaryKey.Sharded {
if n.PartitionByTable.ContainsPartitions() && !n.PartitionByTable.All {
return nil, pgerror.New(pgcode.FeatureNotSupported, "hash sharded indexes cannot be explicitly partitioned")
Expand Down Expand Up @@ -2763,18 +2760,6 @@ func regionalByRowDefaultColDef(
return c
}

func checkTypeIsSupported(ctx context.Context, settings *cluster.Settings, typ *types.T) error {
version := settings.Version.ActiveVersionOrEmpty(ctx)
if supported := types.IsTypeSupportedInVersion(version, typ); !supported {
return pgerror.Newf(
pgcode.FeatureNotSupported,
"type %s is not supported until version upgrade is finalized",
typ.SQLString(),
)
}
return nil
}

// setSequenceOwner adds sequence id to the sequence id list owned by a column
// and set ownership values of sequence options.
func setSequenceOwner(
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/gcjob/gcjobnotifier"
"github.com/cockroachdb/cockroach/pkg/sql/lex"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
Expand Down Expand Up @@ -2806,7 +2807,7 @@ func (m *sessionDataMutator) SetAvoidBuffering(b bool) {
m.data.AvoidBuffering = b
}

func (m *sessionDataMutator) SetBytesEncodeFormat(val sessiondatapb.BytesEncodeFormat) {
func (m *sessionDataMutator) SetBytesEncodeFormat(val lex.BytesEncodeFormat) {
m.data.DataConversionConfig.BytesEncodeFormat = val
}

Expand Down
30 changes: 25 additions & 5 deletions pkg/sql/lex/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "lex",
srcs = ["encode.go"],
embed = [":lex_go_proto"],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/lex",
visibility = ["//visibility:public"],
deps = [
"//pkg/sql/pgwire/pgcode",
"//pkg/sql/pgwire/pgerror",
"//pkg/sql/sessiondatapb",
"@com_github_cockroachdb_errors//:errors",
"@org_golang_x_text//language",
],
)

go_test(
name = "lex_test",
srcs = ["encode_test.go"],
deps = [
":lex",
"//pkg/sql/sessiondatapb",
srcs = [
"dep_test.go",
"encode_test.go",
],
embed = [":lex"],
deps = ["//pkg/testutils/buildutil"],
)

proto_library(
name = "lex_proto",
srcs = ["encode.proto"],
strip_import_prefix = "/pkg",
visibility = ["//visibility:public"],
deps = ["@com_github_gogo_protobuf//gogoproto:gogo_proto"],
)

go_proto_library(
name = "lex_go_proto",
compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_compiler"],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/lex",
proto = ":lex_proto",
visibility = ["//visibility:public"],
deps = ["@com_github_gogo_protobuf//gogoproto"],
)
26 changes: 26 additions & 0 deletions pkg/sql/lex/dep_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 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 lex

import (
"testing"

"github.com/cockroachdb/cockroach/pkg/testutils/buildutil"
)

func TestNoLinkForbidden(t *testing.T) {
buildutil.VerifyNoImports(t,
"github.com/cockroachdb/cockroach/pkg/sql/lex", true,
[]string{
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb",
}, nil,
)
}
52 changes: 39 additions & 13 deletions pkg/sql/lex/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"unicode"

"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/errors"
"golang.org/x/text/language"
)
Expand Down Expand Up @@ -92,11 +93,9 @@ func LocaleNamesAreEqual(a, b string) bool {
// If the skipHexPrefix argument is set, the hexadecimal encoding does not
// prefix the output with "\x". This is suitable e.g. for the encode()
// built-in.
func EncodeByteArrayToRawBytes(
data string, be sessiondatapb.BytesEncodeFormat, skipHexPrefix bool,
) string {
func EncodeByteArrayToRawBytes(data string, be BytesEncodeFormat, skipHexPrefix bool) string {
switch be {
case sessiondatapb.BytesEncodeHex:
case BytesEncodeHex:
head := 2
if skipHexPrefix {
head = 0
Expand All @@ -109,7 +108,7 @@ func EncodeByteArrayToRawBytes(
hex.Encode(res[head:], []byte(data))
return string(res)

case sessiondatapb.BytesEncodeEscape:
case BytesEncodeEscape:
// PostgreSQL does not allow all the escapes formats recognized by
// CockroachDB's scanner. It only recognizes octal and \\ for the
// backslash itself.
Expand All @@ -131,7 +130,7 @@ func EncodeByteArrayToRawBytes(
}
return string(res)

case sessiondatapb.BytesEncodeBase64:
case BytesEncodeBase64:
return base64.StdEncoding.EncodeToString([]byte(data))

default:
Expand All @@ -144,12 +143,12 @@ func EncodeByteArrayToRawBytes(
// When using the Hex format, the caller is responsible for skipping the
// "\x" prefix, if any. See DecodeRawBytesToByteArrayAuto() below for
// an alternative.
func DecodeRawBytesToByteArray(data string, be sessiondatapb.BytesEncodeFormat) ([]byte, error) {
func DecodeRawBytesToByteArray(data string, be BytesEncodeFormat) ([]byte, error) {
switch be {
case sessiondatapb.BytesEncodeHex:
case BytesEncodeHex:
return hex.DecodeString(data)

case sessiondatapb.BytesEncodeEscape:
case BytesEncodeEscape:
// PostgreSQL does not allow all the escapes formats recognized by
// CockroachDB's scanner. It only recognizes octal and \\ for the
// backslash itself.
Expand Down Expand Up @@ -188,7 +187,7 @@ func DecodeRawBytesToByteArray(data string, be sessiondatapb.BytesEncodeFormat)
}
return res, nil

case sessiondatapb.BytesEncodeBase64:
case BytesEncodeBase64:
return base64.StdEncoding.DecodeString(data)

default:
Expand All @@ -201,7 +200,34 @@ func DecodeRawBytesToByteArray(data string, be sessiondatapb.BytesEncodeFormat)
// and escape.
func DecodeRawBytesToByteArrayAuto(data []byte) ([]byte, error) {
if len(data) >= 2 && data[0] == '\\' && (data[1] == 'x' || data[1] == 'X') {
return DecodeRawBytesToByteArray(string(data[2:]), sessiondatapb.BytesEncodeHex)
return DecodeRawBytesToByteArray(string(data[2:]), BytesEncodeHex)
}
return DecodeRawBytesToByteArray(string(data), BytesEncodeEscape)
}

func (f BytesEncodeFormat) String() string {
switch f {
case BytesEncodeHex:
return "hex"
case BytesEncodeEscape:
return "escape"
case BytesEncodeBase64:
return "base64"
default:
return fmt.Sprintf("invalid (%d)", f)
}
}

// BytesEncodeFormatFromString converts a string into a BytesEncodeFormat.
func BytesEncodeFormatFromString(val string) (_ BytesEncodeFormat, ok bool) {
switch strings.ToUpper(val) {
case "HEX":
return BytesEncodeHex, true
case "ESCAPE":
return BytesEncodeEscape, true
case "BASE64":
return BytesEncodeBase64, true
default:
return -1, false
}
return DecodeRawBytesToByteArray(string(data), sessiondatapb.BytesEncodeEscape)
}
29 changes: 29 additions & 0 deletions pkg/sql/lex/encode.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022 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.

syntax = "proto3";
package cockroach.sql.sessiondatapb;
option go_package = "lex";

import "gogoproto/gogo.proto";

// BytesEncodeFormat is the configuration for bytes to string conversions.
enum BytesEncodeFormat {
option (gogoproto.goproto_enum_prefix) = false;
option (gogoproto.goproto_enum_stringer) = false;

// BytesEncodeHex uses the hex format: e'abc\n'::BYTES::STRING -> '\x61626312'.
// This is the default, for compatibility with PostgreSQL.
BytesEncodeHex = 0;
// BytesEncodeEscape uses the escaped format: e'abc\n'::BYTES::STRING -> 'abc\012'.
BytesEncodeEscape = 1;
// BytesEncodeBase64 uses base64 encoding.
BytesEncodeBase64 = 2;
}
19 changes: 9 additions & 10 deletions pkg/sql/lex/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ import (
"testing"

"github.com/cockroachdb/cockroach/pkg/sql/lex"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
)

func TestByteArrayDecoding(t *testing.T) {
const (
fmtHex = sessiondatapb.BytesEncodeHex
fmtEsc = sessiondatapb.BytesEncodeEscape
fmtB64 = sessiondatapb.BytesEncodeBase64
fmtHex = lex.BytesEncodeHex
fmtEsc = lex.BytesEncodeEscape
fmtB64 = lex.BytesEncodeBase64
)
testData := []struct {
in string
auto bool
inFmt sessiondatapb.BytesEncodeFormat
inFmt lex.BytesEncodeFormat
out string
err string
}{
Expand Down Expand Up @@ -103,10 +102,10 @@ func TestByteArrayEncoding(t *testing.T) {

for _, s := range testData {
t.Run(s.in, func(t *testing.T) {
for _, format := range []sessiondatapb.BytesEncodeFormat{
sessiondatapb.BytesEncodeHex,
sessiondatapb.BytesEncodeEscape,
sessiondatapb.BytesEncodeBase64,
for _, format := range []lex.BytesEncodeFormat{
lex.BytesEncodeHex,
lex.BytesEncodeEscape,
lex.BytesEncodeBase64,
} {
t.Run(format.String(), func(t *testing.T) {
enc := lex.EncodeByteArrayToRawBytes(s.in, format, false)
Expand All @@ -116,7 +115,7 @@ func TestByteArrayEncoding(t *testing.T) {
t.Fatalf("encoded %q, expected %q", enc, expEnc)
}

if format == sessiondatapb.BytesEncodeHex {
if format == lex.BytesEncodeHex {
// Check that the \x also can be skipped.
enc2 := lex.EncodeByteArrayToRawBytes(s.in, format, true)
if enc[2:] != enc2 {
Expand Down
16 changes: 3 additions & 13 deletions pkg/sql/schemachanger/scbuild/builder_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
package scbuild

import (
"context"
"strings"

"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catconstants"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
Expand Down Expand Up @@ -298,18 +296,10 @@ func (b *builderState) ResolveTypeRef(ref tree.ResolvableTypeReference) scpb.Typ
if err != nil {
panic(err)
}
return newTypeT(b.ctx, b.clusterSettings, toType)
return newTypeT(toType)
}

func newTypeT(ctx context.Context, settings *cluster.Settings, t *types.T) scpb.TypeT {
version := settings.Version.ActiveVersionOrEmpty(ctx)
supported := types.IsTypeSupportedInVersion(version, t)
if !supported {
panic(pgerror.Newf(pgcode.FeatureNotSupported,
"type %s is not supported until version upgrade is finalized", t.SQLString(),
))
}

func newTypeT(t *types.T) scpb.TypeT {
m, err := typedesc.GetTypeDescriptorClosure(t)
if err != nil {
panic(err)
Expand Down Expand Up @@ -416,7 +406,7 @@ func (b *builderState) ComputedColumnExpression(
if err != nil {
panic(err)
}
return parsedExpr, newTypeT(b.ctx, b.clusterSettings, typ)
return parsedExpr, newTypeT(typ)
}

var _ scbuildstmt.ElementReferences = (*builderState)(nil)
Expand Down
Loading