From f9d8edda36acbd83a552a34392d0df926646d849 Mon Sep 17 00:00:00 2001 From: Mark Wolfe Date: Fri, 21 Jul 2023 00:40:33 +1000 Subject: [PATCH] =?UTF-8?q?GH-36698:=20[Go][Parquet]=20Add=20a=20Timestamp?= =?UTF-8?q?LogicalType=20creation=20function=20=E2=80=A6=20(#36699)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …with more options This change introduces a more flexible creation function for TimestampLogicalType which will enable changes to all the flags provided by this type, but without requiring a lot of parameters. Following on from other great examples in arrow it uses the functional options pattern. ### Rationale for this change Add a `TimestampLogicalType` creation function with more options, in particular an option to set `fromConverted` as I can't see another way to set this private struct property after creation. ### What changes are included in this PR? This change introduces a more flexible creation function for `TimestampLogicalType` which will enable changes to all the flags provided by this type, but without requiring a lot of parameters. ### Are these changes tested? Yes I have updated one of the existing tests. ### Are there any user-facing changes? * Closes: #36698 Authored-by: Mark Wolfe Signed-off-by: Matt Topol --- go/parquet/schema/logical_types.go | 49 +++++++++++++++++++++++++ go/parquet/schema/logical_types_test.go | 1 + 2 files changed, 50 insertions(+) diff --git a/go/parquet/schema/logical_types.go b/go/parquet/schema/logical_types.go index ade6e750adacb..4075edc1e9402 100644 --- a/go/parquet/schema/logical_types.go +++ b/go/parquet/schema/logical_types.go @@ -616,6 +616,55 @@ func NewTimestampLogicalTypeForce(isAdjustedToUTC bool, unit TimeUnitType) Logic } } +// TimestampOpt options used with New Timestamp Logical Type +type TimestampOpt func(*TimestampLogicalType) + +// WithTSIsAdjustedToUTC sets the IsAdjustedToUTC field of the timestamp type. +func WithTSIsAdjustedToUTC() TimestampOpt { + return func(t *TimestampLogicalType) { + t.typ.IsAdjustedToUTC = true + } +} + +// WithTSTimeUnitType sets the time unit for the timestamp type +func WithTSTimeUnitType(unit TimeUnitType) TimestampOpt { + return func(t *TimestampLogicalType) { + t.typ.Unit = createTimeUnit(unit) + } +} + +// WithTSForceConverted enable force converted mode +func WithTSForceConverted() TimestampOpt { + return func(t *TimestampLogicalType) { + t.forceConverted = true + } +} + +// WithTSFromConverted enable the timestamp logical type to be +// constructed from a converted type. +func WithTSFromConverted() TimestampOpt { + return func(t *TimestampLogicalType) { + t.fromConverted = true + } +} + +// NewTimestampLogicalTypeWithOpts creates a new TimestampLogicalType with the provided options. +// +// TimestampType Unit defaults to milliseconds (TimeUnitMillis) +func NewTimestampLogicalTypeWithOpts(opts ...TimestampOpt) LogicalType { + ts := &TimestampLogicalType{ + typ: &format.TimestampType{ + Unit: createTimeUnit(TimeUnitMillis), // default to milliseconds + }, + } + + for _, o := range opts { + o(ts) + } + + return ts +} + // TimestampLogicalType represents an int64 number that can be decoded // into a year, month, day, hour, minute, second, and subsecond type TimestampLogicalType struct { diff --git a/go/parquet/schema/logical_types_test.go b/go/parquet/schema/logical_types_test.go index 540899d79a02a..117157f95ef83 100644 --- a/go/parquet/schema/logical_types_test.go +++ b/go/parquet/schema/logical_types_test.go @@ -93,6 +93,7 @@ func TestConvertedTypeCompatibility(t *testing.T) { {"time_micro", schema.NewTimeLogicalType(true /* adjutedToUTC */, schema.TimeUnitMicros), schema.ConvertedTypes.TimeMicros}, {"timestamp_milli", schema.NewTimestampLogicalType(true /* adjutedToUTC */, schema.TimeUnitMillis), schema.ConvertedTypes.TimestampMillis}, {"timestamp_micro", schema.NewTimestampLogicalType(true /* adjutedToUTC */, schema.TimeUnitMicros), schema.ConvertedTypes.TimestampMicros}, + {"timestamp_milli_opts", schema.NewTimestampLogicalTypeWithOpts(schema.WithTSIsAdjustedToUTC(), schema.WithTSTimeUnitType(schema.TimeUnitMillis)), schema.ConvertedTypes.TimestampMillis}, {"uint8", schema.NewIntLogicalType(8 /* bitWidth */, false /* signed */), schema.ConvertedTypes.Uint8}, {"uint16", schema.NewIntLogicalType(16 /* bitWidth */, false /* signed */), schema.ConvertedTypes.Uint16}, {"uint32", schema.NewIntLogicalType(32 /* bitWidth */, false /* signed */), schema.ConvertedTypes.Uint32},