Skip to content

Commit

Permalink
GH-36698: [Go][Parquet] Add a TimestampLogicalType creation function … (
Browse files Browse the repository at this point in the history
#36699)

…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 <mark@wolfe.id.au>
Signed-off-by: Matt Topol <zotthewizard@gmail.com>
  • Loading branch information
wolfeidau authored Jul 20, 2023
1 parent 819b7d5 commit f9d8edd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions go/parquet/schema/logical_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions go/parquet/schema/logical_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down

0 comments on commit f9d8edd

Please sign in to comment.