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
|
// 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 compute
import (
"fmt"
"io"
"time"
"github.com/apache/arrow-go/v18/arrow"
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/arrow/compute/exec"
"github.com/apache/arrow-go/v18/arrow/compute/internal/kernels"
"github.com/apache/arrow-go/v18/arrow/internal/debug"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/apache/arrow-go/v18/internal/utils"
"golang.org/x/xerrors"
)
type bufferWriteSeeker struct {
buf *memory.Buffer
pos int
mem memory.Allocator
}
func (b *bufferWriteSeeker) Reserve(nbytes int) {
if b.buf == nil {
b.buf = memory.NewResizableBuffer(b.mem)
}
newCap := utils.Max(b.buf.Cap(), 256)
for newCap < b.pos+nbytes {
newCap = bitutil.NextPowerOf2(b.pos + nbytes)
}
b.buf.Reserve(newCap)
}
func (b *bufferWriteSeeker) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
if b.buf == nil {
b.Reserve(len(p))
} else if b.pos+len(p) >= b.buf.Cap() {
b.Reserve(len(p))
}
return b.UnsafeWrite(p)
}
func (b *bufferWriteSeeker) UnsafeWrite(p []byte) (n int, err error) {
n = copy(b.buf.Buf()[b.pos:], p)
b.pos += len(p)
if b.pos > b.buf.Len() {
b.buf.ResizeNoShrink(b.pos)
}
return
}
func (b *bufferWriteSeeker) Seek(offset int64, whence int) (int64, error) {
newpos, offs := 0, int(offset)
switch whence {
case io.SeekStart:
newpos = offs
case io.SeekCurrent:
newpos = b.pos + offs
case io.SeekEnd:
newpos = b.buf.Len() + offs
}
if newpos < 0 {
return 0, xerrors.New("negative result pos")
}
b.pos = newpos
return int64(newpos), nil
}
// ensureDictionaryDecoded is used by DispatchBest to determine
// the proper types for promotion. Casting is then performed by
// the executor before continuing execution: see the implementation
// of execInternal in exec.go after calling DispatchBest.
//
// That casting is where actual decoding would be performed for
// the dictionary
func ensureDictionaryDecoded(vals ...arrow.DataType) {
for i, v := range vals {
if v.ID() == arrow.DICTIONARY {
vals[i] = v.(*arrow.DictionaryType).ValueType
}
}
}
func ensureNoExtensionType(vals ...arrow.DataType) {
for i, v := range vals {
if v.ID() == arrow.EXTENSION {
vals[i] = v.(arrow.ExtensionType).StorageType()
}
}
}
func replaceNullWithOtherType(vals ...arrow.DataType) {
debug.Assert(len(vals) == 2, "should be length 2")
if vals[0].ID() == arrow.NULL {
vals[0] = vals[1]
return
}
if vals[1].ID() == arrow.NULL {
vals[1] = vals[0]
return
}
}
func commonTemporalResolution(vals ...arrow.DataType) (arrow.TimeUnit, bool) {
isTimeUnit := false
finestUnit := arrow.Second
for _, v := range vals {
switch dt := v.(type) {
case *arrow.Date32Type:
isTimeUnit = true
continue
case *arrow.Date64Type:
finestUnit = exec.Max(finestUnit, arrow.Millisecond)
isTimeUnit = true
case arrow.TemporalWithUnit:
finestUnit = exec.Max(finestUnit, dt.TimeUnit())
isTimeUnit = true
default:
continue
}
}
return finestUnit, isTimeUnit
}
func replaceTemporalTypes(unit arrow.TimeUnit, vals ...arrow.DataType) {
for i, v := range vals {
switch dt := v.(type) {
case *arrow.TimestampType:
dt.Unit = unit
vals[i] = dt
case *arrow.Time32Type, *arrow.Time64Type:
if unit > arrow.Millisecond {
vals[i] = &arrow.Time64Type{Unit: unit}
} else {
vals[i] = &arrow.Time32Type{Unit: unit}
}
case *arrow.DurationType:
dt.Unit = unit
vals[i] = dt
case *arrow.Date32Type, *arrow.Date64Type:
vals[i] = &arrow.TimestampType{Unit: unit}
}
}
}
func replaceTypes(replacement arrow.DataType, vals ...arrow.DataType) {
for i := range vals {
vals[i] = replacement
}
}
func commonNumeric(vals ...arrow.DataType) arrow.DataType {
for _, v := range vals {
if !arrow.IsFloating(v.ID()) && !arrow.IsInteger(v.ID()) {
// a common numeric type is only possible if all are numeric
return nil
}
if v.ID() == arrow.FLOAT16 {
// float16 arithmetic is not currently supported
return nil
}
}
for _, v := range vals {
if v.ID() == arrow.FLOAT64 {
return arrow.PrimitiveTypes.Float64
}
}
for _, v := range vals {
if v.ID() == arrow.FLOAT32 {
return arrow.PrimitiveTypes.Float32
}
}
maxWidthSigned, maxWidthUnsigned := 0, 0
for _, v := range vals {
if arrow.IsUnsignedInteger(v.ID()) {
maxWidthUnsigned = exec.Max(v.(arrow.FixedWidthDataType).BitWidth(), maxWidthUnsigned)
} else {
maxWidthSigned = exec.Max(v.(arrow.FixedWidthDataType).BitWidth(), maxWidthSigned)
}
}
if maxWidthSigned == 0 {
switch {
case maxWidthUnsigned >= 64:
return arrow.PrimitiveTypes.Uint64
case maxWidthUnsigned == 32:
return arrow.PrimitiveTypes.Uint32
case maxWidthUnsigned == 16:
return arrow.PrimitiveTypes.Uint16
default:
debug.Assert(maxWidthUnsigned == 8, "bad maxWidthUnsigned")
return arrow.PrimitiveTypes.Uint8
}
}
if maxWidthSigned <= maxWidthUnsigned {
maxWidthSigned = bitutil.NextPowerOf2(maxWidthUnsigned + 1)
}
switch {
case maxWidthSigned >= 64:
return arrow.PrimitiveTypes.Int64
case maxWidthSigned == 32:
return arrow.PrimitiveTypes.Int32
case maxWidthSigned == 16:
return arrow.PrimitiveTypes.Int16
default:
debug.Assert(maxWidthSigned == 8, "bad maxWidthSigned")
return arrow.PrimitiveTypes.Int8
}
}
func hasDecimal(vals ...arrow.DataType) bool {
for _, v := range vals {
if arrow.IsDecimal(v.ID()) {
return true
}
}
return false
}
type decimalPromotion uint8
const (
decPromoteNone decimalPromotion = iota
decPromoteAdd
decPromoteMultiply
decPromoteDivide
)
func castBinaryDecimalArgs(promote decimalPromotion, vals ...arrow.DataType) error {
left, right := vals[0], vals[1]
debug.Assert(arrow.IsDecimal(left.ID()) || arrow.IsDecimal(right.ID()), "at least one of the types should be decimal")
// decimal + float = float
if arrow.IsFloating(left.ID()) {
vals[1] = vals[0]
return nil
} else if arrow.IsFloating(right.ID()) {
vals[0] = vals[1]
return nil
}
var prec1, scale1, prec2, scale2 int32
var err error
// decimal + integer = decimal
if arrow.IsDecimal(left.ID()) {
dec := left.(arrow.DecimalType)
prec1, scale1 = dec.GetPrecision(), dec.GetScale()
} else {
debug.Assert(arrow.IsInteger(left.ID()), "floats were already handled, this should be an int")
if prec1, err = kernels.MaxDecimalDigitsForInt(left.ID()); err != nil {
return err
}
}
if arrow.IsDecimal(right.ID()) {
dec := right.(arrow.DecimalType)
prec2, scale2 = dec.GetPrecision(), dec.GetScale()
} else {
debug.Assert(arrow.IsInteger(right.ID()), "float already handled, should be ints")
if prec2, err = kernels.MaxDecimalDigitsForInt(right.ID()); err != nil {
return err
}
}
if scale1 < 0 || scale2 < 0 {
return fmt.Errorf("%w: decimals with negative scales not supported", arrow.ErrNotImplemented)
}
// decimal128 + decimal256 = decimal256
castedID := arrow.DECIMAL128
if left.ID() == arrow.DECIMAL256 || right.ID() == arrow.DECIMAL256 {
castedID = arrow.DECIMAL256
}
// decimal promotion rules compatible with amazon redshift
// https://docs.aws.amazon.com/redshift/latest/dg/r_numeric_computations201.html
var leftScaleup, rightScaleup int32
switch promote {
case decPromoteAdd:
leftScaleup = exec.Max(scale1, scale2) - scale1
rightScaleup = exec.Max(scale1, scale2) - scale2
case decPromoteMultiply:
case decPromoteDivide:
leftScaleup = exec.Max(4, scale1+prec2-scale2+1) + scale2 - scale1
default:
debug.Assert(false, fmt.Sprintf("invalid DecimalPromotion value %d", promote))
}
vals[0], err = arrow.NewDecimalType(castedID, prec1+leftScaleup, scale1+leftScaleup)
if err != nil {
return err
}
vals[1], err = arrow.NewDecimalType(castedID, prec2+rightScaleup, scale2+rightScaleup)
return err
}
func commonTemporal(vals ...arrow.DataType) arrow.DataType {
var (
finestUnit = arrow.Second
zone *string
loc *time.Location
sawDate32, sawDate64 bool
)
for _, ty := range vals {
switch ty.ID() {
case arrow.DATE32:
// date32's unit is days, but the coarsest we have is seconds
sawDate32 = true
case arrow.DATE64:
finestUnit = exec.Max(finestUnit, arrow.Millisecond)
sawDate64 = true
case arrow.TIMESTAMP:
ts := ty.(*arrow.TimestampType)
if ts.TimeZone != "" {
tz, _ := ts.GetZone()
if loc != nil && loc != tz {
return nil
}
loc = tz
}
zone = &ts.TimeZone
finestUnit = exec.Max(finestUnit, ts.Unit)
default:
return nil
}
}
switch {
case zone != nil:
// at least one timestamp seen
return &arrow.TimestampType{Unit: finestUnit, TimeZone: *zone}
case sawDate64:
return arrow.FixedWidthTypes.Date64
case sawDate32:
return arrow.FixedWidthTypes.Date32
}
return nil
}
func commonBinary(vals ...arrow.DataType) arrow.DataType {
var (
allUTF8, allOffset32, allFixedWidth = true, true, true
)
for _, ty := range vals {
switch ty.ID() {
case arrow.STRING:
allFixedWidth = false
case arrow.BINARY:
allFixedWidth, allUTF8 = false, false
case arrow.FIXED_SIZE_BINARY:
allUTF8 = false
case arrow.LARGE_BINARY:
allOffset32, allFixedWidth, allUTF8 = false, false, false
case arrow.LARGE_STRING:
allOffset32, allFixedWidth = false, false
default:
return nil
}
}
switch {
case allFixedWidth:
// at least for the purposes of comparison, no need to cast
return nil
case allUTF8:
if allOffset32 {
return arrow.BinaryTypes.String
}
return arrow.BinaryTypes.LargeString
case allOffset32:
return arrow.BinaryTypes.Binary
}
return arrow.BinaryTypes.LargeBinary
}
|