Skip to content

Commit daaf932

Browse files
authored
fix(arrow/compute/exprs): fix literalToDatum for precision types (#447)
### Rationale for this change While #404 fixed support for substrait precision types on inputs, it did not address support for literals that use the Precision types. Fixes #446 ### What changes are included in this PR? Fix `literalToDatum` so that it properly supports the precision types for literals. ### Are these changes tested? Yes, a unit test is added. ### Are there any user-facing changes? Just the increased support
1 parent 82f4943 commit daaf932

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

arrow/compute/exprs/exec.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,36 @@ func literalToDatum(mem memory.Allocator, lit expr.Literal, ext ExtensionIDSet)
379379
case *types.VarCharType:
380380
return compute.NewDatum(scalar.NewExtensionScalar(
381381
scalar.NewStringScalar(v.Value.(string)), varChar(int32(t.Length)))), nil
382+
case *types.PrecisionTimeType:
383+
dt, _, err := FromSubstraitType(t, ext)
384+
if err != nil {
385+
return nil, err
386+
}
387+
388+
switch dt.ID() {
389+
case arrow.TIME32:
390+
return &compute.ScalarDatum{Value: scalar.NewTime32Scalar(
391+
arrow.Time32(v.Value.(int64)), dt)}, nil
392+
case arrow.TIME64:
393+
return &compute.ScalarDatum{Value: scalar.NewTime64Scalar(
394+
arrow.Time64(v.Value.(int64)), dt)}, nil
395+
}
396+
case *types.PrecisionTimestampType:
397+
dt, _, err := FromSubstraitType(t, ext)
398+
if err != nil {
399+
return nil, err
400+
}
401+
402+
return &compute.ScalarDatum{Value: scalar.NewTimestampScalar(
403+
arrow.Timestamp(v.Value.(int64)), dt)}, nil
404+
case *types.PrecisionTimestampTzType:
405+
dt, _, err := FromSubstraitType(t, ext)
406+
if err != nil {
407+
return nil, err
408+
}
409+
410+
return &compute.ScalarDatum{Value: scalar.NewTimestampScalar(
411+
arrow.Timestamp(v.Value.(int64)), dt)}, nil
382412
}
383413
}
384414

arrow/compute/exprs/exec_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,43 @@ func Test_Types(t *testing.T) {
518518
return expr.NewPrimitiveLiteral(types.Time(v), true)
519519
},
520520
},
521+
{
522+
name: "expect arrow.TIME64 (ns) ok",
523+
schema: func() *arrow.Schema {
524+
field := arrow.Field{
525+
Name: "col",
526+
Type: &arrow.Time64Type{Unit: arrow.Nanosecond},
527+
Nullable: true,
528+
}
529+
530+
return arrow.NewSchema([]arrow.Field{field}, nil)
531+
},
532+
record: func(rq *require.Assertions, schema *arrow.Schema) arrow.Record {
533+
b := array.NewTime64Builder(memory.DefaultAllocator, &arrow.Time64Type{Unit: arrow.Nanosecond})
534+
defer b.Release()
535+
536+
t1, err := arrow.Time64FromString("10:00:00.000000", arrow.Nanosecond)
537+
rq.NoError(err, "Failed to create Time64 value")
538+
539+
b.AppendValues([]arrow.Time64{t1}, []bool{true})
540+
541+
return array.NewRecord(schema, []arrow.Array{b.NewArray()}, 1)
542+
},
543+
val: func(rq *require.Assertions) expr.Literal {
544+
v, err := arrow.Time64FromString("11:00:00.000000", arrow.Nanosecond)
545+
rq.NoError(err, "Failed to create Time64 value")
546+
547+
pt := &types.PrecisionTime{
548+
Precision: int32(arrow.Nanosecond) * 3,
549+
Value: int64(v),
550+
}
551+
552+
lit, err := expr.NewLiteral(pt, true)
553+
rq.NoError(err, "Failed to create literal")
554+
555+
return lit
556+
},
557+
},
521558
{
522559
name: "expect arrow.TIMESTAMP (ns) ok",
523560
schema: func() *arrow.Schema {

0 commit comments

Comments
 (0)