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