Skip to content

Commit 5f4b874

Browse files
committed
feat(stdlib): construct column names with StringBuilder
Update the way row keys and column keys are constructed in the pivot function. They are now built in a StringBuilder. Previously where they were constructed the string would be rebuilt with every column added to the key. If the key only consisted of a single column then a reference to the column contents would be retained, This reference would no be counted in the reference count, but would be sufficient to keep an entire record batch from being garbage collected.
1 parent 0f143c6 commit 5f4b874

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

stdlib/universe/pivot.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"sort"
77
"strconv"
8+
"strings"
89

910
arrowmemory "github.com/apache/arrow/go/v7/arrow/memory"
1011
"github.com/influxdata/flux"
@@ -268,23 +269,24 @@ func (t *pivotTransformation) Process(id execute.DatasetID, tbl flux.Table) erro
268269

269270
return tbl.Do(func(cr flux.ColReader) error {
270271
for row := 0; row < cr.Len(); row++ {
271-
rowKey := ""
272-
colKey := ""
272+
var rkb strings.Builder
273273
for _, rk := range t.spec.RowKey {
274274
j := rowKeyIndex[rk]
275275
c := cr.Cols()[j]
276-
rowKey += valueToStr(cr, c, row, j)
276+
rkb.WriteString(valueToStr(cr, c, row, j))
277277
}
278+
rowKey := rkb.String()
278279

280+
var ckb strings.Builder
279281
for _, ck := range t.spec.ColumnKey {
280282
j := colKeyIndex[ck]
281283
c := cr.Cols()[j]
282-
if colKey == "" {
283-
colKey = valueToStr(cr, c, row, j)
284-
} else {
285-
colKey = colKey + "_" + valueToStr(cr, c, row, j)
284+
if ckb.Len() > 0 {
285+
ckb.WriteByte('_')
286286
}
287+
ckb.WriteString(valueToStr(cr, c, row, j))
287288
}
289+
colKey := ckb.String()
288290

289291
// we have columns for the copy-over in place;
290292
// we know the row key;

0 commit comments

Comments
 (0)