-
Notifications
You must be signed in to change notification settings - Fork 500
/
clickhouse.go
105 lines (92 loc) · 2.41 KB
/
clickhouse.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
package types
// This file contains some code extracted from github.com/ClickHouse/clickhouse-go
//
// Copyright 2016-2023 ClickHouse, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import (
"fmt"
"strings"
"time"
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
)
// From clickhouse-go
func params(t column.Type) string {
switch start, end := strings.Index(string(t), "("), strings.LastIndex(string(t), ")"); {
case len(t) == 0, start <= 0, end <= 0, end < start:
return ""
default:
return string(t[start+1 : end])
}
}
type namedCol struct {
name string
colType column.Type
}
func tupleFieldSpec(spec string) *namedCol {
spec = strings.TrimSpace(spec)
if len(spec) == 0 {
return nil
}
if parts := strings.SplitN(spec, " ", 2); len(parts) == 2 {
if !strings.Contains(parts[0], "(") {
return &namedCol{
name: strings.TrimSpace(parts[0]),
colType: column.Type(strings.TrimSpace(parts[1])),
}
}
}
return &namedCol{colType: column.Type(strings.TrimSpace(spec))}
}
func parseTupleType(t column.Type, tz *time.Location) ([]column.Interface, error) {
var (
elements []namedCol
brackets int
)
p := params(t)
spec := make([]rune, 0, len(p))
for _, r := range p {
switch r {
case '(':
brackets++
case ')':
brackets--
case ',':
if brackets == 0 {
col := tupleFieldSpec(string(spec))
if col != nil {
elements = append(elements, *col)
}
spec = spec[:0] // cleanup
continue
}
}
spec = append(spec, r)
}
col := tupleFieldSpec(string(spec))
if col != nil {
elements = append(elements, *col)
}
columns := make([]column.Interface, len(elements))
for i, ct := range elements {
if len(ct.name) == 0 {
return nil, fmt.Errorf("unsupported ClickHouse Tuple type (Aache Arrow requires named fields): %s", t)
}
col, err := ct.colType.Column(ct.name, tz)
if err != nil {
return nil, err
}
columns[i] = col
}
return columns, nil
}