Skip to content

Commit df60255

Browse files
committed
Use chdb stable ABI v2
1 parent 6d90f19 commit df60255

File tree

6 files changed

+58
-26
lines changed

6 files changed

+58
-26
lines changed

chdb/driver/driver.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func init() {
2727
sql.Register("chdb", Driver{})
2828
}
2929

30-
type queryHandle func(string, ...string) *chdbstable.LocalResult
30+
type queryHandle func(string, ...string) (*chdbstable.LocalResult, error)
3131

3232
type connector struct {
3333
udfPath string
@@ -127,7 +127,10 @@ func (c *conn) Query(query string, values []driver.Value) (driver.Rows, error) {
127127
}
128128

129129
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
130-
result := c.QueryFun(query, "Arrow", c.udfPath)
130+
result, err := c.QueryFun(query, "Arrow", c.udfPath)
131+
if err != nil {
132+
return nil, err
133+
}
131134
buf := result.Buf()
132135
if buf == nil {
133136
return nil, fmt.Errorf("result is nil")

chdb/driver/driver_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ func TestDbWithSession(t *testing.T) {
9090

9191
session.Query("USE testdb; INSERT INTO testtable VALUES (1), (2), (3);")
9292

93-
ret := session.Query("SELECT * FROM testtable;")
93+
ret, err := session.Query("SELECT * FROM testtable;")
94+
if err != nil {
95+
t.Fatalf("Query fail, err: %s", err)
96+
}
9497
if string(ret.Buf()) != "1\n2\n3\n" {
9598
t.Errorf("Query result should be 1\n2\n3\n, got %s", string(ret.Buf()))
9699
}

chdb/session.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewSession(paths ...string) (*Session, error) {
3636
}
3737

3838
// Query calls queryToBuffer with a default output format of "CSV" if not provided.
39-
func (s *Session) Query(queryStr string, outputFormats ...string) *chdbstable.LocalResult {
39+
func (s *Session) Query(queryStr string, outputFormats ...string) (result *chdbstable.LocalResult, err error) {
4040
outputFormat := "CSV" // Default value
4141
if len(outputFormats) > 0 {
4242
outputFormat = outputFormats[0]

chdb/session_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func TestQuery(t *testing.T) {
6464

6565
session.Query("USE testdb; INSERT INTO testtable VALUES (1), (2), (3);")
6666

67-
ret := session.Query("SELECT * FROM testtable;")
67+
ret, err := session.Query("SELECT * FROM testtable;")
68+
if err != nil {
69+
t.Errorf("Query failed: %s", err)
70+
}
6871
if string(ret.Buf()) != "1\n2\n3\n" {
6972
t.Errorf("Query result should be 1\n2\n3\n, got %s", string(ret.Buf()))
7073
}

chdb/wrapper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
// Query calls queryToBuffer with a default output format of "CSV" if not provided.
8-
func Query(queryStr string, outputFormats ...string) *chdbstable.LocalResult {
8+
func Query(queryStr string, outputFormats ...string) (result *chdbstable.LocalResult, err error) {
99
outputFormat := "CSV" // Default value
1010
if len(outputFormats) > 0 {
1111
outputFormat = outputFormats[0]
@@ -14,7 +14,7 @@ func Query(queryStr string, outputFormats ...string) *chdbstable.LocalResult {
1414
}
1515

1616
// queryToBuffer constructs the arguments for QueryStable and calls it.
17-
func queryToBuffer(queryStr, outputFormat, path, udfPath string) *chdbstable.LocalResult {
17+
func queryToBuffer(queryStr, outputFormat, path, udfPath string) (result *chdbstable.LocalResult, err error) {
1818
argv := []string{"clickhouse", "--multiquery"}
1919

2020
// Handle output format

chdb/wrapper_test.go

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package chdb
22

33
import (
4-
"io/ioutil"
54
"os"
5+
"path/filepath"
66
"testing"
77
)
88

99
func TestQueryToBuffer(t *testing.T) {
1010
// Create a temporary directory
11-
tempDir, err := ioutil.TempDir("", "example")
12-
if err != nil {
13-
t.Fatalf("Failed to create temporary directory: %v", err)
14-
}
11+
tempDir := filepath.Join(os.TempDir(), "chdb_test")
1512
defer os.RemoveAll(tempDir)
1613

1714
// Define test cases
@@ -21,6 +18,7 @@ func TestQueryToBuffer(t *testing.T) {
2118
outputFormat string
2219
path string
2320
udfPath string
21+
expectedErrMsg string
2422
expectedResult string
2523
}{
2624
{
@@ -29,6 +27,7 @@ func TestQueryToBuffer(t *testing.T) {
2927
outputFormat: "CSV",
3028
path: "",
3129
udfPath: "",
30+
expectedErrMsg: "",
3231
expectedResult: "123\n",
3332
},
3433
// Session
@@ -39,35 +38,59 @@ func TestQueryToBuffer(t *testing.T) {
3938
outputFormat: "CSV",
4039
path: tempDir,
4140
udfPath: "",
41+
expectedErrMsg: "",
4242
expectedResult: "",
4343
},
44+
// {
45+
// name: "Session Query 2",
46+
// queryStr: "USE testdb; INSERT INTO testtable VALUES (1), (2), (3);",
47+
// outputFormat: "CSV",
48+
// path: tempDir,
49+
// udfPath: "",
50+
// expectedErrMsg: "",
51+
// expectedResult: "",
52+
// },
53+
// {
54+
// name: "Session Query 3",
55+
// queryStr: "SELECT * FROM testtable;",
56+
// outputFormat: "CSV",
57+
// path: tempDir,
58+
// udfPath: "",
59+
// expectedErrMsg: "",
60+
// expectedResult: "1\n2\n3\n",
61+
// },
4462
{
45-
name: "Session Query 2",
46-
queryStr: "USE testdb; INSERT INTO testtable VALUES (1), (2), (3);",
63+
name: "Error Query",
64+
queryStr: "SELECT * FROM nonexist; ",
4765
outputFormat: "CSV",
4866
path: tempDir,
4967
udfPath: "",
68+
expectedErrMsg: "Code: 60. DB::Exception: Table _local.nonexist does not exist. (UNKNOWN_TABLE)",
5069
expectedResult: "",
5170
},
52-
{
53-
name: "Session Query 3",
54-
queryStr: "SELECT * FROM testtable;",
55-
outputFormat: "CSV",
56-
path: tempDir,
57-
udfPath: "",
58-
expectedResult: "1\n2\n3\n",
59-
},
6071
}
6172

6273
for _, tc := range testCases {
6374
t.Run(tc.name, func(t *testing.T) {
6475
// Call queryToBuffer
65-
result := queryToBuffer(tc.queryStr, tc.outputFormat, tc.path, tc.udfPath)
76+
result, err := queryToBuffer(tc.queryStr, tc.outputFormat, tc.path, tc.udfPath)
6677

6778
// Verify
68-
if string(result.Buf()) != tc.expectedResult {
69-
t.Errorf("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect result: %v, got result: %v",
70-
tc.name, tc.queryStr, tc.outputFormat, tc.path, tc.udfPath, tc.expectedResult, string(result.Buf()))
79+
if tc.expectedErrMsg != "" {
80+
if err == nil {
81+
t.Errorf("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect error message: %v, got no error",
82+
tc.name, tc.queryStr, tc.outputFormat, tc.path, tc.udfPath, tc.expectedErrMsg)
83+
} else {
84+
if err.Error() != tc.expectedErrMsg {
85+
t.Errorf("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect error message: %v, got error message: %v",
86+
tc.name, tc.queryStr, tc.outputFormat, tc.path, tc.udfPath, tc.expectedErrMsg, err.Error())
87+
}
88+
}
89+
} else {
90+
if string(result.Buf()) != tc.expectedResult {
91+
t.Errorf("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect result: %v, got result: %v",
92+
tc.name, tc.queryStr, tc.outputFormat, tc.path, tc.udfPath, tc.expectedResult, string(result.Buf()))
93+
}
7194
}
7295
})
7396
}

0 commit comments

Comments
 (0)