-
-
Notifications
You must be signed in to change notification settings - Fork 6
add: golang sql interface #1
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
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c27f19d
add: golang sql interface
nmreadelf 89e79f6
impl: get result columns
nmreadelf 1c0bbbe
impl: rows logic
nmreadelf f46bcc5
fix: test fatal error message
nmreadelf 9aae3f2
impl: support session in golang sql interface
nmreadelf f9d2f2e
Format code
auxten 08bcbb4
Merge branch 'main' into feat/sql-interface
auxten 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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
package chdb | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/apache/arrow/go/v14/arrow" | ||
"github.com/apache/arrow/go/v14/arrow/array" | ||
"github.com/apache/arrow/go/v14/arrow/decimal128" | ||
"github.com/apache/arrow/go/v14/arrow/decimal256" | ||
wrapper "github.com/chdb-io/chdb-go/chdb" | ||
"github.com/chdb-io/chdb-go/chdbstable" | ||
|
||
"github.com/apache/arrow/go/v14/arrow/ipc" | ||
) | ||
|
||
func init() { | ||
sql.Register("chdb", Driver{}) | ||
} | ||
|
||
type connector struct { | ||
} | ||
|
||
// Connect returns a connection to a database. | ||
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) { | ||
return &conn{}, nil | ||
} | ||
|
||
// Driver returns the underying Driver of the connector, | ||
// compatibility with the Driver method on sql.DB | ||
func (c *connector) Driver() driver.Driver { return Driver{} } | ||
|
||
type Driver struct{} | ||
|
||
// Open returns a new connection to the database. | ||
func (d Driver) Open(name string) (driver.Conn, error) { | ||
return &conn{}, nil | ||
} | ||
|
||
// OpenConnector expects the same format as driver.Open | ||
func (d Driver) OpenConnector(dataSourceName string) (driver.Connector, error) { | ||
return &connector{}, nil | ||
} | ||
|
||
type conn struct { | ||
} | ||
|
||
func (c *conn) Close() error { | ||
return nil | ||
} | ||
|
||
func (c *conn) Query(query string, values []driver.Value) (driver.Rows, error) { | ||
namedValues := make([]driver.NamedValue, len(values)) | ||
for i, value := range values { | ||
namedValues[i] = driver.NamedValue{ | ||
// nb: Name field is optional | ||
Ordinal: i, | ||
Value: value, | ||
} | ||
} | ||
return c.QueryContext(context.Background(), query, namedValues) | ||
} | ||
|
||
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { | ||
result := wrapper.Query(query, "Arrow") | ||
buf := result.Buf() | ||
if buf == nil { | ||
return nil, fmt.Errorf("result is nil") | ||
} | ||
reader, err := ipc.NewFileReader(bytes.NewReader(buf)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &rows{localResult: result, reader: reader}, nil | ||
} | ||
|
||
func (c *conn) Begin() (driver.Tx, error) { | ||
return nil, fmt.Errorf("does not support Transcation") | ||
} | ||
|
||
func (c *conn) Prepare(query string) (driver.Stmt, error) { | ||
return c.PrepareContext(context.Background(), query) | ||
} | ||
|
||
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { | ||
return nil, fmt.Errorf("does not support prepare statement") | ||
} | ||
|
||
// todo: func(c *conn) Prepare(query string) | ||
// todo: func(c *conn) PrepareContext(ctx context.Context, query string) | ||
// todo: prepared statment | ||
|
||
type rows struct { | ||
localResult *chdbstable.LocalResult | ||
reader *ipc.FileReader | ||
curRecord arrow.Record | ||
curRow int64 | ||
} | ||
|
||
func (r *rows) Columns() (out []string) { | ||
sch := r.reader.Schema() | ||
for i := 0; i < sch.NumFields(); i++ { | ||
out = append(out, sch.Field(i).Name) | ||
} | ||
return | ||
} | ||
|
||
func (r *rows) Close() error { | ||
if r.curRecord != nil { | ||
r.curRecord = nil | ||
} | ||
// ignore reader close | ||
_ = r.reader.Close() | ||
r.reader = nil | ||
r.localResult = nil | ||
return nil | ||
} | ||
|
||
func (r *rows) Next(dest []driver.Value) error { | ||
if r.curRecord != nil && r.curRow == r.curRecord.NumRows() { | ||
r.curRecord = nil | ||
} | ||
for r.curRecord == nil { | ||
record, err := r.reader.Read() | ||
if err != nil { | ||
return err | ||
} | ||
if record.NumRows() == 0 { | ||
continue | ||
} | ||
r.curRecord = record | ||
r.curRow = 0 | ||
} | ||
|
||
for i, col := range r.curRecord.Columns() { | ||
if col.IsNull(int(r.curRow)) { | ||
dest[i] = nil | ||
continue | ||
} | ||
switch col := col.(type) { | ||
case *array.Boolean: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Int8: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Uint8: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Int16: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Uint16: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Int32: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Uint32: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Int64: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Uint64: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Float32: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Float64: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.String: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.LargeString: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Binary: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.LargeBinary: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Date32: | ||
dest[i] = col.Value(int(r.curRow)).ToTime() | ||
case *array.Date64: | ||
dest[i] = col.Value(int(r.curRow)).ToTime() | ||
case *array.Time32: | ||
dest[i] = col.Value(int(r.curRow)).ToTime(col.DataType().(*arrow.Time32Type).Unit) | ||
case *array.Time64: | ||
dest[i] = col.Value(int(r.curRow)).ToTime(col.DataType().(*arrow.Time64Type).Unit) | ||
case *array.Timestamp: | ||
dest[i] = col.Value(int(r.curRow)).ToTime(col.DataType().(*arrow.TimestampType).Unit) | ||
case *array.Decimal128: | ||
dest[i] = col.Value(int(r.curRow)) | ||
case *array.Decimal256: | ||
dest[i] = col.Value(int(r.curRow)) | ||
default: | ||
return fmt.Errorf( | ||
"not yet implemented populating from columns of type " + col.DataType().String(), | ||
) | ||
} | ||
} | ||
|
||
r.curRow++ | ||
return nil | ||
} | ||
|
||
func (r *rows) ColumnTypeDatabaseTypeName(index int) string { | ||
return r.reader.Schema().Field(index).Type.String() | ||
} | ||
|
||
func (r *rows) ColumnTypeNullable(index int) (nullable, ok bool) { | ||
return r.reader.Schema().Field(index).Nullable, true | ||
} | ||
|
||
func (r *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { | ||
typ := r.reader.Schema().Field(index).Type | ||
switch dt := typ.(type) { | ||
case *arrow.Decimal128Type: | ||
return int64(dt.Precision), int64(dt.Scale), true | ||
case *arrow.Decimal256Type: | ||
return int64(dt.Precision), int64(dt.Scale), true | ||
} | ||
return 0, 0, false | ||
} | ||
|
||
func (r *rows) ColumnTypeScanType(index int) reflect.Type { | ||
switch r.reader.Schema().Field(index).Type.ID() { | ||
case arrow.BOOL: | ||
return reflect.TypeOf(false) | ||
case arrow.INT8: | ||
return reflect.TypeOf(int8(0)) | ||
case arrow.UINT8: | ||
return reflect.TypeOf(uint8(0)) | ||
case arrow.INT16: | ||
return reflect.TypeOf(int16(0)) | ||
case arrow.UINT16: | ||
return reflect.TypeOf(uint16(0)) | ||
case arrow.INT32: | ||
return reflect.TypeOf(int32(0)) | ||
case arrow.UINT32: | ||
return reflect.TypeOf(uint32(0)) | ||
case arrow.INT64: | ||
return reflect.TypeOf(int64(0)) | ||
case arrow.UINT64: | ||
return reflect.TypeOf(uint64(0)) | ||
case arrow.FLOAT32: | ||
return reflect.TypeOf(float32(0)) | ||
case arrow.FLOAT64: | ||
return reflect.TypeOf(float64(0)) | ||
case arrow.DECIMAL128: | ||
return reflect.TypeOf(decimal128.Num{}) | ||
case arrow.DECIMAL256: | ||
return reflect.TypeOf(decimal256.Num{}) | ||
case arrow.BINARY: | ||
return reflect.TypeOf([]byte{}) | ||
case arrow.STRING: | ||
return reflect.TypeOf(string("")) | ||
case arrow.TIME32, arrow.TIME64, arrow.DATE32, arrow.DATE64, arrow.TIMESTAMP: | ||
return reflect.TypeOf(time.Time{}) | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package chdb | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
) | ||
|
||
func TestDb(t *testing.T) { | ||
db, err := sql.Open("chdb", "") | ||
if err != nil { | ||
t.Errorf("open db fail") | ||
} | ||
if db.Ping() != nil { | ||
t.Errorf("ping db fail") | ||
} | ||
{ | ||
rows, err := db.Query(`SELECT 1,'abc'`) | ||
if err != nil { | ||
t.Errorf("run Query fail, err:%s", err) | ||
} | ||
cols, err := rows.Columns() | ||
if err != nil { | ||
t.Errorf("get result columns fail, err: %s", err) | ||
} | ||
if len(cols) != 2 { | ||
t.Errorf("select result columns length should be 2") | ||
} | ||
var ( | ||
bar int | ||
foo string | ||
) | ||
defer rows.Close() | ||
for rows.Next() { | ||
err := rows.Scan(&bar, &foo) | ||
if err != nil { | ||
t.Errorf("scan fail, err: %s", err) | ||
} | ||
if bar != 1 { | ||
t.Errorf("expected error") | ||
} | ||
if foo != "abc" { | ||
t.Errorf("expected error") | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.