Skip to main content

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) => x.checked_neg().map(Value::Int8),
547            Value::Int16(x) => x.checked_neg().map(Value::Int16),
548            Value::Int32(x) => x.checked_neg().map(Value::Int32),
549            Value::Int64(x) => x.checked_neg().map(Value::Int64),
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) => x.checked_negative().map(Value::Date),
554            Value::Timestamp(x) => x.checked_negative().map(Value::Timestamp),
555            Value::Time(x) => x.checked_negative().map(Value::Time),
556            Value::Duration(x) => x.checked_negative().map(Value::Duration),
557            Value::IntervalYearMonth(x) => x.checked_negative().map(Value::IntervalYearMonth),
558            Value::IntervalDayTime(x) => x.checked_negative().map(Value::IntervalDayTime),
559            Value::IntervalMonthDayNano(x) => x.checked_negative().map(Value::IntervalMonthDayNano),
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)
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).try_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 output_fields = output_type.fields();
1089        ensure!(
1090            self.items.len() == output_fields.len(),
1091            InconsistentStructFieldsAndItemsSnafu {
1092                field_len: output_fields.len(),
1093                item_len: self.items.len()
1094            }
1095        );
1096        let arrays = self
1097            .items
1098            .iter()
1099            .zip(output_fields.iter())
1100            .map(|(value, field)| {
1101                // Null values need the declared field type to produce a typed null array.
1102                let scalar_value = value.try_to_scalar_value(field.data_type())?;
1103                scalar_value
1104                    .to_array()
1105                    .context(ConvertScalarToArrowArraySnafu)
1106            })
1107            .collect::<Result<Vec<Arc<dyn Array>>>>()?;
1108
1109        let fields = output_type.as_arrow_fields();
1110        let struct_array =
1111            StructArray::try_new_with_length(fields, arrays, None, 1).map_err(|error| {
1112                error::ToScalarValueSnafu {
1113                    reason: error.to_string(),
1114                }
1115                .build()
1116            })?;
1117        Ok(ScalarValue::Struct(Arc::new(struct_array)))
1118    }
1119}
1120
1121impl Default for StructValue {
1122    fn default() -> StructValue {
1123        StructValue::try_new(vec![], StructType::new(Arc::new(vec![]))).unwrap()
1124    }
1125}
1126
1127// TODO(ruihang): Implement this type
1128/// Dictionary value.
1129#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1130pub struct DictionaryValue {
1131    /// Inner values datatypes
1132    key_type: ConcreteDataType,
1133    value_type: ConcreteDataType,
1134}
1135
1136impl Eq for DictionaryValue {}
1137
1138impl TryFrom<ScalarValue> for Value {
1139    type Error = error::Error;
1140
1141    fn try_from(v: ScalarValue) -> Result<Self> {
1142        let v = match v {
1143            ScalarValue::Null => Value::Null,
1144            ScalarValue::Boolean(b) => Value::from(b),
1145            ScalarValue::Float32(f) => Value::from(f),
1146            ScalarValue::Float64(f) => Value::from(f),
1147            ScalarValue::Int8(i) => Value::from(i),
1148            ScalarValue::Int16(i) => Value::from(i),
1149            ScalarValue::Int32(i) => Value::from(i),
1150            ScalarValue::Int64(i) => Value::from(i),
1151            ScalarValue::UInt8(u) => Value::from(u),
1152            ScalarValue::UInt16(u) => Value::from(u),
1153            ScalarValue::UInt32(u) => Value::from(u),
1154            ScalarValue::UInt64(u) => Value::from(u),
1155            ScalarValue::Utf8(s) | ScalarValue::LargeUtf8(s) | ScalarValue::Utf8View(s) => {
1156                Value::from(s.map(StringBytes::from))
1157            }
1158            ScalarValue::Binary(b)
1159            | ScalarValue::LargeBinary(b)
1160            | ScalarValue::FixedSizeBinary(_, b)
1161            | ScalarValue::BinaryView(b) => Value::from(b.map(Bytes::from)),
1162            ScalarValue::List(array) => {
1163                // this is for item type
1164                let datatype = ConcreteDataType::try_from(&array.value_type())?;
1165                let scalar_values = ScalarValue::convert_array_to_scalar_vec(array.as_ref())
1166                    .context(ConvertArrowArrayToScalarsSnafu)?;
1167                let items = scalar_values
1168                    .into_iter()
1169                    .flat_map(|v| v.unwrap_or_else(|| vec![ScalarValue::Null]))
1170                    .map(|x| x.try_into())
1171                    .collect::<Result<Vec<Value>>>()?;
1172                Value::List(ListValue::new(items, Arc::new(datatype)))
1173            }
1174            ScalarValue::Date32(d) => d.map(|x| Value::Date(Date::new(x))).unwrap_or(Value::Null),
1175            ScalarValue::TimestampSecond(t, _) => t
1176                .map(|x| Value::Timestamp(Timestamp::new(x, TimeUnit::Second)))
1177                .unwrap_or(Value::Null),
1178            ScalarValue::TimestampMillisecond(t, _) => t
1179                .map(|x| Value::Timestamp(Timestamp::new(x, TimeUnit::Millisecond)))
1180                .unwrap_or(Value::Null),
1181            ScalarValue::TimestampMicrosecond(t, _) => t
1182                .map(|x| Value::Timestamp(Timestamp::new(x, TimeUnit::Microsecond)))
1183                .unwrap_or(Value::Null),
1184            ScalarValue::TimestampNanosecond(t, _) => t
1185                .map(|x| Value::Timestamp(Timestamp::new(x, TimeUnit::Nanosecond)))
1186                .unwrap_or(Value::Null),
1187            ScalarValue::Time32Second(t) => t
1188                .map(|x| Value::Time(Time::new(x as i64, TimeUnit::Second)))
1189                .unwrap_or(Value::Null),
1190            ScalarValue::Time32Millisecond(t) => t
1191                .map(|x| Value::Time(Time::new(x as i64, TimeUnit::Millisecond)))
1192                .unwrap_or(Value::Null),
1193            ScalarValue::Time64Microsecond(t) => t
1194                .map(|x| Value::Time(Time::new(x, TimeUnit::Microsecond)))
1195                .unwrap_or(Value::Null),
1196            ScalarValue::Time64Nanosecond(t) => t
1197                .map(|x| Value::Time(Time::new(x, TimeUnit::Nanosecond)))
1198                .unwrap_or(Value::Null),
1199
1200            ScalarValue::IntervalYearMonth(t) => t
1201                .map(|x| Value::IntervalYearMonth(IntervalYearMonth::from_i32(x)))
1202                .unwrap_or(Value::Null),
1203            ScalarValue::IntervalDayTime(t) => t
1204                .map(|x| Value::IntervalDayTime(IntervalDayTime::from(x)))
1205                .unwrap_or(Value::Null),
1206            ScalarValue::IntervalMonthDayNano(t) => t
1207                .map(|x| Value::IntervalMonthDayNano(IntervalMonthDayNano::from(x)))
1208                .unwrap_or(Value::Null),
1209            ScalarValue::DurationSecond(d) => d
1210                .map(|x| Value::Duration(Duration::new(x, TimeUnit::Second)))
1211                .unwrap_or(Value::Null),
1212            ScalarValue::DurationMillisecond(d) => d
1213                .map(|x| Value::Duration(Duration::new(x, TimeUnit::Millisecond)))
1214                .unwrap_or(Value::Null),
1215            ScalarValue::DurationMicrosecond(d) => d
1216                .map(|x| Value::Duration(Duration::new(x, TimeUnit::Microsecond)))
1217                .unwrap_or(Value::Null),
1218            ScalarValue::DurationNanosecond(d) => d
1219                .map(|x| Value::Duration(Duration::new(x, TimeUnit::Nanosecond)))
1220                .unwrap_or(Value::Null),
1221            ScalarValue::Decimal128(v, p, s) => v
1222                .map(|v| Value::Decimal128(Decimal128::new(v, p, s)))
1223                .unwrap_or(Value::Null),
1224            ScalarValue::Struct(struct_array) => {
1225                // A struct scalar carries a single element; a null (or empty)
1226                // element means the struct itself is null, for example a null
1227                // struct inside a list.
1228                if struct_array.is_empty() || struct_array.is_null(0) {
1229                    return Ok(Value::Null);
1230                }
1231                // Build the struct type fallibly: an unrepresentable arrow
1232                // field type must surface as an error, not a panic.
1233                let struct_type = StructType::try_from_arrow_fields(struct_array.fields())?;
1234                let items = struct_array
1235                    .columns()
1236                    .iter()
1237                    .map(|array| {
1238                        // we only take first element from each array
1239                        let field_scalar_value = ScalarValue::try_from_array(array.as_ref(), 0)
1240                            .context(ConvertArrowArrayToScalarsSnafu)?;
1241                        field_scalar_value.try_into()
1242                    })
1243                    .collect::<Result<Vec<Value>>>()?;
1244                Value::Struct(StructValue::try_new(items, struct_type)?)
1245            }
1246            ScalarValue::Dictionary(_, value) => (*value).try_into()?,
1247            ScalarValue::Decimal32(_, _, _)
1248            | ScalarValue::Decimal64(_, _, _)
1249            | ScalarValue::Decimal256(_, _, _)
1250            | ScalarValue::FixedSizeList(_)
1251            | ScalarValue::LargeList(_)
1252            | ScalarValue::ListView(_)
1253            | ScalarValue::LargeListView(_)
1254            | ScalarValue::Union(_, _, _)
1255            | ScalarValue::Float16(_)
1256            | ScalarValue::Map(_)
1257            | ScalarValue::Date64(_)
1258            | ScalarValue::RunEndEncoded(_, _, _) => {
1259                return error::UnsupportedArrowTypeSnafu {
1260                    arrow_type: v.data_type(),
1261                }
1262                .fail();
1263            }
1264        };
1265        Ok(v)
1266    }
1267}
1268
1269/// Converts the value at `index` of an arrow array into a [`Value`].
1270///
1271/// Unlike the unchecked conversion in [`crate::vectors::StructVector`]'s `get`,
1272/// this propagates conversion errors (for example arrow types greptimedb
1273/// cannot represent, such as `Decimal256`) instead of panicking, so callers
1274/// can surface a proper error to the user.
1275pub fn try_value_from_array(array: &dyn Array, index: usize) -> Result<Value> {
1276    if array.is_null(index) {
1277        return Ok(Value::Null);
1278    }
1279    let scalar =
1280        ScalarValue::try_from_array(array, index).context(ConvertArrowArrayToScalarsSnafu)?;
1281    Value::try_from(scalar)
1282}
1283
1284impl From<ValueRef<'_>> for Value {
1285    fn from(value: ValueRef<'_>) -> Self {
1286        match value {
1287            ValueRef::Null => Value::Null,
1288            ValueRef::Boolean(v) => Value::Boolean(v),
1289            ValueRef::UInt8(v) => Value::UInt8(v),
1290            ValueRef::UInt16(v) => Value::UInt16(v),
1291            ValueRef::UInt32(v) => Value::UInt32(v),
1292            ValueRef::UInt64(v) => Value::UInt64(v),
1293            ValueRef::Int8(v) => Value::Int8(v),
1294            ValueRef::Int16(v) => Value::Int16(v),
1295            ValueRef::Int32(v) => Value::Int32(v),
1296            ValueRef::Int64(v) => Value::Int64(v),
1297            ValueRef::Float32(v) => Value::Float32(v),
1298            ValueRef::Float64(v) => Value::Float64(v),
1299            ValueRef::String(v) => Value::String(v.into()),
1300            ValueRef::Binary(v) => Value::Binary(v.into()),
1301            ValueRef::Date(v) => Value::Date(v),
1302            ValueRef::Timestamp(v) => Value::Timestamp(v),
1303            ValueRef::Time(v) => Value::Time(v),
1304            ValueRef::IntervalYearMonth(v) => Value::IntervalYearMonth(v),
1305            ValueRef::IntervalDayTime(v) => Value::IntervalDayTime(v),
1306            ValueRef::IntervalMonthDayNano(v) => Value::IntervalMonthDayNano(v),
1307            ValueRef::Duration(v) => Value::Duration(v),
1308            ValueRef::List(v) => v.to_value(),
1309            ValueRef::Decimal128(v) => Value::Decimal128(v),
1310            ValueRef::Struct(v) => v.to_value(),
1311            ValueRef::Json(v) => Value::Json(Box::new(JsonValue::from(*v))),
1312        }
1313    }
1314}
1315
1316/// Reference to [Value].
1317#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1318pub enum ValueRef<'a> {
1319    Null,
1320
1321    // Numeric types:
1322    Boolean(bool),
1323    UInt8(u8),
1324    UInt16(u16),
1325    UInt32(u32),
1326    UInt64(u64),
1327    Int8(i8),
1328    Int16(i16),
1329    Int32(i32),
1330    Int64(i64),
1331    Float32(OrderedF32),
1332    Float64(OrderedF64),
1333
1334    // Decimal type:
1335    Decimal128(Decimal128),
1336
1337    // String types:
1338    String(&'a str),
1339    Binary(&'a [u8]),
1340
1341    // Date & Time types:
1342    Date(Date),
1343    Timestamp(Timestamp),
1344    Time(Time),
1345    Duration(Duration),
1346    // Interval types:
1347    IntervalYearMonth(IntervalYearMonth),
1348    IntervalDayTime(IntervalDayTime),
1349    IntervalMonthDayNano(IntervalMonthDayNano),
1350
1351    // Compound types:
1352    List(ListValueRef<'a>),
1353    Struct(StructValueRef<'a>),
1354
1355    Json(Box<JsonValueRef<'a>>),
1356}
1357
1358macro_rules! impl_as_for_value_ref {
1359    ($value: ident, $Variant: ident) => {
1360        match $value {
1361            ValueRef::Null => Ok(None),
1362            ValueRef::$Variant(v) => Ok(Some(v.clone())),
1363            other => error::CastTypeSnafu {
1364                msg: format!(
1365                    "Failed to cast value ref {:?} to {}",
1366                    other,
1367                    stringify!($Variant)
1368                ),
1369            }
1370            .fail(),
1371        }
1372    };
1373}
1374
1375impl<'a> ValueRef<'a> {
1376    define_data_type_func!(ValueRef);
1377
1378    /// Returns true if this is null.
1379    pub fn is_null(&self) -> bool {
1380        match self {
1381            ValueRef::Null => true,
1382            ValueRef::Json(v) => v.is_null(),
1383            _ => false,
1384        }
1385    }
1386
1387    /// Cast itself to binary slice.
1388    pub fn try_into_binary(&self) -> Result<Option<&'a [u8]>> {
1389        impl_as_for_value_ref!(self, Binary)
1390    }
1391
1392    /// Cast itself to string slice.
1393    pub fn try_into_string(&self) -> Result<Option<&'a str>> {
1394        impl_as_for_value_ref!(self, String)
1395    }
1396
1397    /// Cast itself to boolean.
1398    pub fn try_into_boolean(&self) -> Result<Option<bool>> {
1399        impl_as_for_value_ref!(self, Boolean)
1400    }
1401
1402    pub fn try_into_i8(&self) -> Result<Option<i8>> {
1403        impl_as_for_value_ref!(self, Int8)
1404    }
1405
1406    pub fn try_into_u8(&self) -> Result<Option<u8>> {
1407        impl_as_for_value_ref!(self, UInt8)
1408    }
1409
1410    pub fn try_into_i16(&self) -> Result<Option<i16>> {
1411        impl_as_for_value_ref!(self, Int16)
1412    }
1413
1414    pub fn try_into_u16(&self) -> Result<Option<u16>> {
1415        impl_as_for_value_ref!(self, UInt16)
1416    }
1417
1418    pub fn try_into_i32(&self) -> Result<Option<i32>> {
1419        impl_as_for_value_ref!(self, Int32)
1420    }
1421
1422    pub fn try_into_u32(&self) -> Result<Option<u32>> {
1423        impl_as_for_value_ref!(self, UInt32)
1424    }
1425
1426    pub fn try_into_i64(&self) -> Result<Option<i64>> {
1427        impl_as_for_value_ref!(self, Int64)
1428    }
1429
1430    pub fn try_into_u64(&self) -> Result<Option<u64>> {
1431        impl_as_for_value_ref!(self, UInt64)
1432    }
1433
1434    pub fn try_into_f32(&self) -> Result<Option<f32>> {
1435        match self {
1436            ValueRef::Null => Ok(None),
1437            ValueRef::Float32(f) => Ok(Some(f.0)),
1438            ValueRef::Json(v) => Ok(v.as_f32()),
1439            other => error::CastTypeSnafu {
1440                msg: format!("Failed to cast value ref {:?} to ValueRef::Float32", other,),
1441            }
1442            .fail(),
1443        }
1444    }
1445
1446    pub fn try_into_f64(&self) -> Result<Option<f64>> {
1447        match self {
1448            ValueRef::Null => Ok(None),
1449            ValueRef::Float64(f) => Ok(Some(f.0)),
1450            ValueRef::Json(v) => Ok(v.as_f64()),
1451            other => error::CastTypeSnafu {
1452                msg: format!("Failed to cast value ref {:?} to ValueRef::Float64", other,),
1453            }
1454            .fail(),
1455        }
1456    }
1457
1458    /// Cast itself to [Date].
1459    pub fn try_into_date(&self) -> Result<Option<Date>> {
1460        impl_as_for_value_ref!(self, Date)
1461    }
1462
1463    /// Cast itself to [Timestamp].
1464    pub fn try_into_timestamp(&self) -> Result<Option<Timestamp>> {
1465        impl_as_for_value_ref!(self, Timestamp)
1466    }
1467
1468    /// Cast itself to [Time].
1469    pub fn try_into_time(&self) -> Result<Option<Time>> {
1470        impl_as_for_value_ref!(self, Time)
1471    }
1472
1473    pub fn try_into_duration(&self) -> Result<Option<Duration>> {
1474        impl_as_for_value_ref!(self, Duration)
1475    }
1476
1477    /// Cast itself to [IntervalYearMonth].
1478    pub fn try_into_interval_year_month(&self) -> Result<Option<IntervalYearMonth>> {
1479        impl_as_for_value_ref!(self, IntervalYearMonth)
1480    }
1481
1482    /// Cast itself to [IntervalDayTime].
1483    pub fn try_into_interval_day_time(&self) -> Result<Option<IntervalDayTime>> {
1484        impl_as_for_value_ref!(self, IntervalDayTime)
1485    }
1486
1487    /// Cast itself to [IntervalMonthDayNano].
1488    pub fn try_into_interval_month_day_nano(&self) -> Result<Option<IntervalMonthDayNano>> {
1489        impl_as_for_value_ref!(self, IntervalMonthDayNano)
1490    }
1491
1492    /// Cast itself to [ListValueRef].
1493    pub fn try_into_list(&self) -> Result<Option<ListValueRef<'_>>> {
1494        impl_as_for_value_ref!(self, List)
1495    }
1496
1497    /// Cast itself to [StructValueRef].
1498    pub fn try_into_struct(&self) -> Result<Option<StructValueRef<'_>>> {
1499        impl_as_for_value_ref!(self, Struct)
1500    }
1501
1502    /// Cast itself to [Decimal128].
1503    pub fn try_into_decimal128(&self) -> Result<Option<Decimal128>> {
1504        impl_as_for_value_ref!(self, Decimal128)
1505    }
1506}
1507
1508impl PartialOrd for ValueRef<'_> {
1509    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1510        Some(self.cmp(other))
1511    }
1512}
1513
1514impl Ord for ValueRef<'_> {
1515    fn cmp(&self, other: &Self) -> Ordering {
1516        impl_ord_for_value_like!(ValueRef, self, other)
1517    }
1518}
1519
1520macro_rules! impl_value_ref_from {
1521    ($Variant:ident, $Type:ident) => {
1522        impl From<$Type> for ValueRef<'_> {
1523            fn from(value: $Type) -> Self {
1524                ValueRef::$Variant(value.into())
1525            }
1526        }
1527
1528        impl From<Option<$Type>> for ValueRef<'_> {
1529            fn from(value: Option<$Type>) -> Self {
1530                match value {
1531                    Some(v) => ValueRef::$Variant(v.into()),
1532                    None => ValueRef::Null,
1533                }
1534            }
1535        }
1536    };
1537}
1538
1539impl_value_ref_from!(Boolean, bool);
1540impl_value_ref_from!(UInt8, u8);
1541impl_value_ref_from!(UInt16, u16);
1542impl_value_ref_from!(UInt32, u32);
1543impl_value_ref_from!(UInt64, u64);
1544impl_value_ref_from!(Int8, i8);
1545impl_value_ref_from!(Int16, i16);
1546impl_value_ref_from!(Int32, i32);
1547impl_value_ref_from!(Int64, i64);
1548impl_value_ref_from!(Float32, f32);
1549impl_value_ref_from!(Float64, f64);
1550impl_value_ref_from!(Date, Date);
1551impl_value_ref_from!(Timestamp, Timestamp);
1552impl_value_ref_from!(Time, Time);
1553impl_value_ref_from!(IntervalYearMonth, IntervalYearMonth);
1554impl_value_ref_from!(IntervalDayTime, IntervalDayTime);
1555impl_value_ref_from!(IntervalMonthDayNano, IntervalMonthDayNano);
1556impl_value_ref_from!(Duration, Duration);
1557impl_value_ref_from!(Decimal128, Decimal128);
1558
1559impl<'a> From<&'a str> for ValueRef<'a> {
1560    fn from(string: &'a str) -> ValueRef<'a> {
1561        ValueRef::String(string)
1562    }
1563}
1564
1565impl<'a> From<&'a [u8]> for ValueRef<'a> {
1566    fn from(bytes: &'a [u8]) -> ValueRef<'a> {
1567        ValueRef::Binary(bytes)
1568    }
1569}
1570
1571impl<'a> From<Option<ListValueRef<'a>>> for ValueRef<'a> {
1572    fn from(list: Option<ListValueRef>) -> ValueRef {
1573        match list {
1574            Some(v) => ValueRef::List(v),
1575            None => ValueRef::Null,
1576        }
1577    }
1578}
1579
1580/// Reference to a [ListValue].
1581///
1582/// Now comparison still requires some allocation (call of `to_value()`) and
1583/// might be avoidable by downcasting and comparing the underlying array slice
1584/// if it becomes bottleneck.
1585#[derive(Debug, Clone)]
1586pub enum ListValueRef<'a> {
1587    // TODO(yingwen): Consider replace this by VectorRef.
1588    Indexed {
1589        vector: &'a ListVector,
1590        idx: usize,
1591    },
1592    Ref {
1593        val: &'a ListValue,
1594    },
1595    RefList {
1596        val: Vec<ValueRef<'a>>,
1597        item_datatype: Arc<ConcreteDataType>,
1598    },
1599}
1600
1601impl ListValueRef<'_> {
1602    /// Convert self to [Value]. This method would clone the underlying data.
1603    fn to_value(&self) -> Value {
1604        match self {
1605            ListValueRef::Indexed { vector, idx } => vector.get(*idx),
1606            ListValueRef::Ref { val } => Value::List((*val).clone()),
1607            ListValueRef::RefList { val, item_datatype } => Value::List(ListValue::new(
1608                val.iter().map(|v| Value::from(v.clone())).collect(),
1609                item_datatype.clone(),
1610            )),
1611        }
1612    }
1613    /// Returns the inner element's data type.
1614    fn datatype(&self) -> Arc<ConcreteDataType> {
1615        match self {
1616            ListValueRef::Indexed { vector, .. } => vector.item_type(),
1617            ListValueRef::Ref { val } => val.datatype().clone(),
1618            ListValueRef::RefList { item_datatype, .. } => item_datatype.clone(),
1619        }
1620    }
1621}
1622
1623impl Serialize for ListValueRef<'_> {
1624    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
1625        match self {
1626            ListValueRef::Indexed { vector, idx } => match vector.get(*idx) {
1627                Value::List(v) => v.serialize(serializer),
1628                _ => unreachable!(),
1629            },
1630            ListValueRef::Ref { val } => val.serialize(serializer),
1631            ListValueRef::RefList { val, .. } => val.serialize(serializer),
1632        }
1633    }
1634}
1635
1636impl PartialEq for ListValueRef<'_> {
1637    fn eq(&self, other: &Self) -> bool {
1638        self.to_value().eq(&other.to_value())
1639    }
1640}
1641
1642impl Eq for ListValueRef<'_> {}
1643
1644impl Ord for ListValueRef<'_> {
1645    fn cmp(&self, other: &Self) -> Ordering {
1646        // Respect the order of `Value` by converting into value before comparison.
1647        self.to_value().cmp(&other.to_value())
1648    }
1649}
1650
1651impl PartialOrd for ListValueRef<'_> {
1652    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1653        Some(self.cmp(other))
1654    }
1655}
1656
1657#[derive(Debug, Clone)]
1658pub enum StructValueRef<'a> {
1659    Indexed {
1660        vector: &'a StructVector,
1661        idx: usize,
1662    },
1663    Ref(&'a StructValue),
1664    RefList {
1665        val: Vec<ValueRef<'a>>,
1666        fields: StructType,
1667    },
1668}
1669
1670impl<'a> StructValueRef<'a> {
1671    pub fn to_value(&self) -> Value {
1672        match self {
1673            StructValueRef::Indexed { vector, idx } => vector.get(*idx),
1674            StructValueRef::Ref(val) => Value::Struct((*val).clone()),
1675            StructValueRef::RefList { val, fields } => {
1676                let items = val.iter().map(|v| Value::from(v.clone())).collect();
1677                Value::Struct(StructValue::try_new(items, fields.clone()).unwrap())
1678            }
1679        }
1680    }
1681
1682    pub fn struct_type(&self) -> &StructType {
1683        match self {
1684            StructValueRef::Indexed { vector, .. } => vector.struct_type(),
1685            StructValueRef::Ref(val) => val.struct_type(),
1686            StructValueRef::RefList { fields, .. } => fields,
1687        }
1688    }
1689}
1690
1691impl Serialize for StructValueRef<'_> {
1692    fn serialize<S: Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
1693        match self {
1694            StructValueRef::Indexed { vector, idx } => match vector.get(*idx) {
1695                Value::Struct(v) => v.serialize(serializer),
1696                _ => unreachable!(),
1697            },
1698            StructValueRef::Ref(val) => val.serialize(serializer),
1699            StructValueRef::RefList { val, .. } => val.serialize(serializer),
1700        }
1701    }
1702}
1703
1704impl PartialEq for StructValueRef<'_> {
1705    fn eq(&self, other: &Self) -> bool {
1706        self.to_value().eq(&other.to_value())
1707    }
1708}
1709
1710impl Eq for StructValueRef<'_> {}
1711
1712impl Ord for StructValueRef<'_> {
1713    fn cmp(&self, other: &Self) -> Ordering {
1714        // Respect the order of `Value` by converting into value before comparison.
1715        self.to_value().cmp(&other.to_value())
1716    }
1717}
1718
1719impl PartialOrd for StructValueRef<'_> {
1720    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1721        Some(self.cmp(other))
1722    }
1723}
1724
1725impl ValueRef<'_> {
1726    /// Returns the size of the underlying data in bytes,
1727    /// The size is estimated and only considers the data size.
1728    pub fn data_size(&self) -> usize {
1729        match self {
1730            // Since the `Null` type is also considered to occupy space, we have opted to use the
1731            // size of `i64` as an initial approximation.
1732            ValueRef::Null => 8,
1733            ValueRef::Boolean(_) => 1,
1734            ValueRef::UInt8(_) => 1,
1735            ValueRef::UInt16(_) => 2,
1736            ValueRef::UInt32(_) => 4,
1737            ValueRef::UInt64(_) => 8,
1738            ValueRef::Int8(_) => 1,
1739            ValueRef::Int16(_) => 2,
1740            ValueRef::Int32(_) => 4,
1741            ValueRef::Int64(_) => 8,
1742            ValueRef::Float32(_) => 4,
1743            ValueRef::Float64(_) => 8,
1744            ValueRef::String(v) => std::mem::size_of_val(*v),
1745            ValueRef::Binary(v) => std::mem::size_of_val(*v),
1746            ValueRef::Date(_) => 4,
1747            ValueRef::Timestamp(_) => 16,
1748            ValueRef::Time(_) => 16,
1749            ValueRef::Duration(_) => 16,
1750            ValueRef::IntervalYearMonth(_) => 4,
1751            ValueRef::IntervalDayTime(_) => 8,
1752            ValueRef::IntervalMonthDayNano(_) => 16,
1753            ValueRef::Decimal128(_) => 32,
1754            ValueRef::List(v) => match v {
1755                ListValueRef::Indexed { vector, .. } => vector.memory_size() / vector.len(),
1756                ListValueRef::Ref { val } => val.estimated_size(),
1757                ListValueRef::RefList { val, .. } => {
1758                    val.iter().map(|v| v.data_size()).sum::<usize>()
1759                        + std::mem::size_of::<Arc<ConcreteDataType>>()
1760                }
1761            },
1762            ValueRef::Struct(val) => match val {
1763                StructValueRef::Indexed { vector, .. } => vector.memory_size() / vector.len(),
1764                StructValueRef::Ref(val) => val.estimated_size(),
1765                StructValueRef::RefList { val, .. } => {
1766                    val.iter().map(|v| v.data_size()).sum::<usize>()
1767                        + std::mem::size_of::<StructType>()
1768                }
1769            },
1770            ValueRef::Json(v) => v.data_size(),
1771        }
1772    }
1773}
1774
1775#[cfg(test)]
1776pub(crate) mod tests {
1777    use arrow::datatypes::{DataType as ArrowDataType, Field};
1778    use common_time::timezone::set_default_timezone;
1779    use num_traits::Float;
1780
1781    use super::*;
1782    use crate::json::value::{JsonVariant, JsonVariantRef};
1783    use crate::types::StructField;
1784    use crate::types::json_type::{JsonNativeType, JsonObjectType};
1785    use crate::vectors::ListVectorBuilder;
1786
1787    #[test]
1788    fn test_try_negative_overflow() {
1789        // Negating a MIN value overflows, so try_negative returns None instead
1790        // of panicking (consistent with the unsigned arms).
1791        assert_eq!(Value::Int8(i8::MIN).try_negative(), None);
1792        assert_eq!(Value::Int16(i16::MIN).try_negative(), None);
1793        assert_eq!(Value::Int32(i32::MIN).try_negative(), None);
1794        assert_eq!(Value::Int64(i64::MIN).try_negative(), None);
1795        assert_eq!(
1796            Value::Timestamp(Timestamp::new_nanosecond(i64::MIN)).try_negative(),
1797            None
1798        );
1799        assert_eq!(Value::Date(Date::new(i32::MIN)).try_negative(), None);
1800        assert_eq!(
1801            Value::Time(Time::new_nanosecond(i64::MIN)).try_negative(),
1802            None
1803        );
1804        assert_eq!(
1805            Value::Duration(Duration::new_nanosecond(i64::MIN)).try_negative(),
1806            None
1807        );
1808        assert_eq!(
1809            Value::IntervalYearMonth(IntervalYearMonth::new(i32::MIN)).try_negative(),
1810            None
1811        );
1812        assert_eq!(
1813            Value::IntervalDayTime(IntervalDayTime::new(i32::MIN, i32::MIN)).try_negative(),
1814            None
1815        );
1816        assert_eq!(
1817            Value::IntervalMonthDayNano(IntervalMonthDayNano::new(i32::MIN, i32::MIN, i64::MIN))
1818                .try_negative(),
1819            None
1820        );
1821
1822        // Non-MIN values still negate.
1823        assert_eq!(Value::Int64(5).try_negative(), Some(Value::Int64(-5)));
1824        assert_eq!(
1825            Value::Timestamp(Timestamp::new_nanosecond(5)).try_negative(),
1826            Some(Value::Timestamp(Timestamp::new_nanosecond(-5)))
1827        );
1828    }
1829
1830    pub(crate) fn build_struct_type() -> StructType {
1831        StructType::new(Arc::new(vec![
1832            StructField::new("id".to_string(), ConcreteDataType::int32_datatype(), false),
1833            StructField::new(
1834                "name".to_string(),
1835                ConcreteDataType::string_datatype(),
1836                true,
1837            ),
1838            StructField::new("age".to_string(), ConcreteDataType::uint8_datatype(), true),
1839            StructField::new(
1840                "address".to_string(),
1841                ConcreteDataType::string_datatype(),
1842                true,
1843            ),
1844            StructField::new(
1845                "awards".to_string(),
1846                ConcreteDataType::list_datatype(Arc::new(ConcreteDataType::boolean_datatype())),
1847                true,
1848            ),
1849        ]))
1850    }
1851
1852    pub(crate) fn build_struct_value() -> StructValue {
1853        let struct_type = build_struct_type();
1854
1855        let struct_items = vec![
1856            Value::Int32(1),
1857            Value::String("tom".into()),
1858            Value::UInt8(25),
1859            Value::String("94038".into()),
1860            Value::List(build_list_value()),
1861        ];
1862        StructValue::try_new(struct_items, struct_type).unwrap()
1863    }
1864
1865    pub(crate) fn build_scalar_struct_value() -> ScalarValue {
1866        let struct_type = build_struct_type();
1867        let arrays = vec![
1868            ScalarValue::Int32(Some(1)).to_array().unwrap(),
1869            ScalarValue::Utf8(Some("tom".into())).to_array().unwrap(),
1870            ScalarValue::UInt8(Some(25)).to_array().unwrap(),
1871            ScalarValue::Utf8(Some("94038".into())).to_array().unwrap(),
1872            build_scalar_list_value().to_array().unwrap(),
1873        ];
1874        let struct_arrow_array = StructArray::new(struct_type.as_arrow_fields(), arrays, None);
1875        ScalarValue::Struct(Arc::new(struct_arrow_array))
1876    }
1877
1878    pub(crate) fn build_list_value() -> ListValue {
1879        let items = vec![Value::Boolean(true), Value::Boolean(false)];
1880        ListValue::new(items, Arc::new(ConcreteDataType::boolean_datatype()))
1881    }
1882
1883    pub(crate) fn build_scalar_list_value() -> ScalarValue {
1884        let items = vec![
1885            ScalarValue::Boolean(Some(true)),
1886            ScalarValue::Boolean(Some(false)),
1887        ];
1888        ScalarValue::List(ScalarValue::new_list(&items, &ArrowDataType::Boolean, true))
1889    }
1890
1891    #[test]
1892    fn test_try_from_scalar_value() {
1893        assert_eq!(
1894            Value::Boolean(true),
1895            ScalarValue::Boolean(Some(true)).try_into().unwrap()
1896        );
1897        assert_eq!(
1898            Value::Boolean(false),
1899            ScalarValue::Boolean(Some(false)).try_into().unwrap()
1900        );
1901        assert_eq!(Value::Null, ScalarValue::Boolean(None).try_into().unwrap());
1902
1903        assert_eq!(
1904            Value::Float32(1.0f32.into()),
1905            ScalarValue::Float32(Some(1.0f32)).try_into().unwrap()
1906        );
1907        assert_eq!(Value::Null, ScalarValue::Float32(None).try_into().unwrap());
1908
1909        assert_eq!(
1910            Value::Float64(2.0f64.into()),
1911            ScalarValue::Float64(Some(2.0f64)).try_into().unwrap()
1912        );
1913        assert_eq!(Value::Null, ScalarValue::Float64(None).try_into().unwrap());
1914
1915        assert_eq!(
1916            Value::Int8(i8::MAX),
1917            ScalarValue::Int8(Some(i8::MAX)).try_into().unwrap()
1918        );
1919        assert_eq!(Value::Null, ScalarValue::Int8(None).try_into().unwrap());
1920
1921        assert_eq!(
1922            Value::Int16(i16::MAX),
1923            ScalarValue::Int16(Some(i16::MAX)).try_into().unwrap()
1924        );
1925        assert_eq!(Value::Null, ScalarValue::Int16(None).try_into().unwrap());
1926
1927        assert_eq!(
1928            Value::Int32(i32::MAX),
1929            ScalarValue::Int32(Some(i32::MAX)).try_into().unwrap()
1930        );
1931        assert_eq!(Value::Null, ScalarValue::Int32(None).try_into().unwrap());
1932
1933        assert_eq!(
1934            Value::Int64(i64::MAX),
1935            ScalarValue::Int64(Some(i64::MAX)).try_into().unwrap()
1936        );
1937        assert_eq!(Value::Null, ScalarValue::Int64(None).try_into().unwrap());
1938
1939        assert_eq!(
1940            Value::UInt8(u8::MAX),
1941            ScalarValue::UInt8(Some(u8::MAX)).try_into().unwrap()
1942        );
1943        assert_eq!(Value::Null, ScalarValue::UInt8(None).try_into().unwrap());
1944
1945        assert_eq!(
1946            Value::UInt16(u16::MAX),
1947            ScalarValue::UInt16(Some(u16::MAX)).try_into().unwrap()
1948        );
1949        assert_eq!(Value::Null, ScalarValue::UInt16(None).try_into().unwrap());
1950
1951        assert_eq!(
1952            Value::UInt32(u32::MAX),
1953            ScalarValue::UInt32(Some(u32::MAX)).try_into().unwrap()
1954        );
1955        assert_eq!(Value::Null, ScalarValue::UInt32(None).try_into().unwrap());
1956
1957        assert_eq!(
1958            Value::UInt64(u64::MAX),
1959            ScalarValue::UInt64(Some(u64::MAX)).try_into().unwrap()
1960        );
1961        assert_eq!(Value::Null, ScalarValue::UInt64(None).try_into().unwrap());
1962
1963        assert_eq!(
1964            Value::from("hello"),
1965            ScalarValue::Utf8(Some("hello".to_string()))
1966                .try_into()
1967                .unwrap()
1968        );
1969        assert_eq!(Value::Null, ScalarValue::Utf8(None).try_into().unwrap());
1970
1971        assert_eq!(
1972            Value::from("dictionary"),
1973            ScalarValue::Dictionary(
1974                Box::new(ArrowDataType::UInt32),
1975                Box::new(ScalarValue::Utf8(Some("dictionary".to_string()))),
1976            )
1977            .try_into()
1978            .unwrap()
1979        );
1980
1981        assert_eq!(
1982            Value::from("large_hello"),
1983            ScalarValue::LargeUtf8(Some("large_hello".to_string()))
1984                .try_into()
1985                .unwrap()
1986        );
1987        assert_eq!(
1988            Value::Null,
1989            ScalarValue::LargeUtf8(None).try_into().unwrap()
1990        );
1991
1992        assert_eq!(
1993            Value::from("world".as_bytes()),
1994            ScalarValue::Binary(Some("world".as_bytes().to_vec()))
1995                .try_into()
1996                .unwrap()
1997        );
1998        assert_eq!(Value::Null, ScalarValue::Binary(None).try_into().unwrap());
1999
2000        assert_eq!(
2001            Value::from("large_world".as_bytes()),
2002            ScalarValue::LargeBinary(Some("large_world".as_bytes().to_vec()))
2003                .try_into()
2004                .unwrap()
2005        );
2006        assert_eq!(
2007            Value::Null,
2008            ScalarValue::LargeBinary(None).try_into().unwrap()
2009        );
2010
2011        assert_eq!(
2012            Value::List(build_list_value()),
2013            build_scalar_list_value().try_into().unwrap()
2014        );
2015        assert_eq!(
2016            Value::List(ListValue::new(
2017                vec![],
2018                Arc::new(ConcreteDataType::uint32_datatype())
2019            )),
2020            ScalarValue::List(ScalarValue::new_list(&[], &ArrowDataType::UInt32, true))
2021                .try_into()
2022                .unwrap()
2023        );
2024
2025        assert_eq!(
2026            Value::Date(Date::new(123)),
2027            ScalarValue::Date32(Some(123)).try_into().unwrap()
2028        );
2029        assert_eq!(Value::Null, ScalarValue::Date32(None).try_into().unwrap());
2030
2031        assert_eq!(
2032            Value::Timestamp(Timestamp::new(1, TimeUnit::Second)),
2033            ScalarValue::TimestampSecond(Some(1), None)
2034                .try_into()
2035                .unwrap()
2036        );
2037        assert_eq!(
2038            Value::Null,
2039            ScalarValue::TimestampSecond(None, None).try_into().unwrap()
2040        );
2041
2042        assert_eq!(
2043            Value::Timestamp(Timestamp::new(1, TimeUnit::Millisecond)),
2044            ScalarValue::TimestampMillisecond(Some(1), None)
2045                .try_into()
2046                .unwrap()
2047        );
2048        assert_eq!(
2049            Value::Null,
2050            ScalarValue::TimestampMillisecond(None, None)
2051                .try_into()
2052                .unwrap()
2053        );
2054
2055        assert_eq!(
2056            Value::Timestamp(Timestamp::new(1, TimeUnit::Microsecond)),
2057            ScalarValue::TimestampMicrosecond(Some(1), None)
2058                .try_into()
2059                .unwrap()
2060        );
2061        assert_eq!(
2062            Value::Null,
2063            ScalarValue::TimestampMicrosecond(None, None)
2064                .try_into()
2065                .unwrap()
2066        );
2067
2068        assert_eq!(
2069            Value::Timestamp(Timestamp::new(1, TimeUnit::Nanosecond)),
2070            ScalarValue::TimestampNanosecond(Some(1), None)
2071                .try_into()
2072                .unwrap()
2073        );
2074        assert_eq!(
2075            Value::Null,
2076            ScalarValue::TimestampNanosecond(None, None)
2077                .try_into()
2078                .unwrap()
2079        );
2080        assert_eq!(
2081            Value::Null,
2082            ScalarValue::IntervalMonthDayNano(None).try_into().unwrap()
2083        );
2084        assert_eq!(
2085            Value::IntervalMonthDayNano(IntervalMonthDayNano::new(1, 1, 1)),
2086            ScalarValue::IntervalMonthDayNano(Some(IntervalMonthDayNano::new(1, 1, 1).into()))
2087                .try_into()
2088                .unwrap()
2089        );
2090
2091        assert_eq!(
2092            Value::Time(Time::new(1, TimeUnit::Second)),
2093            ScalarValue::Time32Second(Some(1)).try_into().unwrap()
2094        );
2095        assert_eq!(
2096            Value::Null,
2097            ScalarValue::Time32Second(None).try_into().unwrap()
2098        );
2099
2100        assert_eq!(
2101            Value::Time(Time::new(1, TimeUnit::Millisecond)),
2102            ScalarValue::Time32Millisecond(Some(1)).try_into().unwrap()
2103        );
2104        assert_eq!(
2105            Value::Null,
2106            ScalarValue::Time32Millisecond(None).try_into().unwrap()
2107        );
2108
2109        assert_eq!(
2110            Value::Time(Time::new(1, TimeUnit::Microsecond)),
2111            ScalarValue::Time64Microsecond(Some(1)).try_into().unwrap()
2112        );
2113        assert_eq!(
2114            Value::Null,
2115            ScalarValue::Time64Microsecond(None).try_into().unwrap()
2116        );
2117
2118        assert_eq!(
2119            Value::Time(Time::new(1, TimeUnit::Nanosecond)),
2120            ScalarValue::Time64Nanosecond(Some(1)).try_into().unwrap()
2121        );
2122        assert_eq!(
2123            Value::Null,
2124            ScalarValue::Time64Nanosecond(None).try_into().unwrap()
2125        );
2126
2127        assert_eq!(
2128            Value::Duration(Duration::new_second(1)),
2129            ScalarValue::DurationSecond(Some(1)).try_into().unwrap()
2130        );
2131        assert_eq!(
2132            Value::Null,
2133            ScalarValue::DurationSecond(None).try_into().unwrap()
2134        );
2135
2136        assert_eq!(
2137            Value::Duration(Duration::new_millisecond(1)),
2138            ScalarValue::DurationMillisecond(Some(1))
2139                .try_into()
2140                .unwrap()
2141        );
2142        assert_eq!(
2143            Value::Null,
2144            ScalarValue::DurationMillisecond(None).try_into().unwrap()
2145        );
2146
2147        assert_eq!(
2148            Value::Duration(Duration::new_microsecond(1)),
2149            ScalarValue::DurationMicrosecond(Some(1))
2150                .try_into()
2151                .unwrap()
2152        );
2153        assert_eq!(
2154            Value::Null,
2155            ScalarValue::DurationMicrosecond(None).try_into().unwrap()
2156        );
2157
2158        assert_eq!(
2159            Value::Duration(Duration::new_nanosecond(1)),
2160            ScalarValue::DurationNanosecond(Some(1)).try_into().unwrap()
2161        );
2162        assert_eq!(
2163            Value::Null,
2164            ScalarValue::DurationNanosecond(None).try_into().unwrap()
2165        );
2166
2167        assert_eq!(
2168            Value::Decimal128(Decimal128::new(1, 38, 10)),
2169            ScalarValue::Decimal128(Some(1), 38, 10).try_into().unwrap()
2170        );
2171        assert_eq!(
2172            Value::Null,
2173            ScalarValue::Decimal128(None, 0, 0).try_into().unwrap()
2174        );
2175
2176        let struct_value = build_struct_value();
2177        let scalar_struct_value = build_scalar_struct_value();
2178        assert_eq!(
2179            Value::Struct(struct_value),
2180            scalar_struct_value.try_into().unwrap()
2181        );
2182
2183        // view-typed strings and binaries convert like their non-view forms
2184        assert_eq!(
2185            Value::String("abc".into()),
2186            ScalarValue::Utf8View(Some("abc".into()))
2187                .try_into()
2188                .unwrap()
2189        );
2190        assert_eq!(Value::Null, ScalarValue::Utf8View(None).try_into().unwrap());
2191        assert_eq!(
2192            Value::Binary(Bytes::from(vec![1, 2])),
2193            ScalarValue::BinaryView(Some(vec![1, 2]))
2194                .try_into()
2195                .unwrap()
2196        );
2197        assert_eq!(
2198            Value::Null,
2199            ScalarValue::BinaryView(None).try_into().unwrap()
2200        );
2201
2202        // a null struct scalar is a null value, not a struct of null fields
2203        let null_struct = ScalarStructBuilder::new_null(build_struct_type().as_arrow_fields());
2204        assert_eq!(Value::Null, null_struct.try_into().unwrap());
2205    }
2206
2207    #[test]
2208    fn test_try_value_from_array() {
2209        use arrow_array::Int32Array;
2210        use datafusion_common::arrow::datatypes::i256;
2211
2212        let array = Int32Array::from(vec![Some(1), None]);
2213        assert_eq!(Value::Int32(1), try_value_from_array(&array, 0).unwrap());
2214        assert_eq!(Value::Null, try_value_from_array(&array, 1).unwrap());
2215
2216        // a struct with representable fields converts
2217        let supported = ScalarStructBuilder::new()
2218            .with_name_and_scalar("i", ScalarValue::Int32(Some(7)))
2219            .build()
2220            .unwrap();
2221        let ScalarValue::Struct(array) = supported else {
2222            unreachable!();
2223        };
2224        assert_eq!(
2225            Value::Struct(StructValue::new(
2226                vec![Value::Int32(7)],
2227                StructType::new(std::sync::Arc::new(vec![StructField::new(
2228                    "i",
2229                    ConcreteDataType::int32_datatype(),
2230                    false,
2231                )]))
2232            )),
2233            try_value_from_array(array.as_ref(), 0).unwrap()
2234        );
2235
2236        // an unsupported arrow field type errors instead of panicking
2237        let unsupported = ScalarStructBuilder::new()
2238            .with_name_and_scalar(
2239                "d",
2240                ScalarValue::Decimal256(Some(i256::from_i128(1)), 38, 10),
2241            )
2242            .build()
2243            .unwrap();
2244        let ScalarValue::Struct(array) = unsupported else {
2245            unreachable!();
2246        };
2247        let error = try_value_from_array(array.as_ref(), 0).unwrap_err();
2248        assert!(error.to_string().contains("Unsupported arrow data type"));
2249    }
2250
2251    #[test]
2252    fn test_value_from_inner() {
2253        assert_eq!(Value::Boolean(true), Value::from(true));
2254        assert_eq!(Value::Boolean(false), Value::from(false));
2255
2256        assert_eq!(Value::UInt8(u8::MIN), Value::from(u8::MIN));
2257        assert_eq!(Value::UInt8(u8::MAX), Value::from(u8::MAX));
2258
2259        assert_eq!(Value::UInt16(u16::MIN), Value::from(u16::MIN));
2260        assert_eq!(Value::UInt16(u16::MAX), Value::from(u16::MAX));
2261
2262        assert_eq!(Value::UInt32(u32::MIN), Value::from(u32::MIN));
2263        assert_eq!(Value::UInt32(u32::MAX), Value::from(u32::MAX));
2264
2265        assert_eq!(Value::UInt64(u64::MIN), Value::from(u64::MIN));
2266        assert_eq!(Value::UInt64(u64::MAX), Value::from(u64::MAX));
2267
2268        assert_eq!(Value::Int8(i8::MIN), Value::from(i8::MIN));
2269        assert_eq!(Value::Int8(i8::MAX), Value::from(i8::MAX));
2270
2271        assert_eq!(Value::Int16(i16::MIN), Value::from(i16::MIN));
2272        assert_eq!(Value::Int16(i16::MAX), Value::from(i16::MAX));
2273
2274        assert_eq!(Value::Int32(i32::MIN), Value::from(i32::MIN));
2275        assert_eq!(Value::Int32(i32::MAX), Value::from(i32::MAX));
2276
2277        assert_eq!(Value::Int64(i64::MIN), Value::from(i64::MIN));
2278        assert_eq!(Value::Int64(i64::MAX), Value::from(i64::MAX));
2279
2280        assert_eq!(
2281            Value::Float32(OrderedFloat(f32::MIN)),
2282            Value::from(f32::MIN)
2283        );
2284        assert_eq!(
2285            Value::Float32(OrderedFloat(f32::MAX)),
2286            Value::from(f32::MAX)
2287        );
2288
2289        assert_eq!(
2290            Value::Float64(OrderedFloat(f64::MIN)),
2291            Value::from(f64::MIN)
2292        );
2293        assert_eq!(
2294            Value::Float64(OrderedFloat(f64::MAX)),
2295            Value::from(f64::MAX)
2296        );
2297
2298        let string_bytes = StringBytes::from("hello");
2299        assert_eq!(
2300            Value::String(string_bytes.clone()),
2301            Value::from(string_bytes)
2302        );
2303
2304        let bytes = Bytes::from(b"world".as_slice());
2305        assert_eq!(Value::Binary(bytes.clone()), Value::from(bytes));
2306    }
2307
2308    fn check_type_and_value(data_type: &ConcreteDataType, value: &Value) {
2309        assert_eq!(*data_type, value.data_type());
2310        assert_eq!(data_type.logical_type_id(), value.logical_type_id());
2311    }
2312
2313    #[test]
2314    fn test_value_datatype() {
2315        check_type_and_value(&ConcreteDataType::boolean_datatype(), &Value::Boolean(true));
2316        check_type_and_value(&ConcreteDataType::uint8_datatype(), &Value::UInt8(u8::MIN));
2317        check_type_and_value(
2318            &ConcreteDataType::uint16_datatype(),
2319            &Value::UInt16(u16::MIN),
2320        );
2321        check_type_and_value(
2322            &ConcreteDataType::uint16_datatype(),
2323            &Value::UInt16(u16::MAX),
2324        );
2325        check_type_and_value(
2326            &ConcreteDataType::uint32_datatype(),
2327            &Value::UInt32(u32::MIN),
2328        );
2329        check_type_and_value(
2330            &ConcreteDataType::uint64_datatype(),
2331            &Value::UInt64(u64::MIN),
2332        );
2333        check_type_and_value(&ConcreteDataType::int8_datatype(), &Value::Int8(i8::MIN));
2334        check_type_and_value(&ConcreteDataType::int16_datatype(), &Value::Int16(i16::MIN));
2335        check_type_and_value(&ConcreteDataType::int32_datatype(), &Value::Int32(i32::MIN));
2336        check_type_and_value(&ConcreteDataType::int64_datatype(), &Value::Int64(i64::MIN));
2337        check_type_and_value(
2338            &ConcreteDataType::float32_datatype(),
2339            &Value::Float32(OrderedFloat(f32::MIN)),
2340        );
2341        check_type_and_value(
2342            &ConcreteDataType::float64_datatype(),
2343            &Value::Float64(OrderedFloat(f64::MIN)),
2344        );
2345        check_type_and_value(
2346            &ConcreteDataType::string_datatype(),
2347            &Value::String(StringBytes::from("hello")),
2348        );
2349        check_type_and_value(
2350            &ConcreteDataType::binary_datatype(),
2351            &Value::Binary(Bytes::from(b"world".as_slice())),
2352        );
2353        let item_type = Arc::new(ConcreteDataType::int32_datatype());
2354        check_type_and_value(
2355            &ConcreteDataType::list_datatype(item_type.clone()),
2356            &Value::List(ListValue::new(vec![Value::Int32(10)], item_type.clone())),
2357        );
2358        check_type_and_value(
2359            &ConcreteDataType::list_datatype(Arc::new(ConcreteDataType::null_datatype())),
2360            &Value::List(ListValue::default()),
2361        );
2362        check_type_and_value(
2363            &ConcreteDataType::date_datatype(),
2364            &Value::Date(Date::new(1)),
2365        );
2366        check_type_and_value(
2367            &ConcreteDataType::timestamp_millisecond_datatype(),
2368            &Value::Timestamp(Timestamp::new_millisecond(1)),
2369        );
2370        check_type_and_value(
2371            &ConcreteDataType::time_second_datatype(),
2372            &Value::Time(Time::new_second(1)),
2373        );
2374        check_type_and_value(
2375            &ConcreteDataType::time_millisecond_datatype(),
2376            &Value::Time(Time::new_millisecond(1)),
2377        );
2378        check_type_and_value(
2379            &ConcreteDataType::time_microsecond_datatype(),
2380            &Value::Time(Time::new_microsecond(1)),
2381        );
2382        check_type_and_value(
2383            &ConcreteDataType::time_nanosecond_datatype(),
2384            &Value::Time(Time::new_nanosecond(1)),
2385        );
2386        check_type_and_value(
2387            &ConcreteDataType::interval_year_month_datatype(),
2388            &Value::IntervalYearMonth(IntervalYearMonth::new(1)),
2389        );
2390        check_type_and_value(
2391            &ConcreteDataType::interval_day_time_datatype(),
2392            &Value::IntervalDayTime(IntervalDayTime::new(1, 2)),
2393        );
2394        check_type_and_value(
2395            &ConcreteDataType::interval_month_day_nano_datatype(),
2396            &Value::IntervalMonthDayNano(IntervalMonthDayNano::new(1, 2, 3)),
2397        );
2398        check_type_and_value(
2399            &ConcreteDataType::duration_second_datatype(),
2400            &Value::Duration(Duration::new_second(1)),
2401        );
2402        check_type_and_value(
2403            &ConcreteDataType::duration_millisecond_datatype(),
2404            &Value::Duration(Duration::new_millisecond(1)),
2405        );
2406        check_type_and_value(
2407            &ConcreteDataType::duration_microsecond_datatype(),
2408            &Value::Duration(Duration::new_microsecond(1)),
2409        );
2410        check_type_and_value(
2411            &ConcreteDataType::duration_nanosecond_datatype(),
2412            &Value::Duration(Duration::new_nanosecond(1)),
2413        );
2414        check_type_and_value(
2415            &ConcreteDataType::decimal128_datatype(38, 10),
2416            &Value::Decimal128(Decimal128::new(1, 38, 10)),
2417        );
2418
2419        let item_type = Arc::new(ConcreteDataType::boolean_datatype());
2420        check_type_and_value(
2421            &ConcreteDataType::list_datatype(item_type.clone()),
2422            &Value::List(ListValue::new(
2423                vec![Value::Boolean(true)],
2424                item_type.clone(),
2425            )),
2426        );
2427
2428        check_type_and_value(
2429            &ConcreteDataType::struct_datatype(build_struct_type()),
2430            &Value::Struct(build_struct_value()),
2431        );
2432
2433        check_type_and_value(
2434            &ConcreteDataType::json2(JsonNativeType::Bool),
2435            &Value::Json(Box::new(true.into())),
2436        );
2437
2438        check_type_and_value(
2439            &ConcreteDataType::json2(JsonNativeType::Array(Box::new(JsonNativeType::Bool))),
2440            &Value::Json(Box::new([true].into())),
2441        );
2442
2443        check_type_and_value(
2444            &ConcreteDataType::json2(JsonNativeType::Object(JsonObjectType::from([
2445                ("address".to_string(), JsonNativeType::String),
2446                ("age".to_string(), JsonNativeType::u64()),
2447                (
2448                    "awards".to_string(),
2449                    JsonNativeType::Array(Box::new(JsonNativeType::Bool)),
2450                ),
2451                ("id".to_string(), JsonNativeType::i64()),
2452                ("name".to_string(), JsonNativeType::String),
2453            ]))),
2454            &Value::Json(Box::new(
2455                [
2456                    ("id", JsonVariant::from(1i64)),
2457                    ("name", "Alice".into()),
2458                    ("age", 1u64.into()),
2459                    ("address", "blah".into()),
2460                    ("awards", [true, false].into()),
2461                ]
2462                .into(),
2463            )),
2464        );
2465    }
2466
2467    #[test]
2468    fn test_value_from_string() {
2469        let hello = "hello".to_string();
2470        assert_eq!(
2471            Value::String(StringBytes::from(hello.clone())),
2472            Value::from(hello)
2473        );
2474
2475        let world = "world";
2476        assert_eq!(Value::String(StringBytes::from(world)), Value::from(world));
2477    }
2478
2479    #[test]
2480    fn test_value_from_bytes() {
2481        let hello = b"hello".to_vec();
2482        assert_eq!(
2483            Value::Binary(Bytes::from(hello.clone())),
2484            Value::from(hello)
2485        );
2486
2487        let world: &[u8] = b"world";
2488        assert_eq!(Value::Binary(Bytes::from(world)), Value::from(world));
2489    }
2490
2491    fn to_json(value: Value) -> serde_json::Value {
2492        value.try_into().unwrap()
2493    }
2494
2495    #[test]
2496    fn test_to_json_value() {
2497        assert_eq!(serde_json::Value::Null, to_json(Value::Null));
2498        assert_eq!(serde_json::Value::Bool(true), to_json(Value::Boolean(true)));
2499        assert_eq!(
2500            serde_json::Value::Number(20u8.into()),
2501            to_json(Value::UInt8(20))
2502        );
2503        assert_eq!(
2504            serde_json::Value::Number(20i8.into()),
2505            to_json(Value::Int8(20))
2506        );
2507        assert_eq!(
2508            serde_json::Value::Number(2000u16.into()),
2509            to_json(Value::UInt16(2000))
2510        );
2511        assert_eq!(
2512            serde_json::Value::Number(2000i16.into()),
2513            to_json(Value::Int16(2000))
2514        );
2515        assert_eq!(
2516            serde_json::Value::Number(3000u32.into()),
2517            to_json(Value::UInt32(3000))
2518        );
2519        assert_eq!(
2520            serde_json::Value::Number(3000i32.into()),
2521            to_json(Value::Int32(3000))
2522        );
2523        assert_eq!(
2524            serde_json::Value::Number(4000u64.into()),
2525            to_json(Value::UInt64(4000))
2526        );
2527        assert_eq!(
2528            serde_json::Value::Number(4000i64.into()),
2529            to_json(Value::Int64(4000))
2530        );
2531        assert_eq!(
2532            serde_json::Value::from(125.0f32),
2533            to_json(Value::Float32(125.0.into()))
2534        );
2535        assert_eq!(
2536            serde_json::Value::from(125.0f64),
2537            to_json(Value::Float64(125.0.into()))
2538        );
2539        assert_eq!(
2540            serde_json::Value::String(String::from("hello")),
2541            to_json(Value::String(StringBytes::from("hello")))
2542        );
2543        assert_eq!(
2544            serde_json::Value::from(b"world".as_slice()),
2545            to_json(Value::Binary(Bytes::from(b"world".as_slice())))
2546        );
2547        assert_eq!(
2548            serde_json::Value::Number(5000i32.into()),
2549            to_json(Value::Date(Date::new(5000)))
2550        );
2551        assert_eq!(
2552            serde_json::Value::Number(1.into()),
2553            to_json(Value::Timestamp(Timestamp::new_millisecond(1)))
2554        );
2555        assert_eq!(
2556            serde_json::Value::Number(1.into()),
2557            to_json(Value::Time(Time::new_millisecond(1)))
2558        );
2559        assert_eq!(
2560            serde_json::Value::Number(1.into()),
2561            to_json(Value::Duration(Duration::new_millisecond(1)))
2562        );
2563
2564        let json_value: serde_json::Value = serde_json::from_str(r#"[123]"#).unwrap();
2565        assert_eq!(
2566            json_value,
2567            to_json(Value::List(ListValue {
2568                items: vec![Value::Int32(123)],
2569                datatype: Arc::new(ConcreteDataType::int32_datatype()),
2570            }))
2571        );
2572
2573        let struct_value = StructValue::try_new(
2574            vec![
2575                Value::Int64(42),
2576                Value::String("tomcat".into()),
2577                Value::Boolean(true),
2578            ],
2579            StructType::new(Arc::new(vec![
2580                StructField::new("num".to_string(), ConcreteDataType::int64_datatype(), true),
2581                StructField::new(
2582                    "name".to_string(),
2583                    ConcreteDataType::string_datatype(),
2584                    true,
2585                ),
2586                StructField::new(
2587                    "yes_or_no".to_string(),
2588                    ConcreteDataType::boolean_datatype(),
2589                    true,
2590                ),
2591            ])),
2592        )
2593        .unwrap();
2594        assert_eq!(
2595            serde_json::Value::try_from(Value::Struct(struct_value.clone())).unwrap(),
2596            serde_json::json!({
2597                "num": 42,
2598                "name": "tomcat",
2599                "yes_or_no": true
2600            })
2601        );
2602
2603        // string wrapped in json
2604        assert_eq!(
2605            serde_json::Value::try_from(Value::Json(Box::new("hello".into()))).unwrap(),
2606            serde_json::json!("hello")
2607        );
2608
2609        // list wrapped in json
2610        assert_eq!(
2611            serde_json::Value::try_from(Value::Json(Box::new([1i64, 2, 3,].into()))).unwrap(),
2612            serde_json::json!([1, 2, 3])
2613        );
2614
2615        // struct wrapped in json
2616        assert_eq!(
2617            serde_json::Value::try_from(Value::Json(Box::new(
2618                [
2619                    ("num".to_string(), JsonVariant::from(42i64)),
2620                    ("name".to_string(), "tomcat".into()),
2621                    ("yes_or_no".to_string(), true.into()),
2622                ]
2623                .into()
2624            )))
2625            .unwrap(),
2626            serde_json::json!({
2627                "num": 42,
2628                "name": "tomcat",
2629                "yes_or_no": true
2630            })
2631        );
2632    }
2633
2634    #[test]
2635    fn test_null_value() {
2636        assert!(Value::Null.is_null());
2637        assert!(Value::Json(Box::new(JsonValue::null())).is_null());
2638        assert!(!Value::Boolean(true).is_null());
2639        assert!(Value::Null < Value::Boolean(false));
2640        assert!(Value::Boolean(true) > Value::Null);
2641        assert!(Value::Null < Value::Int32(10));
2642        assert!(Value::Int32(10) > Value::Null);
2643    }
2644
2645    #[test]
2646    fn test_null_value_ref() {
2647        assert!(ValueRef::Null.is_null());
2648        assert!(!ValueRef::Boolean(true).is_null());
2649        assert!(ValueRef::Null < ValueRef::Boolean(false));
2650        assert!(ValueRef::Boolean(true) > ValueRef::Null);
2651        assert!(ValueRef::Null < ValueRef::Int32(10));
2652        assert!(ValueRef::Int32(10) > ValueRef::Null);
2653    }
2654
2655    #[test]
2656    fn test_as_value_ref() {
2657        macro_rules! check_as_value_ref {
2658            ($Variant: ident, $data: expr) => {
2659                let value = Value::$Variant($data);
2660                let value_ref = value.as_value_ref();
2661                let expect_ref = ValueRef::$Variant($data);
2662
2663                assert_eq!(expect_ref, value_ref);
2664            };
2665        }
2666
2667        assert_eq!(ValueRef::Null, Value::Null.as_value_ref());
2668        check_as_value_ref!(Boolean, true);
2669        check_as_value_ref!(UInt8, 123);
2670        check_as_value_ref!(UInt16, 123);
2671        check_as_value_ref!(UInt32, 123);
2672        check_as_value_ref!(UInt64, 123);
2673        check_as_value_ref!(Int8, -12);
2674        check_as_value_ref!(Int16, -12);
2675        check_as_value_ref!(Int32, -12);
2676        check_as_value_ref!(Int64, -12);
2677        check_as_value_ref!(Float32, OrderedF32::from(16.0));
2678        check_as_value_ref!(Float64, OrderedF64::from(16.0));
2679        check_as_value_ref!(Timestamp, Timestamp::new_millisecond(1));
2680        check_as_value_ref!(Time, Time::new_millisecond(1));
2681        check_as_value_ref!(IntervalYearMonth, IntervalYearMonth::new(1));
2682        check_as_value_ref!(IntervalDayTime, IntervalDayTime::new(1, 2));
2683        check_as_value_ref!(IntervalMonthDayNano, IntervalMonthDayNano::new(1, 2, 3));
2684        check_as_value_ref!(Duration, Duration::new_millisecond(1));
2685
2686        assert_eq!(
2687            ValueRef::String("hello"),
2688            Value::String("hello".into()).as_value_ref()
2689        );
2690        assert_eq!(
2691            ValueRef::Binary(b"hello"),
2692            Value::Binary("hello".as_bytes().into()).as_value_ref()
2693        );
2694
2695        check_as_value_ref!(Date, Date::new(103));
2696
2697        let list = build_list_value();
2698        assert_eq!(
2699            ValueRef::List(ListValueRef::Ref { val: &list }),
2700            Value::List(list.clone()).as_value_ref()
2701        );
2702
2703        let jsonb_value = jsonb::parse_value(r#"{"key": "value"}"#.as_bytes())
2704            .unwrap()
2705            .to_vec();
2706        assert_eq!(
2707            ValueRef::Binary(jsonb_value.clone().as_slice()),
2708            Value::Binary(jsonb_value.into()).as_value_ref()
2709        );
2710
2711        let struct_value = build_struct_value();
2712        assert_eq!(
2713            ValueRef::Struct(StructValueRef::Ref(&struct_value)),
2714            Value::Struct(struct_value.clone()).as_value_ref()
2715        );
2716    }
2717
2718    #[test]
2719    fn test_value_ref_as() {
2720        macro_rules! check_as_null {
2721            ($method: ident) => {
2722                assert_eq!(None, ValueRef::Null.$method().unwrap());
2723            };
2724        }
2725
2726        check_as_null!(try_into_binary);
2727        check_as_null!(try_into_string);
2728        check_as_null!(try_into_boolean);
2729        check_as_null!(try_into_list);
2730        check_as_null!(try_into_struct);
2731
2732        macro_rules! check_as_correct {
2733            ($data: expr, $Variant: ident, $method: ident) => {
2734                assert_eq!(Some($data), ValueRef::$Variant($data).$method().unwrap());
2735            };
2736        }
2737
2738        check_as_correct!("hello", String, try_into_string);
2739        check_as_correct!("hello".as_bytes(), Binary, try_into_binary);
2740        check_as_correct!(true, Boolean, try_into_boolean);
2741        check_as_correct!(Date::new(123), Date, try_into_date);
2742        check_as_correct!(Time::new_second(12), Time, try_into_time);
2743        check_as_correct!(Duration::new_second(12), Duration, try_into_duration);
2744
2745        let list = build_list_value();
2746        check_as_correct!(ListValueRef::Ref { val: &list }, List, try_into_list);
2747
2748        let struct_value = build_struct_value();
2749        check_as_correct!(StructValueRef::Ref(&struct_value), Struct, try_into_struct);
2750
2751        let wrong_value = ValueRef::Int32(12345);
2752        assert!(wrong_value.try_into_binary().is_err());
2753        assert!(wrong_value.try_into_string().is_err());
2754        assert!(wrong_value.try_into_boolean().is_err());
2755        assert!(wrong_value.try_into_list().is_err());
2756        assert!(wrong_value.try_into_struct().is_err());
2757        assert!(wrong_value.try_into_date().is_err());
2758        assert!(wrong_value.try_into_time().is_err());
2759        assert!(wrong_value.try_into_timestamp().is_err());
2760        assert!(wrong_value.try_into_duration().is_err());
2761    }
2762
2763    #[test]
2764    fn test_display() {
2765        set_default_timezone(Some("Asia/Shanghai")).unwrap();
2766        assert_eq!(Value::Null.to_string(), "Null");
2767        assert_eq!(Value::UInt8(8).to_string(), "8");
2768        assert_eq!(Value::UInt16(16).to_string(), "16");
2769        assert_eq!(Value::UInt32(32).to_string(), "32");
2770        assert_eq!(Value::UInt64(64).to_string(), "64");
2771        assert_eq!(Value::Int8(-8).to_string(), "-8");
2772        assert_eq!(Value::Int16(-16).to_string(), "-16");
2773        assert_eq!(Value::Int32(-32).to_string(), "-32");
2774        assert_eq!(Value::Int64(-64).to_string(), "-64");
2775        assert_eq!(Value::Float32((-32.123).into()).to_string(), "-32.123");
2776        assert_eq!(Value::Float64((-64.123).into()).to_string(), "-64.123");
2777        assert_eq!(Value::Float64(OrderedF64::infinity()).to_string(), "inf");
2778        assert_eq!(Value::Float64(OrderedF64::nan()).to_string(), "NaN");
2779        assert_eq!(Value::String(StringBytes::from("123")).to_string(), "123");
2780        assert_eq!(
2781            Value::Binary(Bytes::from(vec![1, 2, 3])).to_string(),
2782            "010203"
2783        );
2784        assert_eq!(Value::Date(Date::new(0)).to_string(), "1970-01-01");
2785        assert_eq!(
2786            Value::Timestamp(Timestamp::new(1000, TimeUnit::Millisecond)).to_string(),
2787            "1970-01-01 08:00:01+0800"
2788        );
2789        assert_eq!(
2790            Value::Time(Time::new(1000, TimeUnit::Millisecond)).to_string(),
2791            "08:00:01+0800"
2792        );
2793        assert_eq!(
2794            Value::Duration(Duration::new_millisecond(1000)).to_string(),
2795            "1000ms"
2796        );
2797        assert_eq!(
2798            Value::List(build_list_value()).to_string(),
2799            "Boolean[true, false]"
2800        );
2801        assert_eq!(
2802            Value::List(ListValue::new(
2803                vec![],
2804                Arc::new(ConcreteDataType::timestamp_second_datatype()),
2805            ))
2806            .to_string(),
2807            "TimestampSecond[]"
2808        );
2809        assert_eq!(
2810            Value::List(ListValue::new(
2811                vec![],
2812                Arc::new(ConcreteDataType::timestamp_millisecond_datatype()),
2813            ))
2814            .to_string(),
2815            "TimestampMillisecond[]"
2816        );
2817        assert_eq!(
2818            Value::List(ListValue::new(
2819                vec![],
2820                Arc::new(ConcreteDataType::timestamp_microsecond_datatype()),
2821            ))
2822            .to_string(),
2823            "TimestampMicrosecond[]"
2824        );
2825        assert_eq!(
2826            Value::List(ListValue::new(
2827                vec![],
2828                Arc::new(ConcreteDataType::timestamp_nanosecond_datatype()),
2829            ))
2830            .to_string(),
2831            "TimestampNanosecond[]"
2832        );
2833
2834        assert_eq!(
2835            Value::Struct(build_struct_value()).to_string(),
2836            "{ id: 1, name: tom, age: 25, address: 94038, awards: Boolean[true, false] }"
2837        );
2838
2839        assert_eq!(
2840            Value::Json(Box::new(
2841                [
2842                    ("id", JsonVariant::from(1i64)),
2843                    ("name", "tom".into()),
2844                    ("age", 25u64.into()),
2845                    ("address", "94038".into()),
2846                    ("awards", [true, false].into()),
2847                ]
2848                .into()
2849            ))
2850            .to_string(),
2851            "Json({ address: 94038, age: 25, awards: [true, false], id: 1, name: tom })"
2852        )
2853    }
2854
2855    #[test]
2856    fn test_not_null_value_to_scalar_value() {
2857        assert_eq!(
2858            ScalarValue::Boolean(Some(true)),
2859            Value::Boolean(true)
2860                .try_to_scalar_value(&ConcreteDataType::boolean_datatype())
2861                .unwrap()
2862        );
2863        assert_eq!(
2864            ScalarValue::Boolean(Some(false)),
2865            Value::Boolean(false)
2866                .try_to_scalar_value(&ConcreteDataType::boolean_datatype())
2867                .unwrap()
2868        );
2869        assert_eq!(
2870            ScalarValue::UInt8(Some(1)),
2871            Value::UInt8(1)
2872                .try_to_scalar_value(&ConcreteDataType::uint8_datatype())
2873                .unwrap()
2874        );
2875        assert_eq!(
2876            ScalarValue::UInt16(Some(2)),
2877            Value::UInt16(2)
2878                .try_to_scalar_value(&ConcreteDataType::uint16_datatype())
2879                .unwrap()
2880        );
2881        assert_eq!(
2882            ScalarValue::UInt32(Some(3)),
2883            Value::UInt32(3)
2884                .try_to_scalar_value(&ConcreteDataType::uint32_datatype())
2885                .unwrap()
2886        );
2887        assert_eq!(
2888            ScalarValue::UInt64(Some(4)),
2889            Value::UInt64(4)
2890                .try_to_scalar_value(&ConcreteDataType::uint64_datatype())
2891                .unwrap()
2892        );
2893        assert_eq!(
2894            ScalarValue::Int8(Some(i8::MIN + 4)),
2895            Value::Int8(i8::MIN + 4)
2896                .try_to_scalar_value(&ConcreteDataType::int8_datatype())
2897                .unwrap()
2898        );
2899        assert_eq!(
2900            ScalarValue::Int16(Some(i16::MIN + 5)),
2901            Value::Int16(i16::MIN + 5)
2902                .try_to_scalar_value(&ConcreteDataType::int16_datatype())
2903                .unwrap()
2904        );
2905        assert_eq!(
2906            ScalarValue::Int32(Some(i32::MIN + 6)),
2907            Value::Int32(i32::MIN + 6)
2908                .try_to_scalar_value(&ConcreteDataType::int32_datatype())
2909                .unwrap()
2910        );
2911        assert_eq!(
2912            ScalarValue::Int64(Some(i64::MIN + 7)),
2913            Value::Int64(i64::MIN + 7)
2914                .try_to_scalar_value(&ConcreteDataType::int64_datatype())
2915                .unwrap()
2916        );
2917        assert_eq!(
2918            ScalarValue::Float32(Some(8.0f32)),
2919            Value::Float32(OrderedFloat(8.0f32))
2920                .try_to_scalar_value(&ConcreteDataType::float32_datatype())
2921                .unwrap()
2922        );
2923        assert_eq!(
2924            ScalarValue::Float64(Some(9.0f64)),
2925            Value::Float64(OrderedFloat(9.0f64))
2926                .try_to_scalar_value(&ConcreteDataType::float64_datatype())
2927                .unwrap()
2928        );
2929        assert_eq!(
2930            ScalarValue::Utf8(Some("hello".to_string())),
2931            Value::String(StringBytes::from("hello"))
2932                .try_to_scalar_value(&ConcreteDataType::string_datatype(),)
2933                .unwrap()
2934        );
2935        assert_eq!(
2936            ScalarValue::Binary(Some("world".as_bytes().to_vec())),
2937            Value::Binary(Bytes::from("world".as_bytes()))
2938                .try_to_scalar_value(&ConcreteDataType::binary_datatype())
2939                .unwrap()
2940        );
2941
2942        let jsonb_value = jsonb::parse_value(r#"{"key": "value"}"#.as_bytes())
2943            .unwrap()
2944            .to_vec();
2945        assert_eq!(
2946            ScalarValue::Binary(Some(jsonb_value.clone())),
2947            Value::Binary(jsonb_value.into())
2948                .try_to_scalar_value(&ConcreteDataType::json_datatype())
2949                .unwrap()
2950        );
2951
2952        assert_eq!(
2953            build_scalar_struct_value(),
2954            Value::Struct(build_struct_value())
2955                .try_to_scalar_value(&ConcreteDataType::struct_datatype(build_struct_type()))
2956                .unwrap()
2957        );
2958
2959        assert_eq!(
2960            build_scalar_list_value(),
2961            Value::List(build_list_value())
2962                .try_to_scalar_value(&ConcreteDataType::list_datatype(Arc::new(
2963                    ConcreteDataType::boolean_datatype()
2964                )))
2965                .unwrap()
2966        );
2967    }
2968
2969    #[test]
2970    fn test_null_value_to_scalar_value() {
2971        assert_eq!(
2972            ScalarValue::Boolean(None),
2973            Value::Null
2974                .try_to_scalar_value(&ConcreteDataType::boolean_datatype())
2975                .unwrap()
2976        );
2977        assert_eq!(
2978            ScalarValue::UInt8(None),
2979            Value::Null
2980                .try_to_scalar_value(&ConcreteDataType::uint8_datatype())
2981                .unwrap()
2982        );
2983        assert_eq!(
2984            ScalarValue::UInt16(None),
2985            Value::Null
2986                .try_to_scalar_value(&ConcreteDataType::uint16_datatype())
2987                .unwrap()
2988        );
2989        assert_eq!(
2990            ScalarValue::UInt32(None),
2991            Value::Null
2992                .try_to_scalar_value(&ConcreteDataType::uint32_datatype())
2993                .unwrap()
2994        );
2995        assert_eq!(
2996            ScalarValue::UInt64(None),
2997            Value::Null
2998                .try_to_scalar_value(&ConcreteDataType::uint64_datatype())
2999                .unwrap()
3000        );
3001        assert_eq!(
3002            ScalarValue::Int8(None),
3003            Value::Null
3004                .try_to_scalar_value(&ConcreteDataType::int8_datatype())
3005                .unwrap()
3006        );
3007        assert_eq!(
3008            ScalarValue::Int16(None),
3009            Value::Null
3010                .try_to_scalar_value(&ConcreteDataType::int16_datatype())
3011                .unwrap()
3012        );
3013        assert_eq!(
3014            ScalarValue::Int32(None),
3015            Value::Null
3016                .try_to_scalar_value(&ConcreteDataType::int32_datatype())
3017                .unwrap()
3018        );
3019        assert_eq!(
3020            ScalarValue::Int64(None),
3021            Value::Null
3022                .try_to_scalar_value(&ConcreteDataType::int64_datatype())
3023                .unwrap()
3024        );
3025        assert_eq!(
3026            ScalarValue::Float32(None),
3027            Value::Null
3028                .try_to_scalar_value(&ConcreteDataType::float32_datatype())
3029                .unwrap()
3030        );
3031        assert_eq!(
3032            ScalarValue::Float64(None),
3033            Value::Null
3034                .try_to_scalar_value(&ConcreteDataType::float64_datatype())
3035                .unwrap()
3036        );
3037        assert_eq!(
3038            ScalarValue::Utf8(None),
3039            Value::Null
3040                .try_to_scalar_value(&ConcreteDataType::string_datatype())
3041                .unwrap()
3042        );
3043        assert_eq!(
3044            ScalarValue::Binary(None),
3045            Value::Null
3046                .try_to_scalar_value(&ConcreteDataType::binary_datatype())
3047                .unwrap()
3048        );
3049
3050        assert_eq!(
3051            ScalarValue::Time32Second(None),
3052            Value::Null
3053                .try_to_scalar_value(&ConcreteDataType::time_second_datatype())
3054                .unwrap()
3055        );
3056        assert_eq!(
3057            ScalarValue::Time32Millisecond(None),
3058            Value::Null
3059                .try_to_scalar_value(&ConcreteDataType::time_millisecond_datatype())
3060                .unwrap()
3061        );
3062        assert_eq!(
3063            ScalarValue::Time64Microsecond(None),
3064            Value::Null
3065                .try_to_scalar_value(&ConcreteDataType::time_microsecond_datatype())
3066                .unwrap()
3067        );
3068        assert_eq!(
3069            ScalarValue::Time64Nanosecond(None),
3070            Value::Null
3071                .try_to_scalar_value(&ConcreteDataType::time_nanosecond_datatype())
3072                .unwrap()
3073        );
3074
3075        assert_eq!(
3076            ScalarValue::DurationSecond(None),
3077            Value::Null
3078                .try_to_scalar_value(&ConcreteDataType::duration_second_datatype())
3079                .unwrap()
3080        );
3081        assert_eq!(
3082            ScalarValue::DurationMillisecond(None),
3083            Value::Null
3084                .try_to_scalar_value(&ConcreteDataType::duration_millisecond_datatype())
3085                .unwrap()
3086        );
3087        assert_eq!(
3088            ScalarValue::DurationMicrosecond(None),
3089            Value::Null
3090                .try_to_scalar_value(&ConcreteDataType::duration_microsecond_datatype())
3091                .unwrap()
3092        );
3093        assert_eq!(
3094            ScalarValue::DurationNanosecond(None),
3095            Value::Null
3096                .try_to_scalar_value(&ConcreteDataType::duration_nanosecond_datatype())
3097                .unwrap()
3098        );
3099        assert_eq!(
3100            ScalarValue::Binary(None),
3101            Value::Null
3102                .try_to_scalar_value(&ConcreteDataType::json_datatype())
3103                .unwrap()
3104        );
3105
3106        assert_eq!(
3107            ScalarValue::new_null_list(ArrowDataType::Boolean, true, 1),
3108            Value::Null
3109                .try_to_scalar_value(&ConcreteDataType::list_datatype(Arc::new(
3110                    ConcreteDataType::boolean_datatype()
3111                )))
3112                .unwrap()
3113        );
3114
3115        assert_eq!(
3116            ScalarStructBuilder::new_null(build_struct_type().as_arrow_fields()),
3117            Value::Null
3118                .try_to_scalar_value(&ConcreteDataType::struct_datatype(build_struct_type()))
3119                .unwrap()
3120        );
3121    }
3122
3123    #[test]
3124    fn test_list_value_to_scalar_value() {
3125        let items = vec![Value::Int32(-1), Value::Null];
3126        let item_type = Arc::new(ConcreteDataType::int32_datatype());
3127        let list = Value::List(ListValue::new(items, item_type.clone()));
3128        let df_list = list
3129            .try_to_scalar_value(&ConcreteDataType::list_datatype(item_type.clone()))
3130            .unwrap();
3131        assert!(matches!(df_list, ScalarValue::List(_)));
3132        match df_list {
3133            ScalarValue::List(vs) => {
3134                assert_eq!(
3135                    ArrowDataType::List(Arc::new(Field::new_list_field(
3136                        ArrowDataType::Int32,
3137                        true
3138                    ))),
3139                    *vs.data_type()
3140                );
3141
3142                let vs = ScalarValue::convert_array_to_scalar_vec(vs.as_ref())
3143                    .unwrap()
3144                    .into_iter()
3145                    .flatten()
3146                    .flatten()
3147                    .collect::<Vec<_>>();
3148                assert_eq!(
3149                    vs,
3150                    vec![ScalarValue::Int32(Some(-1)), ScalarValue::Int32(None)]
3151                );
3152            }
3153            _ => unreachable!(),
3154        }
3155    }
3156
3157    #[test]
3158    fn test_struct_value_to_scalar_value() {
3159        let struct_value = build_struct_value();
3160        let scalar_value = struct_value
3161            .try_to_scalar_value(&build_struct_type())
3162            .unwrap();
3163
3164        assert_eq!(scalar_value, build_scalar_struct_value());
3165
3166        assert!(matches!(scalar_value, ScalarValue::Struct(_)));
3167        match scalar_value {
3168            ScalarValue::Struct(values) => {
3169                assert_eq!(&build_struct_type().as_arrow_fields(), values.fields());
3170
3171                assert_eq!(
3172                    ScalarValue::try_from_array(values.column(0), 0).unwrap(),
3173                    ScalarValue::Int32(Some(1))
3174                );
3175                assert_eq!(
3176                    ScalarValue::try_from_array(values.column(1), 0).unwrap(),
3177                    ScalarValue::Utf8(Some("tom".into()))
3178                );
3179                assert_eq!(
3180                    ScalarValue::try_from_array(values.column(2), 0).unwrap(),
3181                    ScalarValue::UInt8(Some(25))
3182                );
3183                assert_eq!(
3184                    ScalarValue::try_from_array(values.column(3), 0).unwrap(),
3185                    ScalarValue::Utf8(Some("94038".into()))
3186                );
3187            }
3188            _ => panic!("Unexpected value type"),
3189        }
3190    }
3191
3192    #[test]
3193    fn test_struct_scalar_null_fields() {
3194        let struct_type = StructType::from([
3195            StructField::new("x", ConcreteDataType::int32_datatype(), true),
3196            StructField::new("name", ConcreteDataType::string_datatype(), true),
3197        ]);
3198        let value = StructValue::new(vec![Value::Null, Value::from("hello")], struct_type.clone());
3199        let ScalarValue::Struct(array) = value.try_to_scalar_value(&struct_type).unwrap() else {
3200            panic!("Expected struct scalar");
3201        };
3202        assert_eq!(1, array.len());
3203        assert_eq!(0, array.null_count());
3204        assert_eq!(&struct_type.as_arrow_fields(), array.fields());
3205        assert_eq!(
3206            ScalarValue::Int32(None),
3207            ScalarValue::try_from_array(array.column(0), 0).unwrap()
3208        );
3209
3210        let nested_type = StructType::from([StructField::new(
3211            "nested",
3212            ConcreteDataType::struct_datatype(struct_type),
3213            true,
3214        )]);
3215        for child in [Value::Struct(value), Value::Null] {
3216            let nested = StructValue::new(vec![child.clone()], nested_type.clone());
3217            let ScalarValue::Struct(array) = nested.try_to_scalar_value(&nested_type).unwrap()
3218            else {
3219                panic!("Expected struct scalar");
3220            };
3221            let vector = crate::vectors::Helper::try_into_vector(array.column(0).clone()).unwrap();
3222            assert_eq!(child, vector.get(0));
3223        }
3224
3225        let empty_type = StructType::default();
3226        let empty = StructValue::new(vec![], empty_type.clone());
3227        let ScalarValue::Struct(array) = empty.try_to_scalar_value(&empty_type).unwrap() else {
3228            panic!("Expected struct scalar");
3229        };
3230        assert_eq!(1, array.len());
3231        assert_eq!(0, array.num_columns());
3232        assert_eq!(0, array.null_count());
3233    }
3234
3235    #[test]
3236    fn test_struct_scalar_invalid_fields() {
3237        let struct_type = StructType::from([StructField::new(
3238            "x",
3239            ConcreteDataType::int32_datatype(),
3240            false,
3241        )]);
3242        for child in [Value::Null, Value::from("wrong type")] {
3243            let value = StructValue::new(vec![child], struct_type.clone());
3244            assert!(value.try_to_scalar_value(&struct_type).is_err());
3245        }
3246        let value = StructValue::new(vec![Value::Int32(1)], struct_type.clone());
3247        assert!(value.try_to_scalar_value(&StructType::default()).is_err());
3248        let empty = StructValue::new(vec![], StructType::default());
3249        assert!(empty.try_to_scalar_value(&struct_type).is_err());
3250    }
3251
3252    #[test]
3253    fn test_timestamp_to_scalar_value() {
3254        assert_eq!(
3255            ScalarValue::TimestampSecond(Some(1), None),
3256            timestamp_to_scalar_value(TimeUnit::Second, Some(1))
3257        );
3258        assert_eq!(
3259            ScalarValue::TimestampMillisecond(Some(1), None),
3260            timestamp_to_scalar_value(TimeUnit::Millisecond, Some(1))
3261        );
3262        assert_eq!(
3263            ScalarValue::TimestampMicrosecond(Some(1), None),
3264            timestamp_to_scalar_value(TimeUnit::Microsecond, Some(1))
3265        );
3266        assert_eq!(
3267            ScalarValue::TimestampNanosecond(Some(1), None),
3268            timestamp_to_scalar_value(TimeUnit::Nanosecond, Some(1))
3269        );
3270    }
3271
3272    #[test]
3273    fn test_time_to_scalar_value() {
3274        assert_eq!(
3275            ScalarValue::Time32Second(Some(1)),
3276            time_to_scalar_value(TimeUnit::Second, Some(1)).unwrap()
3277        );
3278        assert_eq!(
3279            ScalarValue::Time32Millisecond(Some(1)),
3280            time_to_scalar_value(TimeUnit::Millisecond, Some(1)).unwrap()
3281        );
3282        assert_eq!(
3283            ScalarValue::Time64Microsecond(Some(1)),
3284            time_to_scalar_value(TimeUnit::Microsecond, Some(1)).unwrap()
3285        );
3286        assert_eq!(
3287            ScalarValue::Time64Nanosecond(Some(1)),
3288            time_to_scalar_value(TimeUnit::Nanosecond, Some(1)).unwrap()
3289        );
3290    }
3291
3292    #[test]
3293    fn test_duration_to_scalar_value() {
3294        assert_eq!(
3295            ScalarValue::DurationSecond(Some(1)),
3296            duration_to_scalar_value(TimeUnit::Second, Some(1))
3297        );
3298        assert_eq!(
3299            ScalarValue::DurationMillisecond(Some(1)),
3300            duration_to_scalar_value(TimeUnit::Millisecond, Some(1))
3301        );
3302        assert_eq!(
3303            ScalarValue::DurationMicrosecond(Some(1)),
3304            duration_to_scalar_value(TimeUnit::Microsecond, Some(1))
3305        );
3306        assert_eq!(
3307            ScalarValue::DurationNanosecond(Some(1)),
3308            duration_to_scalar_value(TimeUnit::Nanosecond, Some(1))
3309        );
3310    }
3311
3312    fn check_value_ref_size_eq(value_ref: &ValueRef, size: usize) {
3313        assert_eq!(value_ref.data_size(), size);
3314    }
3315
3316    #[test]
3317    fn test_value_ref_estimated_size() {
3318        check_value_ref_size_eq(&ValueRef::Null, 8);
3319        check_value_ref_size_eq(&ValueRef::Boolean(true), 1);
3320        check_value_ref_size_eq(&ValueRef::UInt8(1), 1);
3321        check_value_ref_size_eq(&ValueRef::UInt16(1), 2);
3322        check_value_ref_size_eq(&ValueRef::UInt32(1), 4);
3323        check_value_ref_size_eq(&ValueRef::UInt64(1), 8);
3324        check_value_ref_size_eq(&ValueRef::Int8(1), 1);
3325        check_value_ref_size_eq(&ValueRef::Int16(1), 2);
3326        check_value_ref_size_eq(&ValueRef::Int32(1), 4);
3327        check_value_ref_size_eq(&ValueRef::Int64(1), 8);
3328        check_value_ref_size_eq(&ValueRef::Float32(1.0.into()), 4);
3329        check_value_ref_size_eq(&ValueRef::Float64(1.0.into()), 8);
3330        check_value_ref_size_eq(&ValueRef::String("greptimedb"), 10);
3331        check_value_ref_size_eq(&ValueRef::Binary(b"greptimedb"), 10);
3332        check_value_ref_size_eq(&ValueRef::Date(Date::new(1)), 4);
3333        check_value_ref_size_eq(&ValueRef::Timestamp(Timestamp::new_millisecond(1)), 16);
3334        check_value_ref_size_eq(&ValueRef::Time(Time::new_millisecond(1)), 16);
3335        check_value_ref_size_eq(&ValueRef::IntervalYearMonth(IntervalYearMonth::new(1)), 4);
3336        check_value_ref_size_eq(&ValueRef::IntervalDayTime(IntervalDayTime::new(1, 2)), 8);
3337        check_value_ref_size_eq(
3338            &ValueRef::IntervalMonthDayNano(IntervalMonthDayNano::new(1, 2, 3)),
3339            16,
3340        );
3341        check_value_ref_size_eq(&ValueRef::Duration(Duration::new_millisecond(1)), 16);
3342        check_value_ref_size_eq(
3343            &ValueRef::List(ListValueRef::Ref {
3344                val: &ListValue {
3345                    items: vec![
3346                        Value::String("hello world".into()),
3347                        Value::String("greptimedb".into()),
3348                    ],
3349                    datatype: Arc::new(ConcreteDataType::string_datatype()),
3350                },
3351            }),
3352            30,
3353        );
3354
3355        let data = vec![
3356            Some(vec![Some(1), Some(2), Some(3)]),
3357            None,
3358            Some(vec![Some(4), None, Some(6)]),
3359        ];
3360        let item_type = Arc::new(ConcreteDataType::int32_datatype());
3361        let mut builder = ListVectorBuilder::with_type_capacity(item_type.clone(), 8);
3362        for vec_opt in &data {
3363            if let Some(vec) = vec_opt {
3364                let values = vec.iter().map(|v| Value::from(*v)).collect();
3365                let list_value = ListValue::new(values, item_type.clone());
3366
3367                builder.push(Some(ListValueRef::Ref { val: &list_value }));
3368            } else {
3369                builder.push(None);
3370            }
3371        }
3372        let vector = builder.finish();
3373
3374        check_value_ref_size_eq(
3375            &ValueRef::List(ListValueRef::Indexed {
3376                vector: &vector,
3377                idx: 0,
3378            }),
3379            74,
3380        );
3381        check_value_ref_size_eq(
3382            &ValueRef::List(ListValueRef::Indexed {
3383                vector: &vector,
3384                idx: 1,
3385            }),
3386            74,
3387        );
3388        check_value_ref_size_eq(
3389            &ValueRef::List(ListValueRef::Indexed {
3390                vector: &vector,
3391                idx: 2,
3392            }),
3393            74,
3394        );
3395        check_value_ref_size_eq(&ValueRef::Decimal128(Decimal128::new(1234, 3, 1)), 32);
3396
3397        check_value_ref_size_eq(
3398            &ValueRef::Struct(StructValueRef::Ref(&build_struct_value())),
3399            31,
3400        );
3401
3402        check_value_ref_size_eq(
3403            &ValueRef::Json(Box::new(
3404                [
3405                    ("id", JsonVariantRef::from(1i64)),
3406                    ("name", "tom".into()),
3407                    ("age", 25u64.into()),
3408                    ("address", "94038".into()),
3409                    ("awards", [true, false].into()),
3410                ]
3411                .into(),
3412            )),
3413            48,
3414        );
3415    }
3416
3417    #[test]
3418    fn test_incorrect_default_value_issue_3479() {
3419        let value = OrderedF64::from(0.047318541668048164);
3420        let serialized = serde_json::to_string(&value).unwrap();
3421        let deserialized: OrderedF64 = serde_json::from_str(&serialized).unwrap();
3422        assert_eq!(value, deserialized);
3423    }
3424}