aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/cmd/ch-gen-col/safe.go.tmpl
blob: ee1930dc9453931179e5de53e7251e357e65d700 (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
{{- /*gotype: github.com/ClickHouse/ch-go/proto/cmd/ch-gen-col.Variant*/ -}}
{{ if .GenerateUnsafe }} //go:build !(amd64 || arm64 || riscv64) || purego
{{ end }}
// Code generated by ./cmd/ch-gen-col, DO NOT EDIT.

package proto

import (
	"encoding/binary"
{{- if .IsFloat }}
	"math"
{{- end }}

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

var _ = binary.LittleEndian // clickHouse uses LittleEndian

// DecodeColumn decodes {{ .Name }} rows from *Reader.
func (c *{{ .Type }}) DecodeColumn(r *Reader, rows int) error {
	if rows == 0 {
		return nil
	}
	{{- if .SingleByte }}
	data, err := r.ReadRaw(rows)
	{{- else }}
	const size = {{ .Bits }} / 8
	data, err := r.ReadRaw(rows * size)
	{{- end }}
	if err != nil {
		return errors.Wrap(err, "read")
	}
	{{- if .Byte }}
	*c = append(*c, data...)
	{{- else if .SingleByte }}
	v := *c
	v = append(v, make([]{{ .ElemType }}, rows)...)
	for i := range data {
		v[i] = {{ .ElemType }}(data[i])
	}
	*c = v
	{{- else }}
	{{- if .DateTime }}
	v := c.Data
	{{- else }}
	v := *c
	{{- end }}
	// Move bound check out of loop.
	//
	// See https://github.com/golang/go/issues/30945.
	_ = data[len(data)-size]
	for i := 0; i <= len(data)-size; i += size {
		v = append(v,
			{{- if .IsFloat }}
			math.{{ .Name }}frombits(binary.LittleEndian.{{ .BinFunc }}(data[i:i+size])),
		{{- else if .Cast }}
			{{ .ElemType }}({{ .BinGet }}(data[i : i+size])),
		{{- else }}
			{{ .BinGet }}(data[i : i+size]),
		{{- end }}
		)
	}
	{{- if .DateTime }}
	c.Data = v
	{{- else }}
	*c = v
	{{- end }}
	{{- end }}
	return nil
}

// EncodeColumn encodes {{ .Name }} rows to *Buffer.
func (c {{ .Type }}) EncodeColumn(b *Buffer) {
	{{- if .DateTime }}
	v := c.Data
	{{- else }}
	v := c
	{{- end }}
	if len(v) == 0 {
		return
	}
	{{- if .Byte }}
	b.Buf = append(b.Buf, v...)
	{{- else if .SingleByte }}
	start := len(b.Buf)
	b.Buf = append(b.Buf, make([]byte, len(v))...)
	for i := range v {
		b.Buf[i+start] = {{ .UnsignedType }}(v[i])
	}
	{{- else }}
	const size = {{ .Bits }} / 8
	offset := len(b.Buf)
	b.Buf = append(b.Buf, make([]byte, size*len(v))...)
	for _, vv := range v {
		{{ .BinPut }}(
			b.Buf[offset : offset+size],
		{{- if .IsFloat }}
			math.{{ .Name }}bits(vv),
		{{- else if .Cast }}
			{{ .UnsignedType }}(vv),
		{{- else }}
			vv,
		{{- end }}
	)
		offset += size
	}
	{{- end }}
}