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