Skip to content

Commit

Permalink
parser: retain comments
Browse files Browse the repository at this point in the history
This commit adds a new array to the return value from the parser, which
contains the comments that were in the parsed SQL statement.

Release note: None
  • Loading branch information
jordanlewis authored and rafiss committed Dec 1, 2022
1 parent 193c561 commit fc594d5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ type Statement struct {
// AST is the root of the AST tree for the parsed statement.
AST tree.Statement

// Comments is the list of parsed SQL comments.
Comments []string

// SQL is the original SQL from which the statement was parsed. Note that this
// is not appropriate for use in logging, as it may contain passwords and
// other sensitive data.
Expand Down Expand Up @@ -251,6 +254,7 @@ func (p *Parser) parse(
return Statement{
AST: p.lexer.stmt,
SQL: sql,
Comments: p.scanner.Comments,
NumPlaceholders: p.lexer.numPlaceholders,
NumAnnotations: p.lexer.numAnnotations,
}, nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/scanner/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type Scanner struct {
in string
pos int
bytesPrealloc []byte

// Comments is the list of parsed comments from the SQL statement.
Comments []string
}

// In returns the input string.
Expand Down Expand Up @@ -453,6 +456,7 @@ func (s *Scanner) next() int {
func (s *Scanner) skipWhitespace(lval ScanSymType, allowComments bool) (newline, ok bool) {
newline = false
for {
startPos := s.pos
ch := s.peek()
if ch == '\n' {
s.pos++
Expand All @@ -467,6 +471,8 @@ func (s *Scanner) skipWhitespace(lval ScanSymType, allowComments bool) (newline,
if present, cok := s.ScanComment(lval); !cok {
return false, false
} else if present {
// Mark down the comments that we found.
s.Comments = append(s.Comments, s.in[startPos:s.pos])
continue
}
}
Expand Down

0 comments on commit fc594d5

Please sign in to comment.