-
Notifications
You must be signed in to change notification settings - Fork 24
/
clickhouse_test.go
191 lines (171 loc) · 4.37 KB
/
clickhouse_test.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
// Copyright 2024 RunReveal Inc.
// SPDX-License-Identifier: Apache-2.0
package pql
import (
"bytes"
"encoding/csv"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"slices"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestClickhouseLocal(t *testing.T) {
clickhouseExe, err := exec.LookPath("clickhouse")
if err != nil {
t.Skipf("Skipping: clickhouse not found: %v", err)
}
tests, err := findGoldenTests()
if err != nil {
t.Fatal(err)
}
tables, err := findLocalTables(filepath.Join("testdata", "Tables"))
if err != nil {
t.Fatal(err)
}
for _, test := range tests {
wantCSV, wantCSVError := os.ReadFile(filepath.Join(test.dir, "output.csv"))
if errors.Is(wantCSVError, os.ErrNotExist) {
continue
}
t.Run(test.name, func(t *testing.T) {
if test.skip {
t.Skipf("'skip' file present in %s; skipping...", test.dir)
}
if wantCSVError != nil {
t.Fatal("Could not read expected output:", wantCSVError)
}
pqlInput, err := test.input()
if err != nil {
t.Fatal(err)
}
compileOptions, testOptions, err := test.options()
if err != nil {
t.Fatal(err)
}
query, err := compileOptions.Compile(pqlInput)
if err != nil {
t.Fatal("Compile:", err)
}
var args []string
args = append(args, "local", "--format", "CSVWithNames")
fnameBuf := new(strings.Builder)
formatBuf := new(strings.Builder)
for _, tab := range tables {
fnameBuf.Reset()
quoteSQLString(fnameBuf, tab.filename)
formatBuf.Reset()
quoteSQLString(formatBuf, tab.format)
stmt := fmt.Sprintf("CREATE TABLE \"%s\" AS file(%s, %s);", tab.name, fnameBuf, formatBuf)
args = append(args, "--query", stmt)
}
args = appendClickhouseParameterArgs(args, testOptions.parameterValues)
args = append(args, "--query", query)
c := exec.Command(clickhouseExe, args...)
c.Dir = test.dir
gotCSV := new(bytes.Buffer)
c.Stdout = gotCSV
stderr := new(bytes.Buffer)
c.Stderr = stderr
runError := c.Run()
if stderr.Len() > 0 {
t.Logf("clickhouse local stderr:\n%s", stderr)
}
if runError != nil {
t.Fatal("clickhouse local failed:", runError)
}
got, err := csv.NewReader(gotCSV).ReadAll()
if err != nil {
t.Fatal(err)
}
want, err := csv.NewReader(bytes.NewReader(wantCSV)).ReadAll()
if err != nil {
t.Fatal(err)
}
if test.unordered {
sort.Slice(got, func(i, j int) bool {
return isRowLess(got[i], got[j])
})
sort.Slice(want, func(i, j int) bool {
return isRowLess(want[i], want[j])
})
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("query results (-want +got):\n%s", diff)
}
})
}
}
func isRowLess(row1, row2 []string) bool {
for i, n := 0, min(len(row1), len(row2)); i < n; i++ {
if row1[i] < row2[i] {
return true
}
if row1[i] > row2[i] {
return false
}
}
return len(row1) < len(row2)
}
type localTable struct {
name string
filename string
format string
}
// findLocalTables finds all CSV files in a directory that represent tables.
func findLocalTables(dir string) ([]localTable, error) {
var err error
dir, err = filepath.Abs(dir)
if err != nil {
return nil, fmt.Errorf("find local tables: %v", err)
}
listing, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("find local tables: %v", err)
}
var result []localTable
for _, entry := range listing {
filename := entry.Name()
if entry.Type().IsRegular() && !shouldIgnoreFilename(filename) {
if baseName, isCSV := strings.CutSuffix(filename, ".csv"); isCSV {
result = append(result, localTable{
name: baseName,
filename: filepath.Join(dir, filename),
format: "CSVWithNames",
})
} else if baseName, isJSON := strings.CutSuffix(filename, ".json"); isJSON {
result = append(result, localTable{
name: baseName,
filename: filepath.Join(dir, filename),
format: "JSON",
})
}
}
}
return result, nil
}
func appendClickhouseParameterArgs(dst []string, params map[string]string) []string {
if len(params) == 0 {
return dst
}
keys := make([]string, 0, len(params))
for k := range params {
keys = append(keys, k)
}
slices.Sort(keys)
sb := new(strings.Builder)
for _, k := range keys {
sb.WriteString("SET param_")
sb.WriteString(k)
sb.WriteString(" = ")
quoteSQLString(sb, params[k])
sb.WriteString(";")
}
dst = append(dst, "--query", sb.String())
return dst
}