Skip to content

Commit

Permalink
Array(T) performance improvement #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kshvakov committed Feb 21, 2019
1 parent 23e8e90 commit 1d87030
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
4 changes: 0 additions & 4 deletions lib/column/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/kshvakov/clickhouse/lib/binary"
)

type ArrayWriter interface {
WriteArray(encoder *binary.Encoder, column Column) (uint64, error)
}

type Array struct {
base
depth int
Expand Down
18 changes: 10 additions & 8 deletions lib/data/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (block *Block) Read(serverInfo *ServerInfo, decoder *binary.Decoder) (err e
return nil
}

func (block *Block) prepareArray(value reflect.Value, num, level int) (values []interface{}) {
func (block *Block) writeArray(column column.Column, value reflect.Value, num, level int) error {
switch {
case value.Kind() == reflect.Slice:
if len(block.offsets[num]) < level {
Expand All @@ -107,12 +107,16 @@ func (block *Block) prepareArray(value reflect.Value, num, level int) (values []
)
}
for i := 0; i < value.Len(); i++ {
values = append(values, block.prepareArray(value.Index(i), num, level+1)...)
if err := block.writeArray(column, value.Index(i), num, level+1); err != nil {
return err
}
}
default:
values = append(values, value.Interface())
if err := column.Write(block.buffers[num].Column, value.Interface()); err != nil {
return err
}
}
return values
return nil
}

func (block *Block) AppendRow(args []driver.Value) error {
Expand All @@ -130,10 +134,8 @@ func (block *Block) AppendRow(args []driver.Value) error {
if value.Kind() != reflect.Slice {
return fmt.Errorf("unsupported Array(T) type [%T]", value.Interface())
}
for _, value := range block.prepareArray(value, num, 1) {
if err := column.Write(block.buffers[num].Column, value); err != nil {
return err
}
if err := block.writeArray(c, value, num, 1); err != nil {
return err
}
case *column.Nullable:
if err := column.WriteNull(block.buffers[num].Offset, block.buffers[num].Column, args[num]); err != nil {
Expand Down
7 changes: 1 addition & 6 deletions lib/data/block_write_column.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,5 @@ func (block *Block) WriteArray(c int, v interface{}) error {
if value.Kind() != reflect.Slice {
return fmt.Errorf("unsupported Array(T) type [%T]", value.Interface())
}
for _, value := range block.prepareArray(value, c, 1) {
if err := block.Columns[c].Write(block.buffers[c].Column, value); err != nil {
return err
}
}
return nil
return block.writeArray(block.Columns[c], value, c, 1)
}

0 comments on commit 1d87030

Please sign in to comment.