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

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

// ColLowCardinalityRaw is non-generic version of ColLowCardinality.
type ColLowCardinalityRaw struct {
	Index Column // dictionary
	Key   CardinalityKey

	// Keeping all key column variants as fields to reuse
	// memory more efficiently.

	Keys8  ColUInt8
	Keys16 ColUInt16
	Keys32 ColUInt32
	Keys64 ColUInt64
}

func (c *ColLowCardinalityRaw) DecodeState(r *Reader) error {
	keySerialization, err := r.Int64()
	if err != nil {
		return errors.Wrap(err, "version")
	}
	if keySerialization != int64(sharedDictionariesWithAdditionalKeys) {
		return errors.Errorf("got version %d, expected %d",
			keySerialization, sharedDictionariesWithAdditionalKeys,
		)
	}
	if s, ok := c.Index.(StateDecoder); ok {
		if err := s.DecodeState(r); err != nil {
			return errors.Wrap(err, "state")
		}
	}
	return nil
}

func (c ColLowCardinalityRaw) EncodeState(b *Buffer) {
	// Writing key serialization version.
	b.PutInt64(int64(sharedDictionariesWithAdditionalKeys))
	if s, ok := c.Index.(StateEncoder); ok {
		s.EncodeState(b)
	}
}

func (c *ColLowCardinalityRaw) AppendKey(i int) {
	switch c.Key {
	case KeyUInt8:
		c.Keys8 = append(c.Keys8, uint8(i))
	case KeyUInt16:
		c.Keys16 = append(c.Keys16, uint16(i))
	case KeyUInt32:
		c.Keys32 = append(c.Keys32, uint32(i))
	case KeyUInt64:
		c.Keys64 = append(c.Keys64, uint64(i))
	default:
		panic("invalid key type")
	}
}

func (c *ColLowCardinalityRaw) Keys() Column {
	switch c.Key {
	case KeyUInt8:
		return &c.Keys8
	case KeyUInt16:
		return &c.Keys16
	case KeyUInt32:
		return &c.Keys32
	case KeyUInt64:
		return &c.Keys64
	default:
		panic("invalid key type")
	}
}

func (c ColLowCardinalityRaw) Type() ColumnType {
	return ColumnTypeLowCardinality.Sub(c.Index.Type())
}

func (c ColLowCardinalityRaw) Rows() int {
	return c.Keys().Rows()
}

func (c *ColLowCardinalityRaw) DecodeColumn(r *Reader, rows int) error {
	if rows == 0 {
		// Skipping entirely of no rows.
		return nil
	}
	meta, err := r.Int64()
	if err != nil {
		return errors.Wrap(err, "meta")
	}
	if (meta & cardinalityNeedGlobalDictionaryBit) == 1 {
		return errors.New("global dictionary is not supported")
	}
	if (meta & cardinalityHasAdditionalKeysBit) == 0 {
		return errors.New("additional keys bit is missing")
	}

	key := CardinalityKey(meta & cardinalityKeyMask)
	if !key.IsACardinalityKey() {
		return errors.Errorf("invalid low cardinality keys type %d", key)
	}
	c.Key = key

	indexRows, err := r.Int64()
	if err != nil {
		return errors.Wrap(err, "index size")
	}
	if err := checkRows(int(indexRows)); err != nil {
		return errors.Wrap(err, "index size")
	}
	if err := c.Index.DecodeColumn(r, int(indexRows)); err != nil {
		return errors.Wrap(err, "index column")
	}

	keyRows, err := r.Int64()
	if err != nil {
		return errors.Wrap(err, "keys size")
	}
	if err := checkRows(int(keyRows)); err != nil {
		return errors.Wrap(err, "index size")
	}
	if err := c.Keys().DecodeColumn(r, int(keyRows)); err != nil {
		return errors.Wrap(err, "keys column")
	}

	return nil
}

func (c *ColLowCardinalityRaw) Reset() {
	c.Index.Reset()
	c.Keys8.Reset()
	c.Keys16.Reset()
	c.Keys32.Reset()
	c.Keys64.Reset()
}

func (c ColLowCardinalityRaw) EncodeColumn(b *Buffer) {
	if c.Rows() == 0 {
		// Skipping encoding entirely.
		return
	}

	// Meta encodes whether reader should update
	// low cardinality metadata and keys column type.
	meta := cardinalityUpdateAll | int64(c.Key)
	b.PutInt64(meta)

	// Writing index (dictionary).
	b.PutInt64(int64(c.Index.Rows()))
	c.Index.EncodeColumn(b)

	// Sequence of values as indexes in dictionary.
	k := c.Keys()
	b.PutInt64(int64(k.Rows()))
	k.EncodeColumn(b)
}