Skip to content

Commit 83e3408

Browse files
committed
Fix stable wrapper
1 parent df60255 commit 83e3408

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

chdbstable/chdb.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func (e *ChdbError) Error() string {
2424
// ErrNilResult is returned when the C function returns a nil pointer.
2525
var ErrNilResult = errors.New("chDB C function returned nil pointer")
2626

27-
2827
// LocalResult mirrors the C struct local_result_v2 in Go.
2928
type LocalResult struct {
3029
cResult *C.struct_local_result_v2
@@ -52,8 +51,10 @@ func QueryStable(argc int, argv []string) (result *LocalResult, err error) {
5251

5352
cResult := C.query_stable_v2(C.int(argc), &cArgv[0])
5453
if cResult == nil {
55-
// According to the C ABI of chDB, it is not possible to return a nil pointer.
56-
return nil, ErrNilResult
54+
// According to the C ABI of chDB v1.2.0, the C function query_stable_v2
55+
// returns nil if the query returns no data. This is not an error. We
56+
// will change this behavior in the future.
57+
return newLocalResult(cResult), nil
5758
}
5859
if cResult.error_message != nil {
5960
return nil, &ChdbError{msg: C.GoString(cResult.error_message)}
@@ -108,10 +109,3 @@ func (r *LocalResult) BytesRead() uint64 {
108109
}
109110
return uint64(r.cResult.bytes_read)
110111
}
111-
112-
func (r *LocalResult) Error() string {
113-
if r.cResult == nil {
114-
return ""
115-
}
116-
return C.GoString(r.cResult.err)
117-
}

chdbstable/chdb_test.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,48 @@ func TestQueryStableMultipleCases(t *testing.T) {
2727
expectError: false,
2828
expectOutput: "\"abc\"\n",
2929
},
30+
{
31+
name: "Error Query",
32+
argv: []string{"clickhouse", "--multiquery", "--output-format=CSV", "--query=XXX;"},
33+
expectError: true,
34+
expectOutput: "",
35+
},
3036
}
3137

3238
// Iterate over the test cases
3339
for _, tc := range testCases {
3440
t.Run(tc.name, func(t *testing.T) {
35-
result := QueryStable(len(tc.argv), tc.argv)
41+
result, err := QueryStable(len(tc.argv), tc.argv)
3642

3743
// Assert based on the expected outcome of the test case
38-
if (result == nil) && tc.expectError {
39-
t.Errorf("QueryStable() with args %v, expect error: %v, got result: %v", tc.argv, tc.expectError, result)
40-
}
41-
42-
if (result != nil) && string(result.Buf()) != tc.expectOutput {
43-
t.Errorf("QueryStable() with args %v, expect output: %v, got output: %v", tc.argv, tc.expectOutput, string(result.Buf()))
44+
if tc.expectError {
45+
if err == nil {
46+
t.Errorf("Expected error, but got nil")
47+
}
48+
} else {
49+
if err != nil {
50+
t.Errorf("Expected no error, but got %v", err)
51+
} else {
52+
if result == nil {
53+
t.Errorf("Expected non-nil result, but got nil")
54+
} else {
55+
if result.cResult == nil {
56+
t.Errorf("Expected non-nil cResult, but got nil")
57+
} else {
58+
if result.cResult.error_message != nil {
59+
t.Errorf("Expected nil error_message, but got %v", result.cResult.error_message)
60+
} else {
61+
if result.cResult.buf == nil {
62+
t.Errorf("Expected non-nil output, but got nil")
63+
} else {
64+
if tc.expectOutput != string(result.String()) {
65+
t.Errorf("Expected output %v, but got %v", tc.expectOutput, string(result.String()))
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
4472
}
4573
})
4674
}

0 commit comments

Comments
 (0)