Skip to content

Commit 08bcbb4

Browse files
committed
Merge branch 'main' into feat/sql-interface
2 parents 9aae3f2 + f9d2f2e commit 08bcbb4

File tree

9 files changed

+108
-116
lines changed

9 files changed

+108
-116
lines changed

chdb/session.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,10 @@ func NewSession(paths ...string) (*Session, error) {
3838
// Query calls queryToBuffer with a default output format of "CSV" if not provided.
3939
func (s *Session) Query(queryStr string, outputFormats ...string) *chdbstable.LocalResult {
4040
outputFormat := "CSV" // Default value
41-
udfPath := ""
42-
switch len(outputFormats) {
43-
case 0:
44-
case 1:
41+
if len(outputFormats) > 0 {
4542
outputFormat = outputFormats[0]
46-
case 2:
47-
fallthrough
48-
default:
49-
outputFormat = outputFormats[0]
50-
udfPath = outputFormats[1]
5143
}
52-
return queryToBuffer(queryStr, outputFormat, s.path, udfPath)
44+
return queryToBuffer(queryStr, outputFormat, s.path, "")
5345
}
5446

5547
// Close closes the session and removes the temporary directory

chdb/session_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestQuery(t *testing.T) {
6060
defer session.Cleanup()
6161

6262
session.Query("CREATE DATABASE IF NOT EXISTS testdb; " +
63-
"CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;")
63+
"CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;")
6464

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

chdb/wrapper.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import (
66

77
// Query calls queryToBuffer with a default output format of "CSV" if not provided.
88
func Query(queryStr string, outputFormats ...string) *chdbstable.LocalResult {
9-
outputFormat := "CSV" // Default value
10-
if len(outputFormats) > 0 {
11-
outputFormat = outputFormats[0]
12-
}
13-
return queryToBuffer(queryStr, outputFormat, "", "")
9+
outputFormat := "CSV" // Default value
10+
if len(outputFormats) > 0 {
11+
outputFormat = outputFormats[0]
12+
}
13+
return queryToBuffer(queryStr, outputFormat, "", "")
1414
}
1515

1616
// queryToBuffer constructs the arguments for QueryStable and calls it.

chdb/wrapper_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@ func TestQueryToBuffer(t *testing.T) {
3333
},
3434
// Session
3535
{
36-
name: "Session Query 1",
37-
queryStr: "CREATE DATABASE IF NOT EXISTS testdb; "+
38-
"CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;",
39-
outputFormat: "CSV",
40-
path: tempDir,
41-
udfPath: "",
36+
name: "Session Query 1",
37+
queryStr: "CREATE DATABASE IF NOT EXISTS testdb; " +
38+
"CREATE TABLE IF NOT EXISTS testdb.testtable (id UInt32) ENGINE = MergeTree() ORDER BY id;",
39+
outputFormat: "CSV",
40+
path: tempDir,
41+
udfPath: "",
4242
expectedResult: "",
4343
},
4444
{
45-
name: "Session Query 2",
46-
queryStr: "USE testdb; INSERT INTO testtable VALUES (1), (2), (3);",
47-
outputFormat: "CSV",
48-
path: tempDir,
49-
udfPath: "",
45+
name: "Session Query 2",
46+
queryStr: "USE testdb; INSERT INTO testtable VALUES (1), (2), (3);",
47+
outputFormat: "CSV",
48+
path: tempDir,
49+
udfPath: "",
5050
expectedResult: "",
5151
},
5252
{
53-
name: "Session Query 3",
54-
queryStr: "SELECT * FROM testtable;",
55-
outputFormat: "CSV",
56-
path: tempDir,
57-
udfPath: "",
53+
name: "Session Query 3",
54+
queryStr: "SELECT * FROM testtable;",
55+
outputFormat: "CSV",
56+
path: tempDir,
57+
udfPath: "",
5858
expectedResult: "1\n2\n3\n",
5959
},
6060
}
@@ -66,7 +66,7 @@ func TestQueryToBuffer(t *testing.T) {
6666

6767
// Verify
6868
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",
69+
t.Errorf("%v queryToBuffer() with queryStr %v, outputFormat %v, path %v, udfPath %v, expect result: %v, got result: %v",
7070
tc.name, tc.queryStr, tc.outputFormat, tc.path, tc.udfPath, tc.expectedResult, string(result.Buf()))
7171
}
7272
})

