zeroshade commented on code in PR #35654: URL: https://github.com/apache/arrow/pull/35654#discussion_r1200762505
########## go/arrow/compute/exprs/exec.go: ########## @@ -0,0 +1,631 @@ +// 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 exprs + +import ( + "context" + "fmt" + "unsafe" + + "github.com/apache/arrow/go/v13/arrow" + "github.com/apache/arrow/go/v13/arrow/array" + "github.com/apache/arrow/go/v13/arrow/compute" + "github.com/apache/arrow/go/v13/arrow/compute/internal/exec" + "github.com/apache/arrow/go/v13/arrow/decimal128" + "github.com/apache/arrow/go/v13/arrow/endian" + "github.com/apache/arrow/go/v13/arrow/internal/debug" + "github.com/apache/arrow/go/v13/arrow/memory" + "github.com/apache/arrow/go/v13/arrow/scalar" + "github.com/substrait-io/substrait-go/expr" + "github.com/substrait-io/substrait-go/extensions" + "github.com/substrait-io/substrait-go/types" +) + +func makeExecBatch(ctx context.Context, schema *arrow.Schema, partial compute.Datum) (out compute.ExecBatch, err error) { + // cleanup if we get an error + defer func() { + if err != nil { + for _, v := range out.Values { + if v != nil { + v.Release() + } + } + } + }() + + if partial.Kind() == compute.KindRecord { + partialBatch := partial.(*compute.RecordDatum).Value + batchSchema := partialBatch.Schema() + + out.Values = make([]compute.Datum, len(schema.Fields())) + out.Len = partialBatch.NumRows() + + for i, field := range schema.Fields() { + idxes := batchSchema.FieldIndices(field.Name) + switch len(idxes) { + case 0: + out.Values[i] = compute.NewDatum(scalar.MakeNullScalar(field.Type)) + case 1: + col := partialBatch.Column(idxes[0]) + if !arrow.TypeEqual(col.DataType(), field.Type) { + // referenced field was present but didn't have expected type + // we'll cast this case for now + col, err = compute.CastArray(ctx, col, compute.SafeCastOptions(field.Type)) + if err != nil { + return compute.ExecBatch{}, err + } + defer col.Release() + } + out.Values[i] = compute.NewDatum(col) + default: + err = fmt.Errorf("%w: exec batch field '%s' ambiguous, more than one match", + arrow.ErrInvalid, field.Name) + return compute.ExecBatch{}, err + } + } + return + } + + part, ok := partial.(compute.ArrayLikeDatum) + if !ok { + return out, fmt.Errorf("%w: MakeExecBatch from %s", arrow.ErrNotImplemented, partial) + } + + // wasteful but useful for testing + if part.Type().ID() == arrow.STRUCT { + switch part := part.(type) { + case *compute.ArrayDatum: + arr := part.MakeArray().(*array.Struct) + defer arr.Release() + + batch := array.RecordFromStructArray(arr, nil) + defer batch.Release() + return makeExecBatch(ctx, schema, compute.NewDatumWithoutOwning(batch)) + case *compute.ScalarDatum: + out.Len = 1 + out.Values = make([]compute.Datum, len(schema.Fields())) + + s := part.Value.(*scalar.Struct) + dt := s.Type.(*arrow.StructType) + + for i, field := range schema.Fields() { + idx, found := dt.FieldIdx(field.Name) + if !found { + out.Values[i] = compute.NewDatum(scalar.MakeNullScalar(field.Type)) + continue + } + + val := s.Value[idx] + if !arrow.TypeEqual(val.DataType(), field.Type) { + // referenced field was present but didn't have the expected + // type. for now we'll cast this + val, err = val.CastTo(field.Type) + if err != nil { + return compute.ExecBatch{}, err + } + } + out.Values[i] = compute.NewDatum(val) + } + return + } + } + + return out, fmt.Errorf("%w: MakeExecBatch from %s", arrow.ErrNotImplemented, partial) +} + +// ToArrowSchema takes a substrait NamedStruct and an extension set (for +// type resolution mapping) and creates the equivalent Arrow Schema. +func ToArrowSchema(base types.NamedStruct, ext ExtensionIDSet) (*arrow.Schema, error) { + fields := make([]arrow.Field, len(base.Names)) + for i, typ := range base.Struct.Types { + dt, nullable, err := FromSubstraitType(typ, ext) + if err != nil { + return nil, err + } + fields[i] = arrow.Field{ + Name: base.Names[i], + Type: dt, + Nullable: nullable, + } + } + + return arrow.NewSchema(fields, nil), nil +} + +type ( + regCtxKey struct{} + extCtxKey struct{} +) + +func WithExtensionRegistry(ctx context.Context, reg *ExtensionIDRegistry) context.Context { + return context.WithValue(ctx, regCtxKey{}, reg) +} + +func GetExtensionRegistry(ctx context.Context) *ExtensionIDRegistry { + v, ok := ctx.Value(regCtxKey{}).(*ExtensionIDRegistry) + if !ok { + v = DefaultExtensionIDRegistry + } + return v +} + +func WithExtensionIDSet(ctx context.Context, ext ExtensionIDSet) context.Context { + return context.WithValue(ctx, extCtxKey{}, ext) +} + +func GetExtensionIDSet(ctx context.Context) ExtensionIDSet { + v, ok := ctx.Value(extCtxKey{}).(ExtensionIDSet) + if !ok { + return NewExtensionSet( + expr.NewEmptyExtensionRegistry(&extensions.DefaultCollection), + GetExtensionRegistry(ctx)) + } + return v +} + +func literalToDatum(mem memory.Allocator, lit expr.Literal, ext ExtensionIDSet) (compute.Datum, error) { + switch v := lit.(type) { + case *expr.PrimitiveLiteral[bool]: + return compute.NewDatum(scalar.NewBooleanScalar(v.Value)), nil + case *expr.PrimitiveLiteral[int8]: + return compute.NewDatum(scalar.NewInt8Scalar(v.Value)), nil + case *expr.PrimitiveLiteral[int16]: + return compute.NewDatum(scalar.NewInt16Scalar(v.Value)), nil + case *expr.PrimitiveLiteral[int32]: + return compute.NewDatum(scalar.NewInt32Scalar(v.Value)), nil + case *expr.PrimitiveLiteral[int64]: + return compute.NewDatum(scalar.NewInt64Scalar(v.Value)), nil + case *expr.PrimitiveLiteral[float32]: + return compute.NewDatum(scalar.NewFloat32Scalar(v.Value)), nil + case *expr.PrimitiveLiteral[float64]: + return compute.NewDatum(scalar.NewFloat64Scalar(v.Value)), nil + case *expr.PrimitiveLiteral[string]: + return compute.NewDatum(scalar.NewStringScalar(v.Value)), nil + case *expr.PrimitiveLiteral[types.Timestamp]: + return compute.NewDatum(scalar.NewTimestampScalar(arrow.Timestamp(v.Value), &arrow.TimestampType{Unit: arrow.Microsecond})), nil + case *expr.PrimitiveLiteral[types.TimestampTz]: + return compute.NewDatum(scalar.NewTimestampScalar(arrow.Timestamp(v.Value), + &arrow.TimestampType{Unit: arrow.Microsecond, TimeZone: TimestampTzTimezone})), nil Review Comment: It's a constant defined as "UTC", defined at the top of `types.go`, matching the inline function `TimestampTzTimezoneString` in the c++ (`cpp/src/arrow/engine/substrait/type_internal.h`) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
