Skip to content

Commit 7031f9b

Browse files
committed
parser: remove some unused code
This commit removes some of the unused code from sql/parser and plpgsql/parser. Release note: None
1 parent 9469c4b commit 7031f9b

File tree

4 files changed

+25
-60
lines changed

4 files changed

+25
-60
lines changed

pkg/sql/parser/parse.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,18 @@ var defaultNakedIntType = types.Int
8686

8787
// Parse parses the sql and returns a list of statements.
8888
func (p *Parser) Parse(sql string) (statements.Statements, error) {
89-
return p.parseWithDepth(1, sql, DefaultParseOptions)
89+
return p.parseWithOptions(sql, DefaultParseOptions)
9090
}
9191

9292
// ParseWithOptions parses the sql with the provided options and returns a list of statements.
9393
func (p *Parser) ParseWithOptions(sql string, opts ParseOptions) (statements.Statements, error) {
94-
return p.parseWithDepth(1, sql, opts)
94+
return p.parseWithOptions(sql, opts)
9595
}
9696

9797
func (p *Parser) parseOne(
9898
sql string, opts ParseOptions,
9999
) (statements.Statement[tree.Statement], error) {
100-
stmts, err := p.parseWithDepth(1, sql, opts)
100+
stmts, err := p.parseWithOptions(sql, opts)
101101
if err != nil {
102102
return statements.Statement[tree.Statement]{}, err
103103
}
@@ -161,9 +161,7 @@ func (p *Parser) scanOneStmt() (sql string, tokens []sqlSymType, done bool) {
161161
}
162162
}
163163