chdbstable/chdb.go

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,83 @@ package chdbstable
77
*/
88
import "C"
99
import (
10-
"runtime"
11-
"unsafe"
10+
"runtime"
11+
"unsafe"
1212
)
1313

1414
// LocalResult mirrors the C struct local_result in Go.
1515
type LocalResult struct {
16-
cResult *C.struct_local_result
16+
cResult *C.struct_local_result
1717
}
1818

1919
// newLocalResult creates a new LocalResult and sets a finalizer to free C memory.
2020
func newLocalResult(cResult *C.struct_local_result) *LocalResult {
21-
result := &LocalResult{cResult: cResult}
22-
runtime.SetFinalizer(result, freeLocalResult)
23-
return result
21+
result := &LocalResult{cResult: cResult}
22+
runtime.SetFinalizer(result, freeLocalResult)
23+
return result
2424
}
2525

2626
// freeLocalResult is called by the garbage collector.
2727
func freeLocalResult(result *LocalResult) {
28-
C.free_result(result.cResult)
28+
C.free_result(result.cResult)
2929
}
3030

3131
// QueryStable calls the C function query_stable.
3232
func QueryStable(argc int, argv []string) *LocalResult {
33-
cArgv := make([]*C.char, len(argv))
34-
for i, s := range argv {
35-
cArgv[i] = C.CString(s)
36-
defer C.free(unsafe.Pointer(cArgv[i]))
37-
}
33+
cArgv := make([]*C.char, len(argv))
34+
for i, s := range argv {
35+
cArgv[i] = C.CString(s)
36+
defer C.free(unsafe.Pointer(cArgv[i]))
37+
}
3838

39-
cResult := C.query_stable(C.int(argc), &cArgv[0])
40-
return newLocalResult(cResult)
39+
cResult := C.query_stable(C.int(argc), &cArgv[0])
40+
return newLocalResult(cResult)
4141
}
4242

4343
// Accessor methods to access fields of the local_result struct.
4444
func (r *LocalResult) Buf() []byte {
45-
if r.cResult == nil {
46-
return nil
47-
}
48-
if r.cResult.buf == nil {
49-
return nil
50-
}
51-
return C.GoBytes(unsafe.Pointer(r.cResult.buf), C.int(r.cResult.len))
45+
if r.cResult == nil {
46+
return nil
47+
}
48+
if r.cResult.buf == nil {
49+
return nil
50+
}
51+
return C.GoBytes(unsafe.Pointer(r.cResult.buf), C.int(r.cResult.len))
5252
}
5353

5454
// Stringer interface for LocalResult
5555
func (r LocalResult) String() string {
56-
ret := r.Buf()
57-
if ret == nil {
58-
return ""
59-
}
60-
return string(ret)
56+
ret := r.Buf()
57+
if ret == nil {
58+
return ""
59+
}
60+
return string(ret)
6161
}
6262

6363
func (r *LocalResult) Len() int {
64-
if r.cResult == nil {
65-
return 0
66-
}
67-
return int(r.cResult.len)
64+
if r.cResult == nil {
65+
return 0
66+
}
67+
return int(r.cResult.len)
6868
}
6969

7070
func (r *LocalResult) Elapsed() float64 {
71-
if r.cResult == nil {
72-
return 0
73-
}
74-
return float64(r.cResult.elapsed)
71+
if r.cResult == nil {
72+
return 0
73+
}
74+
return float64(r.cResult.elapsed)
7575
}
7676

7777
func (r *LocalResult) RowsRead() uint64 {
78-
if r.cResult == nil {
79-
return 0
80-
}
81-
return uint64(r.cResult.rows_read)
78+
if r.cResult == nil {
79+
return 0
80+
}
81+
return uint64(r.cResult.rows_read)
8282
}
8383

8484
func (r *LocalResult) BytesRead() uint64 {
85-
if r.cResult == nil {
86-
return 0
87-
}
88-
return uint64(r.cResult.bytes_read)
85+
if r.cResult == nil {
86+
return 0
87+
}
88+
return uint64(r.cResult.bytes_read)
8989
}

