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