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

recursive descent parser for simple INSERT/SELECT #2655

Closed
wants to merge 5 commits 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
3 changes: 2 additions & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/dolthub/go-mysql-server/sql/expression/function"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/planbuilder"
"github.com/dolthub/go-mysql-server/sql/rdparser"
"github.com/dolthub/go-mysql-server/sql/rowexec"
"github.com/dolthub/go-mysql-server/sql/transform"
"github.com/dolthub/go-mysql-server/sql/types"
Expand Down Expand Up @@ -192,7 +193,7 @@ func New(a *analyzer.Analyzer, cfg *Config) *Engine {
PreparedDataCache: NewPreparedDataCache(),
mu: &sync.Mutex{},
EventScheduler: nil,
Parser: sql.NewMysqlParser(),
Parser: rdparser.NewParser(),
}
ret.ReadOnly.Store(cfg.IsReadOnly)
return ret
Expand Down
176 changes: 176 additions & 0 deletions sql/rdparser/insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package rdparser

import (
"context"

ast "github.com/dolthub/vitess/go/vt/sqlparser"
)

func (p *parser) insert(ctx context.Context) (ast.Statement, bool) {
id, cur := p.tok.Scan()
ins := new(ast.Insert)
ins.Action = ast.InsertStr
if id == ast.INTO {
id, cur = p.tok.Scan()
} else if id != ast.ID {
return nil, false
}

p.push(id, cur)

var ok bool
ins.Table, ok = p.tableIdent()
if !ok {
return nil, false
}

// optional ()
id, cur = p.pop()
if id == '(' {
ins.Columns, ok = p.columnList(ctx)
if !ok {
return nil, false
}
id, cur = p.pop()
if id != ')' {
return nil, false
}
}

// VALUES or SELECT
if id != ast.VALUES {
return nil, false
}

ins.Rows, ok = p.valueList(ctx)
if !ok {
return nil, false
}
return ins, true
}

func (p *parser) push(id int, cur []byte) {
p.curId, p.cur, p.curOk = id, cur, true
}

func (p *parser) pop() (int, []byte) {
if p.curOk {
p.curOk = false
return p.curId, p.cur
} else {
return p.tok.Scan()
}
}

func (p *parser) tableIdent() (ast.TableName, bool) {
// schema.database.table

id, firstTok := p.pop()
if id != ast.ID {
return ast.TableName{}, false
}

id, tok := p.pop()
if id != '.' {
p.push(id, tok)
return ast.TableName{Name: ast.NewTableIdent(string(firstTok))}, true
}

id, secondTok := p.tok.Scan()
if id != ast.ID {
p.push(id, tok)
return ast.TableName{
Name: ast.NewTableIdent(string(firstTok)),
}, true
}

id, tok = p.pop()
if id != '.' {
p.push(id, tok)
return ast.TableName{
DbQualifier: ast.NewTableIdent(string(firstTok)),
Name: ast.NewTableIdent(string(secondTok)),
}, true
}

id, thirdTok := p.tok.Scan()
if id != ast.ID {
p.push(id, tok)
return ast.TableName{
SchemaQualifier: ast.NewTableIdent(string(firstTok)),
DbQualifier: ast.NewTableIdent(string(secondTok)),
}, true
}

return ast.TableName{
SchemaQualifier: ast.NewTableIdent(string(firstTok)),
DbQualifier: ast.NewTableIdent(string(secondTok)),
Name: ast.NewTableIdent(string(thirdTok)),
}, true
}

func (p *parser) columnList(ctx context.Context) (ast.Columns, bool) {
// id, ...
var cols ast.Columns
id, tok := p.pop()
for {
if id != ast.ID {
break
}
cols = append(cols, ast.NewColIdent(string(tok)))
id, tok = p.tok.Scan()
if id != ',' {
break
}
}
p.push(id, tok)
return cols, true
}

func (p *parser) valueList(ctx context.Context) (ast.InsertRows, bool) {
var rows ast.Values
id, tok := p.pop()
for {
if id != '(' {
break
}
var row ast.ValTuple
for {
id, tok = p.pop()
if id == ',' {
id, tok = p.pop()
}
if id == ')' {
break
}
value, ok := p.value(id, tok)
if !ok {
return nil, false
}
row = append(row, value)
}
rows = append(rows, row)
id, tok = p.tok.Scan()
if id != ',' {
break
}
id, tok = p.tok.Scan()
}
p.push(id, tok)
return &ast.AliasedValues{Values: rows}, true
}

