Skip to content

Commit 629623a

Browse files
craig[bot]DrewKimball
andcommitted
Merge #155864
155864: hints: add builtin for inserting "hint-injection" statement hints r=michae2 a=DrewKimball #### hints: separate out hint protobuf into separate package This commit adds a new package `hintpb` for the protobuf structs used to store hints in the `system.statement_hints` table. This will avoid dependency cycles in future commits. Epic: None Release note: None #### hints: add injection hint and refactor usage of hint union This commit adds the protobuf struct for injecting "internal" hints like (index or join hints) into a statement's AST. This can be serialized and inserted into the `system.statement_hints` table. This commit also refactors the way hints are represented in memory. Informs #153633 Release note: None #### sql/hints: extract hints system table interfacing logic This commit extracts helper functions used by the `hints` package to interface with the `system.statement_hints` table and adds tests for the helpers. Epic: None Release note: None #### sql,hints: add builtin function to add statement hint for hint "injection" This commit adds a new builtin function for inserting "hint injection" hints into the `system.statement_hints` table. Currently, the provided hinted SQL is serialized as-is into the hint. A future PR will add validation to ensure that the hinted SQL matches the fingerprint, and that the hints can be transferred. Informs #153633 Release note: None Co-authored-by: Drew Kimball <[email protected]>
2 parents 888ba33 + 59499db commit 629623a

File tree

33 files changed

+621
-142
lines changed

33 files changed

+621
-142
lines changed

