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