func (p *parser) value(id int, tok []byte) (ast.Expr, bool) {
switch id {
case ast.STRING:
return ast.NewStrVal(tok), true
case ast.INTEGRAL:
return ast.NewIntVal(tok), true
case ast.FLOAT:
return ast.NewFloatVal(tok), true
case ast.NULL:
return ast.NewStrVal(tok), true
default:
return nil, false
}
}
100 changes: 100 additions & 0 deletions sql/rdparser/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package rdparser

import (
"context"
"fmt"

ast "github.com/dolthub/vitess/go/vt/sqlparser"

"github.com/dolthub/go-mysql-server/sql"
)

type parser struct {
tok *ast.Tokenizer
curOk bool
curId int
cur []byte
peekOk bool
peekId int
_peek []byte
}

func NewParser() sql.Parser {
return &parser{}
}

func (p *parser) parse(ctx context.Context, s string, options ast.ParserOptions) (ret ast.Statement, err error) {
defer func() {
if mes := recover(); mes != nil {
_, ok := mes.(parseErr)
if !ok {
err = fmt.Errorf("panic encountered while parsing: %s", mes)
return
}
ret, err = ast.ParseWithOptions(ctx, s, options)
}
}()
// get next token
p.tok = ast.NewStringTokenizer(s)
if options.AnsiQuotes {
p.tok = ast.NewStringTokenizerForAnsiQuotes(s)
}

if prePlan, ok := p.statement(ctx); ok {
return prePlan, nil
}

return ast.ParseWithOptions(ctx, s, options)
}

type parseErr struct {
str string
}

func (p *parser) fail(s string) {
panic(parseErr{s})
}

func (p *parser) next() (int, []byte) {
if p.peekOk {
p.peekOk = false
p.curId, p.cur = p.peekId, p._peek
}
p.curOk = true
p.curId, p.cur = p.tok.Scan()
return p.curId, p.cur
}

func (p *parser) peek() (int, []byte) {
if !p.peekOk {
p.peekOk = true
p.peekId, p._peek = p.tok.Scan()
}
return p.peekId, p._peek
}

var _ sql.Parser = (*parser)(nil)

func (p *parser) ParseSimple(query string) (ast.Statement, error) {
return p.parse(context.Background(), query, ast.ParserOptions{})
}

func (p *parser) Parse(ctx *sql.Context, query string, multi bool) (ast.Statement, string, string, error) {
return p.ParseWithOptions(ctx, query, ';', multi, ast.ParserOptions{})
}

func (p *parser) ParseWithOptions(ctx context.Context, query string, delimiter rune, multi bool, options ast.ParserOptions) (ast.Statement, string, string, error) {
stmt, err := p.parse(context.Background(), query, options)
if err != nil {
return nil, "", "", nil
}
return stmt, "", "", nil
}

func (p *parser) ParseOneWithOptions(ctx context.Context, s string, options ast.ParserOptions) (ast.Statement, int, error) {
ast, err := p.parse(ctx, s, options)
if err != nil {
return nil, 0, err
}
return ast, 0, nil
}
47 changes: 47 additions & 0 deletions sql/rdparser/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package rdparser

import (
"context"
"testing"

ast "github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/stretchr/testify/require"
)

func TestParser(t *testing.T) {
tests := []struct {
q string
exp ast.Statement
ok bool
}{
{
q: "insert into xy values (0,'0', .0), (1,'1', 1.0)",
ok: true,
},
{
q: "insert into xy (x,y,z) values (0,'0', .0), (1,'1', 1.0)",
ok: true,
},
{
q: "insert into db.xy values (0,'0', .0), (1,'1', 1.0)",
ok: true,
},
{
q: "select * from xy where x = 1",
ok: true,
},
{
q: "select id from sbtest1 where id = 1000",
ok: true,
},
}
for _, tt := range tests {
t.Run(tt.q, func(t *testing.T) {
p := new(parser)
p.tok = ast.NewStringTokenizer(tt.q)
res, ok := p.statement(context.Background())
require.Equal(t, tt.ok, ok)
require.Equal(t, tt.exp, res)
})
}
}
Loading
Loading