Skip to content

Commit 42ed92a

Browse files
committed
sessiondatapb: move one enum definition into lex package
This commit moves the definition of `BytesEncodeFormat` enum from `sessiondatapb` to `lex`. This is done in order to make `lex` not depend on a lot of stuff (eventually on `roachpb`) and is a part of the effort to clean up the dependencies of `execgen`. Note that the proto package name is not changed, so this change is backwards-compatible. Release note: None Release justification: low risk change to clean up the dependencies.
1 parent 9899691 commit 42ed92a

16 files changed

+147
-79
lines changed

pkg/gen/protobuf.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ PROTOBUF_SRCS = [
3838
"//pkg/sql/contentionpb:contentionpb_go_proto",
3939
"//pkg/sql/execinfrapb:execinfrapb_go_proto",
4040
"//pkg/sql/inverted:inverted_go_proto",
41+
"//pkg/sql/lex:lex_go_proto",
4142
"//pkg/sql/pgwire/pgerror:pgerror_go_proto",
4243
"//pkg/sql/protoreflect/test:protoreflecttest_go_proto",
4344
"//pkg/sql/rowenc/rowencpb:rowencpb_go_proto",

pkg/sql/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ go_library(
328328
"//pkg/sql/gcjob/gcjobnotifier",
329329
"//pkg/sql/idxusage",
330330
"//pkg/sql/inverted",
331+
"//pkg/sql/lex",
331332
"//pkg/sql/lexbase",
332333
"//pkg/sql/memsize",
333334
"//pkg/sql/mutations",

pkg/sql/exec_util.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import (
6464
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
6565
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
6666
"github.com/cockroachdb/cockroach/pkg/sql/gcjob/gcjobnotifier"
67+
"github.com/cockroachdb/cockroach/pkg/sql/lex"
6768
"github.com/cockroachdb/cockroach/pkg/sql/opt"
6869
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
6970
"github.com/cockroachdb/cockroach/pkg/sql/parser"
@@ -2806,7 +2807,7 @@ func (m *sessionDataMutator) SetAvoidBuffering(b bool) {
28062807
m.data.AvoidBuffering = b
28072808
}
28082809

2809-
func (m *sessionDataMutator) SetBytesEncodeFormat(val sessiondatapb.BytesEncodeFormat) {
2810+
func (m *sessionDataMutator) SetBytesEncodeFormat(val lex.BytesEncodeFormat) {
28102811
m.data.DataConversionConfig.BytesEncodeFormat = val
28112812
}
28122813

pkg/sql/lex/BUILD.bazel

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
13
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
24

35
go_library(
46
name = "lex",
57
srcs = ["encode.go"],
8+
embed = [":lex_go_proto"],
69
importpath = "github.com/cockroachdb/cockroach/pkg/sql/lex",
710
visibility = ["//visibility:public"],
811
deps = [
912
"//pkg/sql/pgwire/pgcode",
1013
"//pkg/sql/pgwire/pgerror",
11-
"//pkg/sql/sessiondatapb",
1214
"@com_github_cockroachdb_errors//:errors",
1315
"@org_golang_x_text//language",
1416
],
1517
)
1618

1719
go_test(
1820
name = "lex_test",
19-
srcs = ["encode_test.go"],
20-
deps = [
21-
":lex",
22-
"//pkg/sql/sessiondatapb",
21+
srcs = [
22+
"dep_test.go",
23+
"encode_test.go",
2324
],
25+
embed = [":lex"],
26+
deps = ["//pkg/testutils/buildutil"],
27+
)
28+
29+
proto_library(
30+
name = "lex_proto",
31+
srcs = ["encode.proto"],
32+
strip_import_prefix = "/pkg",
33+
visibility = ["//visibility:public"],
34+
deps = ["@com_github_gogo_protobuf//gogoproto:gogo_proto"],
35+
)
36+
37+
go_proto_library(
38+
name = "lex_go_proto",
39+
compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_compiler"],
40+
importpath = "github.com/cockroachdb/cockroach/pkg/sql/lex",
41+
proto = ":lex_proto",
42+
visibility = ["//visibility:public"],
43+
deps = ["@com_github_gogo_protobuf//gogoproto"],
2444
)

pkg/sql/lex/dep_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2022 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the file licenses/BSL.txt.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0, included in the file
9+
// licenses/APL.txt.
10+
11+
package lex
12+
13+
import (
14+
"testing"
15+
16+
"github.com/cockroachdb/cockroach/pkg/testutils/buildutil"
17+
)
18+
19+
func TestNoLinkForbidden(t *testing.T) {
20+
buildutil.VerifyNoImports(t,
21+
"github.com/cockroachdb/cockroach/pkg/sql/lex", true,
22+
[]string{
23+
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb",
24+
}, nil,
25+
)
26+
}

pkg/sql/lex/encode.go

+39-13
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ import (
2323
"bytes"
2424
"encoding/base64"
2525
"encoding/hex"
26+
"fmt"
27+
"strings"
2628
"unicode"
2729

2830
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
2931
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
30-
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
3132
"github.com/cockroachdb/errors"
3233
"golang.org/x/text/language"
3334
)
@@ -92,11 +93,9 @@ func LocaleNamesAreEqual(a, b string) bool {
9293
// If the skipHexPrefix argument is set, the hexadecimal encoding does not
9394
// prefix the output with "\x". This is suitable e.g. for the encode()
9495
// built-in.
95-
func EncodeByteArrayToRawBytes(
96-
data string, be sessiondatapb.BytesEncodeFormat, skipHexPrefix bool,
97-
) string {
96+
func EncodeByteArrayToRawBytes(data string, be BytesEncodeFormat, skipHexPrefix bool) string {
9897
switch be {
99-
case sessiondatapb.BytesEncodeHex:
98+
case BytesEncodeHex:
10099
head := 2
101100
if skipHexPrefix {
102101
head = 0
@@ -109,7 +108,7 @@ func EncodeByteArrayToRawBytes(
109108
hex.Encode(res[head:], []byte(data))
110109
return string(res)
111110

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

134-
case sessiondatapb.BytesEncodeBase64:
133+
case BytesEncodeBase64:
135134
return base64.StdEncoding.EncodeToString([]byte(data))
136135

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

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

191-
case sessiondatapb.BytesEncodeBase64:
190+
case BytesEncodeBase64:
192191
return base64.StdEncoding.DecodeString(data)
193192

194193
default:
@@ -201,7 +200,34 @@ func DecodeRawBytesToByteArray(data string, be sessiondatapb.BytesEncodeFormat)
201200
// and escape.
202201
func DecodeRawBytesToByteArrayAuto(data []byte) ([]byte, error) {
203202
if len(data) >= 2 && data[0] == '\\' && (data[1] == 'x' || data[1] == 'X') {
204-
return DecodeRawBytesToByteArray(string(data[2:]), sessiondatapb.BytesEncodeHex)
203+
return DecodeRawBytesToByteArray(string(data[2:]), BytesEncodeHex)
204+
}
205+
return DecodeRawBytesToByteArray(string(data), BytesEncodeEscape)
206+
}
207+
208+
func (f BytesEncodeFormat) String() string {
209+
switch f {
210+
case BytesEncodeHex:
211+
return "hex"
212+
case BytesEncodeEscape:
213+
return "escape"
214+
case BytesEncodeBase64:
215+
return "base64"
216+
default:
217+
return fmt.Sprintf("invalid (%d)", f)
218+
}
219+
}
220+
221+
// BytesEncodeFormatFromString converts a string into a BytesEncodeFormat.
222+
func BytesEncodeFormatFromString(val string) (_ BytesEncodeFormat, ok bool) {
223+
switch strings.ToUpper(val) {
224+
case "HEX":
225+
return BytesEncodeHex, true
226+
case "ESCAPE":
227+
return BytesEncodeEscape, true
228+
case "BASE64":
229+
return BytesEncodeBase64, true
230+
default:
231+
return -1, false
205232
}
206-
return DecodeRawBytesToByteArray(string(data), sessiondatapb.BytesEncodeEscape)
207233
}

pkg/sql/lex/encode.proto

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2022 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the Business Source License
4+
// included in the file licenses/BSL.txt.
5+
//
6+
// As of the Change Date specified in that file, in accordance with
7+
// the Business Source License, use of this software will be governed
8+
// by the Apache License, Version 2.0, included in the file
9+
// licenses/APL.txt.
10+
11+
syntax = "proto3";
12+
package cockroach.sql.sessiondatapb;
13+
option go_package = "lex";
14+
15+
import "gogoproto/gogo.proto";
16+
17+
// BytesEncodeFormat is the configuration for bytes to string conversions.
18+
enum BytesEncodeFormat {
19+
option (gogoproto.goproto_enum_prefix) = false;
20+
option (gogoproto.goproto_enum_stringer) = false;
21+
22+
// BytesEncodeHex uses the hex format: e'abc\n'::BYTES::STRING -> '\x61626312'.
23+
// This is the default, for compatibility with PostgreSQL.
24+
BytesEncodeHex = 0;
25+
// BytesEncodeEscape uses the escaped format: e'abc\n'::BYTES::STRING -> 'abc\012'.
26+
BytesEncodeEscape = 1;
27+
// BytesEncodeBase64 uses base64 encoding.
28+
BytesEncodeBase64 = 2;
29+
}

pkg/sql/lex/encode_test.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ import (
1515
"testing"
1616

1717
"github.com/cockroachdb/cockroach/pkg/sql/lex"
18-
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
1918
)
2019

2120
func TestByteArrayDecoding(t *testing.T) {
2221
const (
23-
fmtHex = sessiondatapb.BytesEncodeHex
24-
fmtEsc = sessiondatapb.BytesEncodeEscape
25-
fmtB64 = sessiondatapb.BytesEncodeBase64
22+
fmtHex = lex.BytesEncodeHex
23+
fmtEsc = lex.BytesEncodeEscape
24+
fmtB64 = lex.BytesEncodeBase64
2625
)
2726
testData := []struct {
2827
in string
2928
auto bool
30-
inFmt sessiondatapb.BytesEncodeFormat
29+
inFmt lex.BytesEncodeFormat
3130
out string
3231
err string
3332
}{
@@ -103,10 +102,10 @@ func TestByteArrayEncoding(t *testing.T) {
103102

104103
for _, s := range testData {
105104
t.Run(s.in, func(t *testing.T) {
106-
for _, format := range []sessiondatapb.BytesEncodeFormat{
107-
sessiondatapb.BytesEncodeHex,
108-
sessiondatapb.BytesEncodeEscape,
109-
sessiondatapb.BytesEncodeBase64,
105+
for _, format := range []lex.BytesEncodeFormat{
106+
lex.BytesEncodeHex,
107+
lex.BytesEncodeEscape,
108+
lex.BytesEncodeBase64,
110109
} {
111110
t.Run(format.String(), func(t *testing.T) {
112111
enc := lex.EncodeByteArrayToRawBytes(s.in, format, false)
@@ -116,7 +115,7 @@ func TestByteArrayEncoding(t *testing.T) {
116115
t.Fatalf("encoded %q, expected %q", enc, expEnc)
117116
}
118117

119-
if format == sessiondatapb.BytesEncodeHex {
118+
if format == lex.BytesEncodeHex {
120119
// Check that the \x also can be skipped.
121120
enc2 := lex.EncodeByteArrayToRawBytes(s.in, format, true)
122121
if enc[2:] != enc2 {

pkg/sql/pgwire/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ go_test(
108108
"//pkg/sql",
109109
"//pkg/sql/catalog/colinfo",
110110
"//pkg/sql/colconv",
111+
"//pkg/sql/lex",
111112
"//pkg/sql/parser",
112113
"//pkg/sql/pgwire/hba",
113114
"//pkg/sql/pgwire/identmap",

pkg/sql/pgwire/conn_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/cockroachdb/cockroach/pkg/security"
3232
"github.com/cockroachdb/cockroach/pkg/sql"
3333
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
34+
"github.com/cockroachdb/cockroach/pkg/sql/lex"
3435
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/hba"
3536
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
3637
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
@@ -569,7 +570,7 @@ func getSessionArgs(ln net.Listener, trustRemoteAddr bool) (net.Conn, sql.Sessio
569570

570571
func makeTestingConvCfg() (sessiondatapb.DataConversionConfig, *time.Location) {
571572
return sessiondatapb.DataConversionConfig{
572-
BytesEncodeFormat: sessiondatapb.BytesEncodeHex,
573+
BytesEncodeFormat: lex.BytesEncodeHex,
573574
}, time.UTC
574575
}
575576

pkg/sql/pgwire/types_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"github.com/cockroachdb/cockroach/pkg/col/coldataext"
2424
"github.com/cockroachdb/cockroach/pkg/col/coldatatestutils"
2525
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
26+
"github.com/cockroachdb/cockroach/pkg/sql/lex"
2627
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgwirebase"
2728
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
2829
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
29-
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
3030
"github.com/cockroachdb/cockroach/pkg/sql/types"
3131
"github.com/cockroachdb/cockroach/pkg/util/duration"
3232
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
@@ -203,9 +203,9 @@ func TestByteArrayRoundTrip(t *testing.T) {
203203
randValues = append(randValues, d)
204204
}
205205

206-
for _, be := range []sessiondatapb.BytesEncodeFormat{
207-
sessiondatapb.BytesEncodeHex,
208-
sessiondatapb.BytesEncodeEscape,
206+
for _, be := range []lex.BytesEncodeFormat{
207+
lex.BytesEncodeHex,
208+
lex.BytesEncodeEscape,
209209
} {
210210
t.Run(be.String(), func(t *testing.T) {
211211
for i, d := range randValues {

pkg/sql/sem/builtins/builtins.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ var builtins = map[string]builtinDefinition{
10981098
ReturnType: tree.FixedReturnType(types.String),
10991099
Fn: func(evalCtx *tree.EvalContext, args tree.Datums) (_ tree.Datum, err error) {
11001100
data, format := *args[0].(*tree.DBytes), string(tree.MustBeDString(args[1]))
1101-
be, ok := sessiondatapb.BytesEncodeFormatFromString(format)
1101+
be, ok := lex.BytesEncodeFormatFromString(format)
11021102
if !ok {
11031103
return nil, pgerror.New(pgcode.InvalidParameterValue,
11041104
"only 'hex', 'escape', and 'base64' formats are supported for encode()")
@@ -1117,7 +1117,7 @@ var builtins = map[string]builtinDefinition{
11171117
ReturnType: tree.FixedReturnType(types.Bytes),
11181118
Fn: func(evalCtx *tree.EvalContext, args tree.Datums) (_ tree.Datum, err error) {
11191119
data, format := string(tree.MustBeDString(args[0])), string(tree.MustBeDString(args[1]))
1120-
be, ok := sessiondatapb.BytesEncodeFormatFromString(format)
1120+
be, ok := lex.BytesEncodeFormatFromString(format)
11211121
if !ok {
11221122
return nil, pgerror.New(pgcode.InvalidParameterValue,
11231123
"only 'hex', 'escape', and 'base64' formats are supported for decode()")

pkg/sql/sessiondatapb/BUILD.bazel

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ proto_library(
3131
strip_import_prefix = "/pkg",
3232
visibility = ["//visibility:public"],
3333
deps = [
34+
"//pkg/sql/lex:lex_proto",
3435
"//pkg/util/duration:duration_proto",
3536
"//pkg/util/timeutil/pgdate:pgdate_proto",
3637
"@com_github_gogo_protobuf//gogoproto:gogo_proto",
@@ -46,6 +47,7 @@ go_proto_library(
4647
proto = ":sessiondatapb_proto",
4748
visibility = ["//visibility:public"],
4849
deps = [
50+
"//pkg/sql/lex",
4951
"//pkg/util/duration",
5052
"//pkg/util/timeutil/pgdate",
5153
"@com_github_gogo_protobuf//gogoproto",

0 commit comments

Comments
 (0)