Skip to content

Commit 363606d

Browse files
committed
* Fixed bug with convert ydb value to time.Duration in result.Scan[WithDefaults,Named]()
* Fixed bug with make ydb value from `time.Duration` in `types.IntervalValueFromDuration(d)`
1 parent 819b51c commit 363606d

File tree

8 files changed

+95
-13
lines changed

8 files changed

+95
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
* Fixed bug with convert ydb value to `time.Duration` in `result.Scan[WithDefaults,Named]()`
2+
* Fixed bug with make ydb value from `time.Duration` in `types.IntervalValueFromDuration(d)`
3+
14
## v3.26.8
25
* Removed the processing of trailer metadata on stream calls
36

internal/table/scanner/scan_raw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (s *rawConverter) Timestamp() (v time.Time) {
173173

174174
func (s *rawConverter) Interval() (v time.Duration) {
175175
s.unwrap()
176-
return timeutil.UnmarshalInterval(s.int64())
176+
return timeutil.MicrosecondsToDuration(s.int64())
177177
}
178178

179179
func (s *rawConverter) TzDate() (v time.Time) {

internal/table/scanner/scanner.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ func (s *scanner) any() interface{} {
378378
case value.TypeInt64:
379379
return s.int64()
380380
case value.TypeInterval:
381-
return timeutil.UnmarshalInterval(s.int64())
381+
return timeutil.MicrosecondsToDuration(s.int64())
382382
case value.TypeTzDate:
383383
src, err := timeutil.UnmarshalTzDate(s.text())
384384
if err != nil {
@@ -750,7 +750,7 @@ func (s *scanner) scanRequired(value interface{}) {
750750
case *time.Time:
751751
s.setTime(v)
752752
case *time.Duration:
753-
*v = timeutil.UnmarshalInterval(s.int64())
753+
*v = timeutil.MicrosecondsToDuration(s.int64())
754754
case *string:
755755
s.setString(v)
756756
case *[]byte:
@@ -910,7 +910,7 @@ func (s *scanner) scanOptional(value interface{}, defaultValueForOptional bool)
910910
if s.isNull() {
911911
*v = nil
912912
} else {
913-
src := timeutil.UnmarshalInterval(s.int64())
913+
src := timeutil.MicrosecondsToDuration(s.int64())
914914
*v = &src
915915
}
916916
case **string:

internal/table/scanner/scanner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func valueFromPrimitiveTypeID(c *column, r xrand.Rand) (*Ydb.Value, interface{})
250250
Int64Value: v,
251251
},
252252
}
253-
src := timeutil.UnmarshalInterval(v)
253+
src := timeutil.MicrosecondsToDuration(v)
254254
if c.optional && !c.testDefault {
255255
vp := &src
256256
return ydbval, &vp

internal/timeutil/time.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ const (
1717

1818
var unix = time.Unix(0, 0)
1919

20-
// UnmarshalInterval up to ±292 years.
21-
func UnmarshalInterval(n int64) time.Duration {
22-
return time.Duration(n)
20+
// MicrosecondsToDuration returns time.Duration from given microseconds
21+
func MicrosecondsToDuration(n int64) time.Duration {
22+
return time.Duration(n) * time.Microsecond
2323
}
2424

25-
func MarshalInterval(d time.Duration) int64 {
26-
return int64(d)
25+
// DurationToMicroseconds returns microseconds from given time.Duration
26+
func DurationToMicroseconds(d time.Duration) int64 {
27+
return int64(d / time.Microsecond)
2728
}
2829

2930
// UnmarshalDate up to 11761191-01-20 00:00:00 +0000 UTC.

internal/value/value.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ func TimestampValue(v uint64) Value {
339339
}
340340
}
341341

342+
// IntervalValue makes Value from given microseconds value
342343
func IntervalValue(v int64) Value {
343344
return Value{
344345
t: TypeInterval,

table/table_regress_test.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"context"
88
"os"
99
"testing"
10+
"time"
1011

1112
"github.com/stretchr/testify/require"
1213

13-
"github.com/ydb-platform/ydb-go-sdk/v3/table"
14-
1514
"github.com/ydb-platform/ydb-go-sdk/v3"
15+
"github.com/ydb-platform/ydb-go-sdk/v3/table"
16+
"github.com/ydb-platform/ydb-go-sdk/v3/table/types"
1617
)
1718

1819
type issue229Struct struct{}
@@ -49,3 +50,31 @@ func connect(t *testing.T) ydb.Connection {
4950
require.NoError(t, err)
5051
return db
5152
}
53+
54+
func TestIssue259IntervalFromDuration(t *testing.T) {
55+
// https://github.com/ydb-platform/ydb-go-sdk/issues/259
56+
ctx, cancel := context.WithCancel(context.Background())
57+
defer cancel()
58+
59+
db := connect(t)
60+
err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error {
61+
res, err := tx.Execute(ctx, `DECLARE $ts as Interval;
62+
$ten_micro = CAST(10 as Interval);
63+
SELECT $ts == $ten_micro, $ten_micro;`, table.NewQueryParameters(
64+
table.ValueParam(`$ts`, types.IntervalValueFromDuration(10*time.Microsecond)),
65+
))
66+
require.NoError(t, err)
67+
require.NoError(t, res.NextResultSetErr(ctx))
68+
require.True(t, res.NextRow())
69+
70+
var (
71+
val bool
72+
tenMicro time.Duration
73+
)
74+
require.NoError(t, res.Scan(&val, &tenMicro))
75+
require.True(t, val)
76+
require.Equal(t, 10*time.Microsecond, tenMicro)
77+
return nil
78+
}, table.WithTxSettings(table.TxSettings(table.WithSerializableReadWrite())))
79+
require.NoError(t, err)
80+
}

table/types/value.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,56 +42,104 @@ func DatetimeValue(v uint32) Value { return value.DatetimeValue(v) }
4242

4343
func TimestampValue(v uint64) Value { return value.TimestampValue(v) }
4444

45+
// IntervalValue makes Value from given microseconds value
4546
func IntervalValue(v int64) Value { return value.IntervalValue(v) }
4647

48+
// TzDateValue makes TzDate value from string
4749
func TzDateValue(v string) Value { return value.TzDateValue(v) }
4850

51+
// TzDatetimeValue makes TzDatetime value from string
4952
func TzDatetimeValue(v string) Value { return value.TzDatetimeValue(v) }
5053

54+
// TzTimestampValue makes TzTimestamp value from string
5155
func TzTimestampValue(v string) Value { return value.TzTimestampValue(v) }
5256

57+
// DateValueFromTime makes Date value from time.Time
58+
//
59+
// Warning: all *From* helpers will be removed at next major release
60+
// (functional will be implements with go1.18 type lists)
5361
func DateValueFromTime(v time.Time) Value { return value.DateValue(timeutil.MarshalDate(v)) }
5462

63+
// DatetimeValueFromTime makes Datetime value from time.Time
64+
//
65+
// Warning: all *From* helpers will be removed at next major release
66+
// (functional will be implements with go1.18 type lists)
5567
func DatetimeValueFromTime(v time.Time) Value {
5668
return value.DatetimeValue(timeutil.MarshalDatetime(v))
5769
}
5870

71+
// TimestampValueFromTime makes Timestamp value from time.Time
72+
//
73+
// Warning: all *From* helpers will be removed at next major release
74+
// (functional will be implements with go1.18 type lists)
5975
func TimestampValueFromTime(v time.Time) Value {
6076
return value.TimestampValue(timeutil.MarshalTimestamp(v))
6177
}
6278

79+
// IntervalValueFromDuration makes Interval value from time.Duration
80+
//
81+
// Warning: all *From* helpers will be removed at next major release
82+
// (functional will be implements with go1.18 type lists)
6383
func IntervalValueFromDuration(v time.Duration) Value {
64-
return value.IntervalValue(timeutil.MarshalInterval(v))
84+
return value.IntervalValue(timeutil.DurationToMicroseconds(v))
6585
}
6686

87+
// TzDateValueFromTime makes TzDate value from time.Time
88+
//
89+
// Warning: all *From* helpers will be removed at next major release
90+
// (functional will be implements with go1.18 type lists)
6791
func TzDateValueFromTime(v time.Time) Value { return value.TzDateValue(timeutil.MarshalTzDate(v)) }
6892

93+
// TzDatetimeValueFromTime makes TzDatetime value from time.Time
94+
//
95+
// Warning: all *From* helpers will be removed at next major release
96+
// (functional will be implements with go1.18 type lists)
6997
func TzDatetimeValueFromTime(v time.Time) Value {
7098
return value.TzDatetimeValue(timeutil.MarshalTzDatetime(v))
7199
}
72100

101+
// TzTimestampValueFromTime makes TzTimestamp value from time.Time
102+
//
103+
// Warning: all *From* helpers will be removed at next major release
104+
// (functional will be implements with go1.18 type lists)
73105
func TzTimestampValueFromTime(v time.Time) Value {
74106
return value.TzTimestampValue(timeutil.MarshalTzTimestamp(v))
75107
}
76108

77109
func StringValue(v []byte) Value { return value.StringValue(v) }
78110

111+
// StringValueFromString makes String value from string
112+
//
113+
// Warning: all *From* helpers will be removed at next major release
114+
// (functional will be implements with go1.18 type lists)
79115
func StringValueFromString(v string) Value { return value.StringValue([]byte(v)) }
80116

81117
func UTF8Value(v string) Value { return value.UTF8Value(v) }
82118

83119
func YSONValue(v string) Value { return value.YSONValue(v) }
84120

121+
// YSONValueFromBytes makes YSON value from bytes
122+
//
123+
// Warning: all *From* helpers will be removed at next major release
124+
// (functional will be implements with go1.18 type lists)
85125
func YSONValueFromBytes(v []byte) Value { return value.YSONValue(string(v)) }
86126

87127
func JSONValue(v string) Value { return value.JSONValue(v) }
88128

129+
// JSONValueFromBytes makes JSON value from bytes
130+
//
131+
// Warning: all *From* helpers will be removed at next major release
132+
// (functional will be implements with go1.18 type lists)
89133
func JSONValueFromBytes(v []byte) Value { return value.JSONValue(string(v)) }
90134

91135
func UUIDValue(v [16]byte) Value { return value.UUIDValue(v) }
92136

93137
func JSONDocumentValue(v string) Value { return value.JSONDocumentValue(v) }
94138

139+
// JSONDocumentValueFromBytes makes JSONDocument value from bytes
140+
//
141+
// Warning: all *From* helpers will be removed at next major release
142+
// (functional will be implements with go1.18 type lists)
95143
func JSONDocumentValueFromBytes(v []byte) Value { return value.JSONDocumentValue(string(v)) }
96144

97145
func DyNumberValue(v string) Value { return value.DyNumberValue(v) }

0 commit comments

Comments
 (0)