Skip to content

Commit 392eb5d

Browse files
committed
feat: variant type support extension types
1 parent 1d9c97b commit 392eb5d

File tree

3 files changed

+81
-5
lines changed

3 files changed

+81
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ color-eyre = { git = "https://github.com/eyre-rs/eyre.git", rev = "e5d92c3" }
638638
deltalake = { git = "https://github.com/delta-io/delta-rs", rev = "c149502" }
639639
display-more = { git = "https://github.com/databendlabs/display-more", tag = "v0.1.2" }
640640
ethnum = { git = "https://github.com/datafuse-extras/ethnum-rs", rev = "4cb05f1" }
641+
jsonb = { git = "https://github.com/b41sh/jsonb", rev = "4e9315efa67c46777fd3097f0d189ff5990ac24c" }
641642
map-api = { git = "https://github.com/databendlabs/map-api", tag = "v0.2.3" }
642643
openai_api_rust = { git = "https://github.com/datafuse-extras/openai-api", rev = "819a0ed" }
643644
openraft = { git = "https://github.com/databendlabs/openraft", tag = "v0.10.0-alpha.9" }

src/query/expression/src/types/variant.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,37 @@ pub fn cast_scalar_to_variant(scalar: ScalarRef, tz: &TimeZone, buf: &mut Vec<u8
230230
NumberScalar::Float32(n) => n.0.into(),
231231
NumberScalar::Float64(n) => n.0.into(),
232232
},
233-
ScalarRef::Decimal(x) => x.to_float64().into(),
233+
ScalarRef::Decimal(x) => {
234+
match x {
235+
DecimalScalar::Decimal128(value, size) => {
236+
let dec = jsonb::Decimal128 {
237+
scale: size.scale,
238+
value,
239+
};
240+
jsonb::Value::Number(jsonb::Number::Decimal128(dec))
241+
}
242+
DecimalScalar::Decimal256(value, size) => {
243+
let dec = jsonb::Decimal256 {
244+
scale: size.scale,
245+
value,
246+
};
247+
jsonb::Value::Number(jsonb::Number::Decimal256(dec))
248+
}
249+
}
250+
}
234251
ScalarRef::Boolean(b) => jsonb::Value::Bool(b),
235-
ScalarRef::Binary(s) => jsonb::Value::String(hex::encode_upper(s).into()),
252+
ScalarRef::Binary(s) => jsonb::Value::Binary(s),
236253
ScalarRef::String(s) => jsonb::Value::String(s.into()),
237-
ScalarRef::Timestamp(ts) => timestamp_to_string(ts, tz).to_string().into(),
238-
ScalarRef::Date(d) => date_to_string(d, tz).to_string().into(),
239-
ScalarRef::Interval(i) => interval_to_string(&i).to_string().into(),
254+
ScalarRef::Timestamp(ts) => jsonb::Value::Timestamp(jsonb::Timestamp {offset:0, value: ts}),
255+
ScalarRef::Date(d) => jsonb::Value::Timestamp(jsonb::Date {offset:0, value: d}),,
256+
ScalarRef::Interval(i) => {
257+
let interval = jsonb::Interval {
258+
months: i.months(),
259+
days: i.days(),
260+
microseconds: i.microseconds(),
261+
};
262+
jsonb::Value::Interval(interval)
263+
}
240264
ScalarRef::Array(col) => {
241265
let items = cast_scalars_to_variants(col.iter(), tz);
242266
let owned_jsonb = OwnedJsonb::build_array(items.iter().map(RawJsonb::new))

src/query/functions/src/scalars/variant.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,57 @@ pub fn register(registry: &mut FunctionRegistry) {
729729
}),
730730
);
731731

732+
registry.register_combine_nullable_1_arg::<VariantType, BinaryType, _, _>(
733+
"as_binary",
734+
|_, _| FunctionDomain::Full,
735+
vectorize_with_builder_1_arg::<VariantType, NullableType<BinaryType>>(|v, output, ctx| {
736+
if let Some(validity) = &ctx.validity {
737+
if !validity.get_bit(output.len()) {
738+
output.push_null();
739+
return;
740+
}
741+
}
742+
match RawJsonb::new(v).as_binary() {
743+
Ok(Some(res)) => output.push(&res),
744+
_ => output.push_null(),
745+
}
746+
}),
747+
);
748+
749+
registry.register_combine_nullable_1_arg::<VariantType, DateType, _, _>(
750+
"as_date",
751+
|_, _| FunctionDomain::Full,
752+
vectorize_with_builder_1_arg::<VariantType, NullableType<DateType>>(|v, output, ctx| {
753+
if let Some(validity) = &ctx.validity {
754+
if !validity.get_bit(output.len()) {
755+
output.push_null();
756+
return;
757+
}
758+
}
759+
match RawJsonb::new(v).as_date() {
760+
Ok(Some(res)) => output.push(res.value),
761+
_ => output.push_null(),
762+
}
763+
}),
764+
);
765+
766+
registry.register_combine_nullable_1_arg::<VariantType, TimestampType, _, _>(
767+
"as_timestamp",
768+
|_, _| FunctionDomain::Full,
769+
vectorize_with_builder_1_arg::<VariantType, NullableType<TimestampType>>(|v, output, ctx| {
770+
if let Some(validity) = &ctx.validity {
771+
if !validity.get_bit(output.len()) {
772+
output.push_null();
773+
return;
774+
}
775+
}
776+
match RawJsonb::new(v).as_timestamp() {
777+
Ok(Some(res)) => output.push(res.value),
778+
_ => output.push_null(),
779+
}
780+
}),
781+
);
782+
732783
registry.register_combine_nullable_1_arg::<VariantType, VariantType, _, _>(
733784
"as_array",
734785
|_, _| FunctionDomain::Full,

0 commit comments

Comments
 (0)