Skip to content

Commit

Permalink
now storing datetime with nanosecond precision
Browse files Browse the repository at this point in the history
  • Loading branch information
evanxg852000 committed Jul 7, 2022
1 parent 6fdf3b8 commit 17c2aca
Show file tree
Hide file tree
Showing 26 changed files with 477 additions and 1,258 deletions.
18 changes: 6 additions & 12 deletions examples/date_time_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@
//
// This example shows how the DateTime field can be used

use std::collections::HashSet;

use tantivy::collector::TopDocs;
use tantivy::query::QueryParser;
use tantivy::schema::{Cardinality, DateTimeOptions, Schema, Value, INDEXED, STORED, STRING};
use tantivy::{DateTimeFormat, DateTimePrecision, Index};
use tantivy::schema::{Cardinality, DateOptions, Schema, Value, INDEXED, STORED, STRING};
use tantivy::Index;

fn main() -> tantivy::Result<()> {
// # Defining the schema
let mut schema_builder = Schema::builder();
let mut date_formats = HashSet::new();
date_formats.insert(DateTimeFormat::ISO8601);
date_formats.insert(DateTimeFormat::Strftime("%Y-%m-%d %H:%M:%S".to_string()));
let opts = DateTimeOptions::from(INDEXED)
let opts = DateOptions::from(INDEXED)
.set_stored()
.set_fast(Cardinality::SingleValue)
.set_input_formats(date_formats)
.set_precision(tantivy::DateTimePrecision::Seconds);
let occurred_at = schema_builder.add_datetime_field("occurred_at", opts);
.set_precision(tantivy::DatePrecision::Seconds);
let occurred_at = schema_builder.add_date_field("occurred_at", opts);
let event_type = schema_builder.add_text_field("event", STRING | STORED);
let schema = schema_builder.build();

Expand Down Expand Up @@ -63,7 +57,7 @@ fn main() -> tantivy::Result<()> {
for (_score, doc_address) in count_docs {
let retrieved_doc = searcher.doc(doc_address)?;
assert!(matches!(retrieved_doc.get_first(occurred_at),
Some(Value::DateTime(dt)) if dt.get_precision() == DateTimePrecision::Seconds));
Some(Value::Date(dt)) if dt.into_timestamp_micros() == 12i64));
assert_eq!(
schema.to_json(&retrieved_doc),
r#"{"event":["comment"],"occurred_at":["2022-06-22T13:00:00Z"]}"#
Expand Down
32 changes: 16 additions & 16 deletions src/aggregation/bucket/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,14 @@ mod tests {

let buckets = collector.buckets;
assert_eq!(buckets[0].range.start, u64::MIN);
assert_eq!(buckets[0].range.end, 10f64.to_u64());
assert_eq!(buckets[1].range.start, 10f64.to_u64());
assert_eq!(buckets[1].range.end, 20f64.to_u64());
assert_eq!(buckets[0].range.end, 10f64.to_u64(None));
assert_eq!(buckets[1].range.start, 10f64.to_u64(None));
assert_eq!(buckets[1].range.end, 20f64.to_u64(None));
// Added bucket to fill hole
assert_eq!(buckets[2].range.start, 20f64.to_u64());
assert_eq!(buckets[2].range.end, 30f64.to_u64());
assert_eq!(buckets[3].range.start, 30f64.to_u64());
assert_eq!(buckets[3].range.end, 40f64.to_u64());
assert_eq!(buckets[2].range.start, 20f64.to_u64(None));
assert_eq!(buckets[2].range.end, 30f64.to_u64(None));
assert_eq!(buckets[3].range.start, 30f64.to_u64(None));
assert_eq!(buckets[3].range.end, 40f64.to_u64(None));
}

#[test]
Expand All @@ -484,10 +484,10 @@ mod tests {

let buckets = collector.buckets;
assert_eq!(buckets[0].range.start, u64::MIN);
assert_eq!(buckets[0].range.end, 10f64.to_u64());
assert_eq!(buckets[1].range.start, 10f64.to_u64());
assert_eq!(buckets[1].range.end, 20f64.to_u64());
assert_eq!(buckets[2].range.start, 20f64.to_u64());
assert_eq!(buckets[0].range.end, 10f64.to_u64(None));
assert_eq!(buckets[1].range.start, 10f64.to_u64(None));
assert_eq!(buckets[1].range.end, 20f64.to_u64(None));
assert_eq!(buckets[2].range.start, 20f64.to_u64(None));
assert_eq!(buckets[2].range.end, u64::MAX);
assert_eq!(buckets.len(), 3);
}
Expand Down Expand Up @@ -561,11 +561,11 @@ mod tests {
let search = |val: u64| collector.get_bucket_pos(val);

assert_eq!(search(u64::MIN), 0);
assert_eq!(search(9f64.to_u64()), 0);
assert_eq!(search(10f64.to_u64()), 1);
assert_eq!(search(11f64.to_u64()), 1);
assert_eq!(search(99f64.to_u64()), 1);
assert_eq!(search(100f64.to_u64()), 2);
assert_eq!(search(9f64.to_u64(None)), 0);
assert_eq!(search(10f64.to_u64(None)), 1);
assert_eq!(search(11f64.to_u64(None)), 1);
assert_eq!(search(99f64.to_u64(None)), 1);
assert_eq!(search(100f64.to_u64(None)), 2);
assert_eq!(search(u64::MAX - 1), 2); // Since the end range is never included,
// the max value
}
Expand Down
4 changes: 2 additions & 2 deletions src/aggregation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ pub(crate) fn f64_from_fastfield_u64(val: u64, field_type: &Type) -> f64 {
pub(crate) fn f64_to_fastfield_u64(val: f64, field_type: &Type) -> Option<u64> {
match field_type {
Type::U64 => Some(val as u64),
Type::I64 => Some((val as i64).to_u64()),
Type::F64 => Some(val.to_u64()),
Type::I64 => Some((val as i64).to_u64(None)),
Type::F64 => Some(val.to_u64(None)),
_ => None,
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/collector/histogram_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,9 @@ impl HistogramCollector {
num_buckets: usize,
) -> HistogramCollector {
let fast_type = TFastValue::to_type();
assert!(
fast_type == Type::U64
|| fast_type == Type::I64
|| fast_type == Type::Date
|| fast_type == Type::DateTime
);
assert!(fast_type == Type::U64 || fast_type == Type::I64 || fast_type == Type::Date);
HistogramCollector {
min_value: min_value.to_u64(),
min_value: min_value.to_u64(None),
num_buckets,
field,
divider: DividerU64::divide_by(bucket_width),
Expand All @@ -77,7 +72,7 @@ impl HistogramComputer {
return;
}
let delta = value - self.min_value;
let delta_u64 = delta.to_u64();
let delta_u64 = delta.to_u64(None);
let bucket_id: usize = self.divider.divide(delta_u64) as usize;
if bucket_id < self.counts.len() {
self.counts[bucket_id] += 1;
Expand Down Expand Up @@ -292,7 +287,7 @@ mod tests {
DateTime::from_primitive(
Date::from_calendar_date(1980, Month::January, 1)?.with_hms(0, 0, 0)?,
),
3600 * 24 * 365, // it is just for a unit test... sorry leap years.
3_600_000_000 * 24 * 365, // it is just for a unit test... sorry leap years.
10,
);
let week_histogram = searcher.search(&all_query, &week_histogram_collector)?;
Expand Down
Loading

0 comments on commit 17c2aca

Please sign in to comment.