/
string_casts.go
409 lines (351 loc) · 13.8 KB
/
string_casts.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build go1.18
package kernels
import (
"fmt"
"strconv"
"unicode/utf8"
"github.com/apache/arrow/go/v11/arrow"
"github.com/apache/arrow/go/v11/arrow/array"
"github.com/apache/arrow/go/v11/arrow/bitutil"
"github.com/apache/arrow/go/v11/arrow/compute/internal/exec"
"github.com/apache/arrow/go/v11/arrow/float16"
"github.com/apache/arrow/go/v11/internal/bitutils"
)
func validateUtf8Fsb(input *exec.ArraySpan) error {
var (
inputData = input.Buffers[1].Buf
width = int64(input.Type.(*arrow.FixedSizeBinaryType).ByteWidth)
bitmap = input.Buffers[0].Buf
)
return bitutils.VisitBitBlocksShort(bitmap, input.Offset, input.Len,
func(pos int64) error {
pos += input.Offset
beg := pos * width
end := (pos + 1) * width
if !utf8.Valid(inputData[beg:end]) {
return fmt.Errorf("%w: invalid UTF8 bytes: %x", arrow.ErrInvalid, inputData[beg:end])
}
return nil
}, func() error { return nil })
}
func validateUtf8[OffsetT int32 | int64](input *exec.ArraySpan) error {
var (
inputOffsets = exec.GetSpanOffsets[OffsetT](input, 1)
inputData = input.Buffers[2].Buf
bitmap = input.Buffers[0].Buf
)
return bitutils.VisitBitBlocksShort(bitmap, input.Offset, input.Len,
func(pos int64) error {
v := inputData[inputOffsets[pos]:inputOffsets[pos+1]]
if !utf8.Valid(v) {
return fmt.Errorf("%w: invalid UTF8 bytes: %x", arrow.ErrInvalid, v)
}
return nil
}, func() error { return nil })
}
func CastFsbToFsb(ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
inputWidth := batch.Values[0].Array.Type.(*arrow.FixedSizeBinaryType).ByteWidth
outputWidth := ctx.State.(CastState).ToType.(*arrow.FixedSizeBinaryType).ByteWidth
if inputWidth != outputWidth {
return fmt.Errorf("%w: failed casting from %s to %s: widths must match",
arrow.ErrInvalid, batch.Values[0].Array.Type, out.Type)
}
return ZeroCopyCastExec(ctx, batch, out)
}
func CastBinaryToBinary[InOffsetsT, OutOffsetsT int32 | int64](ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
opts := ctx.State.(CastState)
input := &batch.Values[0].Array
if !input.Type.(arrow.BinaryDataType).IsUtf8() && out.Type.(arrow.BinaryDataType).IsUtf8() && !opts.AllowInvalidUtf8 {
if err := validateUtf8[InOffsetsT](input); err != nil {
return err
}
}
// start with a zero-copy cast, then change the indices to the
// expected size
if err := ZeroCopyCastExec(ctx, batch, out); err != nil {
return err
}
switch {
case SizeOf[InOffsetsT]() == SizeOf[OutOffsetsT]():
// offsets are the same width, nothing more to do
return nil
case SizeOf[InOffsetsT]() > SizeOf[OutOffsetsT]():
// downcast from int64 -> int32
inputOffsets := exec.GetSpanOffsets[InOffsetsT](input, 1)
// binary offsets are ascending, so it's enough to check
// the last one for overflow
if inputOffsets[input.Len] > InOffsetsT(MaxOf[OutOffsetsT]()) {
return fmt.Errorf("%w: failed casting from %s to %s: input array too large",
arrow.ErrInvalid, input.Type, out.Type)
}
buf := ctx.Allocate(out.Type.(arrow.OffsetsDataType).OffsetTypeTraits().BytesRequired(int(out.Len + out.Offset + 1)))
out.Buffers[1].WrapBuffer(buf)
outOffsets := exec.GetSpanOffsets[OutOffsetsT](out, 1)
castNumericUnsafe(arrow.INT64, arrow.INT32,
exec.GetBytes(inputOffsets), exec.GetBytes(outOffsets), len(inputOffsets))
return nil
default:
// upcast from int32 -> int64
buf := ctx.Allocate(out.Type.(arrow.OffsetsDataType).OffsetTypeTraits().BytesRequired(int(out.Len + out.Offset + 1)))
out.Buffers[1].WrapBuffer(buf)
inputOffsets := exec.GetSpanOffsets[InOffsetsT](input, 1)
outOffsets := exec.GetSpanOffsets[OutOffsetsT](out, 1)
castNumericUnsafe(arrow.INT32, arrow.INT64,
exec.GetBytes(inputOffsets), exec.GetBytes(outOffsets), len(inputOffsets))
return nil
}
}
func CastFsbToBinary[OffsetsT int32 | int64](ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
opts := ctx.State.(CastState)
input := &batch.Values[0].Array
if out.Type.(arrow.BinaryDataType).IsUtf8() && !opts.AllowInvalidUtf8 {
if err := validateUtf8Fsb(input); err != nil {
return err
}
}
// check for overflow
maxOffset := int64(MaxOf[OffsetsT]())
width := OffsetsT(input.Type.(*arrow.FixedSizeBinaryType).ByteWidth)
if (int64(width) * input.Len) > maxOffset {
return fmt.Errorf("%w: failed casting from %s to %s: input array too large",
arrow.ErrInvalid, input.Type, out.Type)
}
out.Len = input.Len
out.Nulls = input.Nulls
if input.Offset == out.Offset {
out.Buffers[0].SetBuffer(input.GetBuffer(0))
} else {
out.Buffers[0].WrapBuffer(ctx.AllocateBitmap(input.Len))
bitutil.CopyBitmap(input.Buffers[0].Buf, int(input.Offset), int(input.Len), out.Buffers[0].Buf, int(out.Offset))
}
// this buffer is preallocated
offsets := exec.GetSpanOffsets[OffsetsT](out, 1)
offsets[0] = OffsetsT(input.Offset) * width
for i := 0; i < int(input.Len); i++ {
offsets[i+1] = offsets[i] + width
}
if len(input.Buffers[1].Buf) > 0 {
out.Buffers[2] = input.Buffers[1]
}
return nil
}
func addBinaryToBinaryCast[InOffsetT, OutOffsetT int32 | int64](inType arrow.Type, outType exec.OutputType) exec.ScalarKernel {
return exec.NewScalarKernel([]exec.InputType{exec.NewIDInput(inType)},
outType, CastBinaryToBinary[InOffsetT, OutOffsetT], nil)
}
func addToBinaryKernels[OffsetsT int32 | int64](outType exec.OutputType, kernels []exec.ScalarKernel) []exec.ScalarKernel {
return append(kernels,
addBinaryToBinaryCast[int32, OffsetsT](arrow.STRING, outType),
addBinaryToBinaryCast[int32, OffsetsT](arrow.BINARY, outType),
addBinaryToBinaryCast[int64, OffsetsT](arrow.LARGE_STRING, outType),
addBinaryToBinaryCast[int64, OffsetsT](arrow.LARGE_BINARY, outType),
exec.NewScalarKernel([]exec.InputType{exec.NewIDInput(arrow.FIXED_SIZE_BINARY)},
outType, CastFsbToBinary[OffsetsT], nil),
)
}
func GetFsbCastKernels() []exec.ScalarKernel {
outputType := exec.NewComputedOutputType(resolveOutputFromOptions)
out := GetCommonCastKernels(arrow.FIXED_SIZE_BINARY, outputType)
kernel := exec.NewScalarKernel([]exec.InputType{exec.NewIDInput(arrow.FIXED_SIZE_BINARY)},
OutputFirstType, CastFsbToFsb, nil)
kernel.NullHandling = exec.NullComputedNoPrealloc
return append(out, kernel)
}
func float16Formatter(v float16.Num) string { return v.String() }
func date32Formatter(v arrow.Date32) string { return v.FormattedString() }
func date64Formatter(v arrow.Date64) string { return v.FormattedString() }
func numericFormatterSigned[T exec.IntTypes](v T) string { return strconv.FormatInt(int64(v), 10) }
func numericFormatterUnsigned[T exec.UintTypes](v T) string { return strconv.FormatUint(uint64(v), 10) }
func float32Formatter(v float32) string { return strconv.FormatFloat(float64(v), 'g', -1, 32) }
func float64Formatter(v float64) string { return strconv.FormatFloat(v, 'g', -1, 64) }
func boolToStringCastExec(ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
var (
input = &batch.Values[0].Array
bldr = array.NewBuilder(exec.GetAllocator(ctx.Ctx), out.Type).(array.StringLikeBuilder)
)
defer bldr.Release()
bitutils.VisitBitBlocks(input.Buffers[0].Buf, input.Offset, input.Len,
func(pos int64) {
bldr.Append(strconv.FormatBool(bitutil.BitIsSet(input.Buffers[1].Buf, int(pos))))
}, func() { bldr.AppendNull() })
arr := bldr.NewArray()
out.TakeOwnership(arr.Data())
return nil
}
type timeIntrinsic interface {
arrow.Time32 | arrow.Time64
FormattedString(arrow.TimeUnit) string
}
func timeToStringCastExec[T timeIntrinsic](ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
var (
input = &batch.Values[0].Array
inputData = exec.GetSpanValues[T](input, 1)
bldr = array.NewBuilder(exec.GetAllocator(ctx.Ctx), out.Type).(array.StringLikeBuilder)
inputType = input.Type.(arrow.TemporalWithUnit)
)
defer bldr.Release()
bitutils.VisitBitBlocks(input.Buffers[0].Buf, input.Offset, input.Len,
func(pos int64) {
bldr.Append(inputData[pos].FormattedString(inputType.TimeUnit()))
}, func() { bldr.AppendNull() })
arr := bldr.NewArray()
out.TakeOwnership(arr.Data())
return nil
}
func numericToStringCastExec[T exec.IntTypes | exec.UintTypes | exec.FloatTypes](formatter func(T) string) exec.ArrayKernelExec {
return func(ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
var (
input = &batch.Values[0].Array
inputData = exec.GetSpanValues[T](input, 1)
bldr = array.NewBuilder(exec.GetAllocator(ctx.Ctx), out.Type).(array.StringLikeBuilder)
)
defer bldr.Release()
bitutils.VisitBitBlocks(input.Buffers[0].Buf, input.Offset, input.Len,
func(pos int64) {
bldr.Append(formatter(inputData[pos]))
}, func() { bldr.AppendNull() })
arr := bldr.NewArray()
out.TakeOwnership(arr.Data())
return nil
}
}
func castTimestampToString(ctx *exec.KernelCtx, batch *exec.ExecSpan, out *exec.ExecResult) error {
var (
input = &batch.Values[0].Array
inputData = exec.GetSpanValues[arrow.Timestamp](input, 1)
inputType = input.Type.(*arrow.TimestampType)
bldr = array.NewBuilder(exec.GetAllocator(ctx.Ctx), out.Type).(array.StringLikeBuilder)
)
defer bldr.Release()
toTime, err := inputType.GetToTimeFunc()
if err != nil {
return err
}
// prealloc
fmtstring := "2006-01-02 15:04:05"
switch inputType.Unit {
case arrow.Millisecond:
fmtstring += ".000"
case arrow.Microsecond:
fmtstring += ".000000"
case arrow.Nanosecond:
fmtstring += ".000000000"
}
switch inputType.TimeZone {
case "UTC":
fmtstring += "Z"
case "":
default:
fmtstring += "-0700"
}
strlen := len(fmtstring)
bldr.Reserve(int(input.Len))
bldr.ReserveData(int(input.Len-input.Nulls) * strlen)
bitutils.VisitBitBlocks(input.Buffers[0].Buf, input.Offset, input.Len,
func(pos int64) {
bldr.Append(toTime(inputData[pos]).Format(fmtstring))
},
func() { bldr.AppendNull() })
arr := bldr.NewArray()
out.TakeOwnership(arr.Data())
return nil
}
func getNumericToStringCastExec(inType arrow.Type) exec.ArrayKernelExec {
switch inType {
case arrow.INT8:
return numericToStringCastExec(numericFormatterSigned[int8])
case arrow.UINT8:
return numericToStringCastExec(numericFormatterUnsigned[uint8])
case arrow.INT16:
return numericToStringCastExec(numericFormatterSigned[int16])
case arrow.UINT16:
return numericToStringCastExec(numericFormatterUnsigned[uint16])
case arrow.INT32:
return numericToStringCastExec(numericFormatterSigned[int32])
case arrow.UINT32:
return numericToStringCastExec(numericFormatterUnsigned[uint32])
case arrow.INT64:
return numericToStringCastExec(numericFormatterSigned[int64])
case arrow.UINT64:
return numericToStringCastExec(numericFormatterUnsigned[uint64])
case arrow.FLOAT16:
return numericToStringCastExec(float16Formatter)
case arrow.FLOAT32:
return numericToStringCastExec(float32Formatter)
case arrow.FLOAT64:
return numericToStringCastExec(float64Formatter)
case arrow.BOOL:
return boolToStringCastExec
case arrow.DATE32:
return numericToStringCastExec(date32Formatter)
case arrow.DATE64:
return numericToStringCastExec(date64Formatter)
case arrow.TIME32:
return timeToStringCastExec[arrow.Time32]
case arrow.TIME64:
return timeToStringCastExec[arrow.Time64]
case arrow.TIMESTAMP:
return castTimestampToString
}
panic("unimplemented cast: " + inType.String())
}
func addNumericAndTemporalToStringCasts(outType exec.OutputType, out []exec.ScalarKernel) []exec.ScalarKernel {
k := exec.NewScalarKernel([]exec.InputType{exec.NewExactInput(arrow.FixedWidthTypes.Boolean)}, outType,
getNumericToStringCastExec(arrow.BOOL), nil)
k.NullHandling = exec.NullComputedNoPrealloc
out = append(out, k)
for _, dt := range numericTypes {
k = exec.NewScalarKernel([]exec.InputType{exec.NewExactInput(dt)}, outType,
getNumericToStringCastExec(dt.ID()), nil)
k.NullHandling = exec.NullComputedNoPrealloc
out = append(out, k)
}
for _, dt := range []arrow.DataType{arrow.FixedWidthTypes.Date32, arrow.FixedWidthTypes.Date64} {
k = exec.NewScalarKernel([]exec.InputType{exec.NewExactInput(dt)}, outType,
getNumericToStringCastExec(dt.ID()), nil)
k.NullHandling = exec.NullComputedNoPrealloc
out = append(out, k)
}
for _, id := range []arrow.Type{arrow.TIME32, arrow.TIME64, arrow.TIMESTAMP} {
k = exec.NewScalarKernel([]exec.InputType{exec.NewIDInput(id)}, outType,
getNumericToStringCastExec(id), nil)
k.NullHandling = exec.NullComputedNoPrealloc
out = append(out, k)
}
return out
}
func GetToBinaryKernels(outType arrow.DataType) []exec.ScalarKernel {
if outType.ID() == arrow.FIXED_SIZE_BINARY {
return nil
}
outputType := exec.NewOutputType(outType)
out := GetCommonCastKernels(outType.ID(), outputType)
switch outType.ID() {
case arrow.BINARY:
return addToBinaryKernels[int32](outputType, out)
case arrow.LARGE_BINARY:
return addToBinaryKernels[int64](outputType, out)
case arrow.STRING:
out = addToBinaryKernels[int32](outputType, out)
return addNumericAndTemporalToStringCasts(outputType, out)
case arrow.LARGE_STRING:
out = addToBinaryKernels[int64](outputType, out)
return addNumericAndTemporalToStringCasts(outputType, out)
}
return nil
}