@@ -40,7 +40,7 @@ pub struct DateHistogramAggregationReq {
40
40
date_interval : Option < String > ,
41
41
/// The field to aggregate on.
42
42
pub field : String ,
43
- /// The format to format dates.
43
+ /// The format to format dates. Unsupported currently.
44
44
pub format : Option < String > ,
45
45
/// The interval to chunk your data range. Each bucket spans a value range of
46
46
/// [0..fixed_interval). Accepted values
@@ -77,7 +77,7 @@ pub struct DateHistogramAggregationReq {
77
77
/// hard_bounds only limits the buckets, to force a range set both extended_bounds and
78
78
/// hard_bounds to the same range.
79
79
///
80
- /// Needs to be provided as timestamp in microseconds precision.
80
+ /// Needs to be provided as timestamp in nanosecond precision.
81
81
///
82
82
/// ## Example
83
83
/// ```json
@@ -88,7 +88,7 @@ pub struct DateHistogramAggregationReq {
88
88
/// "interval": "1d",
89
89
/// "hard_bounds": {
90
90
/// "min": 0,
91
- /// "max": 1420502400000000
91
+ /// "max": 1420502400000000000
92
92
/// }
93
93
/// }
94
94
/// }
@@ -114,11 +114,11 @@ impl DateHistogramAggregationReq {
114
114
self . validate ( ) ?;
115
115
Ok ( HistogramAggregation {
116
116
field : self . field . to_string ( ) ,
117
- interval : parse_into_microseconds ( self . fixed_interval . as_ref ( ) . unwrap ( ) ) ? as f64 ,
117
+ interval : parse_into_nanoseconds ( self . fixed_interval . as_ref ( ) . unwrap ( ) ) ? as f64 ,
118
118
offset : self
119
119
. offset
120
120
. as_ref ( )
121
- . map ( |offset| parse_offset_into_microseconds ( offset) )
121
+ . map ( |offset| parse_offset_into_nanosecs ( offset) )
122
122
. transpose ( ) ?
123
123
. map ( |el| el as f64 ) ,
124
124
min_doc_count : self . min_doc_count ,
@@ -155,7 +155,7 @@ impl DateHistogramAggregationReq {
155
155
) ) ;
156
156
}
157
157
158
- parse_into_microseconds ( self . fixed_interval . as_ref ( ) . unwrap ( ) ) ?;
158
+ parse_into_nanoseconds ( self . fixed_interval . as_ref ( ) . unwrap ( ) ) ?;
159
159
160
160
Ok ( ( ) )
161
161
}
@@ -176,9 +176,12 @@ pub enum DateHistogramParseError {
176
176
/// Offset invalid
177
177
#[ error( "passed offset is invalid {0:?}" ) ]
178
178
InvalidOffset ( String ) ,
179
+ /// Value out of bounds
180
+ #[ error( "passed value is out of bounds: {0:?}" ) ]
181
+ OutOfBounds ( String ) ,
179
182
}
180
183
181
- fn parse_offset_into_microseconds ( input : & str ) -> Result < i64 , AggregationError > {
184
+ fn parse_offset_into_nanosecs ( input : & str ) -> Result < i64 , AggregationError > {
182
185
let is_sign = |byte| & [ byte] == b"-" || & [ byte] == b"+" ;
183
186
if input. is_empty ( ) {
184
187
return Err ( DateHistogramParseError :: InvalidOffset ( input. to_string ( ) ) . into ( ) ) ;
@@ -187,18 +190,18 @@ fn parse_offset_into_microseconds(input: &str) -> Result<i64, AggregationError>
187
190
let has_sign = is_sign ( input. as_bytes ( ) [ 0 ] ) ;
188
191
if has_sign {
189
192
let ( sign, input) = input. split_at ( 1 ) ;
190
- let val = parse_into_microseconds ( input) ?;
193
+ let val = parse_into_nanoseconds ( input) ?;
191
194
if sign == "-" {
192
195
Ok ( -val)
193
196
} else {
194
197
Ok ( val)
195
198
}
196
199
} else {
197
- parse_into_microseconds ( input)
200
+ parse_into_nanoseconds ( input)
198
201
}
199
202
}
200
203
201
- fn parse_into_microseconds ( input : & str ) -> Result < i64 , AggregationError > {
204
+ fn parse_into_nanoseconds ( input : & str ) -> Result < i64 , AggregationError > {
202
205
let split_boundary = input
203
206
. as_bytes ( )
204
207
. iter ( )
@@ -226,7 +229,11 @@ fn parse_into_microseconds(input: &str) -> Result<i64, AggregationError> {
226
229
_ => return Err ( DateHistogramParseError :: UnitNotRecognized ( unit. to_string ( ) ) . into ( ) ) ,
227
230
} ;
228
231
229
- Ok ( number * multiplier_from_unit * 1000 )
232
+ let val = ( number * multiplier_from_unit)
233
+ . checked_mul ( 1_000_000 )
234
+ . ok_or_else ( || DateHistogramParseError :: OutOfBounds ( input. to_string ( ) ) ) ?;
235
+
236
+ Ok ( val)
230
237
}
231
238
232
239
#[ cfg( test) ]
@@ -241,49 +248,49 @@ mod tests {
241
248
use crate :: Index ;
242
249
243
250
#[ test]
244
- fn test_parse_into_microseconds ( ) {
245
- assert_eq ! ( parse_into_microseconds ( "1m" ) . unwrap( ) , 60_000_000 ) ;
246
- assert_eq ! ( parse_into_microseconds ( "2m" ) . unwrap( ) , 120_000_000 ) ;
251
+ fn test_parse_into_nanosecs ( ) {
252
+ assert_eq ! ( parse_into_nanoseconds ( "1m" ) . unwrap( ) , 60_000_000_000 ) ;
253
+ assert_eq ! ( parse_into_nanoseconds ( "2m" ) . unwrap( ) , 120_000_000_000 ) ;
247
254
assert_eq ! (
248
- parse_into_microseconds ( "2y" ) . unwrap_err( ) ,
255
+ parse_into_nanoseconds ( "2y" ) . unwrap_err( ) ,
249
256
DateHistogramParseError :: UnitNotRecognized ( "y" . to_string( ) ) . into( )
250
257
) ;
251
258
assert_eq ! (
252
- parse_into_microseconds ( "2000" ) . unwrap_err( ) ,
259
+ parse_into_nanoseconds ( "2000" ) . unwrap_err( ) ,
253
260
DateHistogramParseError :: UnitMissing ( "2000" . to_string( ) ) . into( )
254
261
) ;
255
262
assert_eq ! (
256
- parse_into_microseconds ( "ms" ) . unwrap_err( ) ,
263
+ parse_into_nanoseconds ( "ms" ) . unwrap_err( ) ,
257
264
DateHistogramParseError :: NumberMissing ( "ms" . to_string( ) ) . into( )
258
265
) ;
259
266
}
260
267
261
268
#[ test]
262
- fn test_parse_offset_into_microseconds ( ) {
263
- assert_eq ! ( parse_offset_into_microseconds ( "1m" ) . unwrap( ) , 60_000_000 ) ;
264
- assert_eq ! ( parse_offset_into_microseconds ( "+1m" ) . unwrap( ) , 60_000_000 ) ;
265
- assert_eq ! ( parse_offset_into_microseconds ( "-1m" ) . unwrap( ) , -60_000_000 ) ;
266
- assert_eq ! ( parse_offset_into_microseconds ( "2m" ) . unwrap( ) , 120_000_000 ) ;
267
- assert_eq ! ( parse_offset_into_microseconds ( "+2m" ) . unwrap( ) , 120_000_000 ) ;
268
- assert_eq ! ( parse_offset_into_microseconds ( "-2m" ) . unwrap( ) , -120_000_000 ) ;
269
- assert_eq ! ( parse_offset_into_microseconds ( "-2ms" ) . unwrap( ) , -2_000 ) ;
269
+ fn test_parse_offset_into_nanosecs ( ) {
270
+ assert_eq ! ( parse_offset_into_nanosecs ( "1m" ) . unwrap( ) , 60_000_000_000 ) ;
271
+ assert_eq ! ( parse_offset_into_nanosecs ( "+1m" ) . unwrap( ) , 60_000_000_000 ) ;
272
+ assert_eq ! ( parse_offset_into_nanosecs ( "-1m" ) . unwrap( ) , -60_000_000_000 ) ;
273
+ assert_eq ! ( parse_offset_into_nanosecs ( "2m" ) . unwrap( ) , 120_000_000_000 ) ;
274
+ assert_eq ! ( parse_offset_into_nanosecs ( "+2m" ) . unwrap( ) , 120_000_000_000 ) ;
275
+ assert_eq ! ( parse_offset_into_nanosecs ( "-2m" ) . unwrap( ) , -120_000_000_000 ) ;
276
+ assert_eq ! ( parse_offset_into_nanosecs ( "-2ms" ) . unwrap( ) , -2_000_000 ) ;
270
277
assert_eq ! (
271
- parse_offset_into_microseconds ( "2y" ) . unwrap_err( ) ,
278
+ parse_offset_into_nanosecs ( "2y" ) . unwrap_err( ) ,
272
279
DateHistogramParseError :: UnitNotRecognized ( "y" . to_string( ) ) . into( )
273
280
) ;
274
281
assert_eq ! (
275
- parse_offset_into_microseconds ( "2000" ) . unwrap_err( ) ,
282
+ parse_offset_into_nanosecs ( "2000" ) . unwrap_err( ) ,
276
283
DateHistogramParseError :: UnitMissing ( "2000" . to_string( ) ) . into( )
277
284
) ;
278
285
assert_eq ! (
279
- parse_offset_into_microseconds ( "ms" ) . unwrap_err( ) ,
286
+ parse_offset_into_nanosecs ( "ms" ) . unwrap_err( ) ,
280
287
DateHistogramParseError :: NumberMissing ( "ms" . to_string( ) ) . into( )
281
288
) ;
282
289
}
283
290
284
291
#[ test]
285
292
fn test_parse_into_milliseconds_do_not_accept_non_ascii ( ) {
286
- assert ! ( parse_into_microseconds ( "1m" ) . is_err( ) ) ;
293
+ assert ! ( parse_into_nanoseconds ( "1m" ) . is_err( ) ) ;
287
294
}
288
295
289
296
pub fn get_test_index_from_docs (
@@ -361,7 +368,7 @@ mod tests {
361
368
"buckets" : [
362
369
{
363
370
"key_as_string" : "2015-01-01T00:00:00Z" ,
364
- "key" : 1420070400000000 .0,
371
+ "key" : 1420070400000000000 .0,
365
372
"doc_count" : 4
366
373
}
367
374
]
@@ -397,7 +404,7 @@ mod tests {
397
404
"buckets" : [
398
405
{
399
406
"key_as_string" : "2015-01-01T00:00:00Z" ,
400
- "key" : 1420070400000000 .0,
407
+ "key" : 1420070400000000000 .0,
401
408
"doc_count" : 4 ,
402
409
"texts" : {
403
410
"buckets" : [
@@ -444,32 +451,32 @@ mod tests {
444
451
"buckets" : [
445
452
{
446
453
"doc_count" : 2 ,
447
- "key" : 1420070400000000 .0,
454
+ "key" : 1420070400000000000 .0,
448
455
"key_as_string" : "2015-01-01T00:00:00Z"
449
456
} ,
450
457
{
451
458
"doc_count" : 1 ,
452
- "key" : 1420156800000000 .0,
459
+ "key" : 1420156800000000000 .0,
453
460
"key_as_string" : "2015-01-02T00:00:00Z"
454
461
} ,
455
462
{
456
463
"doc_count" : 0 ,
457
- "key" : 1420243200000000 .0,
464
+ "key" : 1420243200000000000 .0,
458
465
"key_as_string" : "2015-01-03T00:00:00Z"
459
466
} ,
460
467
{
461
468
"doc_count" : 0 ,
462
- "key" : 1420329600000000 .0,
469
+ "key" : 1420329600000000000 .0,
463
470
"key_as_string" : "2015-01-04T00:00:00Z"
464
471
} ,
465
472
{
466
473
"doc_count" : 0 ,
467
- "key" : 1420416000000000 .0,
474
+ "key" : 1420416000000000000 .0,
468
475
"key_as_string" : "2015-01-05T00:00:00Z"
469
476
} ,
470
477
{
471
478
"doc_count" : 1 ,
472
- "key" : 1420502400000000 .0,
479
+ "key" : 1420502400000000000 .0,
473
480
"key_as_string" : "2015-01-06T00:00:00Z"
474
481
}
475
482
]
0 commit comments