1use std::fmt;
12
13use crate::types::LogicalType;
14
15#[derive(Debug, Clone, PartialEq)]
22#[non_exhaustive]
23pub enum Value {
24 Null,
26 Boolean(bool),
28 TinyInt(i8),
30 SmallInt(i16),
32 Integer(i32),
34 BigInt(i64),
36 HugeInt(i128),
38 UTinyInt(u8),
40 USmallInt(u16),
42 UInteger(u32),
44 UBigInt(u64),
46 UHugeInt(u128),
48 Float(f32),
50 Double(f64),
52 Decimal {
54 unscaled: i128,
56 width: u8,
58 scale: u8,
60 },
61 Varchar(String),
63 Blob(Vec<u8>),
65 Date(i32),
67 Time(i64),
69 Timestamp(i64),
71 Interval {
77 months: i32,
79 days: i32,
81 micros: i64,
83 },
84 List {
86 element: LogicalType,
88 values: Vec<Value>,
90 },
91 Struct(Vec<(String, Value)>),
93}
94
95impl Value {
96 #[must_use]
107 pub fn footprint(&self) -> usize {
108 size_of::<Self>() + self.heap()
109 }
110
111 fn heap(&self) -> usize {
113 match self {
114 Self::Varchar(text) => text.capacity(),
115 Self::Blob(bytes) => bytes.capacity(),
116 Self::List { values, .. } => {
117 values.capacity() * size_of::<Self>() + values.iter().map(Self::heap).sum::<usize>()
118 }
119 Self::Struct(fields) => {
120 fields.capacity() * size_of::<(String, Self)>()
121 + fields
122 .iter()
123 .map(|(name, value)| name.capacity() + value.heap())
124 .sum::<usize>()
125 }
126 _ => 0,
127 }
128 }
129
130 #[must_use]
132 pub fn is_null(&self) -> bool {
133 matches!(self, Self::Null)
134 }
135
136 #[must_use]
138 pub fn logical_type(&self) -> LogicalType {
139 match self {
140 Self::Null => LogicalType::Null,
141 Self::Boolean(_) => LogicalType::Boolean,
142 Self::TinyInt(_) => LogicalType::TinyInt,
143 Self::SmallInt(_) => LogicalType::SmallInt,
144 Self::Integer(_) => LogicalType::Integer,
145 Self::BigInt(_) => LogicalType::BigInt,
146 Self::HugeInt(_) => LogicalType::HugeInt,
147 Self::UTinyInt(_) => LogicalType::UTinyInt,
148 Self::USmallInt(_) => LogicalType::USmallInt,
149 Self::UInteger(_) => LogicalType::UInteger,
150 Self::UBigInt(_) => LogicalType::UBigInt,
151 Self::UHugeInt(_) => LogicalType::UHugeInt,
152 Self::Float(_) => LogicalType::Float,
153 Self::Double(_) => LogicalType::Double,
154 Self::Decimal { width, scale, .. } => {
155 LogicalType::Decimal { width: *width, scale: *scale }
156 }
157 Self::Varchar(_) => LogicalType::Varchar,
158 Self::Blob(_) => LogicalType::Blob,
159 Self::Date(_) => LogicalType::Date,
160 Self::Time(_) => LogicalType::Time,
161 Self::Timestamp(_) => LogicalType::Timestamp,
162 Self::Interval { .. } => LogicalType::Interval,
163 Self::List { element, .. } => LogicalType::list(element.clone()),
164 Self::Struct(fields) => LogicalType::Struct(
165 fields
166 .iter()
167 .map(|(name, value)| crate::types::Field::new(name, value.logical_type()))
168 .collect(),
169 ),
170 }
171 }
172
173 #[must_use]
179 pub fn as_i64(&self) -> Option<i64> {
180 match *self {
181 Self::TinyInt(v) => Some(i64::from(v)),
182 Self::SmallInt(v) => Some(i64::from(v)),
183 Self::Integer(v) => Some(i64::from(v)),
184 Self::BigInt(v) => Some(v),
185 Self::UTinyInt(v) => Some(i64::from(v)),
186 Self::USmallInt(v) => Some(i64::from(v)),
187 Self::UInteger(v) => Some(i64::from(v)),
188 Self::UBigInt(v) => i64::try_from(v).ok(),
189 Self::HugeInt(v) => i64::try_from(v).ok(),
190 Self::UHugeInt(v) => i64::try_from(v).ok(),
191 _ => None,
192 }
193 }
194
195 #[must_use]
197 pub fn as_bool(&self) -> Option<bool> {
198 match *self {
199 Self::Boolean(v) => Some(v),
200 _ => None,
201 }
202 }
203
204 #[must_use]
206 pub fn as_str(&self) -> Option<&str> {
207 match self {
208 Self::Varchar(v) => Some(v),
209 _ => None,
210 }
211 }
212}
213
214impl fmt::Display for Value {
215 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216 match self {
217 Self::Null => f.write_str("NULL"),
218 Self::Boolean(v) => f.write_str(if *v { "true" } else { "false" }),
219 Self::TinyInt(v) => write!(f, "{v}"),
220 Self::SmallInt(v) => write!(f, "{v}"),
221 Self::Integer(v) => write!(f, "{v}"),
222 Self::BigInt(v) => write!(f, "{v}"),
223 Self::HugeInt(v) => write!(f, "{v}"),
224 Self::UTinyInt(v) => write!(f, "{v}"),
225 Self::USmallInt(v) => write!(f, "{v}"),
226 Self::UInteger(v) => write!(f, "{v}"),
227 Self::UBigInt(v) => write!(f, "{v}"),
228 Self::UHugeInt(v) => write!(f, "{v}"),
229 Self::Float(v) => write_float(f, *v),
230 Self::Double(v) => write_float(f, *v),
231 Self::Decimal { unscaled, scale, .. } => write_decimal(f, *unscaled, *scale),
232 Self::Varchar(v) => f.write_str(v),
233 Self::Blob(v) => write_blob(f, v),
234 Self::Date(v) => write_date(f, *v),
235 Self::Time(v) => write_time(f, *v),
236 Self::Timestamp(v) => write_timestamp(f, *v),
237 Self::Interval { months, days, micros } => write_interval(f, *months, *days, *micros),
238 Self::List { values, .. } => {
239 f.write_str("[")?;
240 for (index, value) in values.iter().enumerate() {
241 if index > 0 {
242 f.write_str(", ")?;
243 }
244 write!(f, "{value}")?;
245 }
246 f.write_str("]")
247 }
248 Self::Struct(fields) => {
249 f.write_str("{")?;
250 for (index, (name, value)) in fields.iter().enumerate() {
251 if index > 0 {
252 f.write_str(", ")?;
253 }
254 write!(f, "'{name}': {value}")?;
255 }
256 f.write_str("}")
257 }
258 }
259 }
260}
261
262trait Real: Copy + fmt::Display + fmt::LowerExp {
269 fn is_nan(self) -> bool;
270 fn is_infinite(self) -> bool;
271 fn is_sign_negative(self) -> bool;
272}
273
274impl Real for f32 {
275 fn is_nan(self) -> bool {
276 Self::is_nan(self)
277 }
278
279 fn is_infinite(self) -> bool {
280 Self::is_infinite(self)
281 }
282
283 fn is_sign_negative(self) -> bool {
284 Self::is_sign_negative(self)
285 }
286}
287
288impl Real for f64 {
289 fn is_nan(self) -> bool {
290 Self::is_nan(self)
291 }
292
293 fn is_infinite(self) -> bool {
294 Self::is_infinite(self)
295 }
296
297 fn is_sign_negative(self) -> bool {
298 Self::is_sign_negative(self)
299 }
300}
301
302fn write_float<T: Real>(f: &mut fmt::Formatter<'_>, value: T) -> fmt::Result {
312 if value.is_nan() {
313 return f.write_str("nan");
314 }
315 if value.is_infinite() {
316 return f.write_str(if value.is_sign_negative() { "-inf" } else { "inf" });
317 }
318 let scientific = format!("{value:e}");
319 let (mantissa, exponent) = scientific.split_once('e').unwrap_or((scientific.as_str(), "0"));
320 let exponent: i32 = exponent.parse().unwrap_or(0);
321 if (-4..16).contains(&exponent) {
322 let text = format!("{value}");
323 if text.contains('.') {
324 return f.write_str(&text);
325 }
326 return write!(f, "{text}.0");
327 }
328 let sign = if exponent < 0 { '-' } else { '+' };
329 write!(f, "{mantissa}e{sign}{:02}", exponent.abs())
330}
331
332fn write_decimal(f: &mut fmt::Formatter<'_>, unscaled: i128, scale: u8) -> fmt::Result {
333 if scale == 0 {
334 return write!(f, "{unscaled}");
335 }
336 let negative = unscaled < 0;
337 let digits = unscaled.unsigned_abs().to_string();
339 let scale = usize::from(scale);
340 let (whole, fraction) = if digits.len() > scale {
341 let split = digits.len() - scale;
342 (digits[..split].to_string(), digits[split..].to_string())
343 } else {
344 ("0".to_string(), format!("{:0>scale$}", digits))
345 };
346 if negative {
347 f.write_str("-")?;
348 }
349 write!(f, "{whole}.{fraction}")
350}
351
352fn write_blob(f: &mut fmt::Formatter<'_>, bytes: &[u8]) -> fmt::Result {
359 for &byte in bytes {
360 if (byte.is_ascii_graphic() || byte == b' ') && !matches!(byte, b'\\' | b'\'' | b'"') {
361 write!(f, "{}", byte as char)?;
362 } else {
363 write!(f, "\\x{byte:02X}")?;
364 }
365 }
366 Ok(())
367}
368
369#[must_use]
376pub fn civil_from_days(days: i32) -> (i32, u32, u32) {
377 let z = i64::from(days) + 719_468;
378 let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
379 let day_of_era = z - era * 146_097;
380 let year_of_era =
381 (day_of_era - day_of_era / 1460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
382 let year = year_of_era + era * 400;
383 let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
384 let shifted_month = (5 * day_of_year + 2) / 153;
385 let day = day_of_year - (153 * shifted_month + 2) / 5 + 1;
386 let month = if shifted_month < 10 { shifted_month + 3 } else { shifted_month - 9 };
387 let year = if month <= 2 { year + 1 } else { year };
388 #[expect(clippy::cast_possible_truncation, reason = "the ranges are 1 to 12 and 1 to 31")]
389 (year as i32, month as u32, day as u32)
390}
391
392#[must_use]
394pub fn days_from_civil(year: i32, month: u32, day: u32) -> i32 {
395 let year = i64::from(year) - i64::from(month <= 2);
396 let era = if year >= 0 { year } else { year - 399 } / 400;
397 let year_of_era = year - era * 400;
398 let month = i64::from(month);
399 let shifted_month = if month > 2 { month - 3 } else { month + 9 };
400 let day_of_year = (153 * shifted_month + 2) / 5 + i64::from(day) - 1;
401 let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
402 #[expect(clippy::cast_possible_truncation, reason = "a date in i32 range stays in i32 range")]
403 ((era * 146_097 + day_of_era - 719_468) as i32)
404}
405
406fn write_date(f: &mut fmt::Formatter<'_>, days: i32) -> fmt::Result {
407 let (year, month, day) = civil_from_days(days);
408 if year < 0 {
409 write!(f, "{:04}-{month:02}-{day:02} (BC)", -year + 1)
410 } else {
411 write!(f, "{year:04}-{month:02}-{day:02}")
412 }
413}
414
415fn write_time(f: &mut fmt::Formatter<'_>, micros: i64) -> fmt::Result {
416 let seconds = micros.div_euclid(1_000_000);
417 let fraction = micros.rem_euclid(1_000_000);
418 let (hours, minutes, seconds) = (seconds / 3600, (seconds / 60) % 60, seconds % 60);
419 write!(f, "{hours:02}:{minutes:02}:{seconds:02}")?;
420 if fraction != 0 {
421 let text = format!("{fraction:06}");
423 write!(f, ".{}", text.trim_end_matches('0'))?;
424 }
425 Ok(())
426}
427
428fn write_timestamp(f: &mut fmt::Formatter<'_>, micros: i64) -> fmt::Result {
429 const MICROS_PER_DAY: i64 = 86_400 * 1_000_000;
430 let days = micros.div_euclid(MICROS_PER_DAY);
431 let within_day = micros.rem_euclid(MICROS_PER_DAY);
432 let Ok(days) = i32::try_from(days) else {
433 return f.write_str("timestamp out of range");
434 };
435 write_date(f, days)?;
436 f.write_str(" ")?;
437 write_time(f, within_day)
438}
439
440fn write_interval(f: &mut fmt::Formatter<'_>, months: i32, days: i32, micros: i64) -> fmt::Result {
441 let mut wrote = false;
442 let space = |f: &mut fmt::Formatter<'_>, wrote: &mut bool| -> fmt::Result {
443 if *wrote {
444 f.write_str(" ")?;
445 }
446 *wrote = true;
447 Ok(())
448 };
449 let (years, rest_months) = (months / 12, months % 12);
450 if years != 0 {
451 space(f, &mut wrote)?;
452 write!(f, "{years} year{}", plural(years))?;
453 }
454 if rest_months != 0 {
455 space(f, &mut wrote)?;
456 write!(f, "{rest_months} month{}", plural(rest_months))?;
457 }
458 if days != 0 {
459 space(f, &mut wrote)?;
460 write!(f, "{days} day{}", plural(days))?;
461 }
462 if micros != 0 || !wrote {
463 space(f, &mut wrote)?;
464 if micros < 0 {
465 f.write_str("-")?;
466 }
467 write_time(f, micros.abs())?;
468 }
469 Ok(())
470}
471
472fn plural(n: i32) -> &'static str {
473 if n == 1 || n == -1 { "" } else { "s" }
474}
475
476#[cfg(test)]
477mod tests {
478 use super::{Value, civil_from_days, days_from_civil};
479 use crate::types::LogicalType;
480
481 #[test]
482 fn a_value_knows_its_own_type() {
483 assert_eq!(Value::Integer(1).logical_type(), LogicalType::Integer);
484 assert_eq!(Value::Null.logical_type(), LogicalType::Null);
485 let list = Value::List { element: LogicalType::Varchar, values: Vec::new() };
486 assert_eq!(list.logical_type(), LogicalType::list(LogicalType::Varchar));
489 }
490
491 #[test]
492 fn the_date_conversion_is_its_own_inverse() {
493 for days in days_from_civil(1600, 1, 1)..days_from_civil(2400, 1, 1) {
496 let (year, month, day) = civil_from_days(days);
497 assert_eq!(days_from_civil(year, month, day), days, "{year}-{month}-{day}");
498 }
499 }
500
501 #[test]
502 fn the_epoch_is_where_it_should_be() {
503 assert_eq!(days_from_civil(1970, 1, 1), 0);
504 assert_eq!(civil_from_days(0), (1970, 1, 1));
505 assert_eq!(Value::Date(0).to_string(), "1970-01-01");
506 assert_eq!(Value::Date(19_723).to_string(), "2024-01-01");
507 assert_eq!(Value::Date(19_737).to_string(), "2024-01-15");
508 }
509
510 #[test]
511 fn a_leap_day_is_a_day() {
512 assert_eq!(civil_from_days(days_from_civil(2024, 2, 29)), (2024, 2, 29));
513 assert_eq!(days_from_civil(1900, 3, 1) - days_from_civil(1900, 2, 28), 1);
516 assert_eq!(days_from_civil(2000, 3, 1) - days_from_civil(2000, 2, 28), 2);
517 }
518
519 #[test]
520 fn times_print_with_the_trailing_zeros_trimmed() {
521 assert_eq!(Value::Time(0).to_string(), "00:00:00");
522 assert_eq!(Value::Time(3_723_000_000).to_string(), "01:02:03");
523 assert_eq!(Value::Time(3_723_500_000).to_string(), "01:02:03.5");
524 assert_eq!(Value::Time(3_723_000_001).to_string(), "01:02:03.000001");
525 }
526
527 #[test]
528 fn a_timestamp_before_the_epoch_borrows_from_the_day() {
529 assert_eq!(Value::Timestamp(-1).to_string(), "1969-12-31 23:59:59.999999");
532 assert_eq!(Value::Timestamp(0).to_string(), "1970-01-01 00:00:00");
533 }
534
535 #[test]
536 fn a_decimal_prints_at_its_scale() {
537 let d = |unscaled, scale| Value::Decimal { unscaled, width: 18, scale }.to_string();
538 assert_eq!(d(1234, 2), "12.34");
539 assert_eq!(d(-1234, 2), "-12.34");
540 assert_eq!(d(5, 3), "0.005");
541 assert_eq!(d(-5, 3), "-0.005");
542 assert_eq!(d(1234, 0), "1234");
543 assert_eq!(d(1_000_000, 6), "1.000000");
544 }
545
546 #[test]
547 fn a_float_keeps_the_point_that_says_it_is_one() {
548 assert_eq!(Value::Double(1.0).to_string(), "1.0");
549 assert_eq!(Value::Double(-3.0).to_string(), "-3.0");
550 assert_eq!(Value::Double(1.5).to_string(), "1.5");
551 assert_eq!(Value::Double(-0.0).to_string(), "-0.0");
552 assert_eq!(Value::Float(0.5).to_string(), "0.5");
553 }
554
555 #[test]
556 fn a_float_is_printed_from_its_own_width_rather_than_widened_first() {
557 assert_eq!(Value::Float(0.1).to_string(), "0.1");
560 assert_eq!(Value::Float(1.0).to_string(), "1.0");
561 }
562
563 #[test]
564 fn a_float_switches_to_an_exponent_where_duckdb_switches() {
565 assert_eq!(Value::Double(1e15).to_string(), "1000000000000000.0");
566 assert_eq!(Value::Double(1e16).to_string(), "1e+16");
567 assert_eq!(Value::Double(1e20).to_string(), "1e+20");
568 assert_eq!(Value::Double(1e-4).to_string(), "0.0001");
569 assert_eq!(Value::Double(1e-5).to_string(), "1e-05");
570 assert_eq!(Value::Double(1.234_567_890_123_456_8e17).to_string(), "1.2345678901234568e+17");
571 }
572
573 #[test]
574 fn a_float_that_is_not_a_number_says_so_the_way_duckdb_says_it() {
575 assert_eq!(Value::Double(f64::INFINITY).to_string(), "inf");
576 assert_eq!(Value::Double(f64::NEG_INFINITY).to_string(), "-inf");
577 assert_eq!(Value::Double(f64::NAN).to_string(), "nan");
578 }
579
580 #[test]
581 fn an_interval_keeps_months_days_and_micros_apart() {
582 let i = |months, days, micros| Value::Interval { months, days, micros }.to_string();
583 assert_eq!(i(14, 3, 3_723_000_000), "1 year 2 months 3 days 01:02:03");
584 assert_eq!(i(1, 0, 0), "1 month");
585 assert_eq!(i(0, 0, 0), "00:00:00");
586 assert_eq!(i(0, 0, -1_000_000), "-00:00:01");
587 }
588
589 #[test]
590 fn a_blob_escapes_what_is_not_printable() {
591 assert_eq!(Value::Blob(b"ok".to_vec()).to_string(), "ok");
592 assert_eq!(Value::Blob(vec![0, 1, b'a']).to_string(), "\\x00\\x01a");
593 assert_eq!(Value::Blob(vec![0x7f, 0xff]).to_string(), "\\x7F\\xFF");
594 assert_eq!(Value::Blob(br#"'"\"#.to_vec()).to_string(), "\\x27\\x22\\x5C");
596 assert_eq!(Value::Blob(b" &`~".to_vec()).to_string(), " &`~");
597 }
598
599 #[test]
600 fn a_limit_that_does_not_fit_is_none_rather_than_clamped() {
601 assert_eq!(Value::Integer(5).as_i64(), Some(5));
602 assert_eq!(Value::UBigInt(u64::MAX).as_i64(), None);
603 assert_eq!(Value::Varchar("5".into()).as_i64(), None);
604 }
605
606 #[test]
607 fn a_footprint_is_the_value_plus_what_it_owns() {
608 let bare = Value::Integer(1).footprint();
609 assert_eq!(bare, size_of::<Value>(), "a number owns nothing");
610 assert_eq!(
611 Value::Boolean(true).footprint(),
612 bare,
613 "the enum is one width whatever is in it"
614 );
615 let text = "a string long enough to be on the heap in any implementation".to_string();
616 assert_eq!(Value::Varchar(text.clone()).footprint(), bare + text.capacity());
617 let list = Value::List {
618 element: LogicalType::Varchar,
619 values: vec![Value::Varchar(text.clone())],
620 };
621 assert_eq!(list.footprint(), bare + size_of::<Value>() + text.capacity());
625 }
626}