Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ nom = "8.0.0"
num-traits = "0.2.19"
ordered-float = { version = "5.1.0", default-features = false }
rand = { version = "0.9.2", features = ["small_rng"] }
ryu = "1.0"
serde = "1.0"
serde_json = { version = "1.0", default-features = false, features = ["std"] }
zmij = "1.0"

[dev-dependencies]
goldenfile = "1.8"
Expand Down
11 changes: 8 additions & 3 deletions src/core/databend/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ impl ExtensionValue<'_> {
writer.write_all(&[EXTENSION_TIMESTAMP_TZ])?;
writer.write_all(&v.value.to_be_bytes())?;
writer.write_all(&v.offset.to_be_bytes())?;
Ok(10)
Ok(13)
}
ExtensionValue::Interval(v) => {
writer.write_all(&[EXTENSION_INTERVAL])?;
Expand Down Expand Up @@ -645,11 +645,16 @@ impl ExtensionValue<'_> {
ExtensionValue::Timestamp(Timestamp { value })
}
EXTENSION_TIMESTAMP_TZ => {
if len != 9 {
if len != 9 && len != 12 {
return Err(Error::InvalidJsonbNumber);
}
let value = i64::from_be_bytes(bytes[1..9].try_into().unwrap());
let offset = i8::from_be_bytes(bytes[9..10].try_into().unwrap());
let offset = if len == 9 {
let hours = i8::from_be_bytes(bytes[9..10].try_into().unwrap());
(hours as i32) * 3_600
} else {
i32::from_be_bytes(bytes[9..13].try_into().unwrap())
};

ExtensionValue::TimestampTz(TimestampTz { offset, value })
}
Expand Down
74 changes: 33 additions & 41 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const MICROS_PER_HOUR: i64 = 60 * MICROS_PER_MINUTE;
const MONTHS_PER_YEAR: i32 = 12;

const TIMESTAMP_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.6f";
const TIMESTAMP_TIMEZONE_FORMAT: &str = "%Y-%m-%d %H:%M:%S%.6f %z";

/// Represents extended JSON value types that are not supported in standard JSON.
///
Expand Down Expand Up @@ -80,8 +81,8 @@ pub struct Timestamp {
/// Standard JSON has no native timezone-aware timestamp type.
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct TimestampTz {
/// Timezone offset in hours from UTC
pub offset: i8,
/// Timezone offset in seconds from UTC
pub offset: i32,
/// Microseconds since Unix epoch (January 1, 1970 00:00:00 UTC)
pub value: i64,
}
Expand Down Expand Up @@ -149,58 +150,49 @@ impl Display for TimestampTz {
nanos = 0;
}
let ts = jiff::Timestamp::new(secs, nanos as i32).unwrap();
let tz = Offset::constant(self.offset).to_time_zone();
let tz_offset = Offset::from_seconds(self.offset).expect("invalid timezone offset seconds");
let tz = tz_offset.to_time_zone();
let zoned = ts.to_zoned(tz);

write!(f, "{}", strtime::format(TIMESTAMP_FORMAT, &zoned).unwrap())
write!(
f,
"{}",
strtime::format(TIMESTAMP_TIMEZONE_FORMAT, &zoned).unwrap()
)
}
}

