Skip to content

Commit

Permalink
sql: add plpgsql parser for telemetry purposes
Browse files Browse the repository at this point in the history
This commit  removes portions of the plpgparser
that arent in use or could be shared with the sql
parser/lexer.

Release note: None
  • Loading branch information
e-mbrown committed Mar 16, 2023
1 parent 17d8c3c commit 4e4d76f
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 321 deletions.
5 changes: 3 additions & 2 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1797,7 +1797,8 @@ GO_TARGETS = [
"//pkg/sql/physicalplan/replicaoracle:replicaoracle_test",
"//pkg/sql/physicalplan:physicalplan",
"//pkg/sql/physicalplan:physicalplan_test",
"//pkg/sql/plpgsql/parser/lexbase:lexbase",
"//pkg/sql/plpgsql/parser/plpgsqllexbase/allkeywords:allkeywords",
"//pkg/sql/plpgsql/parser/plpgsqllexbase/allkeywords:allkeywords_lib",
"//pkg/sql/plpgsql/parser/plpgsqllexbase:plpgsqllexbase",
"//pkg/sql/plpgsql/parser:parser",
"//pkg/sql/plpgsql/parser:parser_test",
Expand Down Expand Up @@ -3021,8 +3022,8 @@ GET_X_DATA_TARGETS = [
"//pkg/sql/physicalplan:get_x_data",
"//pkg/sql/physicalplan/replicaoracle:get_x_data",
"//pkg/sql/plpgsql/parser:get_x_data",
"//pkg/sql/plpgsql/parser/lexbase:get_x_data",
"//pkg/sql/plpgsql/parser/plpgsqllexbase:get_x_data",
"//pkg/sql/plpgsql/parser/plpgsqllexbase/allkeywords:get_x_data",
"//pkg/sql/privilege:get_x_data",
"//pkg/sql/protoreflect:get_x_data",
"//pkg/sql/protoreflect/test:get_x_data",
Expand Down
3 changes: 0 additions & 3 deletions pkg/gen/misc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ MISC_SRCS = [
"//pkg/roachprod/vm/aws:terraform/main.tf",
"//pkg/spanconfig/spanconfigstore:entry_interval_btree.go",
"//pkg/spanconfig/spanconfigstore:entry_interval_btree_test.go",
"//pkg/sql/plpgsql/parser/lexbase:keywords.go",
"//pkg/sql/plpgsql/parser/lexbase:reserved_keywords.go",
"//pkg/sql/plpgsql/parser/lexbase:tokens.go",
"//pkg/sql/plpgsql/parser:plpgsql.go",
"//pkg/sql/scanner:token_names_test.go",
"//pkg/sql:txnstatetransitions_diagram.gv",
Expand Down
10 changes: 2 additions & 8 deletions pkg/sql/plpgsql/parser/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ genrule(
],
outs = ["plpgsql.go"],
cmd = """
GO_REL_PATH=`dirname $(location @go_sdk//:bin/go)`
GO_ABS_PATH=`cd $$GO_REL_PATH && pwd`
export PATH=$$GO_ABS_PATH:$$PATH
export HOME=$(GENDIR)
export GOPATH=/nonexist-gopath
$(location :plpgsql-gen) $(location plpgsql.y) \
$(location plpgsql.go) $(location @org_golang_x_tools//cmd/goyacc) \
$(location @com_github_cockroachdb_gostdlib//x/tools/cmd/goimports)
""",
exec_tools = [
":plpgsql-gen",
"@com_github_cockroachdb_gostdlib//x/tools/cmd/goimports",
"@go_sdk//:bin/go",
"@org_golang_x_tools//cmd/goyacc",
],
visibility = ["//visibility:public"],
Expand All @@ -48,6 +42,7 @@ go_library(
"//pkg/sql/pgwire/pgcode",
"//pkg/sql/pgwire/pgerror",
"//pkg/sql/plpgsql/parser/plpgsqllexbase",
"//pkg/sql/scanner",
"//pkg/sql/sem/plpgsqltree",
"//pkg/sql/sem/tree",
"//pkg/sql/types",
Expand All @@ -71,9 +66,8 @@ go_test(
data = glob(["testdata/**"]),
deps = [
":parser",
"//pkg/testutils",
"//pkg/testutils/datapathutils",
"@com_github_cockroachdb_datadriven//:datadriven",
"@com_github_stretchr_testify//require",
],
)

Expand Down
72 changes: 0 additions & 72 deletions pkg/sql/plpgsql/parser/lexbase/BUILD.bazel

This file was deleted.

28 changes: 1 addition & 27 deletions pkg/sql/plpgsql/parser/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ type lexer struct {
numAnnotations tree.AnnotationIdx

lastError error

env *plpgsqltree.PLpgSQLEnv
}

func (l *lexer) init(sql string, tokens []plpgsqlSymType, nakedIntType *types.T) {
Expand Down Expand Up @@ -326,9 +324,7 @@ func (l *lexer) ReadSqlConstruct(
return strings.Join(exprTokenStrs, " "), terminatorMet
}

func (l *lexer) ProcessQueryForCursorWithoutExplicitExpr(
openStmt *plpgsqltree.PLpgSQLStmtOpen,
) {
func (l *lexer) ProcessQueryForCursorWithoutExplicitExpr(openStmt *plpgsqltree.PLpgSQLStmtOpen) {
l.lastPos++
if int(l.Peek().id) == EXECUTE {
dynamicQuery, endToken := l.ReadSqlExpressionStr2(USING, ';')
Expand Down Expand Up @@ -378,24 +374,11 @@ func (l *lexer) lastToken() plpgsqlSymType {
return l.tokens[l.lastPos]
}

// NewAnnotation returns a new annotation index.
func (l *lexer) NewAnnotation() tree.AnnotationIdx {
l.numAnnotations++
return l.numAnnotations
}

// SetStmt is called from the parser when the statement is constructed.
func (l *lexer) SetStmt(stmt plpgsqltree.PLpgSQLStatement) {
l.stmt = stmt.(*plpgsqltree.PLpgSQLStmtBlock)
}

// UpdateNumPlaceholders is called from the parser when a placeholder is constructed.
func (l *lexer) UpdateNumPlaceholders(p *tree.Placeholder) {
if n := int(p.Idx) + 1; l.numPlaceholders < n {
l.numPlaceholders = n
}
}

// PurposelyUnimplemented wraps Error, setting lastUnimplementedError.
func (l *lexer) PurposelyUnimplemented(feature string, reason string) {
// We purposely do not use unimp here, as it appends hints to suggest that
Expand Down Expand Up @@ -495,12 +478,3 @@ func (l *lexer) populateErrorDetails() {
fmt.Fprintf(&buf, "%s^", strings.Repeat(" ", int(lastTok.pos)-j))
l.lastError = errors.WithDetail(l.lastError, buf.String())
}

// specialHelpErrorPrefix is a special prefix that must be present at
// the start of an error message to be considered a valid help
// response payload by the CLI shell.
const specialHelpErrorPrefix = "help token in input"

func (l *lexer) populateHelpMsg(msg string) {
l.lastError = errors.WithHint(errors.Wrap(l.lastError, specialHelpErrorPrefix), msg)
}
17 changes: 1 addition & 16 deletions pkg/sql/plpgsql/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
package parser

import (
"github.com/cockroachdb/cockroach/pkg/sql/scanner"
"go/constant"

"github.com/cockroachdb/cockroach/pkg/sql/scanner"
"github.com/cockroachdb/cockroach/pkg/sql/sem/plpgsqltree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
Expand Down Expand Up @@ -70,7 +70,6 @@ type Parser struct {
lexer lexer
parserImpl plpgsqlParserImpl
tokBuf [8]plpgsqlSymType
stmtBuf [1]Statement
}

// INT8 is the historical interpretation of INT. This should be left
Expand All @@ -84,20 +83,6 @@ func (p *Parser) Parse(sql string) (Statement, error) {
return p.parseWithDepth(1, sql, defaultNakedIntType)
}

// ParseWithInt parses a sql statement string and returns a list of
// Statements. The INT token will result in the specified TInt type.
func (p *Parser) ParseWithInt(sql string, nakedIntType *types.T) (Statement, error) {
return p.parseWithDepth(1, sql, nakedIntType)
}

func (p *Parser) parseOneWithInt(sql string, nakedIntType *types.T) (Statement, error) {
stmt, err := p.parseWithDepth(1, sql, nakedIntType)
if err != nil {
return Statement{}, err
}
return stmt, nil
}

func (p *Parser) scanOneStmt() (sql string, tokens []plpgsqlSymType, done bool) {
var lval plpgsqlSymType
tokens = p.tokBuf[:0]
Expand Down
22 changes: 1 addition & 21 deletions pkg/sql/plpgsql/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,13 @@
package parser_test

import (
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
"testing"

"github.com/cockroachdb/cockroach/pkg/sql/plpgsql/parser"
"github.com/cockroachdb/cockroach/pkg/testutils/datapathutils"
"github.com/cockroachdb/datadriven"
"github.com/stretchr/testify/require"
)

func TestParseDeclareSection(t *testing.T) {
fn := `
DECLARE
order_cnt integer := 10;
BEGIN
CASE
WHEN order_cnt BETWEEN 0 AND 100 THEN
CALL fn(1);
WHEN order_cnt > 100 THEN
CALL fn(2);
ELSE
CALL fn(3);
END CASE;
END`
stmt, err := parser.Parse(fn)
require.NoError(t, err)
require.Equal(t, "DECLARE\nBEGIN\nEND\n", stmt.String())
}

func TestParseDataDriver(t *testing.T) {
datadriven.Walk(t, datapathutils.TestDataPath(t), func(t *testing.T, path string) {
datadriven.RunTest(t, path, func(t *testing.T, d *datadriven.TestData) string {
Expand Down
17 changes: 0 additions & 17 deletions pkg/sql/plpgsql/parser/plpgsql.y
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ func (u *plpgsqlSymUnion) plpgsqlStmtBlock() *plpgsqltree.PLpgSQLStmtBlock {
return u.val.(*plpgsqltree.PLpgSQLStmtBlock)
}

func (u *plpgsqlSymUnion) plpgsqlExpr() plpgsqltree.PLpgSQLExpr {
return u.val.(plpgsqltree.PLpgSQLExpr)
}

func (u *plpgsqlSymUnion) plpgsqlExprs() []plpgsqltree.PLpgSQLExpr {
return u.val.([]plpgsqltree.PLpgSQLExpr)
}

func (u *plpgsqlSymUnion) plpgsqlStmtCaseWhenArm() *plpgsqltree.PLpgSQLStmtCaseWhenArm {
return u.val.(*plpgsqltree.PLpgSQLStmtCaseWhenArm)
}
Expand All @@ -89,19 +81,10 @@ func (u *plpgsqlSymUnion) plpgsqlStatements() []plpgsqltree.PLpgSQLStatement {
return u.val.([]plpgsqltree.PLpgSQLStatement)
}

func (u *plpgsqlSymUnion) plWDatum() *plWDatum {
return u.val.(*plWDatum)
}

func (u *plpgsqlSymUnion) int32() int32 {
return u.val.(int32)
}

func (u *plpgsqlSymUnion) uint32() uint32 {
return u.val.(uint32)
}


func (u *plpgsqlSymUnion) bool() bool {
return u.val.(bool)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/plpgsql/parser/plpgsqllexbase/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# generated by ../parser/Makefile
keywords.go
reserved_keywords.go
tokens.go
17 changes: 17 additions & 0 deletions pkg/sql/plpgsql/parser/plpgsqllexbase/allkeywords/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
load("//build/bazelutil/unused_checker:unused.bzl", "get_x_data")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "allkeywords_lib",
srcs = ["main.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/sql/plpgsql/parser/plpgsqllexbase/allkeywords",
visibility = ["//visibility:private"],
)

go_binary(
name = "allkeywords",
embed = [":allkeywords_lib"],
visibility = ["//visibility:public"],
)

get_x_data(name = "get_x_data")
Loading

0 comments on commit 4e4d76f

Please sign in to comment.