Skip to content

Commit

Permalink
parser: remove scan.go dependency on tree
Browse files Browse the repository at this point in the history
We now inject the NewNumVal and NewPlaceholder functions into scan.go.
This lets us move scan.go into its own package.

Release note: None
  • Loading branch information
rafiss committed Jul 13, 2021
1 parent 9c5aead commit 209c5f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pkg/sql/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package parser

import (
"fmt"
"go/constant"
"strings"

"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
Expand All @@ -30,6 +31,11 @@ import (
"github.com/cockroachdb/errors"
)

func init() {
NewNumValFn = func(a constant.Value, s string, b bool) interface{} { return tree.NewNumVal(a, s, b) }
NewPlaceholderFn = func(s string) (interface{}, error) { return tree.NewPlaceholder(s) }
}

// Statement is the result of parsing a single statement. It contains the AST
// node along with other information.
type Statement struct {
Expand Down
18 changes: 14 additions & 4 deletions pkg/sql/parser/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"unsafe"

"github.com/cockroachdb/cockroach/pkg/sql/lexbase"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)

const eof = -1
Expand All @@ -30,6 +29,17 @@ const errInvalidHexNumeric = "invalid hexadecimal numeric literal"
const singleQuote = '\''
const identQuote = '"'

// NewNumValFn allows us to use tree.NewNumVal without a dependency on tree.
var NewNumValFn = func(constant.Value, string, bool) interface{} {
return struct{}{}
}

// NewPlaceholderFn allows us to use tree.NewPlaceholder without a dependency on
// tree.
var NewPlaceholderFn = func(string) (interface{}, error) {
return struct{}{}, nil
}

// scanner lexes SQL statements.
type scanner struct {
in string
Expand Down Expand Up @@ -655,7 +665,7 @@ func (s *scanner) scanNumber(lval *sqlSymType, ch int) {
lval.str = fmt.Sprintf("could not make constant float from literal %q", lval.str)
return
}
lval.union.val = tree.NewNumVal(floatConst, lval.str, false /* negative */)
lval.union.val = NewNumValFn(floatConst, lval.str, false /* negative */)
} else {
if isHex && s.pos == start+2 {
lval.id = ERROR
Expand All @@ -680,7 +690,7 @@ func (s *scanner) scanNumber(lval *sqlSymType, ch int) {
lval.str = fmt.Sprintf("could not make constant int from literal %q", lval.str)
return
}
lval.union.val = tree.NewNumVal(intConst, lval.str, false /* negative */)
lval.union.val = NewNumValFn(intConst, lval.str, false /* negative */)
}
}

Expand All @@ -691,7 +701,7 @@ func (s *scanner) scanPlaceholder(lval *sqlSymType) {
}
lval.str = s.in[start:s.pos]

placeholder, err := tree.NewPlaceholder(lval.str)
placeholder, err := NewPlaceholderFn(lval.str)
if err != nil {
lval.id = ERROR
lval.str = err.Error()
Expand Down

0 comments on commit 209c5f0

Please sign in to comment.