Skip to content

Commit

Permalink
feat(ch-gen-col): generate byteRange implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Aug 2, 2024
1 parent 550a74d commit efa5429
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
6 changes: 6 additions & 0 deletions proto/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ type BlockWriter struct {
Vec net.Buffers
}

// byteRange represents columns that stored in the memory as-is.
type byteRange interface {
isByteRange() bool
appendSlice(net.Buffers) net.Buffers
}

func (b Block) WriteBlock(w *BlockWriter, version int, input []InputColumn) error {
prevLen := len(w.Buf.Buf)
cutBuffer := func() {
Expand Down
34 changes: 34 additions & 0 deletions proto/cmd/ch-gen-col/test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@ func Test{{ .Type }}_DecodeColumn(t *testing.T) {
})
}


func Test{{ .Type }}_byteRange(t *testing.T) {
t.Parallel()

const rows = 50
var data {{ .Type }}
br, ok := any(data).(byteRange)
if !ok || !br.isByteRange() {
t.Skipf("%T could not be written as-is", data)
return
}

for i := 0; i < rows; i++ {
{{- if .DateTime }}
data.Data = append(data.Data, {{ .New }}(i))
{{- else if .Date }}
data = append(data, {{ .New }}(i))
{{- else }}
v := {{ .New }}(i)
data.Append(v)
require.Equal(t, v, data.Row(i))
{{- end }}
}

var expect Buffer
data.EncodeColumn(&expect)

var got Buffer
for _, part := range any(data).(byteRange).appendSlice(nil) {
got.Buf = append(got.Buf, part...)
}
require.Equal(t, expect.Buf, got.Buf)
}

{{- if not .Time }}
func Test{{ .Type }}Array(t *testing.T) {
const rows = 50
Expand Down
32 changes: 32 additions & 0 deletions proto/cmd/ch-gen-col/unsafe.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package proto

import (
"net"
"unsafe"

"github.com/go-faster/errors"
Expand Down Expand Up @@ -68,3 +69,34 @@ func (c {{ .Type }}) EncodeColumn(b *Buffer) {
dst := b.Buf[offset:]
copy(dst, src)
}

func ({{ .Type }}) isByteRange() bool { return true }

func (c {{ .Type }}) appendSlice(buf net.Buffers) net.Buffers {
{{- if .DateTime }}
v := c.Data
{{- else }}
v := c
{{- end }}
if len(v) == 0 {
return buf
}
{{- if .SingleByte }}
src := *(*[]byte)(unsafe.Pointer(&v))
{{- else }}
{{- if .FixedStr }}
const size = {{ .Bytes }}
{{- else }}
const size = {{ .Bits }} / 8
{{- end }}

s := *(*slice)(unsafe.Pointer(&v))
{{- if not .SingleByte }}
s.Len *= size
s.Cap *= size
{{- end }}

src := *(*[]byte)(unsafe.Pointer(&s))
{{- end }}
return append(buf, src)
}

0 comments on commit efa5429

Please sign in to comment.