164-
func (p *Parser) parseWithDepth(
165-
depth int, sql string, options ParseOptions,
166-
) (statements.Statements, error) {
164+
func (p *Parser) parseWithOptions(sql string, options ParseOptions) (statements.Statements, error) {
167165
stmts := statements.Statements(p.stmtBuf[:0])
168166
p.scanner.Init(sql)
169167
if options.retainComments {
@@ -176,7 +174,7 @@ func (p *Parser) parseWithDepth(
176174
}
177175
for {
178176
sql, tokens, done := p.scanOneStmt()
179-
stmt, err := p.parse(depth+1, sql, tokens, options.intType, numAnnotations)
177+
stmt, err := p.parse(sql, tokens, options.intType, numAnnotations)
180178
if err != nil {
181179
return nil, err
182180
}
@@ -203,11 +201,7 @@ func (p *Parser) parseWithDepth(
203201

204202
// parse parses a statement from the given scanned tokens.
205203
func (p *Parser) parse(
206-
depth int,
207-
sql string,
208-
tokens []sqlSymType,
209-
nakedIntType *types.T,
210-
numAnnotations tree.AnnotationIdx,
204+
sql string, tokens []sqlSymType, nakedIntType *types.T, numAnnotations tree.AnnotationIdx,
211205
) (statements.Statement[tree.Statement], error) {
212206
p.lexer.init(sql, tokens, nakedIntType, numAnnotations)
213207
defer p.lexer.cleanup()

pkg/sql/plpgsql/parser/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ go_library(
4444
"//pkg/sql/scanner",
4545
"//pkg/sql/sem/plpgsqltree",
4646
"//pkg/sql/sem/tree",
47-
"//pkg/sql/types",
47+
"//pkg/sql/types", # keep
4848
"//pkg/util/errorutil/unimplemented",
4949
"@com_github_cockroachdb_errors//:errors",
5050
"@com_github_cockroachdb_redact//:redact", # keep

pkg/sql/plpgsql/parser/lexer.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
1515
"github.com/cockroachdb/cockroach/pkg/sql/sem/plpgsqltree"
1616
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
17-
"github.com/cockroachdb/cockroach/pkg/sql/types"
1817
unimp "github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
1918
"github.com/cockroachdb/errors"
2019
)
@@ -24,33 +23,26 @@ type lexer struct {
2423
// tokens contains tokens generated by the scanner.
2524
tokens []plpgsqlSymType
2625

27-
// The type that should be used when an INT or SERIAL is encountered.
28-
nakedIntType *types.T
29-
3026
// lastPos is the position into the tokens slice of the last
3127
// token returned by Lex().
3228
lastPos int
3329

3430
stmt *plpgsqltree.Block
3531

36-
// numPlaceholders is 1 + the highest placeholder index encountered.
37-
numPlaceholders int
38-
numAnnotations tree.AnnotationIdx
32+
numAnnotations tree.AnnotationIdx
3933

4034
lastError error
4135

4236
parser plpgsqlParser
4337
}
4438

45-
func (l *lexer) init(sql string, tokens []plpgsqlSymType, nakedIntType *types.T, p plpgsqlParser) {
39+
func (l *lexer) init(sql string, tokens []plpgsqlSymType, p plpgsqlParser) {
4640
l.in = sql
4741
l.tokens = tokens
4842
l.lastPos = -1
4943
l.stmt = nil
50-
l.numPlaceholders = 0
5144
l.numAnnotations = 0
5245
l.lastError = nil
53-
l.nakedIntType = nakedIntType
5446
l.parser = p
5547
}
5648

pkg/sql/plpgsql/parser/parse.go

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/cockroachdb/cockroach/pkg/sql/parser/statements"
1414
"github.com/cockroachdb/cockroach/pkg/sql/scanner"
1515
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
16-
"github.com/cockroachdb/cockroach/pkg/sql/types"
1716
"github.com/cockroachdb/errors"
1817
)
1918

@@ -38,17 +37,6 @@ type Parser struct {
3837
tokBuf [8]plpgsqlSymType
3938
}
4039

41-
// INT8 is the historical interpretation of INT. This should be left
42-
// alone in the future, since there are many sql fragments stored
43-
// in various descriptors. Any user input that was created after
44-
// INT := INT4 will simply use INT4 in any resulting code.
45-
var defaultNakedIntType = types.Int
46-
47-
// Parse parses the sql and returns a list of statements.
48-
func (p *Parser) Parse(sql string) (statements.PLpgStatement, error) {
49-
return p.parseWithDepth(1, sql, defaultNakedIntType)
50-
}
51-
5240
func (p *Parser) scanFnBlock() (sql string, tokens []plpgsqlSymType, done bool) {
5341
var lval plpgsqlSymType
5442
tokens = p.tokBuf[:0]
@@ -79,27 +67,9 @@ func (p *Parser) scanFnBlock() (sql string, tokens []plpgsqlSymType, done bool)
7967
}
8068
}
8169

82-
func (p *Parser) parseWithDepth(
83-
depth int, plpgsql string, nakedIntType *types.T,
84-
) (statements.PLpgStatement, error) {
85-
p.scanner.Init(plpgsql)
86-
defer p.scanner.Cleanup()
87-
sql, tokens, done := p.scanFnBlock()
88-
stmt, err := p.parse(depth+1, sql, tokens, nakedIntType)
89-
if err != nil {
90-
return statements.PLpgStatement{}, err
91-
}
92-
if !done {
93-
return statements.PLpgStatement{}, errors.AssertionFailedf("invalid plpgsql function: %s", plpgsql)
94-
}
95-
return stmt, nil
96-
}
97-
9870
// parse parses a statement from the given scanned tokens.
99-
func (p *Parser) parse(
100-
depth int, sql string, tokens []plpgsqlSymType, nakedIntType *types.T,
101-
) (statements.PLpgStatement, error) {
102-
p.lexer.init(sql, tokens, nakedIntType, &p.parserImpl)
71+
func (p *Parser) parse(sql string, tokens []plpgsqlSymType) (statements.PLpgStatement, error) {
72+
p.lexer.init(sql, tokens, &p.parserImpl)
10373
defer p.lexer.cleanup()
10474
if p.parserImpl.Parse(&p.lexer) != 0 {
10575
if p.lexer.lastError == nil {
@@ -125,10 +95,9 @@ func (p *Parser) parse(
12595
return statements.PLpgStatement{}, err
12696
}
12797
return statements.PLpgStatement{
128-
AST: p.lexer.stmt,
129-
SQL: sql,
130-
NumPlaceholders: p.lexer.numPlaceholders,
131-
NumAnnotations: p.lexer.numAnnotations,
98+
AST: p.lexer.stmt,
99+
SQL: sql,
100+
NumAnnotations: p.lexer.numAnnotations,
132101
}, nil
133102
}
134103

@@ -144,5 +113,15 @@ func (p *Parser) parse(
144113
// operate with table OIDTypeReferences.
145114
func Parse(sql string) (statements.PLpgStatement, error) {
146115
var p Parser
147-
return p.parseWithDepth(1, sql, defaultNakedIntType)
116+
p.scanner.Init(sql)
117+
defer p.scanner.Cleanup()
118+
sql, tokens, done := p.scanFnBlock()
119+
stmt, err := p.parse(sql, tokens)
120+
if err != nil {
121+
return statements.PLpgStatement{}, err
122+
}
123+
if !done {
124+
return statements.PLpgStatement{}, errors.AssertionFailedf("invalid plpgsql function: %s", sql)
125+
}
126+
return stmt, nil
148127
}

0 commit comments

Comments
 (0)