Skip to content

Commit fedcf14

Browse files
1NepuNep1Viktor Pentyukhovasmyasnikovkprokopenko
authored
Supported wide time types in optional param builder (#1858)
Co-authored-by: Viktor Pentyukhov <[email protected]> Co-authored-by: Aleksey Myasnikov <[email protected]> Co-authored-by: Konstantin Prokopenko <[email protected]>
1 parent a0d9dfd commit fedcf14

File tree

5 files changed

+249
-0
lines changed

5 files changed

+249
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Added support for nullable `Date32`, `Datetime64`, `Timestamp64`, and `Interval64` types in the `optional` parameter builder
12
* Added method `query.WithIssuesHandler` to get query issues
23

34
## v3.117.2

internal/params/optional.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,24 +119,48 @@ func (p *optional) Timestamp(v *time.Time) *optionalBuilder {
119119
return &optionalBuilder{opt: p}
120120
}
121121

122+
func (p *optional) Timestamp64(v *time.Time) *optionalBuilder {
123+
p.value = value.NullableTimestamp64ValueFromTime(v)
124+
125+
return &optionalBuilder{opt: p}
126+
}
127+
122128
func (p *optional) Date(v *time.Time) *optionalBuilder {
123129
p.value = value.NullableDateValueFromTime(v)
124130

125131
return &optionalBuilder{opt: p}
126132
}
127133

134+
func (p *optional) Date32(v *time.Time) *optionalBuilder {
135+
p.value = value.NullableDate32ValueFromTime(v)
136+
137+
return &optionalBuilder{opt: p}
138+
}
139+
128140
func (p *optional) Datetime(v *time.Time) *optionalBuilder {
129141
p.value = value.NullableDatetimeValueFromTime(v)
130142

131143
return &optionalBuilder{opt: p}
132144
}
133145

146+
func (p *optional) Datetime64(v *time.Time) *optionalBuilder {
147+
p.value = value.NullableDatetime64ValueFromTime(v)
148+
149+
return &optionalBuilder{opt: p}
150+
}
151+
134152
func (p *optional) Interval(v *time.Duration) *optionalBuilder {
135153
p.value = value.NullableIntervalValueFromDuration(v)
136154

137155
return &optionalBuilder{opt: p}
138156
}
139157

158+
func (p *optional) Interval64(v *time.Duration) *optionalBuilder {
159+
p.value = value.NullableInterval64ValueFromDuration(v)
160+
161+
return &optionalBuilder{opt: p}
162+
}
163+
140164
func (p *optional) JSON(v *string) *optionalBuilder {
141165
p.value = value.NullableJSONValue(v)
142166

internal/params/optional_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,26 @@ func TestOptional(t *testing.T) {
317317
},
318318
},
319319
},
320+
{
321+
method: "Interval64",
322+
args: []any{p(time.Duration(123456789))},
323+
expected: expected{
324+
Type: &Ydb.Type{
325+
Type: &Ydb.Type_OptionalType{
326+
OptionalType: &Ydb.OptionalType{
327+
Item: &Ydb.Type{
328+
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_INTERVAL64},
329+
},
330+
},
331+
},
332+
},
333+
Value: &Ydb.Value{
334+
Value: &Ydb.Value_Int64Value{
335+
Int64Value: 123456789, // nanoseconds
336+
},
337+
},
338+
},
339+
},
320340
{
321341
method: "Datetime",
322342
args: []any{p(time.Unix(123456789, 456))},
@@ -338,6 +358,26 @@ func TestOptional(t *testing.T) {
338358
},
339359
},
340360
},
361+
{
362+
method: "Datetime64",
363+
args: []any{p(time.Unix(123456789, 456))},
364+
expected: expected{
365+
Type: &Ydb.Type{
366+
Type: &Ydb.Type_OptionalType{
367+
OptionalType: &Ydb.OptionalType{
368+
Item: &Ydb.Type{
369+
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_DATETIME64},
370+
},
371+
},
372+
},
373+
},
374+
Value: &Ydb.Value{
375+
Value: &Ydb.Value_Int64Value{
376+
Int64Value: 123456789,
377+
},
378+
},
379+
},
380+
},
341381
{
342382
method: "Date",
343383
args: []any{p(time.Unix(123456789, 456))},
@@ -359,6 +399,26 @@ func TestOptional(t *testing.T) {
359399
},
360400
},
361401
},
402+
{
403+
method: "Date32",
404+
args: []any{p(time.Unix(123456789, 456))},
405+
expected: expected{
406+
Type: &Ydb.Type{
407+
Type: &Ydb.Type_OptionalType{
408+
OptionalType: &Ydb.OptionalType{
409+
Item: &Ydb.Type{
410+
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_DATE32},
411+
},
412+
},
413+
},
414+
},
415+
Value: &Ydb.Value{
416+
Value: &Ydb.Value_Int32Value{
417+
Int32Value: 1428,
418+
},
419+
},
420+
},
421+
},
362422
{
363423
method: "Timestamp",
364424
args: []any{p(time.Unix(123456789, 456))},
@@ -380,6 +440,26 @@ func TestOptional(t *testing.T) {
380440
},
381441
},
382442
},
443+
{
444+
method: "Timestamp64",
445+
args: []any{p(time.Unix(123456789, 123456000))},
446+
expected: expected{
447+
Type: &Ydb.Type{
448+
Type: &Ydb.Type_OptionalType{
449+
OptionalType: &Ydb.OptionalType{
450+
Item: &Ydb.Type{
451+
Type: &Ydb.Type_TypeId{TypeId: Ydb.Type_TIMESTAMP64},
452+
},
453+
},
454+
},
455+
},
456+
Value: &Ydb.Value{
457+
Value: &Ydb.Value_Int64Value{
458+
Int64Value: 123456789123456, // microseconds
459+
},
460+
},
461+
},
462+
},
383463
{
384464
method: "Decimal",
385465
args: []any{p([...]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6}), uint32(22), uint32(9)},

