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