Skip to content

Commit f08d331

Browse files
committed
sem/eval: break dependency on sql/parser
This is achieved by injecting `parser.ParseOne` when initializing the sql package. Release note: None
1 parent 22ebc1c commit f08d331

File tree

7 files changed

+30
-3
lines changed

7 files changed

+30
-3
lines changed

pkg/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ ALL_TESTS = [
568568
"//pkg/sql/opt:opt_test",
569569
"//pkg/sql/parser:parser_disallowed_imports_test",
570570
"//pkg/sql/parser:parser_test",
571+
"//pkg/sql/parserutils:parserutils_disallowed_imports_test",
571572
"//pkg/sql/pgrepl/pgreplparser:pgreplparser_test",
572573
"//pkg/sql/pgrepl:pgrepl_test",
573574
"//pkg/sql/pgwire/hba:hba_test",

pkg/sql/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ go_library(
463463
"//pkg/sql/paramparse",
464464
"//pkg/sql/parser",
465465
"//pkg/sql/parser/statements",
466+
"//pkg/sql/parserutils",
466467
"//pkg/sql/pgrepl/lsn",
467468
"//pkg/sql/pgrepl/lsnutil",
468469
"//pkg/sql/pgrepl/pgrepltree",

pkg/sql/exec_util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import (
7474
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
7575
"github.com/cockroachdb/cockroach/pkg/sql/parser"
7676
"github.com/cockroachdb/cockroach/pkg/sql/parser/statements"
77+
"github.com/cockroachdb/cockroach/pkg/sql/parserutils"
7778
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
7879
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
7980
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
@@ -144,6 +145,13 @@ func init() {
144145
ie := evalCtx.JobExecContext.(JobExecContext).ExecCfg().InternalDB.Executor()
145146
return ie.QueryIteratorEx(ctx, opName, txn, override, stmt, qargs...)
146147
}
148+
DoParserInjection()
149+
}
150+
151+
// DoParserInjection performs all the necessary sql/parser injections within the
152+
// sql directory.
153+
func DoParserInjection() {
154+
parserutils.ParseOne = parser.ParseOne
147155
}
148156

149157
// ClusterOrganization is the organization name.

pkg/sql/parserutils/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
load("@io_bazel_rules_go//go:def.bzl", "go_library")
2+
load("//pkg/testutils:buildutil/buildutil.bzl", "disallowed_imports_test")
23

34
go_library(
45
name = "parserutils",
56
srcs = ["utils.go"],
67
importpath = "github.com/cockroachdb/cockroach/pkg/sql/parserutils",
78
visibility = ["//visibility:public"],
89
deps = [
10+
"//pkg/sql/parser/statements",
911
"//pkg/sql/pgwire/pgcode",
1012
"//pkg/sql/pgwire/pgerror",
13+
"//pkg/sql/sem/tree",
1114
"@com_github_cockroachdb_errors//:errors",
1215
],
1316
)
17+
18+
disallowed_imports_test(
19+
src = "parserutils",
20+
disallowed_list = ["//pkg/sql/parser"],
21+
)

pkg/sql/parserutils/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import (
1010
"fmt"
1111
"strings"
1212

13+
"github.com/cockroachdb/cockroach/pkg/sql/parser/statements"
1314
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
1415
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
16+
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
1517
"github.com/cockroachdb/errors"
1618
)
1719

@@ -54,3 +56,9 @@ func PopulateErrorDetails(
5456
fmt.Fprintf(&buf, "%s^", strings.Repeat(" ", int(lastTokPos)-j))
5557
return errors.WithDetail(retErr, buf.String())
5658
}
59+
60+
// ParseOne is the same as sql/parser.ParseOne but is injected to avoid a
61+
// dependency on the parser package.
62+
var ParseOne = func(sql string) (statements.Statement[tree.Statement], error) {
63+
return statements.Statement[tree.Statement]{}, errors.AssertionFailedf("sql.DoParserInjection hasn't been called")
64+
}

pkg/sql/sem/eval/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ go_library(
5757
"//pkg/sql/catalog/descpb",
5858
"//pkg/sql/lex",
5959
"//pkg/sql/oidext",
60-
"//pkg/sql/parser",
60+
"//pkg/sql/parserutils",
6161
"//pkg/sql/pgrepl/lsn",
6262
"//pkg/sql/pgwire/pgcode",
6363
"//pkg/sql/pgwire/pgerror",
@@ -170,5 +170,6 @@ disallowed_imports_test(
170170
src = "eval",
171171
disallowed_list = [
172172
"//pkg/sql/catalog",
173+
"//pkg/sql/parser",
173174
],
174175
)

pkg/sql/sem/eval/parse_doid.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"regexp"
1212
"strings"
1313

14-
"github.com/cockroachdb/cockroach/pkg/sql/parser"
14+
"github.com/cockroachdb/cockroach/pkg/sql/parserutils"
1515
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
1616
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
1717
"github.com/cockroachdb/cockroach/pkg/sql/sem/catid"
@@ -93,7 +93,7 @@ func ParseDOid(ctx context.Context, evalCtx *Context, s string, t *types.T) (*tr
9393
// function signature syntax is sane from grammar perspective. We may
9494
// match postgres' implementation of `parseNameAndArgTypes` to return
9595
// more detailed errors like "expected a left parenthesis".
96-
stmt, err := parser.ParseOne("ALTER FUNCTION " + strings.TrimSpace(s) + " IMMUTABLE")
96+
stmt, err := parserutils.ParseOne("ALTER FUNCTION " + strings.TrimSpace(s) + " IMMUTABLE")
9797
if err != nil {
9898
return nil, errors.Wrapf(err, "invalid function signature: %s", s)
9999
}

0 commit comments

Comments
 (0)