pkg/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ ALL_TESTS = [
500500
"//pkg/sql/gcjob/gcjobnotifier:gcjobnotifier_test",
501501
"//pkg/sql/gcjob:gcjob_test",
502502
"//pkg/sql/gcjob_test:gcjob_test_test",
503+
"//pkg/sql/hintpb:hintpb_test",
503504
"//pkg/sql/hints:hints_test",
504505
"//pkg/sql/idxrecommendations:idxrecommendations_test",
505506
"//pkg/sql/idxusage:idxusage_test",
@@ -2054,6 +2055,8 @@ GO_TARGETS = [
20542055
"//pkg/sql/gcjob:gcjob",
20552056
"//pkg/sql/gcjob:gcjob_test",
20562057
"//pkg/sql/gcjob_test:gcjob_test_test",
2058+
"//pkg/sql/hintpb:hintpb",
2059+
"//pkg/sql/hintpb:hintpb_test",
20572060
"//pkg/sql/hints:hints",
20582061
"//pkg/sql/hints:hints_test",
20592062
"//pkg/sql/idxrecommendations:idxrecommendations",

pkg/ccl/logictestccl/tests/3node-tenant/generated_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ccl/logictestccl/tests/local-read-committed/generated_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ccl/logictestccl/tests/local-repeatable-read/generated_test.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/gen/protobuf.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ PROTOBUF_SRCS = [
6363
"//pkg/sql/catalog/schematelemetry/schematelemetrycontroller:schematelemetrycontroller_go_proto",
6464
"//pkg/sql/contentionpb:contentionpb_go_proto",
6565
"//pkg/sql/execinfrapb:execinfrapb_go_proto",
66-
"//pkg/sql/hints:hints_go_proto",
66+
"//pkg/sql/hintpb:hintpb_go_proto",
6767
"//pkg/sql/inverted:inverted_go_proto",
6868
"//pkg/sql/lex:lex_go_proto",
6969
"//pkg/sql/pgwire/pgerror:pgerror_go_proto",

pkg/sql/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ go_library(
442442
"//pkg/sql/faketreeeval",
443443
"//pkg/sql/flowinfra",
444444
"//pkg/sql/gcjob/gcjobnotifier",
445+
"//pkg/sql/hintpb",
445446
"//pkg/sql/hints",
446447
"//pkg/sql/idxrecommendations",
447448
"//pkg/sql/idxusage",

pkg/sql/faketreeeval/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ go_library(
1111
"//pkg/roachpb",
1212
"//pkg/security/username",
1313
"//pkg/sql/catalog/descpb",
14+
"//pkg/sql/hintpb",
1415
"//pkg/sql/pgwire/pgcode",
1516
"//pkg/sql/pgwire/pgerror",
1617
"//pkg/sql/pgwire/pgnotice",

pkg/sql/faketreeeval/evalctx.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/cockroachdb/cockroach/pkg/roachpb"
1616
"github.com/cockroachdb/cockroach/pkg/security/username"
1717
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
18+
"github.com/cockroachdb/cockroach/pkg/sql/hintpb"
1819
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
1920
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
2021
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
@@ -589,6 +590,13 @@ func (ep *DummyEvalPlanner) ProcessVectorIndexFixups(
589590
return nil
590591
}
591592

593+
// InsertStatementHint is part of the eval.Planner interface.
594+
func (ep *DummyEvalPlanner) InsertStatementHint(
595+
ctx context.Context, statementFingerprint string, hint hintpb.StatementHintUnion,
596+
) (int64, error) {
597+
return 0, nil
598+
}
599+
592600
// DummyPrivilegedAccessor implements the tree.PrivilegedAccessor interface by returning errors.
593601
type DummyPrivilegedAccessor struct{}
594602

pkg/sql/hintpb/BUILD.bazel

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
3+
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
4+
5+
proto_library(
6+
name = "hintpb_proto",
7+
srcs = ["statement_hint.proto"],
8+
strip_import_prefix = "/pkg",
9+
visibility = ["//visibility:public"],
10+
deps = ["@com_github_gogo_protobuf//gogoproto:gogo_proto"],
11+
)
12+
13+
go_proto_library(
14+
name = "hintpb_go_proto",
15+
compilers = ["//pkg/cmd/protoc-gen-gogoroach:protoc-gen-gogoroach_compiler"],
16+
importpath = "github.com/cockroachdb/cockroach/pkg/sql/hintpb",
17+
proto = ":hintpb_proto",
18+
visibility = ["//visibility:public"],
19+
deps = ["@com_github_gogo_protobuf//gogoproto"],
20+
)
21+
22+
go_library(
23+
name = "hintpb",
24+
srcs = ["statement_hint.go"],
25+
embed = [":hintpb_go_proto"],
26+
importpath = "github.com/cockroachdb/cockroach/pkg/sql/hintpb",
27+
visibility = ["//visibility:public"],
28+
deps = [
29+
"//pkg/util/protoutil",
30+
"@com_github_cockroachdb_errors//:errors",
31+
],
32+
)
33+
34+
go_test(
35+
name = "hintpb_test",
36+
srcs = ["statement_hint_test.go"],
37+
embed = [":hintpb"],
38+
deps = ["@com_github_stretchr_testify//require"],
39+
)

pkg/sql/hintpb/statement_hint.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2025 The Cockroach Authors.
2+
//
3+
// Use of this software is governed by the CockroachDB Software License
4+
// included in the /LICENSE file.
5+
6+
package hintpb
7+
8+
import (
9+
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
10+
"github.com/cockroachdb/errors"
11+
)
12+
13+
// FromBytes converts the raw bytes from system.statement_hints into a
14+
// StatementHintUnion object.
15+
func FromBytes(bytes []byte) (StatementHintUnion, error) {
16+
res := StatementHintUnion{}
17+
if err := protoutil.Unmarshal(bytes, &res); err != nil {
18+
return StatementHintUnion{}, err
19+
}
20+
if res.GetValue() == nil {
21+
return StatementHintUnion{}, errors.New("invalid hint bytes: no value set")
22+
}
23+
return res, nil
24+
}
25+
26+
// ToBytes converts the StatementHintUnion to a raw bytes representation that
27+
// can be inserted into the system.statement_hints table.
28+
func ToBytes(hint StatementHintUnion) ([]byte, error) {
29+
if hint.GetValue() == nil {
30+
return nil, errors.New("cannot convert empty hint to bytes")
31+
}
32+
return protoutil.Marshal(&hint)
33+
}

0 commit comments

Comments
 (0)