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