1use std::cmp::Ordering;
16use std::fmt::{Display, Formatter};
17use std::sync::Arc;
18
19use arrow_array::{Array, StructArray};
20use common_base::bytes::{Bytes, StringBytes};
21use common_decimal::Decimal128;
22use common_telemetry::error;
23use common_time::date::Date;
24use common_time::interval::IntervalUnit;
25use common_time::time::Time;
26use common_time::timestamp::{TimeUnit, Timestamp};
27use common_time::{Duration, IntervalDayTime, IntervalMonthDayNano, IntervalYearMonth, Timezone};
28use datafusion_common::ScalarValue;
29use datafusion_common::scalar::ScalarStructBuilder;
30pub use ordered_float::OrderedFloat;
31use serde::{Deserialize, Serialize, Serializer};
32use serde_json::Map;
33use snafu::{ResultExt, ensure};
34
35use crate::error::{
36 self, ConvertArrowArrayToScalarsSnafu, ConvertScalarToArrowArraySnafu, Error,
37 InconsistentStructFieldsAndItemsSnafu, Result, TryFromValueSnafu,
38};
39use crate::prelude::*;
40use crate::type_id::LogicalTypeId;
41use crate::types::{IntervalType, ListType, StructType};
42use crate::vectors::{ListVector, StructVector};
43
44pub type OrderedF32 = OrderedFloat<f32>;
45pub type OrderedF64 = OrderedFloat<f64>;
46
47#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
51pub enum Value {
52 Null,
53
54 Boolean(bool),
56 UInt8(u8),
57 UInt16(u16),
58 UInt32(u32),
59 UInt64(u64),
60 Int8(i8),
61 Int16(i16),
62 Int32(i32),
63 Int64(i64),
64 Float32(OrderedF32),
65 Float64(OrderedF64),
66
67 Decimal128(Decimal128),
69
70 String(StringBytes),
72 Binary(Bytes),
73
74 Date(Date),
76 Timestamp(Timestamp),
77 Time(Time),
78 Duration(Duration),
79 IntervalYearMonth(IntervalYearMonth),
81 IntervalDayTime(IntervalDayTime),
82 IntervalMonthDayNano(IntervalMonthDayNano),
83
84 List(ListValue),
85 Struct(StructValue),
86}
87
88impl Display for Value {
89 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90 match self {
91 Value::Null => write!(f, "{}", self.data_type().name()),
92 Value::Boolean(v) => write!(f, "{v}"),
93 Value::UInt8(v) => write!(f, "{v}"),
94 Value::UInt16(v) => write!(f, "{v}"),
95 Value::UInt32(v) => write!(f, "{v}"),
96 Value::UInt64(v) => write!(f, "{v}"),
97 Value::Int8(v) => write!(f, "{v}"),
98 Value::Int16(v) => write!(f, "{v}"),
99 Value::Int32(v) => write!(f, "{v}"),
100 Value::Int64(v) => write!(f, "{v}"),
101 Value::Float32(v) => write!(f, "{v}"),
102 Value::Float64(v) => write!(f, "{v}"),
103 Value::String(v) => write!(f, "{}", v.as_utf8()),
104 Value::Binary(v) => {
105 let hex = v
106 .iter()
107 .map(|b| format!("{b:02x}"))
108 .collect::<Vec<String>>()
109 .join("");
110 write!(f, "{hex}")
111 }
112 Value::Date(v) => write!(f, "{v}"),
113 Value::Timestamp(v) => write!(f, "{}", v.to_iso8601_string()),
114 Value::Time(t) => write!(f, "{}", t.to_iso8601_string()),
115 Value::IntervalYearMonth(v) => {
116 write!(f, "{}", v.to_iso8601_string())
117 }
118 Value::IntervalDayTime(v) => {
119 write!(f, "{}", v.to_iso8601_string())
120 }
121 Value::IntervalMonthDayNano(v) => {
122 write!(f, "{}", v.to_iso8601_string())
123 }
124 Value::Duration(d) => write!(f, "{d}"),
125 Value::List(v) => {
126 let items = v
127 .items()
128 .iter()
129 .map(|i| i.to_string())
130 .collect::<Vec<String>>()
131 .join(", ");
132 write!(f, "{}[{}]", v.datatype.name(), items)
133 }
134 Value::Decimal128(v) => write!(f, "{}", v),
135 Value::Struct(s) => {
136 let items = s
137 .fields
138 .fields()
139 .iter()
140 .map(|f| f.name())
141 .zip(s.items().iter())
142 .map(|(k, v)| format!("{k}: {v}"))
143 .collect::<Vec<String>>()
144 .join(", ");
145 write!(f, "{{ {items} }}")
146 }
147 }
148 }
149}
150
151macro_rules! define_data_type_func {
152 ($struct: ident) => {
153 pub fn data_type(&self) -> ConcreteDataType {
158 match self {
159 $struct::Null => ConcreteDataType::null_datatype(),
160 $struct::Boolean(_) => ConcreteDataType::boolean_datatype(),
161 $struct::UInt8(_) => ConcreteDataType::uint8_datatype(),
162 $struct::UInt16(_) => ConcreteDataType::uint16_datatype(),
163 $struct::UInt32(_) => ConcreteDataType::uint32_datatype(),
164 $struct::UInt64(_) => ConcreteDataType::uint64_datatype(),
165 $struct::Int8(_) => ConcreteDataType::int8_datatype(),
166 $struct::Int16(_) => ConcreteDataType::int16_datatype(),
167 $struct::Int32(_) => ConcreteDataType::int32_datatype(),
168 $struct::Int64(_) => ConcreteDataType::int64_datatype(),
169 $struct::Float32(_) => ConcreteDataType::float32_datatype(),
170 $struct::Float64(_) => ConcreteDataType::float64_datatype(),
171 $struct::String(_) => ConcreteDataType::string_datatype(),
172 $struct::Binary(_) => ConcreteDataType::binary_datatype(),
173 $struct::Date(_) => ConcreteDataType::date_datatype(),
174 $struct::Time(t) => ConcreteDataType::time_datatype(*t.unit()),
175 $struct::Timestamp(v) => ConcreteDataType::timestamp_datatype(v.unit()),
176 $struct::IntervalYearMonth(_) => {
177 ConcreteDataType::interval_datatype(IntervalUnit::YearMonth)
178 }
179 $struct::IntervalDayTime(_) => {
180 ConcreteDataType::interval_datatype(IntervalUnit::DayTime)
181 }
182 $struct::IntervalMonthDayNano(_) => {
183 ConcreteDataType::interval_datatype(IntervalUnit::MonthDayNano)
184 }
185 $struct::List(list) => ConcreteDataType::list_datatype(list.datatype().clone()),
186 $struct::Duration(d) => ConcreteDataType::duration_datatype(d.unit()),
187 $struct::Decimal128(d) => {
188 ConcreteDataType::decimal128_datatype(d.precision(), d.scale())
189 }
190 $struct::Struct(struct_value) => {
191 ConcreteDataType::struct_datatype(struct_value.struct_type().clone())
192 }
193 }
194 }
195 };
196}
197
198impl Value {
199 define_data_type_func!(Value);
200
201 pub fn is_null(&self) -> bool {
203 matches!(self, Value::Null)
204 }
205
206 pub fn as_list(&self) -> Result<Option<&ListValue>> {
208 match self {
209 Value::Null => Ok(None),
210 Value::List(v) => Ok(Some(v)),
211 other => error::CastTypeSnafu {
212 msg: format!("Failed to cast {other:?} to list value"),
213 }
214 .fail(),
215 }
216 }
217
218 pub fn as_struct(&self) -> Result<Option<&StructValue>> {
219 match self {
220 Value::Null => Ok(None),
221 Value::Struct(v) => Ok(Some(v)),
222 other => error::CastTypeSnafu {
223 msg: format!("Failed to cast {other:?} to struct value"),
224 }
225 .fail(),
226 }
227 }
228
229 pub fn as_value_ref(&self) -> ValueRef<'_> {
231 match self {
232 Value::Null => ValueRef::Null,
233 Value::Boolean(v) => ValueRef::Boolean(*v),
234 Value::UInt8(v) => ValueRef::UInt8(*v),
235 Value::UInt16(v) => ValueRef::UInt16(*v),
236 Value::UInt32(v) => ValueRef::UInt32(*v),
237 Value::UInt64(v) => ValueRef::UInt64(*v),
238 Value::Int8(v) => ValueRef::Int8(*v),
239 Value::Int16(v) => ValueRef::Int16(*v),
240 Value::Int32(v) => ValueRef::Int32(*v),
241 Value::Int64(v) => ValueRef::Int64(*v),
242 Value::Float32(v) => ValueRef::Float32(*v),
243 Value::Float64(v) => ValueRef::Float64(*v),
244 Value::String(v) => ValueRef::String(v.as_utf8()),
245 Value::Binary(v) => ValueRef::Binary(v),
246 Value::Date(v) => ValueRef::Date(*v),
247 Value::List(v) => ValueRef::List(ListValueRef::Ref { val: v }),
248 Value::Timestamp(v) => ValueRef::Timestamp(*v),
249 Value::Time(v) => ValueRef::Time(*v),
250 Value::IntervalYearMonth(v) => ValueRef::IntervalYearMonth(*v),
251 Value::IntervalDayTime(v) => ValueRef::IntervalDayTime(*v),
252 Value::IntervalMonthDayNano(v) => ValueRef::IntervalMonthDayNano(*v),
253 Value::Duration(v) => ValueRef::Duration(*v),
254 Value::Decimal128(v) => ValueRef::Decimal128(*v),
255 Value::Struct(v) => ValueRef::Struct(StructValueRef::Ref(v)),
256 }
257 }
258
259 pub fn as_timestamp(&self) -> Option<Timestamp> {
261 match self {
262 Value::Timestamp(t) => Some(*t),
263 _ => None,
264 }
265 }
266
267 pub fn as_string(&self) -> Option<String> {
269 match self {
270 Value::String(bytes) => Some(bytes.as_utf8().to_string()),
271 _ => None,
272 }
273 }
274
275 pub fn as_date(&self) -> Option<Date> {
277 match self {
278 Value::Date(t) => Some(*t),
279 _ => None,
280 }
281 }
282
283 pub fn as_time(&self) -> Option<Time> {
285 match self {
286 Value::Time(t) => Some(*t),
287 _ => None,
288 }
289 }
290
291 pub fn as_interval_year_month(&self) -> Option<IntervalYearMonth> {
293 match self {
294 Value::IntervalYearMonth(v) => Some(*v),
295 _ => None,
296 }
297 }
298
299 pub fn as_interval_day_time(&self) -> Option<IntervalDayTime> {
301 match self {
302 Value::IntervalDayTime(v) => Some(*v),
303 _ => None,
304 }
305 }
306
307 pub fn as_interval_month_day_nano(&self) -> Option<IntervalMonthDayNano> {
309 match self {
310 Value::IntervalMonthDayNano(v) => Some(*v),
311 _ => None,
312 }
313 }
314
315 pub fn as_i64(&self) -> Option<i64> {
317 match self {
318 Value::Int8(v) => Some(*v as _),
319 Value::Int16(v) => Some(*v as _),
320 Value::Int32(v) => Some(*v as _),
321 Value::Int64(v) => Some(*v),
322 Value::UInt8(v) => Some(*v as _),
323 Value::UInt16(v) => Some(*v as _),
324 Value::UInt32(v) => Some(*v as _),
325 _ => None,
326 }
327 }
328
329 pub fn as_u64(&self) -> Option<u64> {
331 match self {
332 Value::UInt8(v) => Some(*v as _),
333 Value::UInt16(v) => Some(*v as _),
334 Value::UInt32(v) => Some(*v as _),
335 Value::UInt64(v) => Some(*v),
336 _ => None,
337 }
338 }
339 pub fn as_f64_lossy(&self) -> Option<f64> {
341 match self {
342 Value::Float32(v) => Some(v.0 as _),
343 Value::Float64(v) => Some(v.0),
344 Value::Int8(v) => Some(*v as _),
345 Value::Int16(v) => Some(*v as _),
346 Value::Int32(v) => Some(*v as _),
347 Value::Int64(v) => Some(*v as _),
348 Value::UInt8(v) => Some(*v as _),
349 Value::UInt16(v) => Some(*v as _),
350 Value::UInt32(v) => Some(*v as _),
351 Value::UInt64(v) => Some(*v as _),
352 _ => None,
353 }
354 }
355
356 pub fn as_duration(&self) -> Option<Duration> {
358 match self {
359 Value::Duration(d) => Some(*d),
360 _ => None,
361 }
362 }
363
364 pub fn as_bool(&self) -> Option<bool> {
366 match self {
367 Value::Boolean(b) => Some(*b),
368 _ => None,
369 }
370 }
371
372 pub fn logical_type_id(&self) -> LogicalTypeId {
374 match self {
375 Value::Null => LogicalTypeId::Null,
376 Value::Boolean(_) => LogicalTypeId::Boolean,
377 Value::UInt8(_) => LogicalTypeId::UInt8,
378 Value::UInt16(_) => LogicalTypeId::UInt16,
379 Value::UInt32(_) => LogicalTypeId::UInt32,
380 Value::UInt64(_) => LogicalTypeId::UInt64,
381 Value::Int8(_) => LogicalTypeId::Int8,
382 Value::Int16(_) => LogicalTypeId::Int16,
383 Value::Int32(_) => LogicalTypeId::Int32,
384 Value::Int64(_) => LogicalTypeId::Int64,
385 Value::Float32(_) => LogicalTypeId::Float32,
386 Value::Float64(_) => LogicalTypeId::Float64,
387 Value::String(_) => LogicalTypeId::String,
388 Value::Binary(_) => LogicalTypeId::Binary,
389 Value::List(_) => LogicalTypeId::List,
390 Value::Date(_) => LogicalTypeId::Date,
391 Value::Timestamp(t) => match t.unit() {
392 TimeUnit::Second => LogicalTypeId::TimestampSecond,
393 TimeUnit::Millisecond => LogicalTypeId::TimestampMillisecond,
394 TimeUnit::Microsecond => LogicalTypeId::TimestampMicrosecond,
395 TimeUnit::Nanosecond => LogicalTypeId::TimestampNanosecond,
396 },
397 Value::Time(t) => match t.unit() {
398 TimeUnit::Second => LogicalTypeId::TimeSecond,
399 TimeUnit::Millisecond => LogicalTypeId::TimeMillisecond,
400 TimeUnit::Microsecond => LogicalTypeId::TimeMicrosecond,
401 TimeUnit::Nanosecond => LogicalTypeId::TimeNanosecond,
402 },
403 Value::IntervalYearMonth(_) => LogicalTypeId::IntervalYearMonth,
404 Value::IntervalDayTime(_) => LogicalTypeId::IntervalDayTime,
405 Value::IntervalMonthDayNano(_) => LogicalTypeId::IntervalMonthDayNano,
406 Value::Duration(d) => match d.unit() {
407 TimeUnit::Second => LogicalTypeId::DurationSecond,
408 TimeUnit::Millisecond => LogicalTypeId::DurationMillisecond,
409 TimeUnit::Microsecond => LogicalTypeId::DurationMicrosecond,
410 TimeUnit::Nanosecond => LogicalTypeId::DurationNanosecond,
411 },
412 Value::Decimal128(_) => LogicalTypeId::Decimal128,
413 Value::Struct(_) => LogicalTypeId::Struct,
414 }
415 }
416
417 pub fn try_to_scalar_value(&self, output_type: &ConcreteDataType) -> Result<ScalarValue> {
419 let value_type_id = self.logical_type_id();
421 let output_type_id = output_type.logical_type_id();
422 ensure!(
423 output_type_id == value_type_id
425 || self.is_null()
426 || (output_type_id == LogicalTypeId::Json
427 && value_type_id == LogicalTypeId::Binary),
428 error::ToScalarValueSnafu {
429 reason: format!(
430 "expect value to return output_type {output_type_id:?}, actual: {value_type_id:?}",
431 ),
432 }
433 );
434
435 let scalar_value = match self {
436 Value::Boolean(v) => ScalarValue::Boolean(Some(*v)),
437 Value::UInt8(v) => ScalarValue::UInt8(Some(*v)),
438 Value::UInt16(v) => ScalarValue::UInt16(Some(*v)),
439 Value::UInt32(v) => ScalarValue::UInt32(Some(*v)),
440 Value::UInt64(v) => ScalarValue::UInt64(Some(*v)),
441 Value::Int8(v) => ScalarValue::Int8(Some(*v)),
442 Value::Int16(v) => ScalarValue::Int16(Some(*v)),
443 Value::Int32(v) => ScalarValue::Int32(Some(*v)),
444 Value::Int64(v) => ScalarValue::Int64(Some(*v)),
445 Value::Float32(v) => ScalarValue::Float32(Some(v.0)),
446 Value::Float64(v) => ScalarValue::Float64(Some(v.0)),
447 Value::String(v) => ScalarValue::Utf8(Some(v.as_utf8().to_string())),
448 Value::Binary(v) => ScalarValue::Binary(Some(v.to_vec())),
449 Value::Date(v) => ScalarValue::Date32(Some(v.val())),
450 Value::Null => to_null_scalar_value(output_type)?,
451 Value::List(list) => {
452 let list_type = output_type.as_list().unwrap();
454 list.try_to_scalar_value(list_type)?
455 }
456 Value::Timestamp(t) => timestamp_to_scalar_value(t.unit(), Some(t.value())),
457 Value::Time(t) => time_to_scalar_value(*t.unit(), Some(t.value()))?,
458 Value::IntervalYearMonth(v) => ScalarValue::IntervalYearMonth(Some(v.to_i32())),
459 Value::IntervalDayTime(v) => ScalarValue::IntervalDayTime(Some((*v).into())),
460 Value::IntervalMonthDayNano(v) => ScalarValue::IntervalMonthDayNano(Some((*v).into())),
461 Value::Duration(d) => duration_to_scalar_value(d.unit(), Some(d.value())),
462 Value::Decimal128(d) => {
463 let (v, p, s) = d.to_scalar_value();
464 ScalarValue::Decimal128(v, p, s)
465 }
466 Value::Struct(struct_value) => {
467 let struct_type = output_type.as_struct().unwrap();
468 struct_value.try_to_scalar_value(struct_type)?
469 }
470 };
471
472 Ok(scalar_value)
473 }
474
475 pub fn try_negative(&self) -> Option<Self> {
477 match self {
478 Value::Null => Some(Value::Null),
479 Value::UInt8(x) => {
480 if *x == 0 {
481 Some(Value::UInt8(*x))
482 } else {
483 None
484 }
485 }
486 Value::UInt16(x) => {
487 if *x == 0 {
488 Some(Value::UInt16(*x))
489 } else {
490 None
491 }
492 }
493 Value::UInt32(x) => {
494 if *x == 0 {
495 Some(Value::UInt32(*x))
496 } else {
497 None
498 }
499 }
500 Value::UInt64(x) => {
501 if *x == 0 {
502 Some(Value::UInt64(*x))
503 } else {
504 None
505 }
506 }
507 Value::Int8(x) => Some(Value::Int8(-*x)),
508 Value::Int16(x) => Some(Value::Int16(-*x)),
509 Value::Int32(x) => Some(Value::Int32(-*x)),
510 Value::Int64(x) => Some(Value::Int64(-*x)),
511 Value::Float32(x) => Some(Value::Float32(-*x)),
512 Value::Float64(x) => Some(Value::Float64(-*x)),
513 Value::Decimal128(x) => Some(Value::Decimal128(x.negative())),
514 Value::Date(x) => Some(Value::Date(x.negative())),
515 Value::Timestamp(x) => Some(Value::Timestamp(x.negative())),
516 Value::Time(x) => Some(Value::Time(x.negative())),
517 Value::Duration(x) => Some(Value::Duration(x.negative())),
518 Value::IntervalYearMonth(x) => Some(Value::IntervalYearMonth(x.negative())),
519 Value::IntervalDayTime(x) => Some(Value::IntervalDayTime(x.negative())),
520 Value::IntervalMonthDayNano(x) => Some(Value::IntervalMonthDayNano(x.negative())),
521
522 Value::Binary(_)
523 | Value::String(_)
524 | Value::Boolean(_)
525 | Value::List(_)
526 | Value::Struct(_) => None,
527 }
528 }
529}
530
531pub trait TryAsPrimitive<T: LogicalPrimitiveType> {
532 fn try_as_primitive(&self) -> Option<T::Native>;
533}
534
535macro_rules! impl_try_as_primitive {
536 ($Type: ident, $Variant: ident) => {
537 impl TryAsPrimitive<crate::types::$Type> for Value {
538 fn try_as_primitive(
539 &self,
540 ) -> Option<<crate::types::$Type as crate::types::LogicalPrimitiveType>::Native> {
541 match self {
542 Value::$Variant(v) => Some((*v).into()),
543 _ => None,
544 }
545 }
546 }
547 };
548}
549
550impl_try_as_primitive!(Int8Type, Int8);
551impl_try_as_primitive!(Int16Type, Int16);
552impl_try_as_primitive!(Int32Type, Int32);
553impl_try_as_primitive!(Int64Type, Int64);
554impl_try_as_primitive!(UInt8Type, UInt8);
555impl_try_as_primitive!(UInt16Type, UInt16);
556impl_try_as_primitive!(UInt32Type, UInt32);
557impl_try_as_primitive!(UInt64Type, UInt64);
558impl_try_as_primitive!(Float32Type, Float32);
559impl_try_as_primitive!(Float64Type, Float64);
560
561pub fn to_null_scalar_value(output_type: &ConcreteDataType) -> Result<ScalarValue> {
562 Ok(match output_type {
563 ConcreteDataType::Null(_) => ScalarValue::Null,
564 ConcreteDataType::Boolean(_) => ScalarValue::Boolean(None),
565 ConcreteDataType::Int8(_) => ScalarValue::Int8(None),
566 ConcreteDataType::Int16(_) => ScalarValue::Int16(None),
567 ConcreteDataType::Int32(_) => ScalarValue::Int32(None),
568 ConcreteDataType::Int64(_) => ScalarValue::Int64(None),
569 ConcreteDataType::UInt8(_) => ScalarValue::UInt8(None),
570 ConcreteDataType::UInt16(_) => ScalarValue::UInt16(None),
571 ConcreteDataType::UInt32(_) => ScalarValue::UInt32(None),
572 ConcreteDataType::UInt64(_) => ScalarValue::UInt64(None),
573 ConcreteDataType::Float32(_) => ScalarValue::Float32(None),
574 ConcreteDataType::Float64(_) => ScalarValue::Float64(None),
575 ConcreteDataType::Binary(_) | ConcreteDataType::Json(_) | ConcreteDataType::Vector(_) => {
576 ScalarValue::Binary(None)
577 }
578 ConcreteDataType::String(_) => ScalarValue::Utf8(None),
579 ConcreteDataType::Date(_) => ScalarValue::Date32(None),
580 ConcreteDataType::Timestamp(t) => timestamp_to_scalar_value(t.unit(), None),
581 ConcreteDataType::Interval(v) => match v {
582 IntervalType::YearMonth(_) => ScalarValue::IntervalYearMonth(None),
583 IntervalType::DayTime(_) => ScalarValue::IntervalDayTime(None),
584 IntervalType::MonthDayNano(_) => ScalarValue::IntervalMonthDayNano(None),
585 },
586 ConcreteDataType::List(list_type) => {
587 ScalarValue::new_null_list(list_type.item_type().as_arrow_type(), true, 1)
588 }
589 ConcreteDataType::Struct(fields) => {
590 let fields = fields.as_arrow_fields();
591 ScalarStructBuilder::new_null(fields)
592 }
593 ConcreteDataType::Dictionary(dict) => ScalarValue::Dictionary(
594 Box::new(dict.key_type().as_arrow_type()),
595 Box::new(to_null_scalar_value(dict.value_type())?),
596 ),
597 ConcreteDataType::Time(t) => time_to_scalar_value(t.unit(), None)?,
598 ConcreteDataType::Duration(d) => duration_to_scalar_value(d.unit(), None),
599 ConcreteDataType::Decimal128(d) => ScalarValue::Decimal128(None, d.precision(), d.scale()),
600 })
601}
602
603pub fn timestamp_to_scalar_value(unit: TimeUnit, val: Option<i64>) -> ScalarValue {
604 match unit {
605 TimeUnit::Second => ScalarValue::TimestampSecond(val, None),
606 TimeUnit::Millisecond => ScalarValue::TimestampMillisecond(val, None),
607 TimeUnit::Microsecond => ScalarValue::TimestampMicrosecond(val, None),
608 TimeUnit::Nanosecond => ScalarValue::TimestampNanosecond(val, None),
609 }
610}
611
612pub fn time_to_scalar_value(unit: TimeUnit, val: Option<i64>) -> Result<ScalarValue> {
614 Ok(match unit {
615 TimeUnit::Second => ScalarValue::Time32Second(
616 val.map(|i| i.try_into().context(error::CastTimeTypeSnafu))
617 .transpose()?,
618 ),
619 TimeUnit::Millisecond => ScalarValue::Time32Millisecond(
620 val.map(|i| i.try_into().context(error::CastTimeTypeSnafu))
621 .transpose()?,
622 ),
623 TimeUnit::Microsecond => ScalarValue::Time64Microsecond(val),
624 TimeUnit::Nanosecond => ScalarValue::Time64Nanosecond(val),
625 })
626}
627
628pub fn duration_to_scalar_value(unit: TimeUnit, val: Option<i64>) -> ScalarValue {
630 match unit {
631 TimeUnit::Second => ScalarValue::DurationSecond(val),
632 TimeUnit::Millisecond => ScalarValue::DurationMillisecond(val),
633 TimeUnit::Microsecond => ScalarValue::DurationMicrosecond(val),
634 TimeUnit::Nanosecond => ScalarValue::DurationNanosecond(val),
635 }
636}
637
638pub fn scalar_value_to_timestamp(
642 scalar: &ScalarValue,
643 timezone: Option<&Timezone>,
644) -> Option<Timestamp> {
645 match scalar {
646 ScalarValue::Utf8(Some(s)) => match Timestamp::from_str(s, timezone) {
647 Ok(t) => Some(t),
648 Err(e) => {
649 error!(e;"Failed to convert string literal {s} to timestamp");
650 None
651 }
652 },
653 ScalarValue::TimestampSecond(v, _) => v.map(Timestamp::new_second),
654 ScalarValue::TimestampMillisecond(v, _) => v.map(Timestamp::new_millisecond),
655 ScalarValue::TimestampMicrosecond(v, _) => v.map(Timestamp::new_microsecond),
656 ScalarValue::TimestampNanosecond(v, _) => v.map(Timestamp::new_nanosecond),
657 _ => None,
658 }
659}
660
661macro_rules! impl_ord_for_value_like {
662 ($Type: ident, $left: ident, $right: ident) => {
663 if $left.is_null() && !$right.is_null() {
664 return Ordering::Less;
665 } else if !$left.is_null() && $right.is_null() {
666 return Ordering::Greater;
667 } else {
668 match ($left, $right) {
669 ($Type::Null, $Type::Null) => Ordering::Equal,
670 ($Type::Boolean(v1), $Type::Boolean(v2)) => v1.cmp(v2),
671 ($Type::UInt8(v1), $Type::UInt8(v2)) => v1.cmp(v2),
672 ($Type::UInt16(v1), $Type::UInt16(v2)) => v1.cmp(v2),
673 ($Type::UInt32(v1), $Type::UInt32(v2)) => v1.cmp(v2),
674 ($Type::UInt64(v1), $Type::UInt64(v2)) => v1.cmp(v2),
675 ($Type::Int8(v1), $Type::Int8(v2)) => v1.cmp(v2),
676 ($Type::Int16(v1), $Type::Int16(v2)) => v1.cmp(v2),
677 ($Type::Int32(v1), $Type::Int32(v2)) => v1.cmp(v2),
678 ($Type::Int64(v1), $Type::Int64(v2)) => v1.cmp(v2),
679 ($Type::Float32(v1), $Type::Float32(v2)) => v1.cmp(v2),
680 ($Type::Float64(v1), $Type::Float64(v2)) => v1.cmp(v2),
681 ($Type::String(v1), $Type::String(v2)) => v1.cmp(v2),
682 ($Type::Binary(v1), $Type::Binary(v2)) => v1.cmp(v2),
683 ($Type::Date(v1), $Type::Date(v2)) => v1.cmp(v2),
684 ($Type::Timestamp(v1), $Type::Timestamp(v2)) => v1.cmp(v2),
685 ($Type::Time(v1), $Type::Time(v2)) => v1.cmp(v2),
686 ($Type::IntervalYearMonth(v1), $Type::IntervalYearMonth(v2)) => v1.cmp(v2),
687 ($Type::IntervalDayTime(v1), $Type::IntervalDayTime(v2)) => v1.cmp(v2),
688 ($Type::IntervalMonthDayNano(v1), $Type::IntervalMonthDayNano(v2)) => v1.cmp(v2),
689 ($Type::Duration(v1), $Type::Duration(v2)) => v1.cmp(v2),
690 ($Type::List(v1), $Type::List(v2)) => v1.cmp(v2),
691 _ => panic!(
692 "Cannot compare different values {:?} and {:?}",
693 $left, $right
694 ),
695 }
696 }
697 };
698}
699
700impl PartialOrd for Value {
701 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
702 Some(self.cmp(other))
703 }
704}
705
706impl Ord for Value {
707 fn cmp(&self, other: &Self) -> Ordering {
708 impl_ord_for_value_like!(Value, self, other)
709 }
710}
711
712macro_rules! impl_try_from_value {
713 ($Variant: ident, $Type: ident) => {
714 impl TryFrom<Value> for $Type {
715 type Error = Error;
716
717 #[inline]
718 fn try_from(from: Value) -> std::result::Result<Self, Self::Error> {
719 match from {
720 Value::$Variant(v) => Ok(v.into()),
721 _ => TryFromValueSnafu {
722 reason: format!("{:?} is not a {}", from, stringify!($Type)),
723 }
724 .fail(),
725 }
726 }
727 }
728
729 impl TryFrom<Value> for Option<$Type> {
730 type Error = Error;
731
732 #[inline]
733 fn try_from(from: Value) -> std::result::Result<Self, Self::Error> {
734 match from {
735 Value::$Variant(v) => Ok(Some(v.into())),
736 Value::Null => Ok(None),
737 _ => TryFromValueSnafu {
738 reason: format!("{:?} is not a {}", from, stringify!($Type)),
739 }
740 .fail(),
741 }
742 }
743 }
744 };
745}
746
747impl_try_from_value!(Boolean, bool);
748impl_try_from_value!(UInt8, u8);
749impl_try_from_value!(UInt16, u16);
750impl_try_from_value!(UInt32, u32);
751impl_try_from_value!(UInt64, u64);
752impl_try_from_value!(Int8, i8);
753impl_try_from_value!(Int16, i16);
754impl_try_from_value!(Int32, i32);
755impl_try_from_value!(Int64, i64);
756impl_try_from_value!(Float32, f32);
757impl_try_from_value!(Float64, f64);
758impl_try_from_value!(Float32, OrderedF32);
759impl_try_from_value!(Float64, OrderedF64);
760impl_try_from_value!(String, StringBytes);
761impl_try_from_value!(Binary, Bytes);
762impl_try_from_value!(Date, Date);
763impl_try_from_value!(Time, Time);
764impl_try_from_value!(Timestamp, Timestamp);
765impl_try_from_value!(IntervalYearMonth, IntervalYearMonth);
766impl_try_from_value!(IntervalDayTime, IntervalDayTime);
767impl_try_from_value!(IntervalMonthDayNano, IntervalMonthDayNano);
768impl_try_from_value!(Duration, Duration);
769impl_try_from_value!(Decimal128, Decimal128);
770
771macro_rules! impl_value_from {
772 ($Variant: ident, $Type: ident) => {
773 impl From<$Type> for Value {
774 fn from(value: $Type) -> Self {
775 Value::$Variant(value.into())
776 }
777 }
778
779 impl From<Option<$Type>> for Value {
780 fn from(value: Option<$Type>) -> Self {
781 match value {
782 Some(v) => Value::$Variant(v.into()),
783 None => Value::Null,
784 }
785 }
786 }
787 };
788}
789
790impl_value_from!(Boolean, bool);
791impl_value_from!(UInt8, u8);
792impl_value_from!(UInt16, u16);
793impl_value_from!(UInt32, u32);
794impl_value_from!(UInt64, u64);
795impl_value_from!(Int8, i8);
796impl_value_from!(Int16, i16);
797impl_value_from!(Int32, i32);
798impl_value_from!(Int64, i64);
799impl_value_from!(Float32, f32);
800impl_value_from!(Float64, f64);
801impl_value_from!(Float32, OrderedF32);
802impl_value_from!(Float64, OrderedF64);
803impl_value_from!(String, StringBytes);
804impl_value_from!(Binary, Bytes);
805impl_value_from!(Date, Date);
806impl_value_from!(Time, Time);
807impl_value_from!(Timestamp, Timestamp);
808impl_value_from!(IntervalYearMonth, IntervalYearMonth);
809impl_value_from!(IntervalDayTime, IntervalDayTime);
810impl_value_from!(IntervalMonthDayNano, IntervalMonthDayNano);
811impl_value_from!(Duration, Duration);
812impl_value_from!(String, String);
813impl_value_from!(Decimal128, Decimal128);
814
815impl From<&str> for Value {
816 fn from(string: &str) -> Value {
817 Value::String(string.into())
818 }
819}
820
821impl From<Vec<u8>> for Value {
822 fn from(bytes: Vec<u8>) -> Value {
823 Value::Binary(bytes.into())
824 }
825}
826
827impl From<&[u8]> for Value {
828 fn from(bytes: &[u8]) -> Value {
829 Value::Binary(bytes.into())
830 }
831}
832
833impl TryFrom<Value> for serde_json::Value {
834 type Error = serde_json::Error;
835
836 fn try_from(value: Value) -> serde_json::Result<serde_json::Value> {
837 let json_value = match value {
838 Value::Null => serde_json::Value::Null,
839 Value::Boolean(v) => serde_json::Value::Bool(v),
840 Value::UInt8(v) => serde_json::Value::from(v),
841 Value::UInt16(v) => serde_json::Value::from(v),
842 Value::UInt32(v) => serde_json::Value::from(v),
843 Value::UInt64(v) => serde_json::Value::from(v),
844 Value::Int8(v) => serde_json::Value::from(v),
845 Value::Int16(v) => serde_json::Value::from(v),
846 Value::Int32(v) => serde_json::Value::from(v),
847 Value::Int64(v) => serde_json::Value::from(v),
848 Value::Float32(v) => serde_json::Value::from(v.0),
849 Value::Float64(v) => serde_json::Value::from(v.0),
850 Value::String(bytes) => serde_json::Value::String(bytes.into_string()),
851 Value::Binary(bytes) => serde_json::to_value(bytes)?,
852 Value::Date(v) => serde_json::Value::Number(v.val().into()),
853 Value::List(v) => {
854 let items = v
855 .take_items()
856 .into_iter()
857 .map(serde_json::Value::try_from)
858 .collect::<serde_json::Result<Vec<_>>>()?;
859 serde_json::Value::Array(items)
860 }
861 Value::Timestamp(v) => serde_json::to_value(v.value())?,
862 Value::Time(v) => serde_json::to_value(v.value())?,
863 Value::IntervalYearMonth(v) => serde_json::to_value(v.to_i32())?,
864 Value::IntervalDayTime(v) => serde_json::to_value(v.to_i64())?,
865 Value::IntervalMonthDayNano(v) => serde_json::to_value(v.to_i128())?,
866 Value::Duration(v) => serde_json::to_value(v.value())?,
867 Value::Decimal128(v) => serde_json::to_value(v.to_string())?,
868 Value::Struct(v) => {
869 let map = v
870 .fields
871 .clone() .fields()
873 .iter()
874 .zip(v.take_items().into_iter())
875 .map(|(field, value)| {
876 Ok((
877 field.name().to_string(),
878 serde_json::Value::try_from(value)?,
879 ))
880 })
881 .collect::<serde_json::Result<Map<String, serde_json::Value>>>()?;
882 serde_json::Value::Object(map)
883 }
884 };
885
886 Ok(json_value)
887 }
888}
889
890#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)]
893pub struct ListValue {
894 items: Vec<Value>,
895 datatype: ConcreteDataType,
898}
899
900impl Eq for ListValue {}
901
902impl ListValue {
903 pub fn new(items: Vec<Value>, datatype: ConcreteDataType) -> Self {
904 Self { items, datatype }
905 }
906
907 pub fn items(&self) -> &[Value] {
908 &self.items
909 }
910
911 pub fn take_items(self) -> Vec<Value> {
912 self.items
913 }
914
915 pub fn datatype(&self) -> &ConcreteDataType {
916 &self.datatype
917 }
918
919 fn try_to_scalar_value(&self, output_type: &ListType) -> Result<ScalarValue> {
920 let vs = self
921 .items
922 .iter()
923 .map(|v| v.try_to_scalar_value(output_type.item_type()))
924 .collect::<Result<Vec<_>>>()?;
925 Ok(ScalarValue::List(ScalarValue::new_list(
926 &vs,
927 &self.datatype.as_arrow_type(),
928 true,
929 )))
930 }
931
932 fn estimated_size(&self) -> usize {
935 self.items
936 .first()
937 .map(|x| x.as_value_ref().data_size() * self.items.len())
938 .unwrap_or(0)
939 }
940}
941
942impl Default for ListValue {
943 fn default() -> ListValue {
944 ListValue::new(vec![], ConcreteDataType::null_datatype())
945 }
946}
947
948impl PartialOrd for ListValue {
949 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
950 Some(self.cmp(other))
951 }
952}
953
954impl Ord for ListValue {
955 fn cmp(&self, other: &Self) -> Ordering {
956 assert_eq!(
957 self.datatype, other.datatype,
958 "Cannot compare different datatypes!"
959 );
960 self.items.cmp(&other.items)
961 }
962}
963
964#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
965pub struct StructValue {
966 items: Vec<Value>,
967 fields: StructType,
968}
969
970impl StructValue {
971 pub fn try_new(items: Vec<Value>, fields: StructType) -> Result<Self> {
972 ensure!(
973 items.len() == fields.fields().len(),
974 InconsistentStructFieldsAndItemsSnafu {
975 field_len: fields.fields().len(),
976 item_len: items.len()
977 }
978 );
979 Ok(Self { items, fields })
980 }
981
982 pub fn items(&self) -> &[Value] {
983 &self.items
984 }
985
986 pub fn take_items(self) -> Vec<Value> {
987 self.items
988 }
989
990 pub fn struct_type(&self) -> &StructType {
991 &self.fields
992 }
993
994 fn estimated_size(&self) -> usize {
995 self.items
996 .iter()
997 .map(|x| x.as_value_ref().data_size())
998 .sum()
999 }
1000
1001 fn try_to_scalar_value(&self, output_type: &StructType) -> Result<ScalarValue> {
1002 let arrays = self
1003 .items
1004 .iter()
1005 .map(|value| {
1006 let scalar_value = value.try_to_scalar_value(&value.data_type())?;
1007 scalar_value
1008 .to_array()
1009 .context(ConvertScalarToArrowArraySnafu)
1010 })
1011 .collect::<Result<Vec<Arc<dyn Array>>>>()?;
1012
1013 let fields = output_type.as_arrow_fields();
1014 let struct_array = StructArray::new(fields, arrays, None);
1015 Ok(ScalarValue::Struct(Arc::new(struct_array)))
1016 }
1017}
1018
1019impl Default for StructValue {
1020 fn default() -> StructValue {
1021 StructValue::try_new(vec![], StructType::new(vec![])).unwrap()
1022 }
1023}
1024
1025#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1028pub struct DictionaryValue {
1029 key_type: ConcreteDataType,
1031 value_type: ConcreteDataType,
1032}
1033
1034impl Eq for DictionaryValue {}
1035
1036impl TryFrom<ScalarValue> for Value {
1037 type Error = error::Error;
1038
1039 fn try_from(v: ScalarValue) -> Result<Self> {
1040 let v = match v {
1041 ScalarValue::Null => Value::Null,
1042 ScalarValue::Boolean(b) => Value::from(b),
1043 ScalarValue::Float32(f) => Value::from(f),
1044 ScalarValue::Float64(f) => Value::from(f),
1045 ScalarValue::Int8(i) => Value::from(i),
1046 ScalarValue::Int16(i) => Value::from(i),
1047 ScalarValue::Int32(i) => Value::from(i),
1048 ScalarValue::Int64(i) => Value::from(i),
1049 ScalarValue::UInt8(u) => Value::from(u),
1050 ScalarValue::UInt16(u) => Value::from(u),
1051 ScalarValue::UInt32(u) => Value::from(u),
1052 ScalarValue::UInt64(u) => Value::from(u),
1053 ScalarValue::Utf8(s) | ScalarValue::LargeUtf8(s) => {
1054 Value::from(s.map(StringBytes::from))
1055 }
1056 ScalarValue::Binary(b)
1057 | ScalarValue::LargeBinary(b)
1058 | ScalarValue::FixedSizeBinary(_, b) => Value::from(b.map(Bytes::from)),
1059 ScalarValue::List(array) => {
1060 let datatype = ConcreteDataType::try_from(&array.value_type())?;
1062 let items = ScalarValue::convert_array_to_scalar_vec(array.as_ref())
1063 .context(ConvertArrowArrayToScalarsSnafu)?
1064 .into_iter()
1065 .flatten()
1066 .map(|x| x.try_into())
1067 .collect::<Result<Vec<Value>>>()?;
1068 Value::List(ListValue::new(items, datatype))
1069 }
1070 ScalarValue::Date32(d) => d.map(|x| Value::Date(Date::new(x))).unwrap_or(Value::Null),
1071 ScalarValue::TimestampSecond(t, _) => t
1072 .map(|x| Value::Timestamp(Timestamp::new(x, TimeUnit::Second)))
1073 .unwrap_or(Value::Null),
1074 ScalarValue::TimestampMillisecond(t, _) => t
1075 .map(|x| Value::Timestamp(Timestamp::new(x, TimeUnit::Millisecond)))
1076 .unwrap_or(Value::Null),
1077 ScalarValue::TimestampMicrosecond(t, _) => t
1078 .map(|x| Value::Timestamp(Timestamp::new(x, TimeUnit::Microsecond)))
1079 .unwrap_or(Value::Null),
1080 ScalarValue::TimestampNanosecond(t, _) => t
1081 .map(|x| Value::Timestamp(Timestamp::new(x, TimeUnit::Nanosecond)))
1082 .unwrap_or(Value::Null),
1083 ScalarValue::Time32Second(t) => t
1084 .map(|x| Value::Time(Time::new(x as i64, TimeUnit::Second)))
1085 .unwrap_or(Value::Null),
1086 ScalarValue::Time32Millisecond(t) => t
1087 .map(|x| Value::Time(Time::new(x as i64, TimeUnit::Millisecond)))
1088 .unwrap_or(Value::Null),
1089 ScalarValue::Time64Microsecond(t) => t
1090 .map(|x| Value::Time(Time::new(x, TimeUnit::Microsecond)))
1091 .unwrap_or(Value::Null),
1092 ScalarValue::Time64Nanosecond(t) => t
1093 .map(|x| Value::Time(Time::new(x, TimeUnit::Nanosecond)))
1094 .unwrap_or(Value::Null),
1095
1096 ScalarValue::IntervalYearMonth(t) => t
1097 .map(|x| Value::IntervalYearMonth(IntervalYearMonth::from_i32(x)))
1098 .unwrap_or(Value::Null),
1099 ScalarValue::IntervalDayTime(t) => t
1100 .map(|x| Value::IntervalDayTime(IntervalDayTime::from(x)))
1101 .unwrap_or(Value::Null),
1102 ScalarValue::IntervalMonthDayNano(t) => t
1103 .map(|x| Value::IntervalMonthDayNano(IntervalMonthDayNano::from(x)))
1104 .unwrap_or(Value::Null),
1105 ScalarValue::DurationSecond(d) => d
1106 .map(|x| Value::Duration(Duration::new(x, TimeUnit::Second)))
1107 .unwrap_or(Value::Null),
1108 ScalarValue::DurationMillisecond(d) => d
1109 .map(|x| Value::Duration(Duration::new(x, TimeUnit::Millisecond)))
1110 .unwrap_or(Value::Null),
1111 ScalarValue::DurationMicrosecond(d) => d
1112 .map(|x| Value::Duration(Duration::new(x, TimeUnit::Microsecond)))
1113 .unwrap_or(Value::Null),
1114 ScalarValue::DurationNanosecond(d) => d
1115 .map(|x| Value::Duration(Duration::new(x, TimeUnit::Nanosecond)))
1116 .unwrap_or(Value::Null),
1117 ScalarValue::Decimal128(v, p, s) => v
1118 .map(|v| Value::Decimal128(Decimal128::new(v, p, s)))
1119 .unwrap_or(Value::Null),
1120 ScalarValue::Struct(struct_array) => {
1121 let struct_type: StructType = (struct_array.fields()).try_into()?;
1122 let items = struct_array
1123 .columns()
1124 .iter()
1125 .map(|array| {
1126 let field_scalar_value = ScalarValue::try_from_array(array.as_ref(), 0)
1128 .context(ConvertArrowArrayToScalarsSnafu)?;
1129 field_scalar_value.try_into()
1130 })
1131 .collect::<Result<Vec<Value>>>()?;
1132 Value::Struct(StructValue::try_new(items, struct_type)?)
1133 }
1134 ScalarValue::Decimal256(_, _, _)
1135 | ScalarValue::FixedSizeList(_)
1136 | ScalarValue::LargeList(_)
1137 | ScalarValue::Dictionary(_, _)
1138 | ScalarValue::Union(_, _, _)
1139 | ScalarValue::Float16(_)
1140 | ScalarValue::Utf8View(_)
1141 | ScalarValue::BinaryView(_)
1142 | ScalarValue::Map(_)
1143 | ScalarValue::Date64(_) => {
1144 return error::UnsupportedArrowTypeSnafu {
1145 arrow_type: v.data_type(),
1146 }
1147 .fail();
1148 }
1149 };
1150 Ok(v)
1151 }
1152}
1153
1154impl From<ValueRef<'_>> for Value {
1155 fn from(value: ValueRef<'_>) -> Self {
1156 match value {
1157 ValueRef::Null => Value::Null,
1158 ValueRef::Boolean(v) => Value::Boolean(v),
1159 ValueRef::UInt8(v) => Value::UInt8(v),
1160 ValueRef::UInt16(v) => Value::UInt16(v),
1161 ValueRef::UInt32(v) => Value::UInt32(v),
1162 ValueRef::UInt64(v) => Value::UInt64(v),
1163 ValueRef::Int8(v) => Value::Int8(v),
1164 ValueRef::Int16(v) => Value::Int16(v),
1165 ValueRef::Int32(v) => Value::Int32(v),
1166 ValueRef::Int64(v) => Value::Int64(v),
1167 ValueRef::Float32(v) => Value::Float32(v),
1168 ValueRef::Float64(v) => Value::Float64(v),
1169 ValueRef::String(v) => Value::String(v.into()),
1170 ValueRef::Binary(v) => Value::Binary(v.into()),
1171 ValueRef::Date(v) => Value::Date(v),
1172 ValueRef::Timestamp(v) => Value::Timestamp(v),
1173 ValueRef::Time(v) => Value::Time(v),
1174 ValueRef::IntervalYearMonth(v) => Value::IntervalYearMonth(v),
1175 ValueRef::IntervalDayTime(v) => Value::IntervalDayTime(v),
1176 ValueRef::IntervalMonthDayNano(v) => Value::IntervalMonthDayNano(v),
1177 ValueRef::Duration(v) => Value::Duration(v),
1178 ValueRef::List(v) => v.to_value(),
1179 ValueRef::Decimal128(v) => Value::Decimal128(v),
1180 ValueRef::Struct(v) => v.to_value(),
1181 }
1182 }
1183}
1184
1185#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1187pub enum ValueRef<'a> {
1188 Null,
1189
1190 Boolean(bool),
1192 UInt8(u8),
1193 UInt16(u16),
1194 UInt32(u32),
1195 UInt64(u64),
1196 Int8(i8),
1197 Int16(i16),
1198 Int32(i32),
1199 Int64(i64),
1200 Float32(OrderedF32),
1201 Float64(OrderedF64),
1202
1203 Decimal128(Decimal128),
1205
1206 String(&'a str),
1208 Binary(&'a [u8]),
1209
1210 Date(Date),
1212 Timestamp(Timestamp),
1213 Time(Time),
1214 Duration(Duration),
1215 IntervalYearMonth(IntervalYearMonth),
1217 IntervalDayTime(IntervalDayTime),
1218 IntervalMonthDayNano(IntervalMonthDayNano),
1219
1220 List(ListValueRef<'a>),
1222 Struct(StructValueRef<'a>),
1223}
1224
1225macro_rules! impl_as_for_value_ref {
1226 ($value: ident, $Variant: ident) => {
1227 match $value {
1228 ValueRef::Null => Ok(None),
1229 ValueRef::$Variant(v) => Ok(Some(v.clone())),
1230 other => error::CastTypeSnafu {
1231 msg: format!(
1232 "Failed to cast value ref {:?} to {}",
1233 other,
1234 stringify!($Variant)
1235 ),
1236 }
1237 .fail(),
1238 }
1239 };
1240}
1241
1242impl<'a> ValueRef<'a> {
1243 define_data_type_func!(ValueRef);
1244
1245 pub fn is_null(&self) -> bool {
1247 matches!(self, ValueRef::Null)
1248 }
1249
1250 pub fn as_binary(&self) -> Result<Option<&'a [u8]>> {
1252 impl_as_for_value_ref!(self, Binary)
1253 }
1254
1255 pub fn as_string(&self) -> Result<Option<&'a str>> {
1257 impl_as_for_value_ref!(self, String)
1258 }
1259
1260 pub fn as_boolean(&self) -> Result<Option<bool>> {
1262 impl_as_for_value_ref!(self, Boolean)
1263 }
1264
1265 pub fn as_i8(&self) -> Result<Option<i8>> {
1266 impl_as_for_value_ref!(self, Int8)
1267 }
1268
1269 pub fn as_u8(&self) -> Result<Option<u8>> {
1270 impl_as_for_value_ref!(self, UInt8)
1271 }
1272
1273 pub fn as_i16(&self) -> Result<Option<i16>> {
1274 impl_as_for_value_ref!(self, Int16)
1275 }
1276
1277 pub fn as_u16(&self) -> Result<Option<u16>> {
1278 impl_as_for_value_ref!(self, UInt16)
1279 }
1280
1281 pub fn as_i32(&self) -> Result<Option<i32>> {
1282 impl_as_for_value_ref!(self, Int32)
1283 }
1284
1285 pub fn as_u32(&self) -> Result<Option<u32>> {
1286 impl_as_for_value_ref!(self, UInt32)
1287 }
1288
1289 pub fn as_i64(&self) -> Result<Option<i64>> {
1290 impl_as_for_value_ref!(self, Int64)
1291 }
1292
1293 pub fn as_u64(&self) -> Result<Option<u64>> {
1294 impl_as_for_value_ref!(self, UInt64)
1295 }
1296
1297 pub fn as_f32(&self) -> Result<Option<f32>> {
1298 match self {
1299 ValueRef::Null => Ok(None),
1300 ValueRef::Float32(f) => Ok(Some(f.0)),
1301 other => error::CastTypeSnafu {
1302 msg: format!("Failed to cast value ref {:?} to ValueRef::Float32", other,),
1303 }
1304 .fail(),
1305 }
1306 }
1307
1308 pub fn as_f64(&self) -> Result<Option<f64>> {
1309 match self {
1310 ValueRef::Null => Ok(None),
1311 ValueRef::Float64(f) => Ok(Some(f.0)),
1312 other => error::CastTypeSnafu {
1313 msg: format!("Failed to cast value ref {:?} to ValueRef::Float64", other,),
1314 }
1315 .fail(),
1316 }
1317 }
1318
1319 pub fn as_date(&self) -> Result<Option<Date>> {
1321 impl_as_for_value_ref!(self, Date)
1322 }
1323
1324 pub fn as_timestamp(&self) -> Result<Option<Timestamp>> {
1326 impl_as_for_value_ref!(self, Timestamp)
1327 }
1328
1329 pub fn as_time(&self) -> Result<Option<Time>> {
1331 impl_as_for_value_ref!(self, Time)
1332 }
1333
1334 pub fn as_duration(&self) -> Result<Option<Duration>> {
1335 impl_as_for_value_ref!(self, Duration)
1336 }
1337
1338 pub fn as_interval_year_month(&self) -> Result<Option<IntervalYearMonth>> {
1340 impl_as_for_value_ref!(self, IntervalYearMonth)
1341 }
1342
1343 pub fn as_interval_day_time(&self) -> Result<Option<IntervalDayTime>> {
1345 impl_as_for_value_ref!(self, IntervalDayTime)
1346 }
1347
1348 pub fn as_interval_month_day_nano(&self) -> Result<Option<IntervalMonthDayNano>> {
1350 impl_as_for_value_ref!(self, IntervalMonthDayNano)
1351 }
1352
1353 pub fn as_list(&self) -> Result<Option<ListValueRef<'_>>> {
1355 impl_as_for_value_ref!(self, List)
1356 }
1357
1358 pub fn as_struct(&self) -> Result<Option<StructValueRef<'_>>> {
1359 impl_as_for_value_ref!(self, Struct)
1360 }
1361
1362 pub fn as_decimal128(&self) -> Result<Option<Decimal128>> {
1364 impl_as_for_value_ref!(self, Decimal128)
1365 }
1366}
1367
1368impl PartialOrd for ValueRef<'_> {
1369 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1370 Some(self.cmp(other))
1371 }
1372}
1373
1374impl Ord for ValueRef<'_> {
1375 fn cmp(&self, other: &Self) -> Ordering {
1376 impl_ord_for_value_like!(ValueRef, self, other)
1377 }
1378}
1379
1380macro_rules! impl_value_ref_from {
1381 ($Variant:ident, $Type:ident) => {
1382 impl From<$Type> for ValueRef<'_> {
1383 fn from(value: $Type) -> Self {
1384 ValueRef::$Variant(value.into())
1385 }
1386 }
1387
1388 impl From<Option<$Type>> for ValueRef<'_> {
1389 fn from(value: Option<$Type>) -> Self {
1390 match value {
1391 Some(v) => ValueRef::$Variant(v.into()),
1392 None => ValueRef::Null,
1393 }
1394 }
1395 }
1396 };
1397}
1398
1399impl_value_ref_from!(Boolean, bool);
1400impl_value_ref_from!(UInt8, u8);
1401impl_value_ref_from!(UInt16, u16);
1402impl_value_ref_from!(UInt32, u32);
1403impl_value_ref_from!(UInt64, u64);
1404impl_value_ref_from!(Int8, i8);
1405impl_value_ref_from!(Int16, i16);
1406impl_value_ref_from!(Int32, i32);
1407impl_value_ref_from!(Int64, i64);
1408impl_value_ref_from!(Float32, f32);
1409impl_value_ref_from!(Float64, f64);
1410impl_value_ref_from!(Date, Date);
1411impl_value_ref_from!(Timestamp, Timestamp);
1412impl_value_ref_from!(Time, Time);
1413impl_value_ref_from!(IntervalYearMonth, IntervalYearMonth);
1414impl_value_ref_from!(IntervalDayTime, IntervalDayTime);
1415impl_value_ref_from!(IntervalMonthDayNano, IntervalMonthDayNano);
1416impl_value_ref_from!(Duration, Duration);
1417impl_value_ref_from!(Decimal128, Decimal128);
1418
1419impl<'a> From<&'a str> for ValueRef<'a> {
1420 fn from(string: &'a str) -> ValueRef<'a> {
1421 ValueRef::String(string)
1422 }
1423}
1424
1425impl<'a> From<&'a [u8]> for ValueRef<'a> {
1426 fn from(bytes: &'a [u8]) -> ValueRef<'a> {
1427 ValueRef::Binary(bytes)
1428 }
1429}
1430
1431impl<'a> From<Option<ListValueRef<'a>>> for ValueRef<'a> {
1432 fn from(list: Option<ListValueRef>) -> ValueRef {
1433 match list {
1434 Some(v) => ValueRef::List(v),
1435 None => ValueRef::Null,
1436 }
1437 }
1438}
1439
1440#[derive(Debug, Clone)]
1446pub enum ListValueRef<'a> {
1447 Indexed {
1449 vector: &'a ListVector,
1450 idx: usize,
1451 },
1452 Ref {
1453 val: &'a ListValue,
1454 },
1455 RefList {
1456 val: Vec<ValueRef<'a>>,
1457 item_datatype: ConcreteDataType,
1458 },
1459}
1460
1461impl ListValueRef<'_> {
1462 fn to_value(&self) -> Value {
1464 match self {
1465 ListValueRef::Indexed { vector, idx } => vector.get(*idx),
1466 ListValueRef::Ref { val } => Value::List((*val).clone()),
1467 ListValueRef::RefList { val, item_datatype } => Value::List(ListValue::new(
1468 val.iter().map(|v| Value::from(v.clone())).collect(),
1469 item_datatype.clone(),
1470 )),
1471 }
1472 }
1473 fn datatype(&self) -> ConcreteDataType {
1475 match self {
1476 ListValueRef::Indexed { vector, .. } => vector.data_type(),
1477 ListValueRef::Ref { val } => val.datatype().clone(),
1478 ListValueRef::RefList { item_datatype, .. } => item_datatype.clone(),
1479 }
1480 }
1481}
1482
1483impl Serialize for ListValueRef<'_> {
1484 fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
1485 match self {
1486 ListValueRef::Indexed { vector, idx } => match vector.get(*idx) {
1487 Value::List(v) => v.serialize(serializer),
1488 _ => unreachable!(),
1489 },
1490 ListValueRef::Ref { val } => val.serialize(serializer),
1491 ListValueRef::RefList { val, .. } => val.serialize(serializer),
1492 }
1493 }
1494}
1495
1496impl PartialEq for ListValueRef<'_> {
1497 fn eq(&self, other: &Self) -> bool {
1498 self.to_value().eq(&other.to_value())
1499 }
1500}
1501
1502impl Eq for ListValueRef<'_> {}
1503
1504impl Ord for ListValueRef<'_> {
1505 fn cmp(&self, other: &Self) -> Ordering {
1506 self.to_value().cmp(&other.to_value())
1508 }
1509}
1510
1511impl PartialOrd for ListValueRef<'_> {
1512 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1513 Some(self.cmp(other))
1514 }
1515}
1516
1517#[derive(Debug, Clone)]
1518pub enum StructValueRef<'a> {
1519 Indexed {
1520 vector: &'a StructVector,
1521 idx: usize,
1522 },
1523 Ref(&'a StructValue),
1524 RefList {
1525 val: Vec<ValueRef<'a>>,
1526 fields: StructType,
1527 },
1528}
1529
1530impl<'a> StructValueRef<'a> {
1531 pub fn to_value(&self) -> Value {
1532 match self {
1533 StructValueRef::Indexed { vector, idx } => vector.get(*idx),
1534 StructValueRef::Ref(val) => Value::Struct((*val).clone()),
1535 StructValueRef::RefList { val, fields } => {
1536 let items = val.iter().map(|v| Value::from(v.clone())).collect();
1537 Value::Struct(StructValue::try_new(items, fields.clone()).unwrap())
1538 }
1539 }
1540 }
1541
1542 pub fn struct_type(&self) -> &StructType {
1543 match self {
1544 StructValueRef::Indexed { vector, .. } => vector.struct_type(),
1545 StructValueRef::Ref(val) => val.struct_type(),
1546 StructValueRef::RefList { fields, .. } => fields,
1547 }
1548 }
1549}
1550
1551impl Serialize for StructValueRef<'_> {
1552 fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
1553 match self {
1554 StructValueRef::Indexed { vector, idx } => match vector.get(*idx) {
1555 Value::Struct(v) => v.serialize(serializer),
1556 _ => unreachable!(),
1557 },
1558 StructValueRef::Ref(val) => val.serialize(serializer),
1559 StructValueRef::RefList { val, .. } => val.serialize(serializer),
1560 }
1561 }
1562}
1563
1564impl PartialEq for StructValueRef<'_> {
1565 fn eq(&self, other: &Self) -> bool {
1566 self.to_value().eq(&other.to_value())
1567 }
1568}
1569
1570impl Eq for StructValueRef<'_> {}
1571
1572impl Ord for StructValueRef<'_> {
1573 fn cmp(&self, other: &Self) -> Ordering {
1574 self.to_value().cmp(&other.to_value())
1576 }
1577}
1578
1579impl PartialOrd for StructValueRef<'_> {
1580 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1581 Some(self.cmp(other))
1582 }
1583}
1584
1585impl ValueRef<'_> {
1586 pub fn data_size(&self) -> usize {
1589 match self {
1590 ValueRef::Null => 8,
1593 ValueRef::Boolean(_) => 1,
1594 ValueRef::UInt8(_) => 1,
1595 ValueRef::UInt16(_) => 2,
1596 ValueRef::UInt32(_) => 4,
1597 ValueRef::UInt64(_) => 8,
1598 ValueRef::Int8(_) => 1,
1599 ValueRef::Int16(_) => 2,
1600 ValueRef::Int32(_) => 4,
1601 ValueRef::Int64(_) => 8,
1602 ValueRef::Float32(_) => 4,
1603 ValueRef::Float64(_) => 8,
1604 ValueRef::String(v) => std::mem::size_of_val(*v),
1605 ValueRef::Binary(v) => std::mem::size_of_val(*v),
1606 ValueRef::Date(_) => 4,
1607 ValueRef::Timestamp(_) => 16,
1608 ValueRef::Time(_) => 16,
1609 ValueRef::Duration(_) => 16,
1610 ValueRef::IntervalYearMonth(_) => 4,
1611 ValueRef::IntervalDayTime(_) => 8,
1612 ValueRef::IntervalMonthDayNano(_) => 16,
1613 ValueRef::Decimal128(_) => 32,
1614 ValueRef::List(v) => match v {
1615 ListValueRef::Indexed { vector, .. } => vector.memory_size() / vector.len(),
1616 ListValueRef::Ref { val } => val.estimated_size(),
1617 ListValueRef::RefList { val, .. } => val.iter().map(|v| v.data_size()).sum(),
1618 },
1619 ValueRef::Struct(val) => match val {
1620 StructValueRef::Indexed { vector, .. } => vector.memory_size() / vector.len(),
1621 StructValueRef::Ref(val) => val.estimated_size(),
1622 StructValueRef::RefList { val, .. } => val.iter().map(|v| v.data_size()).sum(),
1623 },
1624 }
1625 }
1626}
1627
1628#[cfg(test)]
1629pub(crate) mod tests {
1630 use arrow::datatypes::{DataType as ArrowDataType, Field};
1631 use common_time::timezone::set_default_timezone;
1632 use num_traits::Float;
1633
1634 use super::*;
1635 use crate::types::StructField;
1636 use crate::vectors::ListVectorBuilder;
1637
1638 pub(crate) fn build_struct_type() -> StructType {
1639 StructType::new(vec![
1640 StructField::new("id".to_string(), ConcreteDataType::int32_datatype(), false),
1641 StructField::new(
1642 "name".to_string(),
1643 ConcreteDataType::string_datatype(),
1644 true,
1645 ),
1646 StructField::new("age".to_string(), ConcreteDataType::uint8_datatype(), true),
1647 StructField::new(
1648 "address".to_string(),
1649 ConcreteDataType::string_datatype(),
1650 true,
1651 ),
1652 ])
1653 }
1654
1655 pub(crate) fn build_struct_value() -> StructValue {
1656 let struct_type = build_struct_type();
1657
1658 let struct_items = vec![
1659 Value::Int32(1),
1660 Value::String("tom".into()),
1661 Value::UInt8(25),
1662 Value::String("94038".into()),
1663 ];
1664 StructValue::try_new(struct_items, struct_type).unwrap()
1665 }
1666
1667 pub(crate) fn build_scalar_struct_value() -> ScalarValue {
1668 let struct_type = build_struct_type();
1669 let arrays = vec![
1670 ScalarValue::Int32(Some(1)).to_array().unwrap(),
1671 ScalarValue::Utf8(Some("tom".into())).to_array().unwrap(),
1672 ScalarValue::UInt8(Some(25)).to_array().unwrap(),
1673 ScalarValue::Utf8(Some("94038".into())).to_array().unwrap(),
1674 ];
1675 let struct_arrow_array = StructArray::new(struct_type.as_arrow_fields(), arrays, None);
1676 ScalarValue::Struct(Arc::new(struct_arrow_array))
1677 }
1678
1679 pub(crate) fn build_list_value() -> ListValue {
1680 let items = vec![Value::Boolean(true), Value::Boolean(false)];
1681 ListValue::new(items, ConcreteDataType::boolean_datatype())
1682 }
1683
1684 pub(crate) fn build_scalar_list_value() -> ScalarValue {
1685 let items = vec![
1686 ScalarValue::Boolean(Some(true)),
1687 ScalarValue::Boolean(Some(false)),
1688 ];
1689 ScalarValue::List(ScalarValue::new_list(&items, &ArrowDataType::Boolean, true))
1690 }
1691
1692 #[test]
1693 fn test_try_from_scalar_value() {
1694 assert_eq!(
1695 Value::Boolean(true),
1696 ScalarValue::Boolean(Some(true)).try_into().unwrap()
1697 );
1698 assert_eq!(
1699 Value::Boolean(false),
1700 ScalarValue::Boolean(Some(false)).try_into().unwrap()
1701 );
1702 assert_eq!(Value::Null, ScalarValue::Boolean(None).try_into().unwrap());
1703
1704 assert_eq!(
1705 Value::Float32(1.0f32.into()),
1706 ScalarValue::Float32(Some(1.0f32)).try_into().unwrap()
1707 );
1708 assert_eq!(Value::Null, ScalarValue::Float32(None).try_into().unwrap());
1709
1710 assert_eq!(
1711 Value::Float64(2.0f64.into()),
1712 ScalarValue::Float64(Some(2.0f64)).try_into().unwrap()
1713 );
1714 assert_eq!(Value::Null, ScalarValue::Float64(None).try_into().unwrap());
1715
1716 assert_eq!(
1717 Value::Int8(i8::MAX),
1718 ScalarValue::Int8(Some(i8::MAX)).try_into().unwrap()
1719 );
1720 assert_eq!(Value::Null, ScalarValue::Int8(None).try_into().unwrap());
1721
1722 assert_eq!(
1723 Value::Int16(i16::MAX),
1724 ScalarValue::Int16(Some(i16::MAX)).try_into().unwrap()
1725 );
1726 assert_eq!(Value::Null, ScalarValue::Int16(None).try_into().unwrap());
1727
1728 assert_eq!(
1729 Value::Int32(i32::MAX),
1730 ScalarValue::Int32(Some(i32::MAX)).try_into().unwrap()
1731 );
1732 assert_eq!(Value::Null, ScalarValue::Int32(None).try_into().unwrap());
1733
1734 assert_eq!(
1735 Value::Int64(i64::MAX),
1736 ScalarValue::Int64(Some(i64::MAX)).try_into().unwrap()
1737 );
1738 assert_eq!(Value::Null, ScalarValue::Int64(None).try_into().unwrap());
1739
1740 assert_eq!(
1741 Value::UInt8(u8::MAX),
1742 ScalarValue::UInt8(Some(u8::MAX)).try_into().unwrap()
1743 );
1744 assert_eq!(Value::Null, ScalarValue::UInt8(None).try_into().unwrap());
1745
1746 assert_eq!(
1747 Value::UInt16(u16::MAX),
1748 ScalarValue::UInt16(Some(u16::MAX)).try_into().unwrap()
1749 );
1750 assert_eq!(Value::Null, ScalarValue::UInt16(None).try_into().unwrap());
1751
1752 assert_eq!(
1753 Value::UInt32(u32::MAX),
1754 ScalarValue::UInt32(Some(u32::MAX)).try_into().unwrap()
1755 );
1756 assert_eq!(Value::Null, ScalarValue::UInt32(None).try_into().unwrap());
1757
1758 assert_eq!(
1759 Value::UInt64(u64::MAX),
1760 ScalarValue::UInt64(Some(u64::MAX)).try_into().unwrap()
1761 );
1762 assert_eq!(Value::Null, ScalarValue::UInt64(None).try_into().unwrap());
1763
1764 assert_eq!(
1765 Value::from("hello"),
1766 ScalarValue::Utf8(Some("hello".to_string()))
1767 .try_into()
1768 .unwrap()
1769 );
1770 assert_eq!(Value::Null, ScalarValue::Utf8(None).try_into().unwrap());
1771
1772 assert_eq!(
1773 Value::from("large_hello"),
1774 ScalarValue::LargeUtf8(Some("large_hello".to_string()))
1775 .try_into()
1776 .unwrap()
1777 );
1778 assert_eq!(
1779 Value::Null,
1780 ScalarValue::LargeUtf8(None).try_into().unwrap()
1781 );
1782
1783 assert_eq!(
1784 Value::from("world".as_bytes()),
1785 ScalarValue::Binary(Some("world".as_bytes().to_vec()))
1786 .try_into()
1787 .unwrap()
1788 );
1789 assert_eq!(Value::Null, ScalarValue::Binary(None).try_into().unwrap());
1790
1791 assert_eq!(
1792 Value::from("large_world".as_bytes()),
1793 ScalarValue::LargeBinary(Some("large_world".as_bytes().to_vec()))
1794 .try_into()
1795 .unwrap()
1796 );
1797 assert_eq!(
1798 Value::Null,
1799 ScalarValue::LargeBinary(None).try_into().unwrap()
1800 );
1801
1802 assert_eq!(
1803 Value::List(build_list_value()),
1804 build_scalar_list_value().try_into().unwrap()
1805 );
1806 assert_eq!(
1807 Value::List(ListValue::new(vec![], ConcreteDataType::uint32_datatype())),
1808 ScalarValue::List(ScalarValue::new_list(&[], &ArrowDataType::UInt32, true))
1809 .try_into()
1810 .unwrap()
1811 );
1812
1813 assert_eq!(
1814 Value::Date(Date::new(123)),
1815 ScalarValue::Date32(Some(123)).try_into().unwrap()
1816 );
1817 assert_eq!(Value::Null, ScalarValue::Date32(None).try_into().unwrap());
1818
1819 assert_eq!(
1820 Value::Timestamp(Timestamp::new(1, TimeUnit::Second)),
1821 ScalarValue::TimestampSecond(Some(1), None)
1822 .try_into()
1823 .unwrap()
1824 );
1825 assert_eq!(
1826 Value::Null,
1827 ScalarValue::TimestampSecond(None, None).try_into().unwrap()
1828 );
1829
1830 assert_eq!(
1831 Value::Timestamp(Timestamp::new(1, TimeUnit::Millisecond)),
1832 ScalarValue::TimestampMillisecond(Some(1), None)
1833 .try_into()
1834 .unwrap()
1835 );
1836 assert_eq!(
1837 Value::Null,
1838 ScalarValue::TimestampMillisecond(None, None)
1839 .try_into()
1840 .unwrap()
1841 );
1842
1843 assert_eq!(
1844 Value::Timestamp(Timestamp::new(1, TimeUnit::Microsecond)),
1845 ScalarValue::TimestampMicrosecond(Some(1), None)
1846 .try_into()
1847 .unwrap()
1848 );
1849 assert_eq!(
1850 Value::Null,
1851 ScalarValue::TimestampMicrosecond(None, None)
1852 .try_into()
1853 .unwrap()
1854 );
1855
1856 assert_eq!(
1857 Value::Timestamp(Timestamp::new(1, TimeUnit::Nanosecond)),
1858 ScalarValue::TimestampNanosecond(Some(1), None)
1859 .try_into()
1860 .unwrap()
1861 );
1862 assert_eq!(
1863 Value::Null,
1864 ScalarValue::TimestampNanosecond(None, None)
1865 .try_into()
1866 .unwrap()
1867 );
1868 assert_eq!(
1869 Value::Null,
1870 ScalarValue::IntervalMonthDayNano(None).try_into().unwrap()
1871 );
1872 assert_eq!(
1873 Value::IntervalMonthDayNano(IntervalMonthDayNano::new(1, 1, 1)),
1874 ScalarValue::IntervalMonthDayNano(Some(IntervalMonthDayNano::new(1, 1, 1).into()))
1875 .try_into()
1876 .unwrap()
1877 );
1878
1879 assert_eq!(
1880 Value::Time(Time::new(1, TimeUnit::Second)),
1881 ScalarValue::Time32Second(Some(1)).try_into().unwrap()
1882 );
1883 assert_eq!(
1884 Value::Null,
1885 ScalarValue::Time32Second(None).try_into().unwrap()
1886 );
1887
1888 assert_eq!(
1889 Value::Time(Time::new(1, TimeUnit::Millisecond)),
1890 ScalarValue::Time32Millisecond(Some(1)).try_into().unwrap()
1891 );
1892 assert_eq!(
1893 Value::Null,
1894 ScalarValue::Time32Millisecond(None).try_into().unwrap()
1895 );
1896
1897 assert_eq!(
1898 Value::Time(Time::new(1, TimeUnit::Microsecond)),
1899 ScalarValue::Time64Microsecond(Some(1)).try_into().unwrap()
1900 );
1901 assert_eq!(
1902 Value::Null,
1903 ScalarValue::Time64Microsecond(None).try_into().unwrap()
1904 );
1905
1906 assert_eq!(
1907 Value::Time(Time::new(1, TimeUnit::Nanosecond)),
1908 ScalarValue::Time64Nanosecond(Some(1)).try_into().unwrap()
1909 );
1910 assert_eq!(
1911 Value::Null,
1912 ScalarValue::Time64Nanosecond(None).try_into().unwrap()
1913 );
1914
1915 assert_eq!(
1916 Value::Duration(Duration::new_second(1)),
1917 ScalarValue::DurationSecond(Some(1)).try_into().unwrap()
1918 );
1919 assert_eq!(
1920 Value::Null,
1921 ScalarValue::DurationSecond(None).try_into().unwrap()
1922 );
1923
1924 assert_eq!(
1925 Value::Duration(Duration::new_millisecond(1)),
1926 ScalarValue::DurationMillisecond(Some(1))
1927 .try_into()
1928 .unwrap()
1929 );
1930 assert_eq!(
1931 Value::Null,
1932 ScalarValue::DurationMillisecond(None).try_into().unwrap()
1933 );
1934
1935 assert_eq!(
1936 Value::Duration(Duration::new_microsecond(1)),
1937 ScalarValue::DurationMicrosecond(Some(1))
1938 .try_into()
1939 .unwrap()
1940 );
1941 assert_eq!(
1942 Value::Null,
1943 ScalarValue::DurationMicrosecond(None).try_into().unwrap()
1944 );
1945
1946 assert_eq!(
1947 Value::Duration(Duration::new_nanosecond(1)),
1948 ScalarValue::DurationNanosecond(Some(1)).try_into().unwrap()
1949 );
1950 assert_eq!(
1951 Value::Null,
1952 ScalarValue::DurationNanosecond(None).try_into().unwrap()
1953 );
1954
1955 assert_eq!(
1956 Value::Decimal128(Decimal128::new(1, 38, 10)),
1957 ScalarValue::Decimal128(Some(1), 38, 10).try_into().unwrap()
1958 );
1959 assert_eq!(
1960 Value::Null,
1961 ScalarValue::Decimal128(None, 0, 0).try_into().unwrap()
1962 );
1963
1964 let struct_value = build_struct_value();
1965 let scalar_struct_value = build_scalar_struct_value();
1966 assert_eq!(
1967 Value::Struct(struct_value),
1968 scalar_struct_value.try_into().unwrap()
1969 );
1970 }
1971
1972 #[test]
1973 fn test_value_from_inner() {
1974 assert_eq!(Value::Boolean(true), Value::from(true));
1975 assert_eq!(Value::Boolean(false), Value::from(false));
1976
1977 assert_eq!(Value::UInt8(u8::MIN), Value::from(u8::MIN));
1978 assert_eq!(Value::UInt8(u8::MAX), Value::from(u8::MAX));
1979
1980 assert_eq!(Value::UInt16(u16::MIN), Value::from(u16::MIN));
1981 assert_eq!(Value::UInt16(u16::MAX), Value::from(u16::MAX));
1982
1983 assert_eq!(Value::UInt32(u32::MIN), Value::from(u32::MIN));
1984 assert_eq!(Value::UInt32(u32::MAX), Value::from(u32::MAX));
1985
1986 assert_eq!(Value::UInt64(u64::MIN), Value::from(u64::MIN));
1987 assert_eq!(Value::UInt64(u64::MAX), Value::from(u64::MAX));
1988
1989 assert_eq!(Value::Int8(i8::MIN), Value::from(i8::MIN));
1990 assert_eq!(Value::Int8(i8::MAX), Value::from(i8::MAX));
1991
1992 assert_eq!(Value::Int16(i16::MIN), Value::from(i16::MIN));
1993 assert_eq!(Value::Int16(i16::MAX), Value::from(i16::MAX));
1994
1995 assert_eq!(Value::Int32(i32::MIN), Value::from(i32::MIN));
1996 assert_eq!(Value::Int32(i32::MAX), Value::from(i32::MAX));
1997
1998 assert_eq!(Value::Int64(i64::MIN), Value::from(i64::MIN));
1999 assert_eq!(Value::Int64(i64::MAX), Value::from(i64::MAX));
2000
2001 assert_eq!(
2002 Value::Float32(OrderedFloat(f32::MIN)),
2003 Value::from(f32::MIN)
2004 );
2005 assert_eq!(
2006 Value::Float32(OrderedFloat(f32::MAX)),
2007 Value::from(f32::MAX)
2008 );
2009
2010 assert_eq!(
2011 Value::Float64(OrderedFloat(f64::MIN)),
2012 Value::from(f64::MIN)
2013 );
2014 assert_eq!(
2015 Value::Float64(OrderedFloat(f64::MAX)),
2016 Value::from(f64::MAX)
2017 );
2018
2019 let string_bytes = StringBytes::from("hello");
2020 assert_eq!(
2021 Value::String(string_bytes.clone()),
2022 Value::from(string_bytes)
2023 );
2024
2025 let bytes = Bytes::from(b"world".as_slice());
2026 assert_eq!(Value::Binary(bytes.clone()), Value::from(bytes));
2027 }
2028
2029 fn check_type_and_value(data_type: &ConcreteDataType, value: &Value) {
2030 assert_eq!(*data_type, value.data_type());
2031 assert_eq!(data_type.logical_type_id(), value.logical_type_id());
2032 }
2033
2034 #[test]
2035 fn test_value_datatype() {
2036 check_type_and_value(&ConcreteDataType::boolean_datatype(), &Value::Boolean(true));
2037 check_type_and_value(&ConcreteDataType::uint8_datatype(), &Value::UInt8(u8::MIN));
2038 check_type_and_value(
2039 &ConcreteDataType::uint16_datatype(),
2040 &Value::UInt16(u16::MIN),
2041 );
2042 check_type_and_value(
2043 &ConcreteDataType::uint16_datatype(),
2044 &Value::UInt16(u16::MAX),
2045 );
2046 check_type_and_value(
2047 &ConcreteDataType::uint32_datatype(),
2048 &Value::UInt32(u32::MIN),
2049 );
2050 check_type_and_value(
2051 &ConcreteDataType::uint64_datatype(),
2052 &Value::UInt64(u64::MIN),
2053 );
2054 check_type_and_value(&ConcreteDataType::int8_datatype(), &Value::Int8(i8::MIN));
2055 check_type_and_value(&ConcreteDataType::int16_datatype(), &Value::Int16(i16::MIN));
2056 check_type_and_value(&ConcreteDataType::int32_datatype(), &Value::Int32(i32::MIN));
2057 check_type_and_value(&ConcreteDataType::int64_datatype(), &Value::Int64(i64::MIN));
2058 check_type_and_value(
2059 &ConcreteDataType::float32_datatype(),
2060 &Value::Float32(OrderedFloat(f32::MIN)),
2061 );
2062 check_type_and_value(
2063 &ConcreteDataType::float64_datatype(),
2064 &Value::Float64(OrderedFloat(f64::MIN)),
2065 );
2066 check_type_and_value(
2067 &ConcreteDataType::string_datatype(),
2068 &Value::String(StringBytes::from("hello")),
2069 );
2070 check_type_and_value(
2071 &ConcreteDataType::binary_datatype(),
2072 &Value::Binary(Bytes::from(b"world".as_slice())),
2073 );
2074 check_type_and_value(
2075 &ConcreteDataType::list_datatype(ConcreteDataType::int32_datatype()),
2076 &Value::List(ListValue::new(
2077 vec![Value::Int32(10)],
2078 ConcreteDataType::int32_datatype(),
2079 )),
2080 );
2081 check_type_and_value(
2082 &ConcreteDataType::list_datatype(ConcreteDataType::null_datatype()),
2083 &Value::List(ListValue::default()),
2084 );
2085 check_type_and_value(
2086 &ConcreteDataType::date_datatype(),
2087 &Value::Date(Date::new(1)),
2088 );
2089 check_type_and_value(
2090 &ConcreteDataType::timestamp_millisecond_datatype(),
2091 &Value::Timestamp(Timestamp::new_millisecond(1)),
2092 );
2093 check_type_and_value(
2094 &ConcreteDataType::time_second_datatype(),
2095 &Value::Time(Time::new_second(1)),
2096 );
2097 check_type_and_value(
2098 &ConcreteDataType::time_millisecond_datatype(),
2099 &Value::Time(Time::new_millisecond(1)),
2100 );
2101 check_type_and_value(
2102 &ConcreteDataType::time_microsecond_datatype(),
2103 &Value::Time(Time::new_microsecond(1)),
2104 );
2105 check_type_and_value(
2106 &ConcreteDataType::time_nanosecond_datatype(),
2107 &Value::Time(Time::new_nanosecond(1)),
2108 );
2109 check_type_and_value(
2110 &ConcreteDataType::interval_year_month_datatype(),
2111 &Value::IntervalYearMonth(IntervalYearMonth::new(1)),
2112 );
2113 check_type_and_value(
2114 &ConcreteDataType::interval_day_time_datatype(),
2115 &Value::IntervalDayTime(IntervalDayTime::new(1, 2)),
2116 );
2117 check_type_and_value(
2118 &ConcreteDataType::interval_month_day_nano_datatype(),
2119 &Value::IntervalMonthDayNano(IntervalMonthDayNano::new(1, 2, 3)),
2120 );
2121 check_type_and_value(
2122 &ConcreteDataType::duration_second_datatype(),
2123 &Value::Duration(Duration::new_second(1)),
2124 );
2125 check_type_and_value(
2126 &ConcreteDataType::duration_millisecond_datatype(),
2127 &Value::Duration(Duration::new_millisecond(1)),
2128 );
2129 check_type_and_value(
2130 &ConcreteDataType::duration_microsecond_datatype(),
2131 &Value::Duration(Duration::new_microsecond(1)),
2132 );
2133 check_type_and_value(
2134 &ConcreteDataType::duration_nanosecond_datatype(),
2135 &Value::Duration(Duration::new_nanosecond(1)),
2136 );
2137 check_type_and_value(
2138 &ConcreteDataType::decimal128_datatype(38, 10),
2139 &Value::Decimal128(Decimal128::new(1, 38, 10)),
2140 );
2141
2142 check_type_and_value(
2143 &ConcreteDataType::list_datatype(ConcreteDataType::boolean_datatype()),
2144 &Value::List(ListValue::new(
2145 vec![Value::Boolean(true)],
2146 ConcreteDataType::boolean_datatype(),
2147 )),
2148 );
2149
2150 check_type_and_value(
2151 &ConcreteDataType::struct_datatype(build_struct_type()),
2152 &Value::Struct(build_struct_value()),
2153 );
2154 }
2155
2156 #[test]
2157 fn test_value_from_string() {
2158 let hello = "hello".to_string();
2159 assert_eq!(
2160 Value::String(StringBytes::from(hello.clone())),
2161 Value::from(hello)
2162 );
2163
2164 let world = "world";
2165 assert_eq!(Value::String(StringBytes::from(world)), Value::from(world));
2166 }
2167
2168 #[test]
2169 fn test_value_from_bytes() {
2170 let hello = b"hello".to_vec();
2171 assert_eq!(
2172 Value::Binary(Bytes::from(hello.clone())),
2173 Value::from(hello)
2174 );
2175
2176 let world: &[u8] = b"world";
2177 assert_eq!(Value::Binary(Bytes::from(world)), Value::from(world));
2178 }
2179
2180 fn to_json(value: Value) -> serde_json::Value {
2181 value.try_into().unwrap()
2182 }
2183
2184 #[test]
2185 fn test_to_json_value() {
2186 assert_eq!(serde_json::Value::Null, to_json(Value::Null));
2187 assert_eq!(serde_json::Value::Bool(true), to_json(Value::Boolean(true)));
2188 assert_eq!(
2189 serde_json::Value::Number(20u8.into()),
2190 to_json(Value::UInt8(20))
2191 );
2192 assert_eq!(
2193 serde_json::Value::Number(20i8.into()),
2194 to_json(Value::Int8(20))
2195 );
2196 assert_eq!(
2197 serde_json::Value::Number(2000u16.into()),
2198 to_json(Value::UInt16(2000))
2199 );
2200 assert_eq!(
2201 serde_json::Value::Number(2000i16.into()),
2202 to_json(Value::Int16(2000))
2203 );
2204 assert_eq!(
2205 serde_json::Value::Number(3000u32.into()),
2206 to_json(Value::UInt32(3000))
2207 );
2208 assert_eq!(
2209 serde_json::Value::Number(3000i32.into()),
2210 to_json(Value::Int32(3000))
2211 );
2212 assert_eq!(
2213 serde_json::Value::Number(4000u64.into()),
2214 to_json(Value::UInt64(4000))
2215 );
2216 assert_eq!(
2217 serde_json::Value::Number(4000i64.into()),
2218 to_json(Value::Int64(4000))
2219 );
2220 assert_eq!(
2221 serde_json::Value::from(125.0f32),
2222 to_json(Value::Float32(125.0.into()))
2223 );
2224 assert_eq!(
2225 serde_json::Value::from(125.0f64),
2226 to_json(Value::Float64(125.0.into()))
2227 );
2228 assert_eq!(
2229 serde_json::Value::String(String::from("hello")),
2230 to_json(Value::String(StringBytes::from("hello")))
2231 );
2232 assert_eq!(
2233 serde_json::Value::from(b"world".as_slice()),
2234 to_json(Value::Binary(Bytes::from(b"world".as_slice())))
2235 );
2236 assert_eq!(
2237 serde_json::Value::Number(5000i32.into()),
2238 to_json(Value::Date(Date::new(5000)))
2239 );
2240 assert_eq!(
2241 serde_json::Value::Number(1.into()),
2242 to_json(Value::Timestamp(Timestamp::new_millisecond(1)))
2243 );
2244 assert_eq!(
2245 serde_json::Value::Number(1.into()),
2246 to_json(Value::Time(Time::new_millisecond(1)))
2247 );
2248 assert_eq!(
2249 serde_json::Value::Number(1.into()),
2250 to_json(Value::Duration(Duration::new_millisecond(1)))
2251 );
2252
2253 let json_value: serde_json::Value = serde_json::from_str(r#"[123]"#).unwrap();
2254 assert_eq!(
2255 json_value,
2256 to_json(Value::List(ListValue {
2257 items: vec![Value::Int32(123)],
2258 datatype: ConcreteDataType::int32_datatype(),
2259 }))
2260 );
2261
2262 let struct_value = StructValue::try_new(
2263 vec![
2264 Value::Int64(42),
2265 Value::String("tomcat".into()),
2266 Value::Boolean(true),
2267 ],
2268 StructType::new(vec![
2269 StructField::new("num".to_string(), ConcreteDataType::int64_datatype(), true),
2270 StructField::new(
2271 "name".to_string(),
2272 ConcreteDataType::string_datatype(),
2273 true,
2274 ),
2275 StructField::new(
2276 "yes_or_no".to_string(),
2277 ConcreteDataType::boolean_datatype(),
2278 true,
2279 ),
2280 ]),
2281 )
2282 .unwrap();
2283 assert_eq!(
2284 serde_json::Value::try_from(Value::Struct(struct_value)).unwrap(),
2285 serde_json::json!({
2286 "num": 42,
2287 "name": "tomcat",
2288 "yes_or_no": true
2289 })
2290 );
2291 }
2292
2293 #[test]
2294 fn test_null_value() {
2295 assert!(Value::Null.is_null());
2296 assert!(!Value::Boolean(true).is_null());
2297 assert!(Value::Null < Value::Boolean(false));
2298 assert!(Value::Boolean(true) > Value::Null);
2299 assert!(Value::Null < Value::Int32(10));
2300 assert!(Value::Int32(10) > Value::Null);
2301 }
2302
2303 #[test]
2304 fn test_null_value_ref() {
2305 assert!(ValueRef::Null.is_null());
2306 assert!(!ValueRef::Boolean(true).is_null());
2307 assert!(ValueRef::Null < ValueRef::Boolean(false));
2308 assert!(ValueRef::Boolean(true) > ValueRef::Null);
2309 assert!(ValueRef::Null < ValueRef::Int32(10));
2310 assert!(ValueRef::Int32(10) > ValueRef::Null);
2311 }
2312
2313 #[test]
2314 fn test_as_value_ref() {
2315 macro_rules! check_as_value_ref {
2316 ($Variant: ident, $data: expr) => {
2317 let value = Value::$Variant($data);
2318 let value_ref = value.as_value_ref();
2319 let expect_ref = ValueRef::$Variant($data);
2320
2321 assert_eq!(expect_ref, value_ref);
2322 };
2323 }
2324
2325 assert_eq!(ValueRef::Null, Value::Null.as_value_ref());
2326 check_as_value_ref!(Boolean, true);
2327 check_as_value_ref!(UInt8, 123);
2328 check_as_value_ref!(UInt16, 123);
2329 check_as_value_ref!(UInt32, 123);
2330 check_as_value_ref!(UInt64, 123);
2331 check_as_value_ref!(Int8, -12);
2332 check_as_value_ref!(Int16, -12);
2333 check_as_value_ref!(Int32, -12);
2334 check_as_value_ref!(Int64, -12);
2335 check_as_value_ref!(Float32, OrderedF32::from(16.0));
2336 check_as_value_ref!(Float64, OrderedF64::from(16.0));
2337 check_as_value_ref!(Timestamp, Timestamp::new_millisecond(1));
2338 check_as_value_ref!(Time, Time::new_millisecond(1));
2339 check_as_value_ref!(IntervalYearMonth, IntervalYearMonth::new(1));
2340 check_as_value_ref!(IntervalDayTime, IntervalDayTime::new(1, 2));
2341 check_as_value_ref!(IntervalMonthDayNano, IntervalMonthDayNano::new(1, 2, 3));
2342 check_as_value_ref!(Duration, Duration::new_millisecond(1));
2343
2344 assert_eq!(
2345 ValueRef::String("hello"),
2346 Value::String("hello".into()).as_value_ref()
2347 );
2348 assert_eq!(
2349 ValueRef::Binary(b"hello"),
2350 Value::Binary("hello".as_bytes().into()).as_value_ref()
2351 );
2352
2353 check_as_value_ref!(Date, Date::new(103));
2354
2355 let list = build_list_value();
2356 assert_eq!(
2357 ValueRef::List(ListValueRef::Ref { val: &list }),
2358 Value::List(list.clone()).as_value_ref()
2359 );
2360
2361 let jsonb_value = jsonb::parse_value(r#"{"key": "value"}"#.as_bytes())
2362 .unwrap()
2363 .to_vec();
2364 assert_eq!(
2365 ValueRef::Binary(jsonb_value.clone().as_slice()),
2366 Value::Binary(jsonb_value.into()).as_value_ref()
2367 );
2368
2369 let struct_value = build_struct_value();
2370 assert_eq!(
2371 ValueRef::Struct(StructValueRef::Ref(&struct_value)),
2372 Value::Struct(struct_value.clone()).as_value_ref()
2373 );
2374 }
2375
2376 #[test]
2377 fn test_value_ref_as() {
2378 macro_rules! check_as_null {
2379 ($method: ident) => {
2380 assert_eq!(None, ValueRef::Null.$method().unwrap());
2381 };
2382 }
2383
2384 check_as_null!(as_binary);
2385 check_as_null!(as_string);
2386 check_as_null!(as_boolean);
2387 check_as_null!(as_date);
2388 check_as_null!(as_list);
2389
2390 macro_rules! check_as_correct {
2391 ($data: expr, $Variant: ident, $method: ident) => {
2392 assert_eq!(Some($data), ValueRef::$Variant($data).$method().unwrap());
2393 };
2394 }
2395
2396 check_as_correct!("hello", String, as_string);
2397 check_as_correct!("hello".as_bytes(), Binary, as_binary);
2398 check_as_correct!(true, Boolean, as_boolean);
2399 check_as_correct!(Date::new(123), Date, as_date);
2400 check_as_correct!(Time::new_second(12), Time, as_time);
2401 check_as_correct!(Duration::new_second(12), Duration, as_duration);
2402
2403 let list = build_list_value();
2404 check_as_correct!(ListValueRef::Ref { val: &list }, List, as_list);
2405
2406 let struct_value = build_struct_value();
2407 check_as_correct!(StructValueRef::Ref(&struct_value), Struct, as_struct);
2408
2409 let wrong_value = ValueRef::Int32(12345);
2410 assert!(wrong_value.as_binary().is_err());
2411 assert!(wrong_value.as_string().is_err());
2412 assert!(wrong_value.as_boolean().is_err());
2413 assert!(wrong_value.as_date().is_err());
2414 assert!(wrong_value.as_list().is_err());
2415 assert!(wrong_value.as_time().is_err());
2416 assert!(wrong_value.as_timestamp().is_err());
2417 }
2418
2419 #[test]
2420 fn test_display() {
2421 set_default_timezone(Some("Asia/Shanghai")).unwrap();
2422 assert_eq!(Value::Null.to_string(), "Null");
2423 assert_eq!(Value::UInt8(8).to_string(), "8");
2424 assert_eq!(Value::UInt16(16).to_string(), "16");
2425 assert_eq!(Value::UInt32(32).to_string(), "32");
2426 assert_eq!(Value::UInt64(64).to_string(), "64");
2427 assert_eq!(Value::Int8(-8).to_string(), "-8");
2428 assert_eq!(Value::Int16(-16).to_string(), "-16");
2429 assert_eq!(Value::Int32(-32).to_string(), "-32");
2430 assert_eq!(Value::Int64(-64).to_string(), "-64");
2431 assert_eq!(Value::Float32((-32.123).into()).to_string(), "-32.123");
2432 assert_eq!(Value::Float64((-64.123).into()).to_string(), "-64.123");
2433 assert_eq!(Value::Float64(OrderedF64::infinity()).to_string(), "inf");
2434 assert_eq!(Value::Float64(OrderedF64::nan()).to_string(), "NaN");
2435 assert_eq!(Value::String(StringBytes::from("123")).to_string(), "123");
2436 assert_eq!(
2437 Value::Binary(Bytes::from(vec![1, 2, 3])).to_string(),
2438 "010203"
2439 );
2440 assert_eq!(Value::Date(Date::new(0)).to_string(), "1970-01-01");
2441 assert_eq!(
2442 Value::Timestamp(Timestamp::new(1000, TimeUnit::Millisecond)).to_string(),
2443 "1970-01-01 08:00:01+0800"
2444 );
2445 assert_eq!(
2446 Value::Time(Time::new(1000, TimeUnit::Millisecond)).to_string(),
2447 "08:00:01+0800"
2448 );
2449 assert_eq!(
2450 Value::Duration(Duration::new_millisecond(1000)).to_string(),
2451 "1000ms"
2452 );
2453 assert_eq!(
2454 Value::List(build_list_value()).to_string(),
2455 "Boolean[true, false]"
2456 );
2457 assert_eq!(
2458 Value::List(ListValue::new(
2459 vec![],
2460 ConcreteDataType::timestamp_second_datatype(),
2461 ))
2462 .to_string(),
2463 "TimestampSecond[]"
2464 );
2465 assert_eq!(
2466 Value::List(ListValue::new(
2467 vec![],
2468 ConcreteDataType::timestamp_millisecond_datatype(),
2469 ))
2470 .to_string(),
2471 "TimestampMillisecond[]"
2472 );
2473 assert_eq!(
2474 Value::List(ListValue::new(
2475 vec![],
2476 ConcreteDataType::timestamp_microsecond_datatype(),
2477 ))
2478 .to_string(),
2479 "TimestampMicrosecond[]"
2480 );
2481 assert_eq!(
2482 Value::List(ListValue::new(
2483 vec![],
2484 ConcreteDataType::timestamp_nanosecond_datatype(),
2485 ))
2486 .to_string(),
2487 "TimestampNanosecond[]"
2488 );
2489
2490 assert_eq!(
2491 Value::Struct(build_struct_value()).to_string(),
2492 "{ id: 1, name: tom, age: 25, address: 94038 }"
2493 );
2494 }
2495
2496 #[test]
2497 fn test_not_null_value_to_scalar_value() {
2498 assert_eq!(
2499 ScalarValue::Boolean(Some(true)),
2500 Value::Boolean(true)
2501 .try_to_scalar_value(&ConcreteDataType::boolean_datatype())
2502 .unwrap()
2503 );
2504 assert_eq!(
2505 ScalarValue::Boolean(Some(false)),
2506 Value::Boolean(false)
2507 .try_to_scalar_value(&ConcreteDataType::boolean_datatype())
2508 .unwrap()
2509 );
2510 assert_eq!(
2511 ScalarValue::UInt8(Some(u8::MIN + 1)),
2512 Value::UInt8(u8::MIN + 1)
2513 .try_to_scalar_value(&ConcreteDataType::uint8_datatype())
2514 .unwrap()
2515 );
2516 assert_eq!(
2517 ScalarValue::UInt16(Some(u16::MIN + 2)),
2518 Value::UInt16(u16::MIN + 2)
2519 .try_to_scalar_value(&ConcreteDataType::uint16_datatype())
2520 .unwrap()
2521 );
2522 assert_eq!(
2523 ScalarValue::UInt32(Some(u32::MIN + 3)),
2524 Value::UInt32(u32::MIN + 3)
2525 .try_to_scalar_value(&ConcreteDataType::uint32_datatype())
2526 .unwrap()
2527 );
2528 assert_eq!(
2529 ScalarValue::UInt64(Some(u64::MIN + 4)),
2530 Value::UInt64(u64::MIN + 4)
2531 .try_to_scalar_value(&ConcreteDataType::uint64_datatype())
2532 .unwrap()
2533 );
2534 assert_eq!(
2535 ScalarValue::Int8(Some(i8::MIN + 4)),
2536 Value::Int8(i8::MIN + 4)
2537 .try_to_scalar_value(&ConcreteDataType::int8_datatype())
2538 .unwrap()
2539 );
2540 assert_eq!(
2541 ScalarValue::Int16(Some(i16::MIN + 5)),
2542 Value::Int16(i16::MIN + 5)
2543 .try_to_scalar_value(&ConcreteDataType::int16_datatype())
2544 .unwrap()
2545 );
2546 assert_eq!(
2547 ScalarValue::Int32(Some(i32::MIN + 6)),
2548 Value::Int32(i32::MIN + 6)
2549 .try_to_scalar_value(&ConcreteDataType::int32_datatype())
2550 .unwrap()
2551 );
2552 assert_eq!(
2553 ScalarValue::Int64(Some(i64::MIN + 7)),
2554 Value::Int64(i64::MIN + 7)
2555 .try_to_scalar_value(&ConcreteDataType::int64_datatype())
2556 .unwrap()
2557 );
2558 assert_eq!(
2559 ScalarValue::Float32(Some(8.0f32)),
2560 Value::Float32(OrderedFloat(8.0f32))
2561 .try_to_scalar_value(&ConcreteDataType::float32_datatype())
2562 .unwrap()
2563 );
2564 assert_eq!(
2565 ScalarValue::Float64(Some(9.0f64)),
2566 Value::Float64(OrderedFloat(9.0f64))
2567 .try_to_scalar_value(&ConcreteDataType::float64_datatype())
2568 .unwrap()
2569 );
2570 assert_eq!(
2571 ScalarValue::Utf8(Some("hello".to_string())),
2572 Value::String(StringBytes::from("hello"))
2573 .try_to_scalar_value(&ConcreteDataType::string_datatype(),)
2574 .unwrap()
2575 );
2576 assert_eq!(
2577 ScalarValue::Binary(Some("world".as_bytes().to_vec())),
2578 Value::Binary(Bytes::from("world".as_bytes()))
2579 .try_to_scalar_value(&ConcreteDataType::binary_datatype())
2580 .unwrap()
2581 );
2582
2583 let jsonb_value = jsonb::parse_value(r#"{"key": "value"}"#.as_bytes())
2584 .unwrap()
2585 .to_vec();
2586 assert_eq!(
2587 ScalarValue::Binary(Some(jsonb_value.clone())),
2588 Value::Binary(jsonb_value.into())
2589 .try_to_scalar_value(&ConcreteDataType::json_datatype())
2590 .unwrap()
2591 );
2592
2593 assert_eq!(
2594 build_scalar_struct_value(),
2595 Value::Struct(build_struct_value())
2596 .try_to_scalar_value(&ConcreteDataType::struct_datatype(build_struct_type()))
2597 .unwrap()
2598 );
2599
2600 assert_eq!(
2601 build_scalar_list_value(),
2602 Value::List(build_list_value())
2603 .try_to_scalar_value(&ConcreteDataType::list_datatype(
2604 ConcreteDataType::boolean_datatype()
2605 ))
2606 .unwrap()
2607 );
2608 }
2609
2610 #[test]
2611 fn test_null_value_to_scalar_value() {
2612 assert_eq!(
2613 ScalarValue::Boolean(None),
2614 Value::Null
2615 .try_to_scalar_value(&ConcreteDataType::boolean_datatype())
2616 .unwrap()
2617 );
2618 assert_eq!(
2619 ScalarValue::UInt8(None),
2620 Value::Null
2621 .try_to_scalar_value(&ConcreteDataType::uint8_datatype())
2622 .unwrap()
2623 );
2624 assert_eq!(
2625 ScalarValue::UInt16(None),
2626 Value::Null
2627 .try_to_scalar_value(&ConcreteDataType::uint16_datatype())
2628 .unwrap()
2629 );
2630 assert_eq!(
2631 ScalarValue::UInt32(None),
2632 Value::Null
2633 .try_to_scalar_value(&ConcreteDataType::uint32_datatype())
2634 .unwrap()
2635 );
2636 assert_eq!(
2637 ScalarValue::UInt64(None),
2638 Value::Null
2639 .try_to_scalar_value(&ConcreteDataType::uint64_datatype())
2640 .unwrap()
2641 );
2642 assert_eq!(
2643 ScalarValue::Int8(None),
2644 Value::Null
2645 .try_to_scalar_value(&ConcreteDataType::int8_datatype())
2646 .unwrap()
2647 );
2648 assert_eq!(
2649 ScalarValue::Int16(None),
2650 Value::Null
2651 .try_to_scalar_value(&ConcreteDataType::int16_datatype())
2652 .unwrap()
2653 );
2654 assert_eq!(
2655 ScalarValue::Int32(None),
2656 Value::Null
2657 .try_to_scalar_value(&ConcreteDataType::int32_datatype())
2658 .unwrap()
2659 );
2660 assert_eq!(
2661 ScalarValue::Int64(None),
2662 Value::Null
2663 .try_to_scalar_value(&ConcreteDataType::int64_datatype())
2664 .unwrap()
2665 );
2666 assert_eq!(
2667 ScalarValue::Float32(None),
2668 Value::Null
2669 .try_to_scalar_value(&ConcreteDataType::float32_datatype())
2670 .unwrap()
2671 );
2672 assert_eq!(
2673 ScalarValue::Float64(None),
2674 Value::Null
2675 .try_to_scalar_value(&ConcreteDataType::float64_datatype())
2676 .unwrap()
2677 );
2678 assert_eq!(
2679 ScalarValue::Utf8(None),
2680 Value::Null
2681 .try_to_scalar_value(&ConcreteDataType::string_datatype())
2682 .unwrap()
2683 );
2684 assert_eq!(
2685 ScalarValue::Binary(None),
2686 Value::Null
2687 .try_to_scalar_value(&ConcreteDataType::binary_datatype())
2688 .unwrap()
2689 );
2690
2691 assert_eq!(
2692 ScalarValue::Time32Second(None),
2693 Value::Null
2694 .try_to_scalar_value(&ConcreteDataType::time_second_datatype())
2695 .unwrap()
2696 );
2697 assert_eq!(
2698 ScalarValue::Time32Millisecond(None),
2699 Value::Null
2700 .try_to_scalar_value(&ConcreteDataType::time_millisecond_datatype())
2701 .unwrap()
2702 );
2703 assert_eq!(
2704 ScalarValue::Time64Microsecond(None),
2705 Value::Null
2706 .try_to_scalar_value(&ConcreteDataType::time_microsecond_datatype())
2707 .unwrap()
2708 );
2709 assert_eq!(
2710 ScalarValue::Time64Nanosecond(None),
2711 Value::Null
2712 .try_to_scalar_value(&ConcreteDataType::time_nanosecond_datatype())
2713 .unwrap()
2714 );
2715
2716 assert_eq!(
2717 ScalarValue::DurationSecond(None),
2718 Value::Null
2719 .try_to_scalar_value(&ConcreteDataType::duration_second_datatype())
2720 .unwrap()
2721 );
2722 assert_eq!(
2723 ScalarValue::DurationMillisecond(None),
2724 Value::Null
2725 .try_to_scalar_value(&ConcreteDataType::duration_millisecond_datatype())
2726 .unwrap()
2727 );
2728 assert_eq!(
2729 ScalarValue::DurationMicrosecond(None),
2730 Value::Null
2731 .try_to_scalar_value(&ConcreteDataType::duration_microsecond_datatype())
2732 .unwrap()
2733 );
2734 assert_eq!(
2735 ScalarValue::DurationNanosecond(None),
2736 Value::Null
2737 .try_to_scalar_value(&ConcreteDataType::duration_nanosecond_datatype())
2738 .unwrap()
2739 );
2740 assert_eq!(
2741 ScalarValue::Binary(None),
2742 Value::Null
2743 .try_to_scalar_value(&ConcreteDataType::json_datatype())
2744 .unwrap()
2745 );
2746
2747 assert_eq!(
2748 ScalarValue::new_null_list(ArrowDataType::Boolean, true, 1),
2749 Value::Null
2750 .try_to_scalar_value(&ConcreteDataType::list_datatype(
2751 ConcreteDataType::boolean_datatype(),
2752 ))
2753 .unwrap()
2754 );
2755
2756 assert_eq!(
2757 ScalarStructBuilder::new_null(build_struct_type().as_arrow_fields()),
2758 Value::Null
2759 .try_to_scalar_value(&ConcreteDataType::struct_datatype(build_struct_type()))
2760 .unwrap()
2761 );
2762 }
2763
2764 #[test]
2765 fn test_list_value_to_scalar_value() {
2766 let items = vec![Value::Int32(-1), Value::Null];
2767 let list = Value::List(ListValue::new(items, ConcreteDataType::int32_datatype()));
2768 let df_list = list
2769 .try_to_scalar_value(&ConcreteDataType::list_datatype(
2770 ConcreteDataType::int32_datatype(),
2771 ))
2772 .unwrap();
2773 assert!(matches!(df_list, ScalarValue::List(_)));
2774 match df_list {
2775 ScalarValue::List(vs) => {
2776 assert_eq!(
2777 ArrowDataType::List(Arc::new(Field::new_list_field(
2778 ArrowDataType::Int32,
2779 true
2780 ))),
2781 *vs.data_type()
2782 );
2783
2784 let vs = ScalarValue::convert_array_to_scalar_vec(vs.as_ref())
2785 .unwrap()
2786 .into_iter()
2787 .flatten()
2788 .collect::<Vec<_>>();
2789 assert_eq!(
2790 vs,
2791 vec![ScalarValue::Int32(Some(-1)), ScalarValue::Int32(None)]
2792 );
2793 }
2794 _ => unreachable!(),
2795 }
2796 }
2797
2798 #[test]
2799 fn test_struct_value_to_scalar_value() {
2800 let struct_value = build_struct_value();
2801 let scalar_value = struct_value
2802 .try_to_scalar_value(&build_struct_type())
2803 .unwrap();
2804
2805 assert_eq!(scalar_value, build_scalar_struct_value());
2806
2807 assert!(matches!(scalar_value, ScalarValue::Struct(_)));
2808 match scalar_value {
2809 ScalarValue::Struct(values) => {
2810 assert_eq!(&build_struct_type().as_arrow_fields(), values.fields());
2811
2812 assert_eq!(
2813 ScalarValue::try_from_array(values.column(0), 0).unwrap(),
2814 ScalarValue::Int32(Some(1))
2815 );
2816 assert_eq!(
2817 ScalarValue::try_from_array(values.column(1), 0).unwrap(),
2818 ScalarValue::Utf8(Some("tom".into()))
2819 );
2820 assert_eq!(
2821 ScalarValue::try_from_array(values.column(2), 0).unwrap(),
2822 ScalarValue::UInt8(Some(25))
2823 );
2824 assert_eq!(
2825 ScalarValue::try_from_array(values.column(3), 0).unwrap(),
2826 ScalarValue::Utf8(Some("94038".into()))
2827 );
2828 }
2829 _ => panic!("Unexpected value type"),
2830 }
2831 }
2832
2833 #[test]
2834 fn test_timestamp_to_scalar_value() {
2835 assert_eq!(
2836 ScalarValue::TimestampSecond(Some(1), None),
2837 timestamp_to_scalar_value(TimeUnit::Second, Some(1))
2838 );
2839 assert_eq!(
2840 ScalarValue::TimestampMillisecond(Some(1), None),
2841 timestamp_to_scalar_value(TimeUnit::Millisecond, Some(1))
2842 );
2843 assert_eq!(
2844 ScalarValue::TimestampMicrosecond(Some(1), None),
2845 timestamp_to_scalar_value(TimeUnit::Microsecond, Some(1))
2846 );
2847 assert_eq!(
2848 ScalarValue::TimestampNanosecond(Some(1), None),
2849 timestamp_to_scalar_value(TimeUnit::Nanosecond, Some(1))
2850 );
2851 }
2852
2853 #[test]
2854 fn test_time_to_scalar_value() {
2855 assert_eq!(
2856 ScalarValue::Time32Second(Some(1)),
2857 time_to_scalar_value(TimeUnit::Second, Some(1)).unwrap()
2858 );
2859 assert_eq!(
2860 ScalarValue::Time32Millisecond(Some(1)),
2861 time_to_scalar_value(TimeUnit::Millisecond, Some(1)).unwrap()
2862 );
2863 assert_eq!(
2864 ScalarValue::Time64Microsecond(Some(1)),
2865 time_to_scalar_value(TimeUnit::Microsecond, Some(1)).unwrap()
2866 );
2867 assert_eq!(
2868 ScalarValue::Time64Nanosecond(Some(1)),
2869 time_to_scalar_value(TimeUnit::Nanosecond, Some(1)).unwrap()
2870 );
2871 }
2872
2873 #[test]
2874 fn test_duration_to_scalar_value() {
2875 assert_eq!(
2876 ScalarValue::DurationSecond(Some(1)),
2877 duration_to_scalar_value(TimeUnit::Second, Some(1))
2878 );
2879 assert_eq!(
2880 ScalarValue::DurationMillisecond(Some(1)),
2881 duration_to_scalar_value(TimeUnit::Millisecond, Some(1))
2882 );
2883 assert_eq!(
2884 ScalarValue::DurationMicrosecond(Some(1)),
2885 duration_to_scalar_value(TimeUnit::Microsecond, Some(1))
2886 );
2887 assert_eq!(
2888 ScalarValue::DurationNanosecond(Some(1)),
2889 duration_to_scalar_value(TimeUnit::Nanosecond, Some(1))
2890 );
2891 }
2892
2893 fn check_value_ref_size_eq(value_ref: &ValueRef, size: usize) {
2894 assert_eq!(value_ref.data_size(), size);
2895 }
2896
2897 #[test]
2898 fn test_value_ref_estimated_size() {
2899 check_value_ref_size_eq(&ValueRef::Null, 8);
2900 check_value_ref_size_eq(&ValueRef::Boolean(true), 1);
2901 check_value_ref_size_eq(&ValueRef::UInt8(1), 1);
2902 check_value_ref_size_eq(&ValueRef::UInt16(1), 2);
2903 check_value_ref_size_eq(&ValueRef::UInt32(1), 4);
2904 check_value_ref_size_eq(&ValueRef::UInt64(1), 8);
2905 check_value_ref_size_eq(&ValueRef::Int8(1), 1);
2906 check_value_ref_size_eq(&ValueRef::Int16(1), 2);
2907 check_value_ref_size_eq(&ValueRef::Int32(1), 4);
2908 check_value_ref_size_eq(&ValueRef::Int64(1), 8);
2909 check_value_ref_size_eq(&ValueRef::Float32(1.0.into()), 4);
2910 check_value_ref_size_eq(&ValueRef::Float64(1.0.into()), 8);
2911 check_value_ref_size_eq(&ValueRef::String("greptimedb"), 10);
2912 check_value_ref_size_eq(&ValueRef::Binary(b"greptimedb"), 10);
2913 check_value_ref_size_eq(&ValueRef::Date(Date::new(1)), 4);
2914 check_value_ref_size_eq(&ValueRef::Timestamp(Timestamp::new_millisecond(1)), 16);
2915 check_value_ref_size_eq(&ValueRef::Time(Time::new_millisecond(1)), 16);
2916 check_value_ref_size_eq(&ValueRef::IntervalYearMonth(IntervalYearMonth::new(1)), 4);
2917 check_value_ref_size_eq(&ValueRef::IntervalDayTime(IntervalDayTime::new(1, 2)), 8);
2918 check_value_ref_size_eq(
2919 &ValueRef::IntervalMonthDayNano(IntervalMonthDayNano::new(1, 2, 3)),
2920 16,
2921 );
2922 check_value_ref_size_eq(&ValueRef::Duration(Duration::new_millisecond(1)), 16);
2923 check_value_ref_size_eq(
2924 &ValueRef::List(ListValueRef::Ref {
2925 val: &ListValue {
2926 items: vec![
2927 Value::String("hello world".into()),
2928 Value::String("greptimedb".into()),
2929 ],
2930 datatype: ConcreteDataType::string_datatype(),
2931 },
2932 }),
2933 22,
2934 );
2935
2936 let data = vec![
2937 Some(vec![Some(1), Some(2), Some(3)]),
2938 None,
2939 Some(vec![Some(4), None, Some(6)]),
2940 ];
2941 let mut builder =
2942 ListVectorBuilder::with_type_capacity(ConcreteDataType::int32_datatype(), 8);
2943 for vec_opt in &data {
2944 if let Some(vec) = vec_opt {
2945 let values = vec.iter().map(|v| Value::from(*v)).collect();
2946 let list_value = ListValue::new(values, ConcreteDataType::int32_datatype());
2947
2948 builder.push(Some(ListValueRef::Ref { val: &list_value }));
2949 } else {
2950 builder.push(None);
2951 }
2952 }
2953 let vector = builder.finish();
2954
2955 check_value_ref_size_eq(
2956 &ValueRef::List(ListValueRef::Indexed {
2957 vector: &vector,
2958 idx: 0,
2959 }),
2960 74,
2961 );
2962 check_value_ref_size_eq(
2963 &ValueRef::List(ListValueRef::Indexed {
2964 vector: &vector,
2965 idx: 1,
2966 }),
2967 74,
2968 );
2969 check_value_ref_size_eq(
2970 &ValueRef::List(ListValueRef::Indexed {
2971 vector: &vector,
2972 idx: 2,
2973 }),
2974 74,
2975 );
2976 check_value_ref_size_eq(&ValueRef::Decimal128(Decimal128::new(1234, 3, 1)), 32);
2977
2978 check_value_ref_size_eq(
2979 &ValueRef::Struct(StructValueRef::Ref(&build_struct_value())),
2980 13,
2981 );
2982 }
2983
2984 #[test]
2985 fn test_incorrect_default_value_issue_3479() {
2986 let value = OrderedF64::from(0.047318541668048164);
2987 let serialized = serde_json::to_string(&value).unwrap();
2988 let deserialized: OrderedF64 = serde_json::from_str(&serialized).unwrap();
2989 assert_eq!(value, deserialized);
2990 }
2991}