datatypes/
value.rs

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