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

parser, infoschema, executor: Add information_schema.keywords (#48807) #54381

Closed
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ bazel-tidb
MODULE.bazel.lock
.ijwb/
/oom_record/
*.log.json
genkeyword
3 changes: 2 additions & 1 deletion pkg/executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2097,7 +2097,8 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) exec.Ex
strings.ToLower(infoschema.ClusterTableMemoryUsageOpsHistory),
strings.ToLower(infoschema.TableResourceGroups),
strings.ToLower(infoschema.TableRunawayWatches),
strings.ToLower(infoschema.TableCheckConstraints):
strings.ToLower(infoschema.TableCheckConstraints),
strings.ToLower(infoschema.TableKeywords):
memTracker := memory.NewTracker(v.ID(), -1)
memTracker.AttachTo(b.ctx.GetSessionVars().StmtCtx.MemTracker)
return &MemTableReaderExec{
Expand Down
13 changes: 13 additions & 0 deletions pkg/executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/pingcap/tidb/pkg/infoschema"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/meta/autoid"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/parser/charset"
"github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
Expand Down Expand Up @@ -197,6 +198,8 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
err = e.setDataFromRunawayWatches(sctx)
case infoschema.TableCheckConstraints:
err = e.setDataFromCheckConstraints(sctx, dbs)
case infoschema.TableKeywords:
err = e.setDataFromKeywords()
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -3390,6 +3393,16 @@ func (e *memtableRetriever) setDataFromResourceGroups() error {
return nil
}

func (e *memtableRetriever) setDataFromKeywords() error {
rows := make([][]types.Datum, 0, len(parser.Keywords))
for _, kw := range parser.Keywords {
row := types.MakeDatums(kw.Word, kw.Reserved)
rows = append(rows, row)
}
e.rows = rows
return nil
}

func checkRule(rule *label.Rule) (dbName, tableName string, partitionName string, err error) {
s := strings.Split(rule.ID, "/")
if len(s) < 3 {
Expand Down
8 changes: 8 additions & 0 deletions pkg/executor/infoschema_reader_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ func TestSetDataFromCheckConstraints(t *testing.T) {
require.Equal(t, types.NewStringDatum("t2_c1"), mt.rows[0][2])
require.Equal(t, types.NewStringDatum("(id<10)"), mt.rows[0][3])
}

func TestSetDataFromKeywords(t *testing.T) {
mt := memtableRetriever{}
err := mt.setDataFromKeywords()
require.NoError(t, err)
require.Equal(t, types.NewStringDatum("ADD"), mt.rows[0][0]) // Keyword: ADD
require.Equal(t, types.NewIntDatum(1), mt.rows[0][1]) // Reserved: true(1)
}
9 changes: 9 additions & 0 deletions pkg/infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ const (
TableRunawayWatches = "RUNAWAY_WATCHES"
// TableCheckConstraints is the list of CHECK constraints.
TableCheckConstraints = "CHECK_CONSTRAINTS"
// TableKeywords is the list of keywords.
TableKeywords = "KEYWORDS"
)

const (
Expand Down Expand Up @@ -318,6 +320,7 @@ var tableIDMap = map[string]int64{
TableResourceGroups: autoid.InformationSchemaDBID + 88,
TableRunawayWatches: autoid.InformationSchemaDBID + 89,
TableCheckConstraints: autoid.InformationSchemaDBID + 90,
TableKeywords: autoid.InformationSchemaDBID + 92,
}

// columnInfo represents the basic column information of all kinds of INFORMATION_SCHEMA tables
Expand Down Expand Up @@ -1648,6 +1651,11 @@ var tableCheckConstraintsCols = []columnInfo{
{name: "CHECK_CLAUSE", tp: mysql.TypeLongBlob, size: types.UnspecifiedLength, flag: mysql.NotNullFlag},
}

var tableKeywords = []columnInfo{
{name: "WORD", tp: mysql.TypeVarchar, size: 128},
{name: "RESERVED", tp: mysql.TypeLong, size: 11},
}

// GetShardingInfo returns a nil or description string for the sharding information of given TableInfo.
// The returned description string may be:
// - "NOT_SHARDED": for tables that SHARD_ROW_ID_BITS is not specified.
Expand Down Expand Up @@ -2166,6 +2174,7 @@ var tableNameToColumns = map[string][]columnInfo{
TableResourceGroups: tableResourceGroupsCols,
TableRunawayWatches: tableRunawayWatchListCols,
TableCheckConstraints: tableCheckConstraintsCols,
TableKeywords: tableKeywords,
}

func createInfoSchemaTable(_ autoid.Allocators, meta *model.TableInfo) (table.Table, error) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/parser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ go_library(
name = "parser",
srcs = [
"digester.go",
"generate.go",
"hintparser.go",
"hintparserimpl.go",
"keywords.go",
"lexer.go",
"misc.go",
"parser.go",
Expand Down Expand Up @@ -41,6 +43,7 @@ go_test(
"consistent_test.go",
"digester_test.go",
"hintparser_test.go",
"keywords_test.go",
"lexer_test.go",
"main_test.go",
"parser_test.go",
Expand Down
8 changes: 7 additions & 1 deletion pkg/parser/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
.PHONY: all parser clean

all: fmt parser
all: fmt parser generate

test: fmt parser
sh test.sh

parser: parser.go hintparser.go

genkeyword: generate_keyword/genkeyword.go
go build -C generate_keyword -o ../genkeyword

generate: genkeyword parser.y
go generate

%arser.go: prefix = $(@:parser.go=)
%arser.go: %arser.y bin/goyacc
@echo "bin/goyacc -o $@ -p yy$(prefix) -t $(prefix)Parser $<"
Expand Down
4 changes: 4 additions & 0 deletions pkg/parser/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package parser

// Generate keywords.go
//go:generate ./genkeyword
11 changes: 10 additions & 1 deletion pkg/parser/generate_keyword/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")

go_library(
name = "generate_keyword_lib",
Expand All @@ -12,3 +12,12 @@ go_binary(
embed = [":generate_keyword_lib"],
visibility = ["//visibility:public"],
)

go_test(
name = "generate_keyword_test",
timeout = "short",
srcs = ["genkeyword_test.go"],
embed = [":generate_keyword_lib"],
flaky = True,
deps = ["@com_github_stretchr_testify//require"],
)
15 changes: 15 additions & 0 deletions pkg/parser/generate_keyword/genkeyword_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestParseLine(t *testing.T) {
add := parseLine(" add \"ADD\"")
require.Equal(t, add, "ADD")

tso := parseLine(" tidbCurrentTSO \"TiDB_CURRENT_TSO\"")
require.Equal(t, tso, "TiDB_CURRENT_TSO")
}
Loading