api/
helper.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::sync::Arc;
16
17use common_base::BitVec;
18use common_decimal::decimal128::{DECIMAL128_DEFAULT_SCALE, DECIMAL128_MAX_PRECISION};
19use common_decimal::Decimal128;
20use common_time::time::Time;
21use common_time::timestamp::TimeUnit;
22use common_time::{Date, IntervalDayTime, IntervalMonthDayNano, IntervalYearMonth, Timestamp};
23use datatypes::prelude::{ConcreteDataType, ValueRef};
24use datatypes::scalars::ScalarVector;
25use datatypes::types::{
26    Int16Type, Int8Type, IntervalType, TimeType, TimestampType, UInt16Type, UInt8Type,
27};
28use datatypes::value::{OrderedF32, OrderedF64, Value};
29use datatypes::vectors::{
30    BinaryVector, BooleanVector, DateVector, Decimal128Vector, Float32Vector, Float64Vector,
31    Int32Vector, Int64Vector, IntervalDayTimeVector, IntervalMonthDayNanoVector,
32    IntervalYearMonthVector, PrimitiveVector, StringVector, TimeMicrosecondVector,
33    TimeMillisecondVector, TimeNanosecondVector, TimeSecondVector, TimestampMicrosecondVector,
34    TimestampMillisecondVector, TimestampNanosecondVector, TimestampSecondVector, UInt32Vector,
35    UInt64Vector, VectorRef,
36};
37use greptime_proto::v1::column_data_type_extension::TypeExt;
38use greptime_proto::v1::ddl_request::Expr;
39use greptime_proto::v1::greptime_request::Request;
40use greptime_proto::v1::query_request::Query;
41use greptime_proto::v1::value::ValueData;
42use greptime_proto::v1::{
43    self, ColumnDataTypeExtension, DdlRequest, DecimalTypeExtension, JsonTypeExtension,
44    QueryRequest, Row, SemanticType, VectorTypeExtension,
45};
46use paste::paste;
47use snafu::prelude::*;
48
49use crate::error::{self, Result};
50use crate::v1::column::Values;
51use crate::v1::{Column, ColumnDataType, Value as GrpcValue};
52
53/// ColumnDataTypeWrapper is a wrapper of ColumnDataType and ColumnDataTypeExtension.
54/// It could be used to convert with ConcreteDataType.
55#[derive(Debug, PartialEq)]
56pub struct ColumnDataTypeWrapper {
57    datatype: ColumnDataType,
58    datatype_ext: Option<ColumnDataTypeExtension>,
59}
60
61impl ColumnDataTypeWrapper {
62    /// Try to create a ColumnDataTypeWrapper from i32(ColumnDataType) and ColumnDataTypeExtension.
63    pub fn try_new(datatype: i32, datatype_ext: Option<ColumnDataTypeExtension>) -> Result<Self> {
64        let datatype = ColumnDataType::try_from(datatype)
65            .context(error::UnknownColumnDataTypeSnafu { datatype })?;
66        Ok(Self {
67            datatype,
68            datatype_ext,
69        })
70    }
71
72    /// Create a ColumnDataTypeWrapper from ColumnDataType and ColumnDataTypeExtension.
73    pub fn new(datatype: ColumnDataType, datatype_ext: Option<ColumnDataTypeExtension>) -> Self {
74        Self {
75            datatype,
76            datatype_ext,
77        }
78    }
79
80    /// Get the ColumnDataType.
81    pub fn datatype(&self) -> ColumnDataType {
82        self.datatype
83    }
84
85    /// Get a tuple of ColumnDataType and ColumnDataTypeExtension.
86    pub fn to_parts(&self) -> (ColumnDataType, Option<ColumnDataTypeExtension>) {
87        (self.datatype, self.datatype_ext)
88    }
89}
90
91impl From<ColumnDataTypeWrapper> for ConcreteDataType {
92    fn from(datatype_wrapper: ColumnDataTypeWrapper) -> Self {
93        match datatype_wrapper.datatype {
94            ColumnDataType::Boolean => ConcreteDataType::boolean_datatype(),
95            ColumnDataType::Int8 => ConcreteDataType::int8_datatype(),
96            ColumnDataType::Int16 => ConcreteDataType::int16_datatype(),
97            ColumnDataType::Int32 => ConcreteDataType::int32_datatype(),
98            ColumnDataType::Int64 => ConcreteDataType::int64_datatype(),
99            ColumnDataType::Uint8 => ConcreteDataType::uint8_datatype(),
100            ColumnDataType::Uint16 => ConcreteDataType::uint16_datatype(),
101            ColumnDataType::Uint32 => ConcreteDataType::uint32_datatype(),
102            ColumnDataType::Uint64 => ConcreteDataType::uint64_datatype(),
103            ColumnDataType::Float32 => ConcreteDataType::float32_datatype(),
104            ColumnDataType::Float64 => ConcreteDataType::float64_datatype(),
105            ColumnDataType::Binary => {
106                if let Some(TypeExt::JsonType(_)) = datatype_wrapper
107                    .datatype_ext
108                    .as_ref()
109                    .and_then(|datatype_ext| datatype_ext.type_ext.as_ref())
110                {
111                    ConcreteDataType::json_datatype()
112                } else {
113                    ConcreteDataType::binary_datatype()
114                }
115            }
116            ColumnDataType::Json => ConcreteDataType::json_datatype(),
117            ColumnDataType::String => ConcreteDataType::string_datatype(),
118            ColumnDataType::Date => ConcreteDataType::date_datatype(),
119            ColumnDataType::Datetime => ConcreteDataType::timestamp_microsecond_datatype(),
120            ColumnDataType::TimestampSecond => ConcreteDataType::timestamp_second_datatype(),
121            ColumnDataType::TimestampMillisecond => {
122                ConcreteDataType::timestamp_millisecond_datatype()
123            }
124            ColumnDataType::TimestampMicrosecond => {
125                ConcreteDataType::timestamp_microsecond_datatype()
126            }
127            ColumnDataType::TimestampNanosecond => {
128                ConcreteDataType::timestamp_nanosecond_datatype()
129            }
130            ColumnDataType::TimeSecond => ConcreteDataType::time_second_datatype(),
131            ColumnDataType::TimeMillisecond => ConcreteDataType::time_millisecond_datatype(),
132            ColumnDataType::TimeMicrosecond => ConcreteDataType::time_microsecond_datatype(),
133            ColumnDataType::TimeNanosecond => ConcreteDataType::time_nanosecond_datatype(),
134            ColumnDataType::IntervalYearMonth => ConcreteDataType::interval_year_month_datatype(),
135            ColumnDataType::IntervalDayTime => ConcreteDataType::interval_day_time_datatype(),
136            ColumnDataType::IntervalMonthDayNano => {
137                ConcreteDataType::interval_month_day_nano_datatype()
138            }
139            ColumnDataType::Decimal128 => {
140                if let Some(TypeExt::DecimalType(d)) = datatype_wrapper
141                    .datatype_ext
142                    .as_ref()
143                    .and_then(|datatype_ext| datatype_ext.type_ext.as_ref())
144                {
145                    ConcreteDataType::decimal128_datatype(d.precision as u8, d.scale as i8)
146                } else {
147                    ConcreteDataType::decimal128_default_datatype()
148                }
149            }
150            ColumnDataType::Vector => {
151                if let Some(TypeExt::VectorType(d)) = datatype_wrapper
152                    .datatype_ext
153                    .as_ref()
154                    .and_then(|datatype_ext| datatype_ext.type_ext.as_ref())
155                {
156                    ConcreteDataType::vector_datatype(d.dim)
157                } else {
158                    ConcreteDataType::vector_default_datatype()
159                }
160            }
161        }
162    }
163}
164
165/// This macro is used to generate datatype functions
166/// with lower style for ColumnDataTypeWrapper.
167///
168///
169/// For example: we can use `ColumnDataTypeWrapper::int8_datatype()`,
170/// to get a ColumnDataTypeWrapper with datatype `ColumnDataType::Int8`.
171macro_rules! impl_column_type_functions {
172    ($($Type: ident), +) => {
173        paste! {
174            impl ColumnDataTypeWrapper {
175                $(
176                    pub fn [<$Type:lower _datatype>]() -> ColumnDataTypeWrapper {
177                        ColumnDataTypeWrapper {
178                            datatype: ColumnDataType::$Type,
179                            datatype_ext: None,
180                        }
181                    }
182                )+
183            }
184        }
185    }
186}
187
188/// This macro is used to generate datatype functions
189/// with snake style for ColumnDataTypeWrapper.
190///
191///
192/// For example: we can use `ColumnDataTypeWrapper::duration_second_datatype()`,
193/// to get a ColumnDataTypeWrapper with datatype `ColumnDataType::DurationSecond`.
194macro_rules! impl_column_type_functions_with_snake {
195    ($($TypeName: ident), +) => {
196        paste!{
197            impl ColumnDataTypeWrapper {
198                $(
199                    pub fn [<$TypeName:snake _datatype>]() -> ColumnDataTypeWrapper {
200                        ColumnDataTypeWrapper {
201                            datatype: ColumnDataType::$TypeName,
202                            datatype_ext: None,
203                        }
204                    }
205                )+
206            }
207        }
208    };
209}
210
211impl_column_type_functions!(
212    Boolean, Uint8, Uint16, Uint32, Uint64, Int8, Int16, Int32, Int64, Float32, Float64, Binary,
213    Date, Datetime, String
214);
215
216impl_column_type_functions_with_snake!(
217    TimestampSecond,
218    TimestampMillisecond,
219    TimestampMicrosecond,
220    TimestampNanosecond,
221    TimeSecond,
222    TimeMillisecond,
223    TimeMicrosecond,
224    TimeNanosecond,
225    IntervalYearMonth,
226    IntervalDayTime,
227    IntervalMonthDayNano
228);
229
230impl ColumnDataTypeWrapper {
231    pub fn decimal128_datatype(precision: i32, scale: i32) -> Self {
232        ColumnDataTypeWrapper {
233            datatype: ColumnDataType::Decimal128,
234            datatype_ext: Some(ColumnDataTypeExtension {
235                type_ext: Some(TypeExt::DecimalType(DecimalTypeExtension {
236                    precision,
237                    scale,
238                })),
239            }),
240        }
241    }
242
243    pub fn vector_datatype(dim: u32) -> Self {
244        ColumnDataTypeWrapper {
245            datatype: ColumnDataType::Vector,
246            datatype_ext: Some(ColumnDataTypeExtension {
247                type_ext: Some(TypeExt::VectorType(VectorTypeExtension { dim })),
248            }),
249        }
250    }
251}
252
253impl TryFrom<ConcreteDataType> for ColumnDataTypeWrapper {
254    type Error = error::Error;
255
256    fn try_from(datatype: ConcreteDataType) -> Result<Self> {
257        let column_datatype = match datatype {
258            ConcreteDataType::Boolean(_) => ColumnDataType::Boolean,
259            ConcreteDataType::Int8(_) => ColumnDataType::Int8,
260            ConcreteDataType::Int16(_) => ColumnDataType::Int16,
261            ConcreteDataType::Int32(_) => ColumnDataType::Int32,
262            ConcreteDataType::Int64(_) => ColumnDataType::Int64,
263            ConcreteDataType::UInt8(_) => ColumnDataType::Uint8,
264            ConcreteDataType::UInt16(_) => ColumnDataType::Uint16,
265            ConcreteDataType::UInt32(_) => ColumnDataType::Uint32,
266            ConcreteDataType::UInt64(_) => ColumnDataType::Uint64,
267            ConcreteDataType::Float32(_) => ColumnDataType::Float32,
268            ConcreteDataType::Float64(_) => ColumnDataType::Float64,
269            ConcreteDataType::Binary(_) => ColumnDataType::Binary,
270            ConcreteDataType::String(_) => ColumnDataType::String,
271            ConcreteDataType::Date(_) => ColumnDataType::Date,
272            ConcreteDataType::Timestamp(t) => match t {
273                TimestampType::Second(_) => ColumnDataType::TimestampSecond,
274                TimestampType::Millisecond(_) => ColumnDataType::TimestampMillisecond,
275                TimestampType::Microsecond(_) => ColumnDataType::TimestampMicrosecond,
276                TimestampType::Nanosecond(_) => ColumnDataType::TimestampNanosecond,
277            },
278            ConcreteDataType::Time(t) => match t {
279                TimeType::Second(_) => ColumnDataType::TimeSecond,
280                TimeType::Millisecond(_) => ColumnDataType::TimeMillisecond,
281                TimeType::Microsecond(_) => ColumnDataType::TimeMicrosecond,
282                TimeType::Nanosecond(_) => ColumnDataType::TimeNanosecond,
283            },
284            ConcreteDataType::Interval(i) => match i {
285                IntervalType::YearMonth(_) => ColumnDataType::IntervalYearMonth,
286                IntervalType::DayTime(_) => ColumnDataType::IntervalDayTime,
287                IntervalType::MonthDayNano(_) => ColumnDataType::IntervalMonthDayNano,
288            },
289            ConcreteDataType::Decimal128(_) => ColumnDataType::Decimal128,
290            ConcreteDataType::Json(_) => ColumnDataType::Json,
291            ConcreteDataType::Vector(_) => ColumnDataType::Vector,
292            ConcreteDataType::Null(_)
293            | ConcreteDataType::List(_)
294            | ConcreteDataType::Dictionary(_)
295            | ConcreteDataType::Duration(_) => {
296                return error::IntoColumnDataTypeSnafu { from: datatype }.fail()
297            }
298        };
299        let datatype_extension = match column_datatype {
300            ColumnDataType::Decimal128 => {
301                datatype
302                    .as_decimal128()
303                    .map(|decimal_type| ColumnDataTypeExtension {
304                        type_ext: Some(TypeExt::DecimalType(DecimalTypeExtension {
305                            precision: decimal_type.precision() as i32,
306                            scale: decimal_type.scale() as i32,
307                        })),
308                    })
309            }
310            ColumnDataType::Json => datatype.as_json().map(|_| ColumnDataTypeExtension {
311                type_ext: Some(TypeExt::JsonType(JsonTypeExtension::JsonBinary.into())),
312            }),
313            ColumnDataType::Vector => {
314                datatype
315                    .as_vector()
316                    .map(|vector_type| ColumnDataTypeExtension {
317                        type_ext: Some(TypeExt::VectorType(VectorTypeExtension {
318                            dim: vector_type.dim as _,
319                        })),
320                    })
321            }
322            _ => None,
323        };
324        Ok(Self {
325            datatype: column_datatype,
326            datatype_ext: datatype_extension,
327        })
328    }
329}
330
331pub fn values_with_capacity(datatype: ColumnDataType, capacity: usize) -> Values {
332    match datatype {
333        ColumnDataType::Boolean => Values {
334            bool_values: Vec::with_capacity(capacity),
335            ..Default::default()
336        },
337        ColumnDataType::Int8 => Values {
338            i8_values: Vec::with_capacity(capacity),
339            ..Default::default()
340        },
341        ColumnDataType::Int16 => Values {
342            i16_values: Vec::with_capacity(capacity),
343            ..Default::default()
344        },
345        ColumnDataType::Int32 => Values {
346            i32_values: Vec::with_capacity(capacity),
347            ..Default::default()
348        },
349        ColumnDataType::Int64 => Values {
350            i64_values: Vec::with_capacity(capacity),
351            ..Default::default()
352        },
353        ColumnDataType::Uint8 => Values {
354            u8_values: Vec::with_capacity(capacity),
355            ..Default::default()
356        },
357        ColumnDataType::Uint16 => Values {
358            u16_values: Vec::with_capacity(capacity),
359            ..Default::default()
360        },
361        ColumnDataType::Uint32 => Values {
362            u32_values: Vec::with_capacity(capacity),
363            ..Default::default()
364        },
365        ColumnDataType::Uint64 => Values {
366            u64_values: Vec::with_capacity(capacity),
367            ..Default::default()
368        },
369        ColumnDataType::Float32 => Values {
370            f32_values: Vec::with_capacity(capacity),
371            ..Default::default()
372        },
373        ColumnDataType::Float64 => Values {
374            f64_values: Vec::with_capacity(capacity),
375            ..Default::default()
376        },
377        ColumnDataType::Binary => Values {
378            binary_values: Vec::with_capacity(capacity),
379            ..Default::default()
380        },
381        ColumnDataType::String => Values {
382            string_values: Vec::with_capacity(capacity),
383            ..Default::default()
384        },
385        ColumnDataType::Date => Values {
386            date_values: Vec::with_capacity(capacity),
387            ..Default::default()
388        },
389        ColumnDataType::Datetime => Values {
390            datetime_values: Vec::with_capacity(capacity),
391            ..Default::default()
392        },
393        ColumnDataType::TimestampSecond => Values {
394            timestamp_second_values: Vec::with_capacity(capacity),
395            ..Default::default()
396        },
397        ColumnDataType::TimestampMillisecond => Values {
398            timestamp_millisecond_values: Vec::with_capacity(capacity),
399            ..Default::default()
400        },
401        ColumnDataType::TimestampMicrosecond => Values {
402            timestamp_microsecond_values: Vec::with_capacity(capacity),
403            ..Default::default()
404        },
405        ColumnDataType::TimestampNanosecond => Values {
406            timestamp_nanosecond_values: Vec::with_capacity(capacity),
407            ..Default::default()
408        },
409        ColumnDataType::TimeSecond => Values {
410            time_second_values: Vec::with_capacity(capacity),
411            ..Default::default()
412        },
413        ColumnDataType::TimeMillisecond => Values {
414            time_millisecond_values: Vec::with_capacity(capacity),
415            ..Default::default()
416        },
417        ColumnDataType::TimeMicrosecond => Values {
418            time_microsecond_values: Vec::with_capacity(capacity),
419            ..Default::default()
420        },
421        ColumnDataType::TimeNanosecond => Values {
422            time_nanosecond_values: Vec::with_capacity(capacity),
423            ..Default::default()
424        },
425        ColumnDataType::IntervalDayTime => Values {
426            interval_day_time_values: Vec::with_capacity(capacity),
427            ..Default::default()
428        },
429        ColumnDataType::IntervalYearMonth => Values {
430            interval_year_month_values: Vec::with_capacity(capacity),
431            ..Default::default()
432        },
433        ColumnDataType::IntervalMonthDayNano => Values {
434            interval_month_day_nano_values: Vec::with_capacity(capacity),
435            ..Default::default()
436        },
437        ColumnDataType::Decimal128 => Values {
438            decimal128_values: Vec::with_capacity(capacity),
439            ..Default::default()
440        },
441        ColumnDataType::Json => Values {
442            string_values: Vec::with_capacity(capacity),
443            ..Default::default()
444        },
445        ColumnDataType::Vector => Values {
446            binary_values: Vec::with_capacity(capacity),
447            ..Default::default()
448        },
449    }
450}
451
452// The type of vals must be same.
453pub fn push_vals(column: &mut Column, origin_count: usize, vector: VectorRef) {
454    let values = column.values.get_or_insert_with(Values::default);
455    let mut null_mask = BitVec::from_slice(&column.null_mask);
456    let len = vector.len();
457    null_mask.reserve_exact(origin_count + len);
458    null_mask.extend(BitVec::repeat(false, len));
459
460    (0..len).for_each(|idx| match vector.get(idx) {
461        Value::Null => null_mask.set(idx + origin_count, true),
462        Value::Boolean(val) => values.bool_values.push(val),
463        Value::UInt8(val) => values.u8_values.push(val.into()),
464        Value::UInt16(val) => values.u16_values.push(val.into()),
465        Value::UInt32(val) => values.u32_values.push(val),
466        Value::UInt64(val) => values.u64_values.push(val),
467        Value::Int8(val) => values.i8_values.push(val.into()),
468        Value::Int16(val) => values.i16_values.push(val.into()),
469        Value::Int32(val) => values.i32_values.push(val),
470        Value::Int64(val) => values.i64_values.push(val),
471        Value::Float32(val) => values.f32_values.push(*val),
472        Value::Float64(val) => values.f64_values.push(*val),
473        Value::String(val) => values.string_values.push(val.as_utf8().to_string()),
474        Value::Binary(val) => values.binary_values.push(val.to_vec()),
475        Value::Date(val) => values.date_values.push(val.val()),
476        Value::Timestamp(val) => match val.unit() {
477            TimeUnit::Second => values.timestamp_second_values.push(val.value()),
478            TimeUnit::Millisecond => values.timestamp_millisecond_values.push(val.value()),
479            TimeUnit::Microsecond => values.timestamp_microsecond_values.push(val.value()),
480            TimeUnit::Nanosecond => values.timestamp_nanosecond_values.push(val.value()),
481        },
482        Value::Time(val) => match val.unit() {
483            TimeUnit::Second => values.time_second_values.push(val.value()),
484            TimeUnit::Millisecond => values.time_millisecond_values.push(val.value()),
485            TimeUnit::Microsecond => values.time_microsecond_values.push(val.value()),
486            TimeUnit::Nanosecond => values.time_nanosecond_values.push(val.value()),
487        },
488        Value::IntervalYearMonth(val) => values.interval_year_month_values.push(val.to_i32()),
489        Value::IntervalDayTime(val) => values.interval_day_time_values.push(val.to_i64()),
490        Value::IntervalMonthDayNano(val) => values
491            .interval_month_day_nano_values
492            .push(convert_month_day_nano_to_pb(val)),
493        Value::Decimal128(val) => values.decimal128_values.push(convert_to_pb_decimal128(val)),
494        Value::List(_) | Value::Duration(_) => unreachable!(),
495    });
496    column.null_mask = null_mask.into_vec();
497}
498
499/// Returns the type name of the [Request].
500pub fn request_type(request: &Request) -> &'static str {
501    match request {
502        Request::Inserts(_) => "inserts",
503        Request::Query(query_req) => query_request_type(query_req),
504        Request::Ddl(ddl_req) => ddl_request_type(ddl_req),
505        Request::Deletes(_) => "deletes",
506        Request::RowInserts(_) => "row_inserts",
507        Request::RowDeletes(_) => "row_deletes",
508    }
509}
510
511/// Returns the type name of the [QueryRequest].
512fn query_request_type(request: &QueryRequest) -> &'static str {
513    match request.query {
514        Some(Query::Sql(_)) => "query.sql",
515        Some(Query::LogicalPlan(_)) => "query.logical_plan",
516        Some(Query::PromRangeQuery(_)) => "query.prom_range",
517        Some(Query::InsertIntoPlan(_)) => "query.insert_into_plan",
518        None => "query.empty",
519    }
520}
521
522/// Returns the type name of the [DdlRequest].
523fn ddl_request_type(request: &DdlRequest) -> &'static str {
524    match request.expr {
525        Some(Expr::CreateDatabase(_)) => "ddl.create_database",
526        Some(Expr::CreateTable(_)) => "ddl.create_table",
527        Some(Expr::AlterTable(_)) => "ddl.alter_table",
528        Some(Expr::DropTable(_)) => "ddl.drop_table",
529        Some(Expr::TruncateTable(_)) => "ddl.truncate_table",
530        Some(Expr::CreateFlow(_)) => "ddl.create_flow",
531        Some(Expr::DropFlow(_)) => "ddl.drop_flow",
532        Some(Expr::CreateView(_)) => "ddl.create_view",
533        Some(Expr::DropView(_)) => "ddl.drop_view",
534        Some(Expr::AlterDatabase(_)) => "ddl.alter_database",
535        None => "ddl.empty",
536    }
537}
538
539/// Converts an interval to google protobuf type [IntervalMonthDayNano].
540pub fn convert_month_day_nano_to_pb(v: IntervalMonthDayNano) -> v1::IntervalMonthDayNano {
541    v1::IntervalMonthDayNano {
542        months: v.months,
543        days: v.days,
544        nanoseconds: v.nanoseconds,
545    }
546}
547
548/// Convert common decimal128 to grpc decimal128 without precision and scale.
549pub fn convert_to_pb_decimal128(v: Decimal128) -> v1::Decimal128 {
550    let (hi, lo) = v.split_value();
551    v1::Decimal128 { hi, lo }
552}
553
554pub fn pb_value_to_value_ref<'a>(
555    value: &'a v1::Value,
556    datatype_ext: &'a Option<ColumnDataTypeExtension>,
557) -> ValueRef<'a> {
558    let Some(value) = &value.value_data else {
559        return ValueRef::Null;
560    };
561
562    match value {
563        ValueData::I8Value(v) => ValueRef::Int8(*v as i8),
564        ValueData::I16Value(v) => ValueRef::Int16(*v as i16),
565        ValueData::I32Value(v) => ValueRef::Int32(*v),
566        ValueData::I64Value(v) => ValueRef::Int64(*v),
567        ValueData::U8Value(v) => ValueRef::UInt8(*v as u8),
568        ValueData::U16Value(v) => ValueRef::UInt16(*v as u16),
569        ValueData::U32Value(v) => ValueRef::UInt32(*v),
570        ValueData::U64Value(v) => ValueRef::UInt64(*v),
571        ValueData::F32Value(f) => ValueRef::Float32(OrderedF32::from(*f)),
572        ValueData::F64Value(f) => ValueRef::Float64(OrderedF64::from(*f)),
573        ValueData::BoolValue(b) => ValueRef::Boolean(*b),
574        ValueData::BinaryValue(bytes) => ValueRef::Binary(bytes.as_slice()),
575        ValueData::StringValue(string) => ValueRef::String(string.as_str()),
576        ValueData::DateValue(d) => ValueRef::Date(Date::from(*d)),
577        ValueData::TimestampSecondValue(t) => ValueRef::Timestamp(Timestamp::new_second(*t)),
578        ValueData::TimestampMillisecondValue(t) => {
579            ValueRef::Timestamp(Timestamp::new_millisecond(*t))
580        }
581        ValueData::DatetimeValue(t) | ValueData::TimestampMicrosecondValue(t) => {
582            ValueRef::Timestamp(Timestamp::new_microsecond(*t))
583        }
584        ValueData::TimestampNanosecondValue(t) => {
585            ValueRef::Timestamp(Timestamp::new_nanosecond(*t))
586        }
587        ValueData::TimeSecondValue(t) => ValueRef::Time(Time::new_second(*t)),
588        ValueData::TimeMillisecondValue(t) => ValueRef::Time(Time::new_millisecond(*t)),
589        ValueData::TimeMicrosecondValue(t) => ValueRef::Time(Time::new_microsecond(*t)),
590        ValueData::TimeNanosecondValue(t) => ValueRef::Time(Time::new_nanosecond(*t)),
591        ValueData::IntervalYearMonthValue(v) => {
592            ValueRef::IntervalYearMonth(IntervalYearMonth::from_i32(*v))
593        }
594        ValueData::IntervalDayTimeValue(v) => {
595            ValueRef::IntervalDayTime(IntervalDayTime::from_i64(*v))
596        }
597        ValueData::IntervalMonthDayNanoValue(v) => {
598            let interval = IntervalMonthDayNano::new(v.months, v.days, v.nanoseconds);
599            ValueRef::IntervalMonthDayNano(interval)
600        }
601        ValueData::Decimal128Value(v) => {
602            // get precision and scale from datatype_extension
603            if let Some(TypeExt::DecimalType(d)) = datatype_ext
604                .as_ref()
605                .and_then(|column_ext| column_ext.type_ext.as_ref())
606            {
607                ValueRef::Decimal128(Decimal128::from_value_precision_scale(
608                    v.hi,
609                    v.lo,
610                    d.precision as u8,
611                    d.scale as i8,
612                ))
613            } else {
614                // If the precision and scale are not set, use the default value.
615                ValueRef::Decimal128(Decimal128::from_value_precision_scale(
616                    v.hi,
617                    v.lo,
618                    DECIMAL128_MAX_PRECISION,
619                    DECIMAL128_DEFAULT_SCALE,
620                ))
621            }
622        }
623    }
624}
625
626pub fn pb_values_to_vector_ref(data_type: &ConcreteDataType, values: Values) -> VectorRef {
627    match data_type {
628        ConcreteDataType::Boolean(_) => Arc::new(BooleanVector::from(values.bool_values)),
629        ConcreteDataType::Int8(_) => Arc::new(PrimitiveVector::<Int8Type>::from_iter_values(
630            values.i8_values.into_iter().map(|x| x as i8),
631        )),
632        ConcreteDataType::Int16(_) => Arc::new(PrimitiveVector::<Int16Type>::from_iter_values(
633            values.i16_values.into_iter().map(|x| x as i16),
634        )),
635        ConcreteDataType::Int32(_) => Arc::new(Int32Vector::from_vec(values.i32_values)),
636        ConcreteDataType::Int64(_) => Arc::new(Int64Vector::from_vec(values.i64_values)),
637        ConcreteDataType::UInt8(_) => Arc::new(PrimitiveVector::<UInt8Type>::from_iter_values(
638            values.u8_values.into_iter().map(|x| x as u8),
639        )),
640        ConcreteDataType::UInt16(_) => Arc::new(PrimitiveVector::<UInt16Type>::from_iter_values(
641            values.u16_values.into_iter().map(|x| x as u16),
642        )),
643        ConcreteDataType::UInt32(_) => Arc::new(UInt32Vector::from_vec(values.u32_values)),
644        ConcreteDataType::UInt64(_) => Arc::new(UInt64Vector::from_vec(values.u64_values)),
645        ConcreteDataType::Float32(_) => Arc::new(Float32Vector::from_vec(values.f32_values)),
646        ConcreteDataType::Float64(_) => Arc::new(Float64Vector::from_vec(values.f64_values)),
647        ConcreteDataType::Binary(_) => Arc::new(BinaryVector::from(values.binary_values)),
648        ConcreteDataType::String(_) => Arc::new(StringVector::from_vec(values.string_values)),
649        ConcreteDataType::Date(_) => Arc::new(DateVector::from_vec(values.date_values)),
650        ConcreteDataType::Timestamp(unit) => match unit {
651            TimestampType::Second(_) => Arc::new(TimestampSecondVector::from_vec(
652                values.timestamp_second_values,
653            )),
654            TimestampType::Millisecond(_) => Arc::new(TimestampMillisecondVector::from_vec(
655                values.timestamp_millisecond_values,
656            )),
657            TimestampType::Microsecond(_) => Arc::new(TimestampMicrosecondVector::from_vec(
658                values.timestamp_microsecond_values,
659            )),
660            TimestampType::Nanosecond(_) => Arc::new(TimestampNanosecondVector::from_vec(
661                values.timestamp_nanosecond_values,
662            )),
663        },
664        ConcreteDataType::Time(unit) => match unit {
665            TimeType::Second(_) => Arc::new(TimeSecondVector::from_iter_values(
666                values.time_second_values.iter().map(|x| *x as i32),
667            )),
668            TimeType::Millisecond(_) => Arc::new(TimeMillisecondVector::from_iter_values(
669                values.time_millisecond_values.iter().map(|x| *x as i32),
670            )),
671            TimeType::Microsecond(_) => Arc::new(TimeMicrosecondVector::from_vec(
672                values.time_microsecond_values,
673            )),
674            TimeType::Nanosecond(_) => Arc::new(TimeNanosecondVector::from_vec(
675                values.time_nanosecond_values,
676            )),
677        },
678
679        ConcreteDataType::Interval(unit) => match unit {
680            IntervalType::YearMonth(_) => Arc::new(IntervalYearMonthVector::from_vec(
681                values.interval_year_month_values,
682            )),
683            IntervalType::DayTime(_) => Arc::new(IntervalDayTimeVector::from_iter_values(
684                values
685                    .interval_day_time_values
686                    .iter()
687                    .map(|x| IntervalDayTime::from_i64(*x).into()),
688            )),
689            IntervalType::MonthDayNano(_) => {
690                Arc::new(IntervalMonthDayNanoVector::from_iter_values(
691                    values
692                        .interval_month_day_nano_values
693                        .iter()
694                        .map(|x| IntervalMonthDayNano::new(x.months, x.days, x.nanoseconds).into()),
695                ))
696            }
697        },
698        ConcreteDataType::Decimal128(d) => Arc::new(Decimal128Vector::from_values(
699            values.decimal128_values.iter().map(|x| {
700                Decimal128::from_value_precision_scale(x.hi, x.lo, d.precision(), d.scale()).into()
701            }),
702        )),
703        ConcreteDataType::Vector(_) => Arc::new(BinaryVector::from_vec(values.binary_values)),
704        ConcreteDataType::Null(_)
705        | ConcreteDataType::List(_)
706        | ConcreteDataType::Dictionary(_)
707        | ConcreteDataType::Duration(_)
708        | ConcreteDataType::Json(_) => {
709            unreachable!()
710        }
711    }
712}
713
714pub fn pb_values_to_values(data_type: &ConcreteDataType, values: Values) -> Vec<Value> {
715    match data_type {
716        ConcreteDataType::Int64(_) => values
717            .i64_values
718            .into_iter()
719            .map(|val| val.into())
720            .collect(),
721        ConcreteDataType::Float64(_) => values
722            .f64_values
723            .into_iter()
724            .map(|val| val.into())
725            .collect(),
726        ConcreteDataType::String(_) => values
727            .string_values
728            .into_iter()
729            .map(|val| val.into())
730            .collect(),
731        ConcreteDataType::Boolean(_) => values
732            .bool_values
733            .into_iter()
734            .map(|val| val.into())
735            .collect(),
736        ConcreteDataType::Int8(_) => values
737            .i8_values
738            .into_iter()
739            // Safety: Since i32 only stores i8 data here, so i32 as i8 is safe.
740            .map(|val| (val as i8).into())
741            .collect(),
742        ConcreteDataType::Int16(_) => values
743            .i16_values
744            .into_iter()
745            // Safety: Since i32 only stores i16 data here, so i32 as i16 is safe.
746            .map(|val| (val as i16).into())
747            .collect(),
748        ConcreteDataType::Int32(_) => values
749            .i32_values
750            .into_iter()
751            .map(|val| val.into())
752            .collect(),
753        ConcreteDataType::UInt8(_) => values
754            .u8_values
755            .into_iter()
756            // Safety: Since i32 only stores u8 data here, so i32 as u8 is safe.
757            .map(|val| (val as u8).into())
758            .collect(),
759        ConcreteDataType::UInt16(_) => values
760            .u16_values
761            .into_iter()
762            // Safety: Since i32 only stores u16 data here, so i32 as u16 is safe.
763            .map(|val| (val as u16).into())
764            .collect(),
765        ConcreteDataType::UInt32(_) => values
766            .u32_values
767            .into_iter()
768            .map(|val| val.into())
769            .collect(),
770        ConcreteDataType::UInt64(_) => values
771            .u64_values
772            .into_iter()
773            .map(|val| val.into())
774            .collect(),
775        ConcreteDataType::Float32(_) => values
776            .f32_values
777            .into_iter()
778            .map(|val| val.into())
779            .collect(),
780        ConcreteDataType::Binary(_) => values
781            .binary_values
782            .into_iter()
783            .map(|val| val.into())
784            .collect(),
785        ConcreteDataType::Date(_) => values
786            .date_values
787            .into_iter()
788            .map(|v| Value::Date(v.into()))
789            .collect(),
790        ConcreteDataType::Timestamp(TimestampType::Second(_)) => values
791            .timestamp_second_values
792            .into_iter()
793            .map(|v| Value::Timestamp(Timestamp::new_second(v)))
794            .collect(),
795        ConcreteDataType::Timestamp(TimestampType::Millisecond(_)) => values
796            .timestamp_millisecond_values
797            .into_iter()
798            .map(|v| Value::Timestamp(Timestamp::new_millisecond(v)))
799            .collect(),
800        ConcreteDataType::Timestamp(TimestampType::Microsecond(_)) => values
801            .timestamp_microsecond_values
802            .into_iter()
803            .map(|v| Value::Timestamp(Timestamp::new_microsecond(v)))
804            .collect(),
805        ConcreteDataType::Timestamp(TimestampType::Nanosecond(_)) => values
806            .timestamp_nanosecond_values
807            .into_iter()
808            .map(|v| Value::Timestamp(Timestamp::new_nanosecond(v)))
809            .collect(),
810        ConcreteDataType::Time(TimeType::Second(_)) => values
811            .time_second_values
812            .into_iter()
813            .map(|v| Value::Time(Time::new_second(v)))
814            .collect(),
815        ConcreteDataType::Time(TimeType::Millisecond(_)) => values
816            .time_millisecond_values
817            .into_iter()
818            .map(|v| Value::Time(Time::new_millisecond(v)))
819            .collect(),
820        ConcreteDataType::Time(TimeType::Microsecond(_)) => values
821            .time_microsecond_values
822            .into_iter()
823            .map(|v| Value::Time(Time::new_microsecond(v)))
824            .collect(),
825        ConcreteDataType::Time(TimeType::Nanosecond(_)) => values
826            .time_nanosecond_values
827            .into_iter()
828            .map(|v| Value::Time(Time::new_nanosecond(v)))
829            .collect(),
830
831        ConcreteDataType::Interval(IntervalType::YearMonth(_)) => values
832            .interval_year_month_values
833            .into_iter()
834            .map(|v| Value::IntervalYearMonth(IntervalYearMonth::from_i32(v)))
835            .collect(),
836        ConcreteDataType::Interval(IntervalType::DayTime(_)) => values
837            .interval_day_time_values
838            .into_iter()
839            .map(|v| Value::IntervalDayTime(IntervalDayTime::from_i64(v)))
840            .collect(),
841        ConcreteDataType::Interval(IntervalType::MonthDayNano(_)) => values
842            .interval_month_day_nano_values
843            .into_iter()
844            .map(|v| {
845                Value::IntervalMonthDayNano(IntervalMonthDayNano::new(
846                    v.months,
847                    v.days,
848                    v.nanoseconds,
849                ))
850            })
851            .collect(),
852        ConcreteDataType::Decimal128(d) => values
853            .decimal128_values
854            .into_iter()
855            .map(|v| {
856                Value::Decimal128(Decimal128::from_value_precision_scale(
857                    v.hi,
858                    v.lo,
859                    d.precision(),
860                    d.scale(),
861                ))
862            })
863            .collect(),
864        ConcreteDataType::Vector(_) => values.binary_values.into_iter().map(|v| v.into()).collect(),
865        ConcreteDataType::Null(_)
866        | ConcreteDataType::List(_)
867        | ConcreteDataType::Dictionary(_)
868        | ConcreteDataType::Duration(_)
869        | ConcreteDataType::Json(_) => {
870            unreachable!()
871        }
872    }
873}
874
875/// Returns true if the pb semantic type is valid.
876pub fn is_semantic_type_eq(type_value: i32, semantic_type: SemanticType) -> bool {
877    type_value == semantic_type as i32
878}
879
880/// Returns true if the pb type value is valid.
881pub fn is_column_type_value_eq(
882    type_value: i32,
883    type_extension: Option<ColumnDataTypeExtension>,
884    expect_type: &ConcreteDataType,
885) -> bool {
886    ColumnDataTypeWrapper::try_new(type_value, type_extension)
887        .map(|wrapper| {
888            let datatype = ConcreteDataType::from(wrapper);
889            expect_type == &datatype
890        })
891        .unwrap_or(false)
892}
893
894/// Convert value into proto's value.
895pub fn to_proto_value(value: Value) -> Option<v1::Value> {
896    let proto_value = match value {
897        Value::Null => v1::Value { value_data: None },
898        Value::Boolean(v) => v1::Value {
899            value_data: Some(ValueData::BoolValue(v)),
900        },
901        Value::UInt8(v) => v1::Value {
902            value_data: Some(ValueData::U8Value(v.into())),
903        },
904        Value::UInt16(v) => v1::Value {
905            value_data: Some(ValueData::U16Value(v.into())),
906        },
907        Value::UInt32(v) => v1::Value {
908            value_data: Some(ValueData::U32Value(v)),
909        },
910        Value::UInt64(v) => v1::Value {
911            value_data: Some(ValueData::U64Value(v)),
912        },
913        Value::Int8(v) => v1::Value {
914            value_data: Some(ValueData::I8Value(v.into())),
915        },
916        Value::Int16(v) => v1::Value {
917            value_data: Some(ValueData::I16Value(v.into())),
918        },
919        Value::Int32(v) => v1::Value {
920            value_data: Some(ValueData::I32Value(v)),
921        },
922        Value::Int64(v) => v1::Value {
923            value_data: Some(ValueData::I64Value(v)),
924        },
925        Value::Float32(v) => v1::Value {
926            value_data: Some(ValueData::F32Value(*v)),
927        },
928        Value::Float64(v) => v1::Value {
929            value_data: Some(ValueData::F64Value(*v)),
930        },
931        Value::String(v) => v1::Value {
932            value_data: Some(ValueData::StringValue(v.as_utf8().to_string())),
933        },
934        Value::Binary(v) => v1::Value {
935            value_data: Some(ValueData::BinaryValue(v.to_vec())),
936        },
937        Value::Date(v) => v1::Value {
938            value_data: Some(ValueData::DateValue(v.val())),
939        },
940        Value::Timestamp(v) => match v.unit() {
941            TimeUnit::Second => v1::Value {
942                value_data: Some(ValueData::TimestampSecondValue(v.value())),
943            },
944            TimeUnit::Millisecond => v1::Value {
945                value_data: Some(ValueData::TimestampMillisecondValue(v.value())),
946            },
947            TimeUnit::Microsecond => v1::Value {
948                value_data: Some(ValueData::TimestampMicrosecondValue(v.value())),
949            },
950            TimeUnit::Nanosecond => v1::Value {
951                value_data: Some(ValueData::TimestampNanosecondValue(v.value())),
952            },
953        },
954        Value::Time(v) => match v.unit() {
955            TimeUnit::Second => v1::Value {
956                value_data: Some(ValueData::TimeSecondValue(v.value())),
957            },
958            TimeUnit::Millisecond => v1::Value {
959                value_data: Some(ValueData::TimeMillisecondValue(v.value())),
960            },
961            TimeUnit::Microsecond => v1::Value {
962                value_data: Some(ValueData::TimeMicrosecondValue(v.value())),
963            },
964            TimeUnit::Nanosecond => v1::Value {
965                value_data: Some(ValueData::TimeNanosecondValue(v.value())),
966            },
967        },
968        Value::IntervalYearMonth(v) => v1::Value {
969            value_data: Some(ValueData::IntervalYearMonthValue(v.to_i32())),
970        },
971        Value::IntervalDayTime(v) => v1::Value {
972            value_data: Some(ValueData::IntervalDayTimeValue(v.to_i64())),
973        },
974        Value::IntervalMonthDayNano(v) => v1::Value {
975            value_data: Some(ValueData::IntervalMonthDayNanoValue(
976                convert_month_day_nano_to_pb(v),
977            )),
978        },
979        Value::Decimal128(v) => v1::Value {
980            value_data: Some(ValueData::Decimal128Value(convert_to_pb_decimal128(v))),
981        },
982        Value::List(_) | Value::Duration(_) => return None,
983    };
984
985    Some(proto_value)
986}
987
988/// Returns the [ColumnDataTypeWrapper] of the value.
989///
990/// If value is null, returns `None`.
991pub fn proto_value_type(value: &v1::Value) -> Option<ColumnDataType> {
992    let value_type = match value.value_data.as_ref()? {
993        ValueData::I8Value(_) => ColumnDataType::Int8,
994        ValueData::I16Value(_) => ColumnDataType::Int16,
995        ValueData::I32Value(_) => ColumnDataType::Int32,
996        ValueData::I64Value(_) => ColumnDataType::Int64,
997        ValueData::U8Value(_) => ColumnDataType::Uint8,
998        ValueData::U16Value(_) => ColumnDataType::Uint16,
999        ValueData::U32Value(_) => ColumnDataType::Uint32,
1000        ValueData::U64Value(_) => ColumnDataType::Uint64,
1001        ValueData::F32Value(_) => ColumnDataType::Float32,
1002        ValueData::F64Value(_) => ColumnDataType::Float64,
1003        ValueData::BoolValue(_) => ColumnDataType::Boolean,
1004        ValueData::BinaryValue(_) => ColumnDataType::Binary,
1005        ValueData::StringValue(_) => ColumnDataType::String,
1006        ValueData::DateValue(_) => ColumnDataType::Date,
1007        ValueData::DatetimeValue(_) => ColumnDataType::Datetime,
1008        ValueData::TimestampSecondValue(_) => ColumnDataType::TimestampSecond,
1009        ValueData::TimestampMillisecondValue(_) => ColumnDataType::TimestampMillisecond,
1010        ValueData::TimestampMicrosecondValue(_) => ColumnDataType::TimestampMicrosecond,
1011        ValueData::TimestampNanosecondValue(_) => ColumnDataType::TimestampNanosecond,
1012        ValueData::TimeSecondValue(_) => ColumnDataType::TimeSecond,
1013        ValueData::TimeMillisecondValue(_) => ColumnDataType::TimeMillisecond,
1014        ValueData::TimeMicrosecondValue(_) => ColumnDataType::TimeMicrosecond,
1015        ValueData::TimeNanosecondValue(_) => ColumnDataType::TimeNanosecond,
1016        ValueData::IntervalYearMonthValue(_) => ColumnDataType::IntervalYearMonth,
1017        ValueData::IntervalDayTimeValue(_) => ColumnDataType::IntervalDayTime,
1018        ValueData::IntervalMonthDayNanoValue(_) => ColumnDataType::IntervalMonthDayNano,
1019        ValueData::Decimal128Value(_) => ColumnDataType::Decimal128,
1020    };
1021    Some(value_type)
1022}
1023
1024pub fn vectors_to_rows<'a>(
1025    columns: impl Iterator<Item = &'a VectorRef>,
1026    row_count: usize,
1027) -> Vec<Row> {
1028    let mut rows = vec![Row { values: vec![] }; row_count];
1029    for column in columns {
1030        for (row_index, row) in rows.iter_mut().enumerate() {
1031            row.values.push(value_to_grpc_value(column.get(row_index)))
1032        }
1033    }
1034
1035    rows
1036}
1037
1038pub fn value_to_grpc_value(value: Value) -> GrpcValue {
1039    GrpcValue {
1040        value_data: match value {
1041            Value::Null => None,
1042            Value::Boolean(v) => Some(ValueData::BoolValue(v)),
1043            Value::UInt8(v) => Some(ValueData::U8Value(v as _)),
1044            Value::UInt16(v) => Some(ValueData::U16Value(v as _)),
1045            Value::UInt32(v) => Some(ValueData::U32Value(v)),
1046            Value::UInt64(v) => Some(ValueData::U64Value(v)),
1047            Value::Int8(v) => Some(ValueData::I8Value(v as _)),
1048            Value::Int16(v) => Some(ValueData::I16Value(v as _)),
1049            Value::Int32(v) => Some(ValueData::I32Value(v)),
1050            Value::Int64(v) => Some(ValueData::I64Value(v)),
1051            Value::Float32(v) => Some(ValueData::F32Value(*v)),
1052            Value::Float64(v) => Some(ValueData::F64Value(*v)),
1053            Value::String(v) => Some(ValueData::StringValue(v.into_string())),
1054            Value::Binary(v) => Some(ValueData::BinaryValue(v.to_vec())),
1055            Value::Date(v) => Some(ValueData::DateValue(v.val())),
1056            Value::Timestamp(v) => Some(match v.unit() {
1057                TimeUnit::Second => ValueData::TimestampSecondValue(v.value()),
1058                TimeUnit::Millisecond => ValueData::TimestampMillisecondValue(v.value()),
1059                TimeUnit::Microsecond => ValueData::TimestampMicrosecondValue(v.value()),
1060                TimeUnit::Nanosecond => ValueData::TimestampNanosecondValue(v.value()),
1061            }),
1062            Value::Time(v) => Some(match v.unit() {
1063                TimeUnit::Second => ValueData::TimeSecondValue(v.value()),
1064                TimeUnit::Millisecond => ValueData::TimeMillisecondValue(v.value()),
1065                TimeUnit::Microsecond => ValueData::TimeMicrosecondValue(v.value()),
1066                TimeUnit::Nanosecond => ValueData::TimeNanosecondValue(v.value()),
1067            }),
1068            Value::IntervalYearMonth(v) => Some(ValueData::IntervalYearMonthValue(v.to_i32())),
1069            Value::IntervalDayTime(v) => Some(ValueData::IntervalDayTimeValue(v.to_i64())),
1070            Value::IntervalMonthDayNano(v) => Some(ValueData::IntervalMonthDayNanoValue(
1071                convert_month_day_nano_to_pb(v),
1072            )),
1073            Value::Decimal128(v) => Some(ValueData::Decimal128Value(convert_to_pb_decimal128(v))),
1074            Value::List(_) | Value::Duration(_) => unreachable!(),
1075        },
1076    }
1077}
1078
1079#[cfg(test)]
1080mod tests {
1081    use std::sync::Arc;
1082
1083    use common_time::interval::IntervalUnit;
1084    use datatypes::types::{
1085        Int32Type, IntervalDayTimeType, IntervalMonthDayNanoType, IntervalYearMonthType,
1086        TimeMillisecondType, TimeSecondType, TimestampMillisecondType, TimestampSecondType,
1087        UInt32Type,
1088    };
1089    use datatypes::vectors::{
1090        BooleanVector, IntervalDayTimeVector, IntervalMonthDayNanoVector, IntervalYearMonthVector,
1091        TimeMicrosecondVector, TimeMillisecondVector, TimeNanosecondVector, TimeSecondVector,
1092        TimestampMicrosecondVector, TimestampMillisecondVector, TimestampNanosecondVector,
1093        TimestampSecondVector, Vector,
1094    };
1095    use paste::paste;
1096
1097    use super::*;
1098
1099    #[test]
1100    fn test_values_with_capacity() {
1101        let values = values_with_capacity(ColumnDataType::Int8, 2);
1102        let values = values.i8_values;
1103        assert_eq!(2, values.capacity());
1104
1105        let values = values_with_capacity(ColumnDataType::Int32, 2);
1106        let values = values.i32_values;
1107        assert_eq!(2, values.capacity());
1108
1109        let values = values_with_capacity(ColumnDataType::Int64, 2);
1110        let values = values.i64_values;
1111        assert_eq!(2, values.capacity());
1112
1113        let values = values_with_capacity(ColumnDataType::Uint8, 2);
1114        let values = values.u8_values;
1115        assert_eq!(2, values.capacity());
1116
1117        let values = values_with_capacity(ColumnDataType::Uint32, 2);
1118        let values = values.u32_values;
1119        assert_eq!(2, values.capacity());
1120
1121        let values = values_with_capacity(ColumnDataType::Uint64, 2);
1122        let values = values.u64_values;
1123        assert_eq!(2, values.capacity());
1124
1125        let values = values_with_capacity(ColumnDataType::Float32, 2);
1126        let values = values.f32_values;
1127        assert_eq!(2, values.capacity());
1128
1129        let values = values_with_capacity(ColumnDataType::Float64, 2);
1130        let values = values.f64_values;
1131        assert_eq!(2, values.capacity());
1132
1133        let values = values_with_capacity(ColumnDataType::Binary, 2);
1134        let values = values.binary_values;
1135        assert_eq!(2, values.capacity());
1136
1137        let values = values_with_capacity(ColumnDataType::Boolean, 2);
1138        let values = values.bool_values;
1139        assert_eq!(2, values.capacity());
1140
1141        let values = values_with_capacity(ColumnDataType::String, 2);
1142        let values = values.string_values;
1143        assert_eq!(2, values.capacity());
1144
1145        let values = values_with_capacity(ColumnDataType::Date, 2);
1146        let values = values.date_values;
1147        assert_eq!(2, values.capacity());
1148
1149        let values = values_with_capacity(ColumnDataType::Datetime, 2);
1150        let values = values.datetime_values;
1151        assert_eq!(2, values.capacity());
1152
1153        let values = values_with_capacity(ColumnDataType::TimestampMillisecond, 2);
1154        let values = values.timestamp_millisecond_values;
1155        assert_eq!(2, values.capacity());
1156
1157        let values = values_with_capacity(ColumnDataType::TimeMillisecond, 2);
1158        let values = values.time_millisecond_values;
1159        assert_eq!(2, values.capacity());
1160
1161        let values = values_with_capacity(ColumnDataType::IntervalDayTime, 2);
1162        let values = values.interval_day_time_values;
1163        assert_eq!(2, values.capacity());
1164
1165        let values = values_with_capacity(ColumnDataType::IntervalMonthDayNano, 2);
1166        let values = values.interval_month_day_nano_values;
1167        assert_eq!(2, values.capacity());
1168
1169        let values = values_with_capacity(ColumnDataType::Decimal128, 2);
1170        let values = values.decimal128_values;
1171        assert_eq!(2, values.capacity());
1172
1173        let values = values_with_capacity(ColumnDataType::Vector, 2);
1174        let values = values.binary_values;
1175        assert_eq!(2, values.capacity());
1176    }
1177
1178    #[test]
1179    fn test_concrete_datatype_from_column_datatype() {
1180        assert_eq!(
1181            ConcreteDataType::boolean_datatype(),
1182            ColumnDataTypeWrapper::boolean_datatype().into()
1183        );
1184        assert_eq!(
1185            ConcreteDataType::int8_datatype(),
1186            ColumnDataTypeWrapper::int8_datatype().into()
1187        );
1188        assert_eq!(
1189            ConcreteDataType::int16_datatype(),
1190            ColumnDataTypeWrapper::int16_datatype().into()
1191        );
1192        assert_eq!(
1193            ConcreteDataType::int32_datatype(),
1194            ColumnDataTypeWrapper::int32_datatype().into()
1195        );
1196        assert_eq!(
1197            ConcreteDataType::int64_datatype(),
1198            ColumnDataTypeWrapper::int64_datatype().into()
1199        );
1200        assert_eq!(
1201            ConcreteDataType::uint8_datatype(),
1202            ColumnDataTypeWrapper::uint8_datatype().into()
1203        );
1204        assert_eq!(
1205            ConcreteDataType::uint16_datatype(),
1206            ColumnDataTypeWrapper::uint16_datatype().into()
1207        );
1208        assert_eq!(
1209            ConcreteDataType::uint32_datatype(),
1210            ColumnDataTypeWrapper::uint32_datatype().into()
1211        );
1212        assert_eq!(
1213            ConcreteDataType::uint64_datatype(),
1214            ColumnDataTypeWrapper::uint64_datatype().into()
1215        );
1216        assert_eq!(
1217            ConcreteDataType::float32_datatype(),
1218            ColumnDataTypeWrapper::float32_datatype().into()
1219        );
1220        assert_eq!(
1221            ConcreteDataType::float64_datatype(),
1222            ColumnDataTypeWrapper::float64_datatype().into()
1223        );
1224        assert_eq!(
1225            ConcreteDataType::binary_datatype(),
1226            ColumnDataTypeWrapper::binary_datatype().into()
1227        );
1228        assert_eq!(
1229            ConcreteDataType::string_datatype(),
1230            ColumnDataTypeWrapper::string_datatype().into()
1231        );
1232        assert_eq!(
1233            ConcreteDataType::date_datatype(),
1234            ColumnDataTypeWrapper::date_datatype().into()
1235        );
1236        assert_eq!(
1237            ConcreteDataType::timestamp_microsecond_datatype(),
1238            ColumnDataTypeWrapper::datetime_datatype().into()
1239        );
1240        assert_eq!(
1241            ConcreteDataType::timestamp_millisecond_datatype(),
1242            ColumnDataTypeWrapper::timestamp_millisecond_datatype().into()
1243        );
1244        assert_eq!(
1245            ConcreteDataType::time_datatype(TimeUnit::Millisecond),
1246            ColumnDataTypeWrapper::time_millisecond_datatype().into()
1247        );
1248        assert_eq!(
1249            ConcreteDataType::interval_datatype(IntervalUnit::DayTime),
1250            ColumnDataTypeWrapper::interval_day_time_datatype().into()
1251        );
1252        assert_eq!(
1253            ConcreteDataType::interval_datatype(IntervalUnit::YearMonth),
1254            ColumnDataTypeWrapper::interval_year_month_datatype().into()
1255        );
1256        assert_eq!(
1257            ConcreteDataType::interval_datatype(IntervalUnit::MonthDayNano),
1258            ColumnDataTypeWrapper::interval_month_day_nano_datatype().into()
1259        );
1260        assert_eq!(
1261            ConcreteDataType::decimal128_datatype(10, 2),
1262            ColumnDataTypeWrapper::decimal128_datatype(10, 2).into()
1263        );
1264        assert_eq!(
1265            ConcreteDataType::vector_datatype(3),
1266            ColumnDataTypeWrapper::vector_datatype(3).into()
1267        );
1268    }
1269
1270    #[test]
1271    fn test_column_datatype_from_concrete_datatype() {
1272        assert_eq!(
1273            ColumnDataTypeWrapper::boolean_datatype(),
1274            ConcreteDataType::boolean_datatype().try_into().unwrap()
1275        );
1276        assert_eq!(
1277            ColumnDataTypeWrapper::int8_datatype(),
1278            ConcreteDataType::int8_datatype().try_into().unwrap()
1279        );
1280        assert_eq!(
1281            ColumnDataTypeWrapper::int16_datatype(),
1282            ConcreteDataType::int16_datatype().try_into().unwrap()
1283        );
1284        assert_eq!(
1285            ColumnDataTypeWrapper::int32_datatype(),
1286            ConcreteDataType::int32_datatype().try_into().unwrap()
1287        );
1288        assert_eq!(
1289            ColumnDataTypeWrapper::int64_datatype(),
1290            ConcreteDataType::int64_datatype().try_into().unwrap()
1291        );
1292        assert_eq!(
1293            ColumnDataTypeWrapper::uint8_datatype(),
1294            ConcreteDataType::uint8_datatype().try_into().unwrap()
1295        );
1296        assert_eq!(
1297            ColumnDataTypeWrapper::uint16_datatype(),
1298            ConcreteDataType::uint16_datatype().try_into().unwrap()
1299        );
1300        assert_eq!(
1301            ColumnDataTypeWrapper::uint32_datatype(),
1302            ConcreteDataType::uint32_datatype().try_into().unwrap()
1303        );
1304        assert_eq!(
1305            ColumnDataTypeWrapper::uint64_datatype(),
1306            ConcreteDataType::uint64_datatype().try_into().unwrap()
1307        );
1308        assert_eq!(
1309            ColumnDataTypeWrapper::float32_datatype(),
1310            ConcreteDataType::float32_datatype().try_into().unwrap()
1311        );
1312        assert_eq!(
1313            ColumnDataTypeWrapper::float64_datatype(),
1314            ConcreteDataType::float64_datatype().try_into().unwrap()
1315        );
1316        assert_eq!(
1317            ColumnDataTypeWrapper::binary_datatype(),
1318            ConcreteDataType::binary_datatype().try_into().unwrap()
1319        );
1320        assert_eq!(
1321            ColumnDataTypeWrapper::string_datatype(),
1322            ConcreteDataType::string_datatype().try_into().unwrap()
1323        );
1324        assert_eq!(
1325            ColumnDataTypeWrapper::date_datatype(),
1326            ConcreteDataType::date_datatype().try_into().unwrap()
1327        );
1328        assert_eq!(
1329            ColumnDataTypeWrapper::timestamp_millisecond_datatype(),
1330            ConcreteDataType::timestamp_millisecond_datatype()
1331                .try_into()
1332                .unwrap()
1333        );
1334        assert_eq!(
1335            ColumnDataTypeWrapper::interval_year_month_datatype(),
1336            ConcreteDataType::interval_datatype(IntervalUnit::YearMonth)
1337                .try_into()
1338                .unwrap()
1339        );
1340        assert_eq!(
1341            ColumnDataTypeWrapper::interval_day_time_datatype(),
1342            ConcreteDataType::interval_datatype(IntervalUnit::DayTime)
1343                .try_into()
1344                .unwrap()
1345        );
1346        assert_eq!(
1347            ColumnDataTypeWrapper::interval_month_day_nano_datatype(),
1348            ConcreteDataType::interval_datatype(IntervalUnit::MonthDayNano)
1349                .try_into()
1350                .unwrap()
1351        );
1352
1353        assert_eq!(
1354            ColumnDataTypeWrapper::decimal128_datatype(10, 2),
1355            ConcreteDataType::decimal128_datatype(10, 2)
1356                .try_into()
1357                .unwrap()
1358        );
1359        assert_eq!(
1360            ColumnDataTypeWrapper::vector_datatype(3),
1361            ConcreteDataType::vector_datatype(3).try_into().unwrap()
1362        );
1363
1364        let result: Result<ColumnDataTypeWrapper> = ConcreteDataType::null_datatype().try_into();
1365        assert!(result.is_err());
1366        assert_eq!(
1367            result.unwrap_err().to_string(),
1368            "Failed to create column datatype from Null(NullType)"
1369        );
1370
1371        let result: Result<ColumnDataTypeWrapper> =
1372            ConcreteDataType::list_datatype(ConcreteDataType::boolean_datatype()).try_into();
1373        assert!(result.is_err());
1374        assert_eq!(
1375            result.unwrap_err().to_string(),
1376            "Failed to create column datatype from List(ListType { item_type: Boolean(BooleanType) })"
1377        );
1378    }
1379
1380    #[test]
1381    fn test_column_put_timestamp_values() {
1382        let mut column = Column {
1383            column_name: "test".to_string(),
1384            semantic_type: 0,
1385            values: Some(Values {
1386                ..Default::default()
1387            }),
1388            null_mask: vec![],
1389            datatype: 0,
1390            ..Default::default()
1391        };
1392
1393        let vector = Arc::new(TimestampNanosecondVector::from_vec(vec![1, 2, 3]));
1394        push_vals(&mut column, 3, vector);
1395        assert_eq!(
1396            vec![1, 2, 3],
1397            column.values.as_ref().unwrap().timestamp_nanosecond_values
1398        );
1399
1400        let vector = Arc::new(TimestampMillisecondVector::from_vec(vec![4, 5, 6]));
1401        push_vals(&mut column, 3, vector);
1402        assert_eq!(
1403            vec![4, 5, 6],
1404            column.values.as_ref().unwrap().timestamp_millisecond_values
1405        );
1406
1407        let vector = Arc::new(TimestampMicrosecondVector::from_vec(vec![7, 8, 9]));
1408        push_vals(&mut column, 3, vector);
1409        assert_eq!(
1410            vec![7, 8, 9],
1411            column.values.as_ref().unwrap().timestamp_microsecond_values
1412        );
1413
1414        let vector = Arc::new(TimestampSecondVector::from_vec(vec![10, 11, 12]));
1415        push_vals(&mut column, 3, vector);
1416        assert_eq!(
1417            vec![10, 11, 12],
1418            column.values.as_ref().unwrap().timestamp_second_values
1419        );
1420    }
1421
1422    #[test]
1423    fn test_column_put_time_values() {
1424        let mut column = Column {
1425            column_name: "test".to_string(),
1426            semantic_type: 0,
1427            values: Some(Values {
1428                ..Default::default()
1429            }),
1430            null_mask: vec![],
1431            datatype: 0,
1432            ..Default::default()
1433        };
1434
1435        let vector = Arc::new(TimeNanosecondVector::from_vec(vec![1, 2, 3]));
1436        push_vals(&mut column, 3, vector);
1437        assert_eq!(
1438            vec![1, 2, 3],
1439            column.values.as_ref().unwrap().time_nanosecond_values
1440        );
1441
1442        let vector = Arc::new(TimeMillisecondVector::from_vec(vec![4, 5, 6]));
1443        push_vals(&mut column, 3, vector);
1444        assert_eq!(
1445            vec![4, 5, 6],
1446            column.values.as_ref().unwrap().time_millisecond_values
1447        );
1448
1449        let vector = Arc::new(TimeMicrosecondVector::from_vec(vec![7, 8, 9]));
1450        push_vals(&mut column, 3, vector);
1451        assert_eq!(
1452            vec![7, 8, 9],
1453            column.values.as_ref().unwrap().time_microsecond_values
1454        );
1455
1456        let vector = Arc::new(TimeSecondVector::from_vec(vec![10, 11, 12]));
1457        push_vals(&mut column, 3, vector);
1458        assert_eq!(
1459            vec![10, 11, 12],
1460            column.values.as_ref().unwrap().time_second_values
1461        );
1462    }
1463
1464    #[test]
1465    fn test_column_put_interval_values() {
1466        let mut column = Column {
1467            column_name: "test".to_string(),
1468            semantic_type: 0,
1469            values: Some(Values {
1470                ..Default::default()
1471            }),
1472            null_mask: vec![],
1473            datatype: 0,
1474            ..Default::default()
1475        };
1476
1477        let vector = Arc::new(IntervalYearMonthVector::from_vec(vec![1, 2, 3]));
1478        push_vals(&mut column, 3, vector);
1479        assert_eq!(
1480            vec![1, 2, 3],
1481            column.values.as_ref().unwrap().interval_year_month_values
1482        );
1483
1484        let vector = Arc::new(IntervalDayTimeVector::from_vec(vec![
1485            IntervalDayTime::new(0, 4).into(),
1486            IntervalDayTime::new(0, 5).into(),
1487            IntervalDayTime::new(0, 6).into(),
1488        ]));
1489        push_vals(&mut column, 3, vector);
1490        assert_eq!(
1491            vec![4, 5, 6],
1492            column.values.as_ref().unwrap().interval_day_time_values
1493        );
1494
1495        let vector = Arc::new(IntervalMonthDayNanoVector::from_vec(vec![
1496            IntervalMonthDayNano::new(0, 0, 7).into(),
1497            IntervalMonthDayNano::new(0, 0, 8).into(),
1498            IntervalMonthDayNano::new(0, 0, 9).into(),
1499        ]));
1500        let len = vector.len();
1501        push_vals(&mut column, 3, vector);
1502        (0..len).for_each(|i| {
1503            assert_eq!(
1504                7 + i as i64,
1505                column
1506                    .values
1507                    .as_ref()
1508                    .unwrap()
1509                    .interval_month_day_nano_values
1510                    .get(i)
1511                    .unwrap()
1512                    .nanoseconds
1513            );
1514        });
1515    }
1516
1517    #[test]
1518    fn test_column_put_vector() {
1519        use crate::v1::SemanticType;
1520        // Some(false), None, Some(true), Some(true)
1521        let mut column = Column {
1522            column_name: "test".to_string(),
1523            semantic_type: SemanticType::Field as i32,
1524            values: Some(Values {
1525                bool_values: vec![false, true, true],
1526                ..Default::default()
1527            }),
1528            null_mask: vec![2],
1529            datatype: ColumnDataType::Boolean as i32,
1530            ..Default::default()
1531        };
1532        let row_count = 4;
1533
1534        let vector = Arc::new(BooleanVector::from(vec![Some(true), None, Some(false)]));
1535        push_vals(&mut column, row_count, vector);
1536        // Some(false), None, Some(true), Some(true), Some(true), None, Some(false)
1537        let bool_values = column.values.unwrap().bool_values;
1538        assert_eq!(vec![false, true, true, true, false], bool_values);
1539        let null_mask = column.null_mask;
1540        assert_eq!(34, null_mask[0]);
1541    }
1542
1543    #[test]
1544    fn test_convert_i128_to_interval() {
1545        let i128_val = 3;
1546        let interval = convert_month_day_nano_to_pb(IntervalMonthDayNano::from_i128(i128_val));
1547        assert_eq!(interval.months, 0);
1548        assert_eq!(interval.days, 0);
1549        assert_eq!(interval.nanoseconds, 3);
1550    }
1551
1552    #[test]
1553    fn test_convert_timestamp_values() {
1554        // second
1555        let actual = pb_values_to_values(
1556            &ConcreteDataType::Timestamp(TimestampType::Second(TimestampSecondType)),
1557            Values {
1558                timestamp_second_values: vec![1_i64, 2_i64, 3_i64],
1559                ..Default::default()
1560            },
1561        );
1562        let expect = vec![
1563            Value::Timestamp(Timestamp::new_second(1_i64)),
1564            Value::Timestamp(Timestamp::new_second(2_i64)),
1565            Value::Timestamp(Timestamp::new_second(3_i64)),
1566        ];
1567        assert_eq!(expect, actual);
1568
1569        // millisecond
1570        let actual = pb_values_to_values(
1571            &ConcreteDataType::Timestamp(TimestampType::Millisecond(TimestampMillisecondType)),
1572            Values {
1573                timestamp_millisecond_values: vec![1_i64, 2_i64, 3_i64],
1574                ..Default::default()
1575            },
1576        );
1577        let expect = vec![
1578            Value::Timestamp(Timestamp::new_millisecond(1_i64)),
1579            Value::Timestamp(Timestamp::new_millisecond(2_i64)),
1580            Value::Timestamp(Timestamp::new_millisecond(3_i64)),
1581        ];
1582        assert_eq!(expect, actual);
1583    }
1584
1585    #[test]
1586    fn test_convert_time_values() {
1587        // second
1588        let actual = pb_values_to_values(
1589            &ConcreteDataType::Time(TimeType::Second(TimeSecondType)),
1590            Values {
1591                time_second_values: vec![1_i64, 2_i64, 3_i64],
1592                ..Default::default()
1593            },
1594        );
1595        let expect = vec![
1596            Value::Time(Time::new_second(1_i64)),
1597            Value::Time(Time::new_second(2_i64)),
1598            Value::Time(Time::new_second(3_i64)),
1599        ];
1600        assert_eq!(expect, actual);
1601
1602        // millisecond
1603        let actual = pb_values_to_values(
1604            &ConcreteDataType::Time(TimeType::Millisecond(TimeMillisecondType)),
1605            Values {
1606                time_millisecond_values: vec![1_i64, 2_i64, 3_i64],
1607                ..Default::default()
1608            },
1609        );
1610        let expect = vec![
1611            Value::Time(Time::new_millisecond(1_i64)),
1612            Value::Time(Time::new_millisecond(2_i64)),
1613            Value::Time(Time::new_millisecond(3_i64)),
1614        ];
1615        assert_eq!(expect, actual);
1616    }
1617
1618    #[test]
1619    fn test_convert_interval_values() {
1620        // year_month
1621        let actual = pb_values_to_values(
1622            &ConcreteDataType::Interval(IntervalType::YearMonth(IntervalYearMonthType)),
1623            Values {
1624                interval_year_month_values: vec![1_i32, 2_i32, 3_i32],
1625                ..Default::default()
1626            },
1627        );
1628        let expect = vec![
1629            Value::IntervalYearMonth(IntervalYearMonth::new(1_i32)),
1630            Value::IntervalYearMonth(IntervalYearMonth::new(2_i32)),
1631            Value::IntervalYearMonth(IntervalYearMonth::new(3_i32)),
1632        ];
1633        assert_eq!(expect, actual);
1634
1635        // day_time
1636        let actual = pb_values_to_values(
1637            &ConcreteDataType::Interval(IntervalType::DayTime(IntervalDayTimeType)),
1638            Values {
1639                interval_day_time_values: vec![1_i64, 2_i64, 3_i64],
1640                ..Default::default()
1641            },
1642        );
1643        let expect = vec![
1644            Value::IntervalDayTime(IntervalDayTime::from_i64(1_i64)),
1645            Value::IntervalDayTime(IntervalDayTime::from_i64(2_i64)),
1646            Value::IntervalDayTime(IntervalDayTime::from_i64(3_i64)),
1647        ];
1648        assert_eq!(expect, actual);
1649
1650        // month_day_nano
1651        let actual = pb_values_to_values(
1652            &ConcreteDataType::Interval(IntervalType::MonthDayNano(IntervalMonthDayNanoType)),
1653            Values {
1654                interval_month_day_nano_values: vec![
1655                    v1::IntervalMonthDayNano {
1656                        months: 1,
1657                        days: 2,
1658                        nanoseconds: 3,
1659                    },
1660                    v1::IntervalMonthDayNano {
1661                        months: 5,
1662                        days: 6,
1663                        nanoseconds: 7,
1664                    },
1665                    v1::IntervalMonthDayNano {
1666                        months: 9,
1667                        days: 10,
1668                        nanoseconds: 11,
1669                    },
1670                ],
1671                ..Default::default()
1672            },
1673        );
1674        let expect = vec![
1675            Value::IntervalMonthDayNano(IntervalMonthDayNano::new(1, 2, 3)),
1676            Value::IntervalMonthDayNano(IntervalMonthDayNano::new(5, 6, 7)),
1677            Value::IntervalMonthDayNano(IntervalMonthDayNano::new(9, 10, 11)),
1678        ];
1679        assert_eq!(expect, actual);
1680    }
1681
1682    macro_rules! test_convert_values {
1683        ($grpc_data_type: ident, $values: expr,  $concrete_data_type: ident, $expected_ret: expr) => {
1684            paste! {
1685                #[test]
1686                fn [<test_convert_ $grpc_data_type _values>]() {
1687                    let values = Values {
1688                        [<$grpc_data_type _values>]: $values,
1689                        ..Default::default()
1690                    };
1691
1692                    let data_type = ConcreteDataType::[<$concrete_data_type _datatype>]();
1693                    let result = pb_values_to_values(&data_type, values);
1694
1695                    assert_eq!(
1696                        $expected_ret,
1697                        result
1698                    );
1699                }
1700            }
1701        };
1702    }
1703
1704    test_convert_values!(
1705        i8,
1706        vec![1_i32, 2, 3],
1707        int8,
1708        vec![Value::Int8(1), Value::Int8(2), Value::Int8(3)]
1709    );
1710
1711    test_convert_values!(
1712        u8,
1713        vec![1_u32, 2, 3],
1714        uint8,
1715        vec![Value::UInt8(1), Value::UInt8(2), Value::UInt8(3)]
1716    );
1717
1718    test_convert_values!(
1719        i16,
1720        vec![1_i32, 2, 3],
1721        int16,
1722        vec![Value::Int16(1), Value::Int16(2), Value::Int16(3)]
1723    );
1724
1725    test_convert_values!(
1726        u16,
1727        vec![1_u32, 2, 3],
1728        uint16,
1729        vec![Value::UInt16(1), Value::UInt16(2), Value::UInt16(3)]
1730    );
1731
1732    test_convert_values!(
1733        i32,
1734        vec![1, 2, 3],
1735        int32,
1736        vec![Value::Int32(1), Value::Int32(2), Value::Int32(3)]
1737    );
1738
1739    test_convert_values!(
1740        u32,
1741        vec![1, 2, 3],
1742        uint32,
1743        vec![Value::UInt32(1), Value::UInt32(2), Value::UInt32(3)]
1744    );
1745
1746    test_convert_values!(
1747        i64,
1748        vec![1, 2, 3],
1749        int64,
1750        vec![Value::Int64(1), Value::Int64(2), Value::Int64(3)]
1751    );
1752
1753    test_convert_values!(
1754        u64,
1755        vec![1, 2, 3],
1756        uint64,
1757        vec![Value::UInt64(1), Value::UInt64(2), Value::UInt64(3)]
1758    );
1759
1760    test_convert_values!(
1761        f32,
1762        vec![1.0, 2.0, 3.0],
1763        float32,
1764        vec![
1765            Value::Float32(1.0.into()),
1766            Value::Float32(2.0.into()),
1767            Value::Float32(3.0.into())
1768        ]
1769    );
1770
1771    test_convert_values!(
1772        f64,
1773        vec![1.0, 2.0, 3.0],
1774        float64,
1775        vec![
1776            Value::Float64(1.0.into()),
1777            Value::Float64(2.0.into()),
1778            Value::Float64(3.0.into())
1779        ]
1780    );
1781
1782    test_convert_values!(
1783        string,
1784        vec!["1".to_string(), "2".to_string(), "3".to_string()],
1785        string,
1786        vec![
1787            Value::String("1".into()),
1788            Value::String("2".into()),
1789            Value::String("3".into())
1790        ]
1791    );
1792
1793    test_convert_values!(
1794        binary,
1795        vec!["1".into(), "2".into(), "3".into()],
1796        binary,
1797        vec![
1798            Value::Binary(b"1".to_vec().into()),
1799            Value::Binary(b"2".to_vec().into()),
1800            Value::Binary(b"3".to_vec().into())
1801        ]
1802    );
1803
1804    test_convert_values!(
1805        date,
1806        vec![1, 2, 3],
1807        date,
1808        vec![
1809            Value::Date(1.into()),
1810            Value::Date(2.into()),
1811            Value::Date(3.into())
1812        ]
1813    );
1814
1815    #[test]
1816    fn test_vectors_to_rows_for_different_types() {
1817        let boolean_vec = BooleanVector::from_vec(vec![true, false, true]);
1818        let int8_vec = PrimitiveVector::<Int8Type>::from_iter_values(vec![1, 2, 3]);
1819        let int32_vec = PrimitiveVector::<Int32Type>::from_iter_values(vec![100, 200, 300]);
1820        let uint8_vec = PrimitiveVector::<UInt8Type>::from_iter_values(vec![10, 20, 30]);
1821        let uint32_vec = PrimitiveVector::<UInt32Type>::from_iter_values(vec![1000, 2000, 3000]);
1822        let float32_vec = Float32Vector::from_vec(vec![1.1, 2.2, 3.3]);
1823        let date_vec = DateVector::from_vec(vec![10, 20, 30]);
1824        let string_vec = StringVector::from_vec(vec!["a", "b", "c"]);
1825
1826        let vector_refs: Vec<VectorRef> = vec![
1827            Arc::new(boolean_vec),
1828            Arc::new(int8_vec),
1829            Arc::new(int32_vec),
1830            Arc::new(uint8_vec),
1831            Arc::new(uint32_vec),
1832            Arc::new(float32_vec),
1833            Arc::new(date_vec),
1834            Arc::new(string_vec),
1835        ];
1836
1837        let result = vectors_to_rows(vector_refs.iter(), 3);
1838
1839        assert_eq!(result.len(), 3);
1840
1841        assert_eq!(result[0].values.len(), 8);
1842        let values = result[0]
1843            .values
1844            .iter()
1845            .map(|v| v.value_data.clone().unwrap())
1846            .collect::<Vec<_>>();
1847        assert_eq!(values[0], ValueData::BoolValue(true));
1848        assert_eq!(values[1], ValueData::I8Value(1));
1849        assert_eq!(values[2], ValueData::I32Value(100));
1850        assert_eq!(values[3], ValueData::U8Value(10));
1851        assert_eq!(values[4], ValueData::U32Value(1000));
1852        assert_eq!(values[5], ValueData::F32Value(1.1));
1853        assert_eq!(values[6], ValueData::DateValue(10));
1854        assert_eq!(values[7], ValueData::StringValue("a".to_string()));
1855
1856        assert_eq!(result[1].values.len(), 8);
1857        let values = result[1]
1858            .values
1859            .iter()
1860            .map(|v| v.value_data.clone().unwrap())
1861            .collect::<Vec<_>>();
1862        assert_eq!(values[0], ValueData::BoolValue(false));
1863        assert_eq!(values[1], ValueData::I8Value(2));
1864        assert_eq!(values[2], ValueData::I32Value(200));
1865        assert_eq!(values[3], ValueData::U8Value(20));
1866        assert_eq!(values[4], ValueData::U32Value(2000));
1867        assert_eq!(values[5], ValueData::F32Value(2.2));
1868        assert_eq!(values[6], ValueData::DateValue(20));
1869        assert_eq!(values[7], ValueData::StringValue("b".to_string()));
1870
1871        assert_eq!(result[2].values.len(), 8);
1872        let values = result[2]
1873            .values
1874            .iter()
1875            .map(|v| v.value_data.clone().unwrap())
1876            .collect::<Vec<_>>();
1877        assert_eq!(values[0], ValueData::BoolValue(true));
1878        assert_eq!(values[1], ValueData::I8Value(3));
1879        assert_eq!(values[2], ValueData::I32Value(300));
1880        assert_eq!(values[3], ValueData::U8Value(30));
1881        assert_eq!(values[4], ValueData::U32Value(3000));
1882        assert_eq!(values[5], ValueData::F32Value(3.3));
1883        assert_eq!(values[6], ValueData::DateValue(30));
1884        assert_eq!(values[7], ValueData::StringValue("c".to_string()));
1885    }
1886
1887    #[test]
1888    fn test_is_column_type_value_eq() {
1889        // test column type eq
1890        let column1 = Column {
1891            column_name: "test".to_string(),
1892            semantic_type: 0,
1893            values: Some(Values {
1894                bool_values: vec![false, true, true],
1895                ..Default::default()
1896            }),
1897            null_mask: vec![2],
1898            datatype: ColumnDataType::Boolean as i32,
1899            datatype_extension: None,
1900            options: None,
1901        };
1902        assert!(is_column_type_value_eq(
1903            column1.datatype,
1904            column1.datatype_extension,
1905            &ConcreteDataType::boolean_datatype(),
1906        ));
1907    }
1908
1909    #[test]
1910    fn test_convert_to_pb_decimal128() {
1911        let decimal = Decimal128::new(123, 3, 1);
1912        let pb_decimal = convert_to_pb_decimal128(decimal);
1913        assert_eq!(pb_decimal.lo, 123);
1914        assert_eq!(pb_decimal.hi, 0);
1915    }
1916}