Skip to content

Commit 2a11c1e

Browse files
committed
chore: dedup floorDiv
related to #1607 Signed-off-by: Ferhat Elmas <elmas.ferhat@gmail.com>
1 parent 3020adb commit 2a11c1e

1 file changed

Lines changed: 4 additions & 13 deletions

File tree

variant_cast.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/apache/arrow-go/v18/arrow/decimal"
2323
"github.com/apache/arrow-go/v18/arrow/decimal128"
2424
"github.com/apache/arrow-go/v18/parquet/variant"
25+
"github.com/apache/iceberg-go/internal"
2526
"github.com/google/uuid"
2627
)
2728

@@ -200,7 +201,7 @@ func castVariantDecimal(raw any, typ DecimalType) (any, bool) {
200201
func castVariantToMicros(pt variant.Type, raw any, tz bool) (any, bool) {
201202
switch {
202203
case tz && pt == variant.TimestampNanos, !tz && pt == variant.TimestampNanosNTZ:
203-
return Timestamp(floorDiv(int64(raw.(arrow.Timestamp)), nanosPerMicro)), true
204+
return Timestamp(internal.FloorDiv(int64(raw.(arrow.Timestamp)), nanosPerMicro)), true
204205
case !tz && pt == variant.Date:
205206
if micros, ok := mulNoOverflow(int64(raw.(arrow.Date32)), microsPerDay); ok {
206207
return Timestamp(micros), true
@@ -230,24 +231,14 @@ func castVariantToNanos(pt variant.Type, raw any, tz bool) (any, bool) {
230231
func castVariantToDate(pt variant.Type, raw any) (any, bool) {
231232
switch pt {
232233
case variant.TimestampMicrosNTZ:
233-
return Date(floorDiv(int64(raw.(arrow.Timestamp)), microsPerDay)), true
234+
return Date(internal.FloorDiv(int64(raw.(arrow.Timestamp)), microsPerDay)), true
234235
case variant.TimestampNanosNTZ:
235-
return Date(floorDiv(int64(raw.(arrow.Timestamp)), nanosPerDay)), true
236+
return Date(internal.FloorDiv(int64(raw.(arrow.Timestamp)), nanosPerDay)), true
236237
}
237238

238239
return nil, false
239240
}
240241

241-
// floorDiv divides rounding toward negative infinity.
242-
func floorDiv(a, b int64) int64 {
243-
q := a / b
244-
if (a%b != 0) && ((a < 0) != (b < 0)) {
245-
q--
246-
}
247-
248-
return q
249-
}
250-
251242
// mulNoOverflow returns a*b, reporting false if the product overflows int64.
252243
func mulNoOverflow(a, b int64) (int64, bool) {
253244
p := a * b

0 commit comments

Comments
 (0)