Skip to content

Commit b3a47b8

Browse files
committed
Better timestamp support with timestamp[us] instead of timestamp[ns] (~290 000 years vs. 585 years respectively)
timestamp_nanos_opt() support only dates between 1677 and 2262
1 parent ffba3d2 commit b3a47b8

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

connectorx/src/destinations/arrow/arrow_assoc.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,7 @@ fn naive_date_to_arrow(nd: NaiveDate) -> i32 {
288288
}
289289

290290
fn naive_datetime_to_arrow(nd: NaiveDateTime) -> i64 {
291-
nd.and_utc()
292-
.timestamp_nanos_opt()
293-
.unwrap_or_else(|| panic!("out of range DateTime"))
291+
nd.and_utc().timestamp_micros()
294292
}
295293

296294
impl ArrowAssoc for Option<NaiveDate> {
@@ -328,10 +326,10 @@ impl ArrowAssoc for NaiveDate {
328326
}
329327

330328
impl ArrowAssoc for Option<NaiveDateTime> {
331-
type Builder = TimestampNanosecondBuilder;
329+
type Builder = TimestampMicrosecondBuilder;
332330

333331
fn builder(nrows: usize) -> Self::Builder {
334-
TimestampNanosecondBuilder::with_capacity(nrows)
332+
TimestampMicrosecondBuilder::with_capacity(nrows)
335333
}
336334

337335
fn append(builder: &mut Self::Builder, value: Option<NaiveDateTime>) -> Result<()> {
@@ -342,17 +340,17 @@ impl ArrowAssoc for Option<NaiveDateTime> {
342340
fn field(header: &str) -> Field {
343341
Field::new(
344342
header,
345-
ArrowDataType::Timestamp(TimeUnit::Nanosecond, None),
343+
ArrowDataType::Timestamp(TimeUnit::Microsecond, None),
346344
true,
347345
)
348346
}
349347
}
350348

351349
impl ArrowAssoc for NaiveDateTime {
352-
type Builder = TimestampNanosecondBuilder;
350+
type Builder = TimestampMicrosecondBuilder;
353351

354352
fn builder(nrows: usize) -> Self::Builder {
355-
TimestampNanosecondBuilder::with_capacity(nrows)
353+
TimestampMicrosecondBuilder::with_capacity(nrows)
356354
}
357355

358356
fn append(builder: &mut Self::Builder, value: NaiveDateTime) -> Result<()> {
@@ -363,7 +361,7 @@ impl ArrowAssoc for NaiveDateTime {
363361
fn field(header: &str) -> Field {
364362
Field::new(
365363
header,
366-
ArrowDataType::Timestamp(TimeUnit::Nanosecond, None),
364+
ArrowDataType::Timestamp(TimeUnit::Microsecond, None),
367365
false,
368366
)
369367
}

connectorx/src/destinations/arrowstream/arrow_assoc.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,7 @@ fn naive_date_to_arrow(nd: NaiveDate) -> i32 {
285285
}
286286

287287
fn naive_datetime_to_arrow(nd: NaiveDateTime) -> i64 {
288-
nd.and_utc()
289-
.timestamp_nanos_opt()
290-
.unwrap_or_else(|| panic!("out of range DateTime"))
288+
nd.and_utc().timestamp_micros()
291289
}
292290

293291
impl ArrowAssoc for Option<NaiveDate> {
@@ -325,10 +323,10 @@ impl ArrowAssoc for NaiveDate {
325323
}
326324

327325
impl ArrowAssoc for Option<NaiveDateTime> {
328-
type Builder = TimestampNanosecondBuilder;
326+
type Builder = TimestampMicrosecondBuilder;
329327

330328
fn builder(nrows: usize) -> Self::Builder {
331-
TimestampNanosecondBuilder::with_capacity(nrows)
329+
TimestampMicrosecondBuilder::with_capacity(nrows)
332330
}
333331

334332
fn append(builder: &mut Self::Builder, value: Option<NaiveDateTime>) -> Result<()> {
@@ -339,17 +337,17 @@ impl ArrowAssoc for Option<NaiveDateTime> {
339337
fn field(header: &str) -> Field {
340338
Field::new(
341339
header,
342-
ArrowDataType::Timestamp(TimeUnit::Nanosecond, None),
340+
ArrowDataType::Timestamp(TimeUnit::Microsecond, None),
343341
true,
344342
)
345343
}
346344
}
347345

348346
impl ArrowAssoc for NaiveDateTime {
349-
type Builder = TimestampNanosecondBuilder;
347+
type Builder = TimestampMicrosecondBuilder;
350348

351349
fn builder(nrows: usize) -> Self::Builder {
352-
TimestampNanosecondBuilder::with_capacity(nrows)
350+
TimestampMicrosecondBuilder::with_capacity(nrows)
353351
}
354352

355353
fn append(builder: &mut Self::Builder, value: NaiveDateTime) -> Result<()> {
@@ -360,7 +358,7 @@ impl ArrowAssoc for NaiveDateTime {
360358
fn field(header: &str) -> Field {
361359
Field::new(
362360
header,
363-
ArrowDataType::Timestamp(TimeUnit::Nanosecond, None),
361+
ArrowDataType::Timestamp(TimeUnit::Microsecond, None),
364362
false,
365363
)
366364
}

0 commit comments

Comments
 (0)