chdbstable/chdb_test.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
package chdbstable
22

33
import (
4-
"testing"
4+
"testing"
55
)
66

77
// TestCase defines the structure of a test case
88
type TestCase struct {
9-
name string // Name of the test case
10-
argv []string // Arguments to pass to QueryStable
11-
expectError bool // Whether an error is expected
12-
expectOutput string // Expected output
9+
name string // Name of the test case
10+
argv []string // Arguments to pass to QueryStable
11+
expectError bool // Whether an error is expected
12+
expectOutput string // Expected output
1313
}
1414

1515
func TestQueryStableMultipleCases(t *testing.T) {
16-
// Define a series of test cases
17-
testCases := []TestCase{
18-
{
19-
name: "Single Query",
20-
argv: []string{"clickhouse", "--multiquery", "--output-format=CSV", "--query=SELECT 123;"},
21-
expectError: false,
22-
expectOutput: "123\n",
23-
},
24-
{
25-
name: "Multiple Queries",
26-
argv: []string{"clickhouse", "--multiquery", "--output-format=CSV", "--query=SELECT 'abc';"},
27-
expectError: false,
28-
expectOutput: "abc",
29-
},
30-
}
16+
// Define a series of test cases
17+
testCases := []TestCase{
18+
{
19+
name: "Single Query",
20+
argv: []string{"clickhouse", "--multiquery", "--output-format=CSV", "--query=SELECT 123;"},
21+
expectError: false,
22+
expectOutput: "123\n",
23+
},
24+
{
25+
name: "Multiple Queries",
26+
argv: []string{"clickhouse", "--multiquery", "--output-format=CSV", "--query=SELECT 'abc';"},
27+
expectError: false,
28+
expectOutput: "abc",
29+
},
30+
}
3131

32-
// Iterate over the test cases
33-
for _, tc := range testCases {
34-
t.Run(tc.name, func(t *testing.T) {
35-
result := QueryStable(len(tc.argv), tc.argv)
32+
// Iterate over the test cases
33+
for _, tc := range testCases {
34+
t.Run(tc.name, func(t *testing.T) {
35+
result := QueryStable(len(tc.argv), tc.argv)
3636

37-
// 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-
}
37+
// 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+
}
4141

42-
if (result != nil) && (string(result) != tc.expectOutput) {
43-
t.Errorf("QueryStable() with args %v, expect output: %v, got output: %v", tc.argv, tc.expectOutput, string(result.Buf()))
44-
}
45-
})
46-
}
42+
if (result != nil) && (string(result) != tc.expectOutput) {
43+
t.Errorf("QueryStable() with args %v, expect output: %v, got output: %v", tc.argv, tc.expectOutput, string(result.Buf()))
44+
}
45+
})
46+
}
4747
}

cli/cli.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package cli
22

33
import (
4-
"github.com/chdb-io/chdb-go/cli/history"
54
"github.com/chdb-io/chdb-go/chdb"
5+
"github.com/chdb-io/chdb-go/cli/history"
66
)
77

88
// CLI object of cli :)
99
type CLI struct {
1010
history *history.History
1111

12-
Session *chdb.Session
12+
Session *chdb.Session
1313
Multiline bool
1414
isMultilineInputStarted bool
1515
query string

cli/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package cli
22

33
import (
44
"context"
5-
"strings"
65
"io/ioutil"
6+
"strings"
77
)
88

99
// GetCurrentDB from chDB

main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717

1818
func main() {
1919
// Define command line flags
20-
pathFlag := flag.String("path", "",
21-
`Specify a custom path for the session, default is a temporary directory and
20+
pathFlag := flag.String("path", "",
21+
`Specify a custom path for the session, default is a temporary directory and
2222
data will lost after exit. If you want to keep the data, specify a path to a directory.`)
23-
24-
helpFlag := flag.Bool("help", false,
25-
`Show this help message and exit.
23+
24+
helpFlag := flag.Bool("help", false,
25+
`Show this help message and exit.
2626
Usage: chdb-go [options] [sql [output format]]
2727
Example:
2828
./chdb-go 'SELECT 123' # default output CSV

0 commit comments

Comments
 (0)