Skip to content

Commit 3354291

Browse files
ovrMazterQyou
andauthored
refactor(cubesql): Extract TimestampValue to pg-srv crate (cube-js#9848)
- Moved TimestampValue from cubesql dataframe to pg-srv where it belongs - Extracted ProtocolError to its own file for cleaner organization - Created values/ folder to group IntervalValue and TimestampValue together --------- Co-authored-by: Alex Qyoun-ae <[email protected]>
1 parent 5d1b009 commit 3354291

File tree

13 files changed

+599
-452
lines changed

13 files changed

+599
-452
lines changed

packages/cubejs-backend-native/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubenativeutils/Cargo.lock

Lines changed: 25 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/Cargo.lock

Lines changed: 47 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/cubesql/cubesql/src/sql/dataframe.rs

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
use chrono::{
2-
format::{
3-
Fixed, Item,
4-
Numeric::{Day, Hour, Minute, Month, Second, Year},
5-
Pad::Zero,
6-
},
7-
prelude::*,
8-
};
9-
use chrono_tz::Tz;
1+
use chrono::prelude::*;
102
use comfy_table::{Cell, Table};
113
use datafusion::arrow::{
124
array::{
@@ -18,15 +10,10 @@ use datafusion::arrow::{
1810
},
1911
datatypes::{DataType, IntervalUnit, Schema, TimeUnit},
2012
record_batch::RecordBatch,
21-
temporal_conversions,
2213
};
23-
use pg_srv::IntervalValue;
2414
use rust_decimal::prelude::*;
2515
use serde::{Serialize, Serializer};
26-
use std::{
27-
fmt::{self, Debug, Formatter},
28-
io,
29-
};
16+
use std::fmt::Debug;
3017

3118
use super::{ColumnFlags, ColumnType};
3219
use crate::CubeError;
@@ -88,6 +75,10 @@ impl Row {
8875
}
8976
}
9077

78+
// Type aliases for compatibility - actual implementations are in pg-srv
79+
pub type IntervalValue = pg_srv::IntervalValue;
80+
pub type TimestampValue = pg_srv::TimestampValue;
81+
9182
#[derive(Debug)]
9283
pub enum TableValue {
9384
Null,
@@ -207,83 +198,6 @@ impl DataFrame {
207198
}
208199
}
209200

210-
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
211-
pub struct TimestampValue {
212-
unix_nano: i64,
213-
tz: Option<String>,
214-
}
215-
216-
impl TimestampValue {
217-
pub fn new(mut unix_nano: i64, tz: Option<String>) -> TimestampValue {
218-
// This is a hack to workaround a mismatch between on-disk and in-memory representations.
219-
// We use millisecond precision on-disk.
220-
unix_nano -= unix_nano % 1000;
221-
TimestampValue { unix_nano, tz }
222-
}
223-
224-
pub fn to_naive_datetime(&self) -> NaiveDateTime {
225-
assert!(self.tz.is_none());
226-
227-
temporal_conversions::timestamp_ns_to_datetime(self.unix_nano)
228-
}
229-
230-
pub fn to_fixed_datetime(&self) -> io::Result<DateTime<Tz>> {
231-
assert!(self.tz.is_some());
232-
233-
let tz = self
234-
.tz
235-
.as_ref()
236-
.unwrap()
237-
.parse::<Tz>()
238-
.map_err(|err| io::Error::new(io::ErrorKind::Other, err.to_string()))?;
239-
240-
let ndt = temporal_conversions::timestamp_ns_to_datetime(self.unix_nano);
241-
Ok(tz.from_utc_datetime(&ndt))
242-
}
243-
244-
pub fn tz_ref(&self) -> &Option<String> {
245-
&self.tz
246-
}
247-
248-
pub fn get_time_stamp(&self) -> i64 {
249-
self.unix_nano
250-
}
251-
}
252-
253-
impl Debug for TimestampValue {
254-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
255-
f.debug_struct("TimestampValue")
256-
.field("unix_nano", &self.unix_nano)
257-
.field("tz", &self.tz)
258-
.field("str", &self.to_string())
259-
.finish()
260-
}
261-
}
262-
263-
impl ToString for TimestampValue {
264-
fn to_string(&self) -> String {
265-
Utc.timestamp_nanos(self.unix_nano)
266-
.format_with_items(
267-
[
268-
Item::Numeric(Year, Zero),
269-
Item::Literal("-"),
270-
Item::Numeric(Month, Zero),
271-
Item::Literal("-"),
272-
Item::Numeric(Day, Zero),
273-
Item::Literal("T"),
274-
Item::Numeric(Hour, Zero),
275-
Item::Literal(":"),
276-
Item::Numeric(Minute, Zero),
277-
Item::Literal(":"),
278-
Item::Numeric(Second, Zero),
279-
Item::Fixed(Fixed::Nanosecond3),
280-
]
281-
.iter(),
282-
)
283-
.to_string()
284-
}
285-
}
286-
287201
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
288202
pub struct Decimal128Value {
289203
n: i128,

0 commit comments

Comments
 (0)