-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
224 lines (198 loc) · 5.77 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import "C"
import (
"context"
"database/sql"
"fmt"
"github.com/jackc/pgx/v5/pgproto3"
"github.com/jackc/pgx/v5/pgtype"
_ "github.com/marcboeker/go-duckdb"
"io"
"net"
)
type DuckDbBackend struct {
backend *pgproto3.Backend
conn net.Conn
db *sql.DB
ctx context.Context
cancel func()
}
func NewDuckDbBackend(conn net.Conn, db *sql.DB, ctx context.Context, cancel func()) *DuckDbBackend {
backend := pgproto3.NewBackend(conn, conn)
connHandler := &DuckDbBackend{
backend: backend,
conn: conn,
db: db,
ctx: ctx,
cancel: cancel,
}
return connHandler
}
func writeMessages(w io.Writer, msgs ...pgproto3.Message) error {
var buf []byte
for _, msg := range msgs {
buf = msg.Encode(buf)
}
_, err := w.Write(buf)
return err
}
var duckdbTypeMap = map[string]int{
"INVALID": pgtype.UnknownOID, // No direct equivalent
"BOOLEAN": pgtype.BoolOID,
"TINYINT": pgtype.Int2OID, // Best guess, as there's no tinyint in PostgreSQL
"SMALLINT": pgtype.Int2OID,
"INTEGER": pgtype.Int4OID,
"BIGINT": pgtype.Int8OID,
"UTINYINT": pgtype.Int2OID, // Best guess
"USMALLINT": pgtype.Int4OID, // Best guess
"UINTEGER": pgtype.Int8OID, // Best guess
"UBIGINT": pgtype.Int8OID, // Best guess
"FLOAT": pgtype.Float4OID,
"DOUBLE": pgtype.Float8OID,
"TIMESTAMP": pgtype.TimestampOID,
"DATE": pgtype.DateOID,
"TIME": pgtype.TimeOID,
"INTERVAL": pgtype.IntervalOID,
"HUGEINT": pgtype.NumericOID, // Best guess
"VARCHAR": pgtype.VarcharOID,
"BLOB": pgtype.ByteaOID,
"DECIMAL": pgtype.NumericOID,
"TIMESTAMP_S": pgtype.TimestampOID, // Assuming standard timestamp
"TIMESTAMP_MS": pgtype.TimestampOID, // Assuming standard timestamp
"TIMESTAMP_NS": pgtype.TimestampOID, // Assuming standard timestamp
"ENUM": pgtype.TextOID, // No direct equivalent
"LIST": pgtype.JSONOID, // Best guess
"STRUCT": pgtype.RecordOID, // Best guess
"MAP": pgtype.JSONOID, // Best guess
"UUID": pgtype.UUIDOID,
}
// duckdbTypeToPgType converts DuckDB type names to corresponding PostgreSQL type
// OIDs.
// See duckdb.rows.typeName
func duckdbTypeToPgType(typeName string) int {
pgType, ok := duckdbTypeMap[typeName]
if !ok {
return pgtype.TextOID // Default to text if no match is found
}
return pgType
}
func toRowDescription(cols []*sql.ColumnType) *pgproto3.RowDescription {
var desc pgproto3.RowDescription
for _, col := range cols {
desc.Fields = append(desc.Fields, pgproto3.FieldDescription{
Name: []byte(col.Name()),
TableOID: 0,
TableAttributeNumber: 0,
DataTypeOID: pgtype.TextOID,
DataTypeSize: -1,
TypeModifier: -1,
Format: 0,
})
}
return &desc
}
func scanRow(rows *sql.Rows, cols []*sql.ColumnType) (*pgproto3.DataRow, error) {
refs := make([]interface{}, len(cols))
values := make([]interface{}, len(cols))
for i := range refs {
refs[i] = &values[i]
}
// Scan from SQLite database.
if err := rows.Scan(refs...); err != nil {
return nil, fmt.Errorf("scan: %w", err)
}
// Convert to TEXT values to return over Postgres wire protocol.
row := pgproto3.DataRow{Values: make([][]byte, len(values))}
for i := range values {
row.Values[i] = []byte(fmt.Sprint(values[i]))
}
return &row, nil
}
func (p *DuckDbBackend) Run() error {
defer p.Close()
err := p.handleStartup()
if err != nil {
return err
}
for {
msg, err := p.backend.Receive()
if err != nil {
return fmt.Errorf("error receiving message: %w", err)
}
switch msg.(type) {
case *pgproto3.Query:
fmt.Println("msg", msg)
query := msg.(*pgproto3.Query)
_, err = p.db.Prepare(query.String)
if err != nil {
writeMessages(p.conn,
&pgproto3.ErrorResponse{Message: err.Error()},
&pgproto3.ReadyForQuery{TxStatus: 'I'},
)
continue
}
rows, err := p.db.QueryContext(p.ctx, query.String)
if err != nil {
writeMessages(p.conn,
&pgproto3.ErrorResponse{Message: err.Error()},
&pgproto3.ReadyForQuery{TxStatus: 'I'},
)
continue
}
defer rows.Close()
cols, err := rows.ColumnTypes()
if err != nil {
return fmt.Errorf("column types: %w", err)
}
buf := toRowDescription(cols).Encode(nil)
// Iterate over each row and encode it to the wire protocol.
for rows.Next() {
row, err := scanRow(rows, cols)
if err != nil {
return fmt.Errorf("scan: %w", err)
}
buf = row.Encode(buf)
}
if err := rows.Err(); err != nil {
return fmt.Errorf("rows: %w", err)
}
buf = (&pgproto3.CommandComplete{CommandTag: []byte("SELECT 1")}).Encode(buf)
buf = (&pgproto3.ReadyForQuery{TxStatus: 'I'}).Encode(buf)
_, err = p.conn.Write(buf)
if err != nil {
return fmt.Errorf("error writing query response: %w", err)
}
case *pgproto3.Terminate:
return nil
default:
return fmt.Errorf("received message other than Query from client: %#v", msg)
}
}
}
func (p *DuckDbBackend) handleStartup() error {
startupMessage, err := p.backend.ReceiveStartupMessage()
if err != nil {
return fmt.Errorf("error receiving startup message: %w", err)
}
switch startupMessage.(type) {
case *pgproto3.StartupMessage:
buf := (&pgproto3.AuthenticationOk{}).Encode(nil)
buf = (&pgproto3.ReadyForQuery{TxStatus: 'I'}).Encode(buf)
_, err = p.conn.Write(buf)
if err != nil {
return fmt.Errorf("error sending ready for query: %w", err)
}
case *pgproto3.SSLRequest:
_, err = p.conn.Write([]byte("N"))
if err != nil {
return fmt.Errorf("error sending deny SSL request: %w", err)
}
return p.handleStartup()
default:
return fmt.Errorf("unknown startup message: %#v", startupMessage)
}
return nil
}
func (p *DuckDbBackend) Close() error {
return p.conn.Close()
}