Skip to content

Commit

Permalink
feat: support PG wire protocol for direct DuckDB access (#107)
Browse files Browse the repository at this point in the history
* Adapt Doltgresql's server package for our purpose
* Add type DuckDB->PG type mapping
* Make psql work
  • Loading branch information
fanyang01 authored Oct 29, 2024
1 parent 79dc72b commit 80ffb3e
Show file tree
Hide file tree
Showing 14 changed files with 2,713 additions and 50 deletions.
4 changes: 3 additions & 1 deletion backend/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func (iter *SQLRowIter) Next(ctx *sql.Context) (sql.Row, error) {

// Prune or fill the values to match the schema
width := len(iter.schema) // the desired width
if len(iter.columns) < width {
if width == 0 {
width = len(iter.columns)
} else if len(iter.columns) < width {
for i := len(iter.columns); i < width; i++ {
iter.buffer[i] = nil
}
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ require (
github.com/Shopify/toxiproxy/v2 v2.9.0
github.com/apache/arrow/go/v17 v17.0.0
github.com/cockroachdb/apd/v3 v3.2.1
github.com/dolthub/doltgresql v0.13.0
github.com/dolthub/go-mysql-server v0.18.2-0.20241018220726-63ed221b1772
github.com/dolthub/vitess v0.0.0-20241016191424-d14e107a654e
github.com/go-sql-driver/mysql v1.8.1
github.com/jackc/pgx/v5 v5.7.1
github.com/jmoiron/sqlx v1.4.0
github.com/lib/pq v1.10.9
github.com/marcboeker/go-duckdb v1.8.2-0.20241002112231-62d5fa8c0697
github.com/prometheus/client_golang v1.20.3
github.com/rs/zerolog v1.33.0
Expand All @@ -23,7 +26,7 @@ require (

replace (
github.com/dolthub/go-mysql-server v0.18.2-0.20241018220726-63ed221b1772 => github.com/fanyang01/go-mysql-server v0.0.0-20241021025444-83e2e88c99aa
github.com/dolthub/vitess v0.0.0-20241016191424-d14e107a654e => github.com/apecloud/dolt-vitess v0.0.0-20241017031156-06988c627a21
github.com/dolthub/vitess v0.0.0-20241016191424-d14e107a654e => github.com/apecloud/dolt-vitess v0.0.0-20241028060845-4a2a0444a0ac
)

require (
Expand All @@ -36,7 +39,6 @@ require (
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 // indirect
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662 // indirect
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 // indirect
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216 // indirect
github.com/go-kit/kit v0.10.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gocraft/dbr/v2 v2.7.2 // indirect
Expand All @@ -62,9 +64,11 @@ require (
github.com/rs/xid v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tetratelabs/wazero v1.1.0 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
Expand Down
78 changes: 33 additions & 45 deletions go.sum

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/apecloud/myduckserver/backend"
"github.com/apecloud/myduckserver/catalog"
"github.com/apecloud/myduckserver/myfunc"
"github.com/apecloud/myduckserver/pgserver"
"github.com/apecloud/myduckserver/plugin"
"github.com/apecloud/myduckserver/replica"
"github.com/apecloud/myduckserver/transpiler"
Expand All @@ -44,6 +45,7 @@ var (
address = "0.0.0.0"
port = 3306
socket string
postgresPort = 5432
dataDirectory = "."
dbFileName = "mysql.db"
logLevel = int(logrus.InfoLevel)
Expand All @@ -58,6 +60,8 @@ func init() {
flag.StringVar(&dataDirectory, "datadir", dataDirectory, "The directory to store the database.")
flag.IntVar(&logLevel, "loglevel", logLevel, "The log level to use.")

flag.IntVar(&postgresPort, "pg-port", postgresPort, "The port to bind to for PostgreSQL wire protocol.")

// The following options need to be set for MySQL Shell's utilities to work properly.

// https://dev.mysql.com/doc/refman/8.4/en/replication-options-replica.html#sysvar_report_host
Expand Down Expand Up @@ -115,11 +119,20 @@ func main() {
Address: fmt.Sprintf("%s:%d", address, port),
Socket: socket,
}
s, err := server.NewServerWithHandler(config, engine, backend.NewSessionBuilder(provider, pool), nil, backend.WrapHandler(pool))
srv, err := server.NewServerWithHandler(config, engine, backend.NewSessionBuilder(provider, pool), nil, backend.WrapHandler(pool))
if err != nil {
panic(err)
}
if err = s.Start(); err != nil {

if postgresPort > 0 {
pgServer, err := pgserver.NewServer(srv, address, postgresPort)
if err != nil {
panic(err)
}
go pgServer.Start()
}

if err = srv.Start(); err != nil {
panic(err)
}
}
1 change: 1 addition & 0 deletions pgserver/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 [the DoltgreSQL project](https://github.com/dolthub/doltgresql) (as of 2024-10-25, https://github.com/dolthub/doltgresql/blob/main/server). The original code is licensed under the Apache License, Version 2.0. The modifications are also licensed under the Apache License, Version 2.0.
Loading

0 comments on commit 80ffb3e

Please sign in to comment.