aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/col_tuple.go
blob: ac7ad6c8cc685fe2171b0e75185c8c15534ac1ad (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
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
package proto

import "github.com/go-faster/errors"

// ColTuple is Tuple column.
//
// Basically it is just a group of columns.
type ColTuple []Column

// Compile-time assertions for ColTuple.
var (
	_ ColInput     = ColTuple(nil)
	_ ColResult    = ColTuple(nil)
	_ Column       = ColTuple(nil)
	_ StateEncoder = ColTuple(nil)
	_ StateDecoder = ColTuple(nil)
	_ Inferable    = ColTuple(nil)
	_ Preparable   = ColTuple(nil)
)

func (c ColTuple) DecodeState(r *Reader) error {
	for i, v := range c {
		if s, ok := v.(StateDecoder); ok {
			if err := s.DecodeState(r); err != nil {
				return errors.Wrapf(err, "[%d]", i)
			}
		}
	}
	return nil
}

// ColNamed is named column.
// Used in named tuples.
type ColNamed[T any] struct {
	ColumnOf[T]
	Name string
}

func (c *ColNamed[T]) Infer(t ColumnType) error {
	if v, ok := c.ColumnOf.(Inferable); ok {
		if err := v.Infer(t); err != nil {
			return errors.Wrap(err, "named")
		}
	}
	return nil
}

func (c *ColNamed[T]) Prepare() error {
	if v, ok := c.ColumnOf.(Preparable); ok {
		if err := v.Prepare(); err != nil {
			return errors.Wrap(err, "named")
		}
	}
	return nil
}

func (c ColNamed[T]) DecodeState(r *Reader) error {
	if v, ok := c.ColumnOf.(StateDecoder); ok {
		if err := v.DecodeState(r); err != nil {
			return errors.Wrap(err, "named")
		}
	}
	return nil
}

func (c ColNamed[T]) EncodeState(b *Buffer) {
	if v, ok := c.ColumnOf.(StateEncoder); ok {
		v.EncodeState(b)
	}
}

// Compile-time assertions for ColNamed.
var (
	_ ColInput     = Named[string]((*ColStr)(nil), "name")
	_ ColResult    = Named[string]((*ColStr)(nil), "name")
	_ Column       = Named[string]((*ColStr)(nil), "name")
	_ StateEncoder = Named[string]((*ColStr)(nil), "name")
	_ StateDecoder = Named[string]((*ColStr)(nil), "name")
	_ Inferable    = Named[string]((*ColStr)(nil), "name")
	_ Preparable   = Named[string]((*ColStr)(nil), "name")
)

func Named[T any](data ColumnOf[T], name string) *ColNamed[T] {
	return &ColNamed[T]{
		ColumnOf: data,
		Name:     name,
	}
}

func (c ColNamed[T]) ColumnName() string {
	return c.Name
}

func (c ColNamed[T]) Type() ColumnType {
	return ColumnType(c.Name + " " + c.ColumnOf.Type().String())
}

func (c ColTuple) Prepare() error {
	for _, v := range c {
		if s, ok := v.(Preparable); ok {
			if err := s.Prepare(); err != nil {
				return errors.Wrap(err, "prepare")
			}
		}
	}
	return nil
}

func (c ColTuple) Infer(t ColumnType) error {
	for _, v := range c {
		if s, ok := v.(Inferable); ok {
			if err := s.Infer(t); err != nil {
				return errors.Wrap(err, "infer")
			}
		}
	}
	return nil
}

func (c ColTuple) EncodeState(b *Buffer) {
	for _, v := range c {
		if s, ok := v.(StateEncoder); ok {
			s.EncodeState(b)
		}
	}
}

func (c ColTuple) Type() ColumnType {
	var types []ColumnType
	for _, v := range c {
		types = append(types, v.Type())
	}
	return ColumnTypeTuple.Sub(types...)
}

func (c ColTuple) First() Column {
	if len(c) == 0 {
		return nil
	}
	return c[0]
}

func (c ColTuple) Rows() int {
	if f := c.First(); f != nil {
		return f.Rows()
	}
	return 0
}

func (c ColTuple) DecodeColumn(r *Reader, rows int) error {
	for i, v := range c {
		if err := v.DecodeColumn(r, rows); err != nil {
			return errors.Wrapf(err, "[%d]", i)
		}
	}
	return nil
}

func (c ColTuple) Reset() {
	for _, v := range c {
		v.Reset()
	}
}

func (c ColTuple) EncodeColumn(b *Buffer) {
	for _, v := range c {
		v.EncodeColumn(b)
	}
}