Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
dev: basic visitor for clickhouse
  • Loading branch information
CNLHC committed Mar 26, 2024
commit 7a57053f8b8dd7cff259fbf5a2aef0ccac91e8f1
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/AfterShip/clickhouse-sql-parser v0.3.3 // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/AfterShip/clickhouse-sql-parser v0.3.3 h1:06Thdd8RMASiyP25YkjYrNzwGKen9bc2Yj5ZSjlSnBc=
github.com/AfterShip/clickhouse-sql-parser v0.3.3/go.mod h1:W0Z82wJWkJxz2RVun/RMwxue3g7ut47Xxl+SFqdJGus=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
Expand Down
151 changes: 149 additions & 2 deletions internal/engine/clickhouse/parse.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package clickhouse

import (
"fmt"
"io"
"reflect"

chparser "github.com/AfterShip/clickhouse-sql-parser/parser"
"github.com/sqlc-dev/sqlc/internal/source"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
)
Expand All @@ -14,9 +17,153 @@ func NewParser() *Parser {
return &Parser{}
}

func (c *Parser) Parse(io.Reader) ([]ast.Statement, error) {
func convertAlterTableCmd(node chparser.AlterTableExpr) (ast.Node, error) {
switch n := node.(type) {
case *chparser.AlterTableAttachPartition:
case *chparser.AlterTableDetachPartition:
case *chparser.AlterTableDropPartition:
case *chparser.AlterTableFreezePartition:
case *chparser.AlterTableAddColumn:
case *chparser.AlterTableAddIndex:
case *chparser.AlterTableDropColumn:
case *chparser.AlterTableDropIndex:
case *chparser.AlterTableRemoveTTL:
case *chparser.AlterTableClearColumn:
case *chparser.AlterTableClearIndex:
case *chparser.AlterTableRenameColumn:
case *chparser.AlterTableModifyTTL:
case *chparser.AlterTableModifyColumn:
case *chparser.AlterTableReplacePartition:
default:
_ = n
}
panic("not implemented")
}

func convertTableName(n *chparser.TableIdentifier) *ast.TableName {
if n.Database != nil {
return &ast.TableName{
Catalog: n.Database.Name,
Schema: "",
Name: n.Table.Name,
}
}
return &ast.TableName{
Catalog: "",
Schema: "",
Name: n.Table.Name,
}

}

type converter struct {
output []ast.Statement

tempColumnDef []*ast.ColumnDef
tempTypeName *ast.TypeName
*chparser.DefaultASTVisitor
}

func (c *converter) VisitAlterTable(expr *chparser.AlterTable) error {
cmdItems := make([]ast.Node, 0)
for _, v := range expr.AlterExprs {
n, err := convertAlterTableCmd(v)
if err != nil {
return err
}
cmdItems = append(cmdItems, n)
}

stmt := &ast.AlterTableStmt{
Relation: &ast.RangeVar{},
Table: convertTableName(expr.TableIdentifier),
Cmds: &ast.List{
Items: cmdItems,
},
MissingOk: false,
}
c.output = append(c.output, ast.Statement{Raw: &ast.RawStmt{
Stmt: stmt,
StmtLocation: int(expr.AlterPos),
StmtLen: int(expr.StatementEnd) - int(expr.AlterPos),
}})
return nil
}

func (c *converter) VisitCreateTable(expr *chparser.CreateTable) error {
c.tempColumnDef = c.tempColumnDef[:]
stmt := &ast.CreateTableStmt{
IfNotExists: expr.IfNotExists,
Name: convertTableName(expr.Name),
Cols: c.tempColumnDef,
}
c.tempColumnDef = c.tempColumnDef[:]
c.output = append(c.output, ast.Statement{Raw: &ast.RawStmt{
Stmt: stmt,
StmtLocation: int(expr.CreatePos),
StmtLen: int(expr.StatementEnd) - int(expr.CreatePos),
}})
return nil
}

func (c *converter) VisitColumnTypeExpr(expr *chparser.ColumnTypeExpr) error {
c.tempTypeName = &ast.TypeName{
Schema: expr.Name.Name,
}
return nil
}
func (c *converter) VisitScalarTypeExpr(expr *chparser.ScalarTypeExpr) error {
c.tempTypeName = &ast.TypeName{
Name: expr.Name.Name,
}
return nil
}

// TODO: remove befor making pr is ready
func debugFallbackVisitor(e chparser.Expr) error {
tn := reflect.TypeOf(e).Elem().Name()
fmt.Println("visit: ", e.String(0), tn)
return nil
}

func (c *converter) VisitColumn(expr *chparser.Column) error {
c.tempTypeName = nil
err := expr.Type.Accept(c)
if err != nil {
return err
}
c.tempColumnDef = append(c.tempColumnDef, &ast.ColumnDef{
Colname: expr.Name.Name,
TypeName: &ast.TypeName{Name: c.tempTypeName.Name},
IsNotNull: expr.NotNull != nil,
})
return nil
}

func (c *converter) Convert(exprs []chparser.Expr) ([]ast.Statement, error) {
for _, e := range exprs {
e.Accept(c)
}
return c.output, nil
}

func (c *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
sql, err := io.ReadAll(r)
if err != nil {
return nil, err
}

return []ast.Statement{}, nil
chParser := chparser.NewParser(string(sql))
statements, err := chParser.ParseStatements()
if err != nil {
return []ast.Statement{}, err
}
conv := &converter{
DefaultASTVisitor: &chparser.DefaultASTVisitor{
Visit: debugFallbackVisitor,
},
}
return conv.Convert(statements)
}
func (c *Parser) CommentSyntax() source.CommentSyntax {
return source.CommentSyntax{}
Expand Down
44 changes: 44 additions & 0 deletions internal/engine/clickhouse/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package clickhouse

import (
"bytes"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
)

func newCatalog() *catalog.Catalog {
c := catalog.New("public")
return c
}

func TestCreateTable(t *testing.T) {

sql := `CREATE TABLE foo(a String, b Int64);`
parser := Parser{}
parsed, err := parser.Parse(bytes.NewBuffer([]byte(sql)))
if err != nil {
t.Error(err)
}

diff := cmp.Diff(parsed, []ast.Statement{
{
Raw: &ast.RawStmt{
Stmt: &ast.CreateTableStmt{
IfNotExists: false,
Name: &ast.TableName{Name: "foo"},
Cols: []*ast.ColumnDef{
{Colname: "a", TypeName: &ast.TypeName{Name: "String"}},
{Colname: "b", TypeName: &ast.TypeName{Name: "Int64"}},
},
},
},
},
}, cmpopts.EquateEmpty())
if diff != "" {
t.Error(diff)
}
}