aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/clickhouse-go/lib/column/tuple.go
blob: 00578a6e2c70bbd20303f13c3493a5c22b6244d0 (plain) (blame)
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
package column

import (
	"fmt"
	"github.com/ClickHouse/clickhouse-go/lib/binary"
	"reflect"
	"strconv"
	"time"
)

type Tuple struct {
	base
	columns []Column
}

func (tuple *Tuple) Read(decoder *binary.Decoder, isNull bool) (interface{}, error) {
	return nil, fmt.Errorf("do not use Read method for Tuple(T) column")
}

func (tuple *Tuple) ReadTuple(decoder *binary.Decoder, rows int) ([]interface{}, error) {
	var values = make([][]interface{}, rows)

	for _, c := range tuple.columns {

		switch column := c.(type) {
		case *Array:
			cols, err := column.ReadArray(decoder, rows)
			if err != nil {
				return nil, err
			}
			for i := 0; i < rows; i++ {
				values[i] = append(values[i], cols[i])
			}

		case *Nullable:
			cols, err := column.ReadNull(decoder, rows)
			if err != nil {
				return nil, err
			}
			for i := 0; i < rows; i++ {
				values[i] = append(values[i], cols[i])
			}

		case *Tuple:
			cols, err := column.ReadTuple(decoder, rows)
			if err != nil {
				return nil, err
			}
			for i := 0; i < rows; i++ {
				values[i] = append(values[i], cols[i])
			}

		default:
			for i := 0; i < rows; i++ {
				value, err := c.Read(decoder, false)
				if err != nil {
					return nil, err
				}
				values[i] = append(values[i], value)
			}
		}
	}

	var ret = make([]interface{}, rows)
	for i := range values {
		ret[i] = values[i]
	}

	return ret, nil
}

func (tuple *Tuple) Write(encoder *binary.Encoder, v interface{}) (err error) {
	return fmt.Errorf("unsupported Tuple(T) type [%T]", v)
}

func parseTuple(name, chType string, timezone *time.Location) (Column, error) {
	var columnType = chType

	chType = chType[6 : len(chType)-1]
	var types []string
	var last, diff int
	for i, b := range chType + "," {
		if b == '(' {
			diff++
		} else if b == ')' {
			diff--
		} else if b == ',' && diff == 0 {
			types = append(types, chType[last:i])
			last = i + 2
		}
	}

	var columns = make([]Column, 0, len(types))
	for i, chType := range types {
		column, err := Factory(name+"."+strconv.Itoa(i+1), chType, timezone)
		if err != nil {
			return nil, fmt.Errorf("%s: %v", chType, err)
		}
		columns = append(columns, column)
	}

	return &Tuple{
		base: base{
			name:    name,
			chType:  columnType,
			valueOf: reflect.ValueOf([]interface{}{}),
		},
		columns: columns,
	}, nil
}