internal/value/nullable.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,22 @@ func NullableDateValueFromTime(v *time.Time) Value {
143143
return OptionalValue(DateValueFromTime(*v))
144144
}
145145

146+
func NullableDate32Value(v *int32) Value {
147+
if v == nil {
148+
return NullValue(types.Date32)
149+
}
150+
151+
return OptionalValue(Date32Value(*v))
152+
}
153+
154+
func NullableDate32ValueFromTime(v *time.Time) Value {
155+
if v == nil {
156+
return NullValue(types.Date32)
157+
}
158+
159+
return OptionalValue(Date32ValueFromTime(*v))
160+
}
161+
146162
func NullableDatetimeValue(v *uint32) Value {
147163
if v == nil {
148164
return NullValue(types.Datetime)
@@ -151,6 +167,22 @@ func NullableDatetimeValue(v *uint32) Value {
151167
return OptionalValue(DatetimeValue(*v))
152168
}
153169

170+
func NullableDatetime64Value(v *int64) Value {
171+
if v == nil {
172+
return NullValue(types.Datetime64)
173+
}
174+
175+
return OptionalValue(Datetime64Value(*v))
176+
}
177+
178+
func NullableDatetime64ValueFromTime(v *time.Time) Value {
179+
if v == nil {
180+
return NullValue(types.Datetime64)
181+
}
182+
183+
return OptionalValue(Datetime64ValueFromTime(*v))
184+
}
185+
154186
func NullableDatetimeValueFromTime(v *time.Time) Value {
155187
if v == nil {
156188
return NullValue(types.Datetime)
@@ -223,6 +255,22 @@ func NullableTimestampValueFromTime(v *time.Time) Value {
223255
return OptionalValue(TimestampValueFromTime(*v))
224256
}
225257

258+
func NullableTimestamp64Value(v *int64) Value {
259+
if v == nil {
260+
return NullValue(types.Timestamp64)
261+
}
262+
263+
return OptionalValue(Timestamp64Value(*v))
264+
}
265+
266+
func NullableTimestamp64ValueFromTime(v *time.Time) Value {
267+
if v == nil {
268+
return NullValue(types.Timestamp64)
269+
}
270+
271+
return OptionalValue(Timestamp64ValueFromTime(*v))
272+
}
273+
226274
func NullableTzTimestampValue(v *string) Value {
227275
if v == nil {
228276
return NullValue(types.TzTimestamp)
@@ -255,6 +303,22 @@ func NullableIntervalValueFromDuration(v *time.Duration) Value {
255303
return OptionalValue(IntervalValueFromDuration(*v))
256304
}
257305

306+
func NullableInterval64ValueFromNanoseconds(v *int64) Value {
307+
if v == nil {
308+
return NullValue(types.Interval64)
309+
}
310+
311+
return OptionalValue(Interval64Value(*v))
312+
}
313+
314+
func NullableInterval64ValueFromDuration(v *time.Duration) Value {
315+
if v == nil {
316+
return NullValue(types.Interval64)
317+
}
318+
319+
return OptionalValue(Interval64ValueFromDuration(*v))
320+
}
321+
258322
func NullableBytesValue(v *[]byte) Value {
259323
if v == nil {
260324
return NullValue(types.Bytes)
@@ -451,6 +515,15 @@ func Nullable(t types.Type, v interface{}) Value {
451515
default:
452516
panic(fmt.Sprintf("unsupported type conversion from %T to TypeDate", tt))
453517
}
518+
case types.Date32:
519+
switch tt := v.(type) {
520+
case *int32:
521+
return NullableDate32Value(tt)
522+
case *time.Time:
523+
return NullableDate32ValueFromTime(tt)
524+
default:
525+
panic(fmt.Sprintf("unsupported type conversion from %T to TypeDate32", tt))
526+
}
454527
case types.Datetime:
455528
switch tt := v.(type) {
456529
case *uint32:
@@ -460,6 +533,15 @@ func Nullable(t types.Type, v interface{}) Value {
460533
default:
461534
panic(fmt.Sprintf("unsupported type conversion from %T to TypeDatetime", tt))
462535
}
536+
case types.Datetime64:
537+
switch tt := v.(type) {
538+
case *int64:
539+
return NullableDatetime64Value(tt)
540+
case *time.Time:
541+
return NullableDatetime64ValueFromTime(tt)
542+
default:
543+
panic(fmt.Sprintf("unsupported type conversion from %T to TypeDatetime64", tt))
544+
}
463545
case types.Timestamp:
464546
switch tt := v.(type) {
465547
case *uint64:
@@ -469,6 +551,15 @@ func Nullable(t types.Type, v interface{}) Value {
469551
default:
470552
panic(fmt.Sprintf("unsupported type conversion from %T to TypeTimestamp", tt))
471553
}
554+
case types.Timestamp64:
555+
switch tt := v.(type) {
556+
case *int64:
557+
return NullableTimestamp64Value(tt)
558+
case *time.Time:
559+
return NullableTimestamp64ValueFromTime(tt)
560+
default:
561+
panic(fmt.Sprintf("unsupported type conversion from %T to TypeTimestamp64", tt))
562+
}
472563
case types.Interval:
473564
switch tt := v.(type) {
474565
case *int64:
@@ -478,6 +569,15 @@ func Nullable(t types.Type, v interface{}) Value {
478569
default:
479570
panic(fmt.Sprintf("unsupported type conversion from %T to TypeInterval", tt))
480571
}
572+
case types.Interval64:
573+
switch tt := v.(type) {
574+
case *int64:
575+
return NullableInterval64ValueFromNanoseconds(tt)
576+
case *time.Duration:
577+
return NullableInterval64ValueFromDuration(tt)
578+
default:
579+
panic(fmt.Sprintf("unsupported type conversion from %T to TypeInterval64", tt))
580+
}
481581
case types.TzDate:
482582
switch tt := v.(type) {
483583
case *string:

0 commit comments

Comments
 (0)