Skip to content

Migrate SQLite Grammar to maintained version #1397

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

Merged
merged 8 commits into from
Jan 27, 2022
Merged
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
181 changes: 106 additions & 75 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ func convertAlter_table_stmtContext(c *parser.Alter_table_stmtContext) ast.Node
}
}

if newCol, ok := c.New_column_name().(*parser.New_column_nameContext); ok {
if newCol, ok := c.GetNew_column_name().(*parser.Column_nameContext); ok {
name := newCol.Any_name().GetText()
return &ast.RenameColumnStmt{
Table: parseTableName(c),
Col: &ast.ColumnRef{
Name: c.Column_name().GetText(),
Name: c.GetOld_column_name().GetText(),
},
NewName: &name,
}
Expand Down Expand Up @@ -54,7 +54,7 @@ func convertAlter_table_stmtContext(c *parser.Alter_table_stmtContext) ast.Node
}

func convertAttach_stmtContext(c *parser.Attach_stmtContext) ast.Node {
name := c.Database_name().GetText()
name := c.Schema_name().GetText()
return &ast.CreateSchemaStmt{
Name: &name,
}
Expand All @@ -63,7 +63,7 @@ func convertAttach_stmtContext(c *parser.Attach_stmtContext) ast.Node {
func convertCreate_table_stmtContext(c *parser.Create_table_stmtContext) ast.Node {
stmt := &ast.CreateTableStmt{
Name: parseTableName(c),
IfNotExists: c.K_EXISTS() != nil,
IfNotExists: c.EXISTS_() != nil,
}
for _, idef := range c.AllColumn_def() {
if def, ok := idef.(*parser.Column_defContext); ok {
Expand All @@ -77,76 +77,115 @@ func convertCreate_table_stmtContext(c *parser.Create_table_stmtContext) ast.Nod
return stmt
}

func convertDrop_table_stmtContext(c *parser.Drop_table_stmtContext) ast.Node {
return &ast.DropTableStmt{
IfExists: c.K_EXISTS() != nil,
Tables: []*ast.TableName{parseTableName(c)},
func convertDrop_stmtContext(c *parser.Drop_stmtContext) ast.Node {
// TODO confirm that this logic does what it looks like it should
if tableName, ok := c.TABLE_().(antlr.TerminalNode); ok {

name := ast.TableName{
Name: tableName.GetText(),
}
if c.Schema_name() != nil {
name.Schema = c.Schema_name().GetText()
}

return &ast.DropTableStmt{
IfExists: c.EXISTS_() != nil,
Tables: []*ast.TableName{&name},
}
} else {
return &ast.TODO{}
}
}

func convertExprContext(c *parser.ExprContext) ast.Node {
return &ast.TODO{}
}

func convertFactored_select_stmtContext(c *parser.Factored_select_stmtContext) ast.Node {
func convertSimpleSelect_stmtContext(c *parser.Simple_select_stmtContext) ast.Node {
if core, ok := c.Select_core().(*parser.Select_coreContext); ok {
cols := getCols(core)
tables := getTables(core)

return &ast.SelectStmt{
FromClause: &ast.List{Items: tables},
TargetList: &ast.List{Items: cols},
}
}

return &ast.TODO{}
}

func convertMultiSelect_stmtContext(c multiselect) ast.Node {
var tables []ast.Node
var cols []ast.Node
for _, icore := range c.AllSelect_core() {
core, ok := icore.(*parser.Select_coreContext)
if !ok {
continue
}
for _, icol := range core.AllResult_column() {
col, ok := icol.(*parser.Result_columnContext)
if !ok {
continue
}
var val ast.Node
iexpr := col.Expr()
switch {
case col.STAR() != nil:
val = &ast.ColumnRef{
Fields: &ast.List{
Items: []ast.Node{
&ast.A_Star{},
},
},
Location: col.GetStart().GetStart(),
}
case iexpr != nil:
val = convert(iexpr)
}
if val == nil {
continue
}
cols = append(cols, &ast.ResTarget{
Val: val,
Location: col.GetStart().GetStart(),
})
}
for _, ifrom := range core.AllTable_or_subquery() {
from, ok := ifrom.(*parser.Table_or_subqueryContext)
if !ok {
continue
}
rel := from.Table_name().GetText()
name := ast.RangeVar{
Relname: &rel,
Location: from.GetStart().GetStart(),
}
if from.Schema_name() != nil {
text := from.Schema_name().GetText()
name.Schemaname = &text
}
tables = append(tables, &name)
}
cols = append(cols, getCols(core)...)
tables = append(cols, getTables(core)...)
}
return &ast.SelectStmt{
FromClause: &ast.List{Items: tables},
TargetList: &ast.List{Items: cols},
}
}

func getTables(core *parser.Select_coreContext) []ast.Node {
var tables []ast.Node
for _, ifrom := range core.AllTable_or_subquery() {
from, ok := ifrom.(*parser.Table_or_subqueryContext)
if !ok {
continue
}
rel := from.Table_name().GetText()
name := ast.RangeVar{
Relname: &rel,
Location: from.GetStart().GetStart(),
}
if from.Schema_name() != nil {
text := from.Schema_name().GetText()
name.Schemaname = &text
}
tables = append(tables, &name)
}
return tables
}

func getCols(core *parser.Select_coreContext) []ast.Node {
var cols []ast.Node
for _, icol := range core.AllResult_column() {
col, ok := icol.(*parser.Result_columnContext)
if !ok {
continue
}
var val ast.Node
iexpr := col.Expr()
switch {
case col.STAR() != nil:
val = &ast.ColumnRef{
Fields: &ast.List{
Items: []ast.Node{
&ast.A_Star{},
},
},
Location: col.GetStart().GetStart(),
}
case iexpr != nil:
val = convert(iexpr)
}
if val == nil {
continue
}
cols = append(cols, &ast.ResTarget{
Val: val,
Location: col.GetStart().GetStart(),
})
}
return cols
}

func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
if stmt := n.Alter_table_stmt(); stmt != nil {
return convert(stmt)
Expand All @@ -163,9 +202,6 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
if stmt := n.Commit_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Compound_select_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Create_index_stmt(); stmt != nil {
return convert(stmt)
}
Expand All @@ -190,19 +226,7 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
if stmt := n.Detach_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Drop_index_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Drop_table_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Drop_trigger_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Drop_view_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Factored_select_stmt(); stmt != nil {
if stmt := n.Drop_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Insert_stmt(); stmt != nil {
Expand All @@ -223,9 +247,6 @@ func convertSql_stmtContext(n *parser.Sql_stmtContext) ast.Node {
if stmt := n.Savepoint_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Simple_select_stmt(); stmt != nil {
return convert(stmt)
}
if stmt := n.Select_stmt(); stmt != nil {
return convert(stmt)
}
Expand Down Expand Up @@ -253,18 +274,28 @@ func convert(node node) ast.Node {
case *parser.Create_table_stmtContext:
return convertCreate_table_stmtContext(n)

case *parser.Drop_table_stmtContext:
return convertDrop_table_stmtContext(n)
case *parser.Drop_stmtContext:
return convertDrop_stmtContext(n)

case *parser.ExprContext:
return convertExprContext(n)

case *parser.Factored_select_stmtContext:
return convertFactored_select_stmtContext(n)
// TODO: need to handle this
return &ast.TODO{}

case *parser.Select_stmtContext:
return convertMultiSelect_stmtContext(n)

case *parser.Sql_stmtContext:
return convertSql_stmtContext(n)

case *parser.Simple_select_stmtContext:
return convertSimpleSelect_stmtContext(n)

case *parser.Compound_select_stmtContext:
return convertMultiSelect_stmtContext(n)

default:
return &ast.TODO{}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/engine/sqlite/parser/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sqlite_parser.go: SQLite.g4
antlr -Dlanguage=Go SQLite.g4
sqlite_parser.go: SQLiteLexer.g4 SQLiteParser.g4
antlr -Dlanguage=Go SQLiteLexer.g4 SQLiteParser.g4
Loading