impl Display for Interval {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
let mut date_parts = vec![];
let mut wrote_date_part = false;
let years = self.months / MONTHS_PER_YEAR;
let months = self.months % MONTHS_PER_YEAR;
match years.cmp(&1) {
Ordering::Equal => {
date_parts.push((years, "year"));
}
Ordering::Greater => {
date_parts.push((years, "years"));
}
_ => {}
}
match months.cmp(&1) {
Ordering::Equal => {
date_parts.push((months, "month"));
}
Ordering::Greater => {
date_parts.push((months, "months"));
}
_ => {}
}
match self.days.cmp(&1) {
Ordering::Equal => {
date_parts.push((self.days, "day"));
}
Ordering::Greater => {
date_parts.push((self.days, "days"));
}
_ => {}
}
if !date_parts.is_empty() {
for (i, (val, name)) in date_parts.into_iter().enumerate() {
if i > 0 {

let mut write_component = |value: i32, singular: &str, plural: &str| -> std::fmt::Result {
if value != 0 {
if wrote_date_part {
write!(f, " ")?;
}
write!(f, "{} {}", val, name)?;
}
if self.micros != 0 {
write!(f, " ")?;
let abs_val = value.abs();
let unit = if abs_val == 1 { singular } else { plural };
if value < 0 {
write!(f, "-{} {}", abs_val, unit)?;
} else {
write!(f, "{} {}", abs_val, unit)?;
}
wrote_date_part = true;
}
}
Ok(())
};

write_component(years, "year", "years")?;
write_component(months, "month", "months")?;
write_component(self.days, "day", "days")?;

if self.micros != 0 {
if wrote_date_part {
write!(f, " ")?;
}
let mut micros = self.micros;
if micros < 0 {
write!(f, "-")?;
Expand All @@ -221,7 +213,7 @@ impl Display for Interval {
if micros != 0 {
write!(f, ".{:06}", micros)?;
}
} else if self.months == 0 && self.days == 0 {
} else if !wrote_date_part {
write!(f, "00:00:00")?;
}
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/functions/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ impl RawJsonb<'_> {
/// use jsonb::Value;
///
/// // TimestampTz value
/// let timestamp_tz_value = Value::TimestampTz(TimestampTz { offset: 8, value: 1760140800000000 });
/// let timestamp_tz_value = Value::TimestampTz(TimestampTz { offset: 8 * 3600, value: 1760140800000000 });
/// let buf = timestamp_tz_value.to_vec();
/// let raw_jsonb = RawJsonb::new(&buf);
/// assert!(raw_jsonb.is_timestamp_tz().unwrap());
Expand Down Expand Up @@ -1830,10 +1830,10 @@ impl RawJsonb<'_> {
/// use jsonb::Value;
///
/// // TimestampTz value
/// let timestamp_tz_value = Value::TimestampTz(TimestampTz { offset: 8, value: 1760140800000000 });
/// let timestamp_tz_value = Value::TimestampTz(TimestampTz { offset: 8 * 3600, value: 1760140800000000 });
/// let buf = timestamp_tz_value.to_vec();
/// let raw_jsonb = RawJsonb::new(&buf);
/// assert_eq!(raw_jsonb.as_timestamp_tz().unwrap(), Some(TimestampTz { offset: 8, value: 1760140800000000 }));
/// assert_eq!(raw_jsonb.as_timestamp_tz().unwrap(), Some(TimestampTz { offset: 8 * 3600, value: 1760140800000000 }));
/// ```
pub fn as_timestamp_tz(&self) -> Result<Option<TimestampTz>> {
let jsonb_item = JsonbItem::from_raw_jsonb(*self)?;
Expand Down
2 changes: 1 addition & 1 deletion src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ impl Display for Number {
write!(f, "{}", s)
}
Number::Float64(v) => {
let mut buffer = ryu::Buffer::new();
let mut buffer = zmij::Buffer::new();
let s = buffer.format(*v);
write!(f, "{}", s)
}
Expand Down
10 changes: 9 additions & 1 deletion tests/it/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,18 @@ fn test_decode_extension() {
value: 1760140800000000,
}),
),
// Backward-compatible implementation with offset as int8 hours
(
b"\x20\0\0\0\x60\0\0\x0a\x30\0\x06\x40\xd6\xb7\x23\x80\0\x08".to_vec(),
Value::TimestampTz(TimestampTz {
offset: 8,
offset: 8 * 3600,
value: 1760140800000000,
}),
),
(
b"\x20\0\0\0\x60\0\0\x0d\x30\0\x06\x40\xd6\xb7\x23\x80\0\0\0\x70\x80".to_vec(),
Value::TimestampTz(TimestampTz {
offset: 8 * 3600,
value: 1760140800000000,
}),
),
Expand Down
12 changes: 6 additions & 6 deletions tests/it/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ fn test_encode_array() {
Value::Binary(&[100, 101, 102, 103]),
Value::Date(Date {value: 20381 }),
Value::Timestamp(Timestamp { value: 1540230120000000 }),
Value::TimestampTz(TimestampTz { offset: 8, value: 1670389100000000 }),
Value::TimestampTz(TimestampTz { offset: 8 * 3600, value: 1670389100000000 }),
Value::Interval(Interval { months: 2, days: 10, micros: 500000000 }),
Value::Number(Number::Decimal256(Decimal256 { scale: 2, value: I256::new(1234) })),
]).to_vec(),
b"\x80\0\0\x07\x30\0\0\0\x60\0\0\x05\x60\0\0\x05\x60\0\0\x09\x60\0\0\x0A\x60\0\0\x11\x20\0\0\x22\0\x64\x65\x66\x67\x10\0\0\x4F\x9D\x20\0\x05\x78\xD4\xC5\x2C\xCA\0\x30\0\x05\xEF\x35\xC4\xF1\x33\0\x08\x40\0\0\0\x02\0\0\0\x0A\0\0\0\0\x1D\xCD\x65\0\x70\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x04\xD2\x02",
b"\x80\0\0\x07\x30\0\0\0\x60\0\0\x05\x60\0\0\x05\x60\0\0\x09\x60\0\0\x0D\x60\0\0\x11\x20\0\0\x22\0\x64\x65\x66\x67\x10\0\0\x4F\x9D\x20\0\x05\x78\xD4\xC5\x2C\xCA\0\x30\0\x05\xEF\x35\xC4\xF1\x33\0\0\0\x70\x80\x40\0\0\0\x02\0\0\0\x0A\0\0\0\0\x1D\xCD\x65\0\x70\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x04\xD2\x02",
);
}

Expand All @@ -207,7 +207,7 @@ fn test_encode_object() {
obj2.insert(
"k5".to_string(),
Value::TimestampTz(TimestampTz {
offset: 8,
offset: 8 * 3600,
value: 1670389100000000,
}),
);
Expand All @@ -229,7 +229,7 @@ fn test_encode_object() {

assert_eq!(
&Value::Object(obj2).to_vec(),
b"\x40\0\0\x07\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x60\0\0\x05\x60\0\0\x05\x60\0\0\x09\x60\0\0\x0A\x60\0\0\x11\x20\0\0\x22\x6B\x31\x6B\x32\x6B\x33\x6B\x34\x6B\x35\x6B\x36\x6B\x37\x76\x31\0\xC8\xC9\xCA\xCB\x10\0\0\x4F\x9D\x20\0\x05\x78\xD4\xC5\x2C\xCA\0\x30\0\x05\xEF\x35\xC4\xF1\x33\0\x08\x40\0\0\0\x02\0\0\0\x0A\0\0\0\0\x1D\xCD\x65\0\x70\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x04\xD2\x02"
b"\x40\0\0\x07\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x10\0\0\x02\x60\0\0\x05\x60\0\0\x05\x60\0\0\x09\x60\0\0\x0D\x60\0\0\x11\x20\0\0\x22\x6B\x31\x6B\x32\x6B\x33\x6B\x34\x6B\x35\x6B\x36\x6B\x37\x76\x31\0\xC8\xC9\xCA\xCB\x10\0\0\x4F\x9D\x20\0\x05\x78\xD4\xC5\x2C\xCA\0\x30\0\x05\xEF\x35\xC4\xF1\x33\0\0\0\x70\x80\x40\0\0\0\x02\0\0\0\x0A\0\0\0\0\x1D\xCD\x65\0\x70\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x04\xD2\x02"
);
}

Expand All @@ -252,11 +252,11 @@ fn test_encode_extension() {
);
assert_eq!(
Value::TimestampTz(TimestampTz {
offset: 8,
offset: 8 * 3600,
value: 1760140800000000
})
.to_vec(),
b"\x20\0\0\0\x60\0\0\x0a\x30\0\x06\x40\xd6\xb7\x23\x80\0\x08"
b"\x20\0\0\0\x60\0\0\x0d\x30\0\x06\x40\xd6\xb7\x23\x80\0\0\0\x70\x80"
);
assert_eq!(
Value::Interval(Interval {
Expand Down
66 changes: 59 additions & 7 deletions tests/it/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,19 +826,32 @@ fn test_to_string() {

let extension_sources = vec![
(Value::Binary(&[97, 98, 99]), r#""616263""#),
(Value::Binary(&[]), r#""""#),
(Value::Date(Date { value: 90570 }), r#""2217-12-22""#),
(Value::Date(Date { value: -1 }), r#""1969-12-31""#),
(
Value::Timestamp(Timestamp {
value: 190390000000,
}),
r#""1970-01-03 04:53:10.000000""#,
),
(
Value::Timestamp(Timestamp { value: 0 }),
r#""1970-01-01 00:00:00.000000""#,
),
(
Value::TimestampTz(TimestampTz {
offset: 8,
offset: 8 * 3600,
value: 190390000000,
}),
r#""1970-01-03 12:53:10.000000""#,
r#""1970-01-03 12:53:10.000000 +0800""#,
),
(
Value::TimestampTz(TimestampTz {
offset: 90 * 60,
value: 0,
}),
r#""1970-01-01 01:30:00.000000 +0130""#,
),
(
Value::Interval(Interval {
Expand All @@ -848,13 +861,52 @@ fn test_to_string() {
}),
r#""10 months 20 days 00:05:00""#,
),
(
Value::Interval(Interval {
months: -14,
days: -3,
micros: -90000000,
}),
r#""-1 year -2 months -3 days -00:01:30""#,
),
(
Value::Interval(Interval {
months: -25,
days: -1,
micros: 0,
}),
r#""-2 years -1 month -1 day""#,
),
(
Value::Interval(Interval {
months: 0,
days: -2,
micros: 5_000_000,
}),
r#""-2 days 00:00:05""#,
),
(
Value::Interval(Interval {
months: 0,
days: 0,
micros: 0,
}),
r#""00:00:00""#,
),
(
Value::Number(Number::Decimal128(Decimal128 {
scale: 2,
value: 1234,
})),
r#"12.34"#,
),
(
Value::Number(Number::Decimal64(Decimal64 {
scale: 2,
value: -765,
})),
r#"-7.65"#,
),
(
Value::Number(Number::Decimal256(Decimal256 {
scale: 2,
Expand All @@ -870,7 +922,7 @@ fn test_to_string() {
value: 190390000000,
}),
Value::TimestampTz(TimestampTz {
offset: 8,
offset: 8 * 3600,
value: 190390000000,
}),
Value::Interval(Interval {
Expand All @@ -887,7 +939,7 @@ fn test_to_string() {
value: I256::new(981724),
})),
]),
r#"["616263","2217-12-22","1970-01-03 04:53:10.000000","1970-01-03 12:53:10.000000","10 months 20 days 00:05:00",12.34,9817.24]"#,
r#"["616263","2217-12-22","1970-01-03 04:53:10.000000","1970-01-03 12:53:10.000000 +0800","10 months 20 days 00:05:00",12.34,9817.24]"#,
),
(
Value::Object(BTreeMap::from([
Expand All @@ -902,7 +954,7 @@ fn test_to_string() {
(
"k4".to_string(),
Value::TimestampTz(TimestampTz {
offset: 8,
offset: 8 * 3600,
value: 190390000000,
}),
),
Expand All @@ -929,7 +981,7 @@ fn test_to_string() {
})),
),
])),
r#"{"k1":"616263","k2":"2217-12-22","k3":"1970-01-03 04:53:10.000000","k4":"1970-01-03 12:53:10.000000","k5":"10 months 20 days 00:05:00","k6":12.34,"k7":9817.24}"#,
r#"{"k1":"616263","k2":"2217-12-22","k3":"1970-01-03 04:53:10.000000","k4":"1970-01-03 12:53:10.000000 +0800","k5":"10 months 20 days 00:05:00","k6":12.34,"k7":9817.24}"#,
),
];

Expand Down Expand Up @@ -1117,7 +1169,7 @@ fn test_type_of() {
),
(
Value::TimestampTz(TimestampTz {
offset: 8,
offset: 8 * 3600,
value: 190390000000,
}),
"TIMESTAMP_TZ",
Expand Down
Loading