Skip to content

Commit

Permalink
feat: add built-in transpiler
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 committed Aug 28, 2024
1 parent b51b076 commit dc7043b
Show file tree
Hide file tree
Showing 15 changed files with 920 additions and 24 deletions.
1 change: 1 addition & 0 deletions binlogreplication/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The code in this directory was copied and modified from [Dolt](https://github.com/dolthub/dolt) (as of 2024-08-28, https://github.com/dolthub/dolt/tree/main/go/libraries/doltcore/sqle/binlogreplication). The original code is licensed under the Apache License, Version 2.0. The modifications are also licensed under the Apache License, Version 2.0.
5 changes: 4 additions & 1 deletion binlogreplication/binlog_json_serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ const maxOffsetSize = uint32(65_535)
// And a third-party description is here:
// https://lafengnan.gitbooks.io/blog/content/mysql/chapter2.html
func encodeJsonDoc(jsonDoc sql.JSONWrapper) (buffer []byte, err error) {
val := jsonDoc.ToInterface()
val, err := jsonDoc.ToInterface()
if err != nil {
return nil, err
}
typeId, encodedValue, err := encodeJsonValue(val)
if err != nil {
return nil, err
Expand Down
7 changes: 4 additions & 3 deletions binlogreplication/binlog_replica_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,12 +815,13 @@ func convertVitessJsonExpressionString(ctx *sql.Context, engine *gms.Engine, val
strValue = strValue[len("EXPRESSION(") : len(strValue)-1]
}

node, err := planbuilder.Parse(ctx, engine.Analyzer.Catalog, "SELECT "+strValue)
binder := planbuilder.New(ctx, engine.Analyzer.Catalog, engine.Parser)
node, _, _, qFlags, err := binder.Parse("SELECT "+strValue, false)
if err != nil {
return nil, err
}

analyze, err := engine.Analyzer.Analyze(ctx, node, nil)
analyze, err := engine.Analyzer.Analyze(ctx, node, nil, qFlags)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -888,7 +889,7 @@ func executeQueryWithEngine(ctx *sql.Context, engine *gms.Engine, query string)
}).Warn("No current database selected")
}

_, iter, err := engine.Query(queryCtx, query)
_, iter, _, err := engine.Query(queryCtx, query)
if err != nil {
// Log any errors, except for commits with "nothing to commit"
if err.Error() != "nothing to commit" {
Expand Down
6 changes: 3 additions & 3 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (b *DuckBuilder) executeQuery(ctx *sql.Context, n sql.Node, conn *stdsql.Co
case *plan.ShowTables:
duckSQL = ctx.Query()
default:
duckSQL, err = translate(ctx.Query())
duckSQL, err = translate(n, ctx.Query())
}
if err != nil {
return nil, err
Expand All @@ -216,7 +216,7 @@ func (b *DuckBuilder) executeQuery(ctx *sql.Context, n sql.Node, conn *stdsql.Co

func (b *DuckBuilder) executeDML(ctx *sql.Context, n sql.Node, conn *stdsql.Conn) (sql.RowIter, error) {
// Translate the MySQL query to a DuckDB query
duckSQL, err := translate(ctx.Query())
duckSQL, err := translate(n, ctx.Query())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func (b *DuckBuilder) executeDDL(ctx *sql.Context, n sql.Node, table sql.Node, c
duckSQL = fmt.Sprintf(`DROP SCHEMA %s "%s" CASCADE`, ifExists, n.DbName)
default:
// Translate the MySQL query to a DuckDB query
duckSQL, err = translate(ctx.Query())
duckSQL, err = translate(n, ctx.Query())
}
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func TestDuckBuilder_ShowCreateTable(t *testing.T) {
ctx := sql.NewContext(context.Background(), sql.WithSession(session))

engine := sqle.NewDefault(provider)
_, _, err := engine.Query(ctx, "CREATE DATABASE test_schema")
_, _, _, err := engine.Query(ctx, "CREATE DATABASE test_schema")
assert.NoError(t, err)

ctx.SetCurrentDatabase("test_schema")
Expand All @@ -342,7 +342,7 @@ func TestDuckBuilder_ShowCreateTable(t *testing.T) {
name VARCHAR(255),
PRIMARY KEY (id)
)`
_, _, err = engine.Query(ctx, createTableStatement)
_, _, _, err = engine.Query(ctx, createTableStatement)
assert.NoError(t, err)

ctx = sql.NewContext(context.Background(), sql.WithSession(session),
Expand Down
26 changes: 23 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,53 @@ module github.com/apecloud/myduckserver
go 1.22.4

require (
github.com/dolthub/go-mysql-server v0.18.1
github.com/dolthub/vitess v0.0.0-20240404214255-c5a87fc7b325
github.com/DATA-DOG/go-sqlmock v1.5.2
github.com/dolthub/doltgresql v0.11.1
github.com/dolthub/go-mysql-server v0.18.2-0.20240815142344-761713e36043
github.com/dolthub/vitess v0.0.0-20240807181005-71d735078e24
github.com/lib/pq v1.10.9
github.com/marcboeker/go-duckdb v1.7.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.9.0
)

require (
github.com/DATA-DOG/go-sqlmock v1.5.2 // indirect
github.com/apache/arrow/go/v17 v17.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cockroachdb/apd/v2 v2.0.3-0.20200518165714-d020e156310a // indirect
github.com/cockroachdb/errors v1.7.5 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
github.com/cockroachdb/redact v1.0.6 // indirect
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 // indirect
github.com/dolthub/go-icu-regex v0.0.0-20230524105445-af7e7991c97e // indirect
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 // indirect
github.com/go-kit/kit v0.10.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/geo v0.0.0-20200730024412-e86565bf3f35 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/flatbuffers v24.3.25+incompatible // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/pierrre/geohash v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/tetratelabs/wazero v1.1.0 // indirect
github.com/twpayne/go-geom v1.3.6 // indirect
github.com/twpayne/go-kml v1.5.2-0.20200728095708-9f2fd4dfcbfe // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
Expand All @@ -42,6 +60,8 @@ require (
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.22.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
Loading

0 comments on commit dc7043b

Please sign in to comment.