Skip to content

Commit

Permalink
ARROW-17551: [Go] Implement Temporal Cast Functions (#14006)
Browse files Browse the repository at this point in the history
Authored-by: Matt Topol <zotthewizard@gmail.com>
Signed-off-by: Matt Topol <zotthewizard@gmail.com>
  • Loading branch information
zeroshade committed Sep 1, 2022
1 parent d5f80cb commit 5d0ed86
Show file tree
Hide file tree
Showing 19 changed files with 4,312 additions and 45 deletions.
2 changes: 1 addition & 1 deletion go/arrow/array/numericbuilder.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/arrow/array/numericbuilder.gen.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (b *{{.Name}}Builder) unmarshalOne(dec *json.Decoder) error {
case string:
{{if (eq .Name "Timestamp") -}}
loc, _ := b.dtype.GetZone()
tm, err := arrow.TimestampFromStringInLocation(v, b.dtype.Unit, loc)
tm, _, err := arrow.TimestampFromStringInLocation(v, b.dtype.Unit, loc)
{{else -}}
tm, err := {{.QualifiedType}}FromString(v, b.dtype.Unit)
{{end}}
Expand Down
23 changes: 23 additions & 0 deletions go/arrow/compute/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func initCastTable() {
castTable = make(map[arrow.Type]*castFunction)
addCastFuncs(getBooleanCasts())
addCastFuncs(getNumericCasts())
addCastFuncs(getTemporalCasts())
}

func getCastFunction(to arrow.DataType) (*castFunction, error) {
Expand All @@ -189,6 +190,28 @@ func getBooleanCasts() []*castFunction {
return []*castFunction{fn}
}

func getTemporalCasts() []*castFunction {
output := make([]*castFunction, 0)
addFn := func(name string, id arrow.Type, kernels []exec.ScalarKernel) {
fn := newCastFunction(name, id)
for _, k := range kernels {
if err := fn.AddTypeCast(k.Signature.InputTypes[0].MatchID(), k); err != nil {
panic(err)
}
}
output = append(output, fn)
}

addFn("cast_timestamp", arrow.TIMESTAMP, kernels.GetTimestampCastKernels())
addFn("cast_date32", arrow.DATE32, kernels.GetDate32CastKernels())
addFn("cast_date64", arrow.DATE64, kernels.GetDate64CastKernels())
addFn("cast_time32", arrow.TIME32, kernels.GetTime32CastKernels())
addFn("cast_time64", arrow.TIME64, kernels.GetTime64CastKernels())
addFn("cast_duration", arrow.DURATION, kernels.GetDurationCastKernels())
addFn("cast_month_day_nano_interval", arrow.INTERVAL_MONTH_DAY_NANO, kernels.GetIntervalCastKernels())
return output
}

func getNumericCasts() []*castFunction {
out := make([]*castFunction, 0)

Expand Down
570 changes: 567 additions & 3 deletions go/arrow/compute/cast_test.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions go/arrow/compute/internal/exec/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ type FixedWidthTypes interface {
arrow.DayTimeInterval | arrow.MonthDayNanoInterval
}

type TemporalTypes interface {
arrow.Date32 | arrow.Date64 | arrow.Time32 | arrow.Time64 |
arrow.Timestamp | arrow.Duration | arrow.DayTimeInterval |
arrow.MonthInterval | arrow.MonthDayNanoInterval
}

// GetSpanValues returns a properly typed slice bye reinterpreting
// the buffer at index i using unsafe.Slice. This will take into account
// the offset of the given ArraySpan.
Expand Down
16 changes: 15 additions & 1 deletion go/arrow/compute/internal/kernels/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ALL_SOURCES := $(shell find . -path ./_lib -prune -o -name '*.go' -name '*.s' -n
.PHONEY: assembly

INTEL_SOURCES := \
cast_numeric_avx2_amd64.s cast_numeric_sse4_amd64.s
cast_numeric_avx2_amd64.s cast_numeric_sse4_amd64.s constant_factor_avx2_amd64.s constant_factor_sse4_amd64.s

#
# ARROW-15336: DO NOT add the assembly target for Arm64 (ARM_SOURCES) until c2goasm added the Arm64 support.
Expand All @@ -55,12 +55,26 @@ _lib/cast_numeric_sse4_amd64.s: _lib/cast_numeric.cc
_lib/cast_numeric_neon.s: _lib/cast_numeric.cc
$(CXX) -std=c++17 -S $(C_FLAGS_NEON) $^ -o $@ ; $(PERL_FIXUP_ROTATE) $@

_lib/constant_factor_avx2_amd64.s: _lib/constant_factor.c
$(CC) -S $(C_FLAGS) $(ASM_FLAGS_AVX2) $^ -o $@ ; $(PERL_FIXUP_ROTATE) $@

_lib/constant_factor_sse4_amd64.s: _lib/constant_factor.c
$(CC) -S $(C_FLAGS) $(ASM_FLAGS_SSE4) $^ -o $@ ; $(PERL_FIXUP_ROTATE) $@

_lib/constant_factor_neon.s: _lib/constant_factor.c
$(CC) -S $(C_FLAGS_NEON) $^ -o $@ ; $(PERL_FIXUP_ROTATE) $@

cast_numeric_avx2_amd64.s: _lib/cast_numeric_avx2_amd64.s
$(C2GOASM) -a -f $^ $@

cast_numeric_sse4_amd64.s: _lib/cast_numeric_sse4_amd64.s
$(C2GOASM) -a -f $^ $@

constant_factor_avx2_amd64.s: _lib/constant_factor_avx2_amd64.s
$(C2GOASM) -a -f $^ $@

constant_factor_sse4_amd64.s: _lib/constant_factor_sse4_amd64.s
$(C2GOASM) -a -f $^ $@

clean:
rm -f $(INTEL_SOURCES)
Expand Down
35 changes: 35 additions & 0 deletions go/arrow/compute/internal/kernels/_lib/constant_factor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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.

#include <arch.h>
#include <stdint.h>

#define CREATE_CONSTANT_FACTOR(SRC, DEST) \
void FULL_NAME(multiply_constant_##SRC##_##DEST)(const SRC##_t* src, DEST##_t* dest, const int len, const int64_t factor) { \
for (int i = 0; i < len; ++i) { \
dest[i] = (DEST##_t)(src[i] * factor); \
} \
} \
void FULL_NAME(divide_constant_##SRC##_##DEST)(const SRC##_t* src, DEST##_t* dest, const int len, const int64_t factor) { \
for (int i = 0; i < len; ++i) { \
dest[i] = (DEST##_t)(src[i] / factor); \
} \
}

CREATE_CONSTANT_FACTOR(int32, int32)
CREATE_CONSTANT_FACTOR(int32, int64)
CREATE_CONSTANT_FACTOR(int64, int32)
CREATE_CONSTANT_FACTOR(int64, int64)
Loading

0 comments on commit 5d0ed86

Please sign in to comment.