Skip to content

Commit 8ec20fc

Browse files
committed
fix
1 parent 7fca5f5 commit 8ec20fc

File tree

9 files changed

+236
-6
lines changed

9 files changed

+236
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ backtrace = { git = "https://github.com/rust-lang/backtrace-rs.git", rev = "7226
637637
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" }
640-
jsonb = { git = "https://github.com/b41sh/jsonb", rev = "90b9b0155daf395f22ec47c0cacfb70ba8e96985" }
640+
jsonb = { git = "https://github.com/b41sh/jsonb", rev = "73871c14bc11b65a1cba90bdb4ccc85f06c356e2" }
641641
map-api = { git = "https://github.com/databendlabs/map-api", tag = "v0.2.3" }
642642
openai_api_rust = { git = "https://github.com/datafuse-extras/openai-api", rev = "819a0ed" }
643643
openraft = { git = "https://github.com/databendlabs/openraft", tag = "v0.10.0-alpha.9" }

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,23 @@ pub fn register(registry: &mut FunctionRegistry) {
732732
}),
733733
);
734734

735+
registry.register_passthrough_nullable_1_arg::<VariantType, BooleanType, _, _>(
736+
"is_binary",
737+
|_, _| FunctionDomain::Full,
738+
vectorize_with_builder_1_arg::<VariantType, BooleanType>(|v, output, ctx| {
739+
if let Some(validity) = &ctx.validity {
740+
if !validity.get_bit(output.len()) {
741+
output.push(false);
742+
return;
743+
}
744+
}
745+
match RawJsonb::new(v).is_binary() {
746+
Ok(res) => output.push(res),
747+
Err(_) => output.push(false),
748+
}
749+
}),
750+
);
751+
735752
registry.register_combine_nullable_1_arg::<VariantType, BinaryType, _, _>(
736753
"as_binary",
737754
|_, _| FunctionDomain::Full,
@@ -749,6 +766,23 @@ pub fn register(registry: &mut FunctionRegistry) {
749766
}),
750767
);
751768

769+
registry.register_passthrough_nullable_1_arg::<VariantType, BooleanType, _, _>(
770+
"is_date",
771+
|_, _| FunctionDomain::Full,
772+
vectorize_with_builder_1_arg::<VariantType, BooleanType>(|v, output, ctx| {
773+
if let Some(validity) = &ctx.validity {
774+
if !validity.get_bit(output.len()) {
775+
output.push(false);
776+
return;
777+
}
778+
}
779+
match RawJsonb::new(v).is_date() {
780+
Ok(res) => output.push(res),
781+
Err(_) => output.push(false),
782+
}
783+
}),
784+
);
785+
752786
registry.register_combine_nullable_1_arg::<VariantType, DateType, _, _>(
753787
"as_date",
754788
|_, _| FunctionDomain::Full,
@@ -766,6 +800,23 @@ pub fn register(registry: &mut FunctionRegistry) {
766800
}),
767801
);
768802

803+
registry.register_passthrough_nullable_1_arg::<VariantType, BooleanType, _, _>(
804+
"is_timestamp",
805+
|_, _| FunctionDomain::Full,
806+
vectorize_with_builder_1_arg::<VariantType, BooleanType>(|v, output, ctx| {
807+
if let Some(validity) = &ctx.validity {
808+
if !validity.get_bit(output.len()) {
809+
output.push(false);
810+
return;
811+
}
812+
}
813+
match RawJsonb::new(v).is_timestamp() {
814+
Ok(res) => output.push(res),
815+
Err(_) => output.push(false),
816+
}
817+
}),
818+
);
819+
769820
registry.register_combine_nullable_1_arg::<VariantType, TimestampType, _, _>(
770821
"as_timestamp",
771822
|_, _| FunctionDomain::Full,
@@ -785,6 +836,23 @@ pub fn register(registry: &mut FunctionRegistry) {
785836
),
786837
);
787838

839+
registry.register_passthrough_nullable_1_arg::<VariantType, BooleanType, _, _>(
840+
"is_interval",
841+
|_, _| FunctionDomain::Full,
842+
vectorize_with_builder_1_arg::<VariantType, BooleanType>(|v, output, ctx| {
843+
if let Some(validity) = &ctx.validity {
844+
if !validity.get_bit(output.len()) {
845+
output.push(false);
846+
return;
847+
}
848+
}
849+
match RawJsonb::new(v).is_interval() {
850+
Ok(res) => output.push(res),
851+
Err(_) => output.push(false),
852+
}
853+
}),
854+
);
855+
788856
registry.register_combine_nullable_1_arg::<VariantType, IntervalType, _, _>(
789857
"as_interval",
790858
|_, _| FunctionDomain::Full,
@@ -1199,6 +1267,10 @@ pub fn register(registry: &mut FunctionRegistry) {
11991267
output.push_null();
12001268
return;
12011269
}
1270+
if let Ok(Some(date)) = raw_jsonb.as_date() {
1271+
output.push(date.value);
1272+
return;
1273+
}
12021274
match raw_jsonb
12031275
.as_str()
12041276
.map_err(|e| format!("{e}"))
@@ -1233,6 +1305,10 @@ pub fn register(registry: &mut FunctionRegistry) {
12331305
}
12341306
}
12351307
let raw_jsonb = RawJsonb::new(val);
1308+
if let Ok(Some(date)) = raw_jsonb.as_date() {
1309+
output.push(date.value);
1310+
return;
1311+
}
12361312
match raw_jsonb
12371313
.as_str()
12381314
.map_err(|e| format!("{e}"))
@@ -1268,6 +1344,10 @@ pub fn register(registry: &mut FunctionRegistry) {
12681344
output.push_null();
12691345
return;
12701346
}
1347+
if let Ok(Some(ts)) = raw_jsonb.as_timestamp() {
1348+
output.push(ts.value);
1349+
return;
1350+
}
12711351
match raw_jsonb
12721352
.as_str()
12731353
.map_err(|e| format!("{e}"))
@@ -1301,6 +1381,10 @@ pub fn register(registry: &mut FunctionRegistry) {
13011381
}
13021382

13031383
let raw_jsonb = RawJsonb::new(val);
1384+
if let Ok(Some(ts)) = raw_jsonb.as_timestamp() {
1385+
output.push(ts.value);
1386+
return;
1387+
}
13041388
match raw_jsonb
13051389
.as_str()
13061390
.map_err(|e| format!("{e}"))

src/query/functions/tests/it/scalars/testdata/cast.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ output : '-1'
231231
ast : CAST(1.1 AS VARIANT)
232232
raw expr : CAST(1.1 AS Variant)
233233
checked expr : CAST<Decimal(2, 1)>(1.1_d128(2,1) AS Variant)
234-
optimized expr : 0x2000000020000009603ff199999999999a
234+
optimized expr : 0x2000000020000013700000000000000000000000000000000b0201
235235
output type : Variant
236236
output domain : Undefined
237237
output : '1.1'
@@ -266,7 +266,7 @@ output : '[0,"a"]'
266266
ast : CAST(to_timestamp(1000000) AS VARIANT)
267267
raw expr : CAST(to_timestamp(1000000) AS Variant)
268268
checked expr : CAST<Timestamp>(CAST<Int64>(CAST<UInt32>(1000000_u32 AS Int64) AS Timestamp) AS Variant)
269-
optimized expr : 0x200000001000001a313937302d30312d31322031333a34363a34302e303030303030
269+
optimized expr : 0x200000006000000920000000e8d4a51000
270270
output type : Variant
271271
output domain : Undefined
272272
output : '"1970-01-12 13:46:40.000000"'
@@ -2409,7 +2409,7 @@ output : '-1'
24092409
ast : TRY_CAST(1.1 AS VARIANT)
24102410
raw expr : TRY_CAST(1.1 AS Variant)
24112411
checked expr : TRY_CAST<Decimal(2, 1)>(1.1_d128(2,1) AS Variant NULL)
2412-
optimized expr : 0x2000000020000009603ff199999999999a
2412+
optimized expr : 0x2000000020000013700000000000000000000000000000000b0201
24132413
output type : Variant NULL
24142414
output domain : Undefined
24152415
output : '1.1'
@@ -2445,7 +2445,7 @@ output : '[0,"a"]'
24452445
ast : TRY_CAST(to_timestamp(1000000) AS VARIANT)
24462446
raw expr : TRY_CAST(to_timestamp(1000000) AS Variant)
24472447
checked expr : TRY_CAST<Timestamp>(CAST<Int64>(CAST<UInt32>(1000000_u32 AS Int64) AS Timestamp) AS Variant NULL)
2448-
optimized expr : 0x200000001000001a313937302d30312d31322031333a34363a34302e303030303030
2448+
optimized expr : 0x200000006000000920000000e8d4a51000
24492449
output type : Variant NULL
24502450
output domain : Undefined
24512451
output : '"1970-01-12 13:46:40.000000"'

src/query/functions/tests/it/scalars/testdata/function_list.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,12 +2061,18 @@ Functions overloads:
20612061
1 instr(String NULL, String NULL) :: UInt64 NULL
20622062
0 is_array(Variant) :: Boolean
20632063
1 is_array(Variant NULL) :: Boolean NULL
2064+
0 is_binary(Variant) :: Boolean
2065+
1 is_binary(Variant NULL) :: Boolean NULL
20642066
0 is_boolean(Variant) :: Boolean
20652067
1 is_boolean(Variant NULL) :: Boolean NULL
2068+
0 is_date(Variant) :: Boolean
2069+
1 is_date(Variant NULL) :: Boolean NULL
20662070
0 is_float(Variant) :: Boolean
20672071
1 is_float(Variant NULL) :: Boolean NULL
20682072
0 is_integer(Variant) :: Boolean
20692073
1 is_integer(Variant NULL) :: Boolean NULL
2074+
0 is_interval(Variant) :: Boolean
2075+
1 is_interval(Variant NULL) :: Boolean NULL
20702076
0 is_not_error(T0) :: Boolean
20712077
0 is_not_null(NULL) :: Boolean
20722078
1 is_not_null(T0 NULL) :: Boolean
@@ -2076,6 +2082,8 @@ Functions overloads:
20762082
1 is_object(Variant NULL) :: Boolean NULL
20772083
0 is_string(Variant) :: Boolean
20782084
1 is_string(Variant NULL) :: Boolean NULL
2085+
0 is_timestamp(Variant) :: Boolean
2086+
1 is_timestamp(Variant NULL) :: Boolean NULL
20792087
0 is_true(Boolean) :: Boolean
20802088
1 is_true(Boolean NULL) :: Boolean
20812089
0 jaro_winkler(String, String) :: Float64

src/query/functions/tests/it/scalars/testdata/variant.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,42 @@ output domain : Undefined
10641064
output : '{"a":"b"}'
10651065

10661066

1067+
ast : as_binary(to_binary('abcd')::variant)
1068+
raw expr : as_binary(CAST(to_binary('abcd') AS Variant))
1069+
checked expr : as_binary<Variant>(CAST<Binary>(CAST<String>("abcd" AS Binary) AS Variant))
1070+
optimized expr : 61626364
1071+
output type : Binary NULL
1072+
output domain : Undefined
1073+
output : 61626364
1074+
1075+
1076+
ast : as_date(to_date('2025-10-11')::variant)
1077+
raw expr : as_date(CAST(to_date('2025-10-11') AS Variant))
1078+
checked expr : as_date<Variant>(CAST<Date>(CAST<String>("2025-10-11" AS Date) AS Variant))
1079+
optimized expr : 20372
1080+
output type : Date NULL
1081+
output domain : {20372..=20372}
1082+
output : '2025-10-11'
1083+
1084+
1085+
ast : as_timestamp(to_timestamp('2025-05-01 10:00:00')::variant)
1086+
raw expr : as_timestamp(CAST(to_timestamp('2025-05-01 10:00:00') AS Variant))
1087+
checked expr : as_timestamp<Variant>(CAST<Timestamp>(CAST<String>("2025-05-01 10:00:00" AS Timestamp) AS Variant))
1088+
optimized expr : 1746093600000000
1089+
output type : Timestamp NULL
1090+
output domain : {1746093600000000..=1746093600000000}
1091+
output : '2025-05-01 10:00:00.000000'
1092+
1093+
1094+
ast : as_interval(to_interval('1 year 2 month')::variant)
1095+
raw expr : as_interval(CAST(to_interval('1 year 2 month') AS Variant))
1096+
checked expr : as_interval<Variant>(CAST<Interval>(CAST<String>("1 year 2 month" AS Interval) AS Variant))
1097+
optimized expr : 1 year 2 months
1098+
output type : Interval NULL
1099+
output domain : SimpleDomain { min: months_days_micros(1109194275199700726309615304704), max: months_days_micros(1109194275199700726309615304704) }
1100+
output : '1 year 2 months'
1101+
1102+
10671103
ast : as_boolean(parse_json(s))
10681104
raw expr : as_boolean(parse_json(s::String))
10691105
checked expr : as_boolean<Variant>(CAST<String>(s AS Variant))
@@ -1346,6 +1382,42 @@ output domain : {TRUE}
13461382
output : true
13471383

13481384

1385+
ast : is_binary(to_binary('abcd')::variant)
1386+
raw expr : is_binary(CAST(to_binary('abcd') AS Variant))
1387+
checked expr : is_binary<Variant>(CAST<Binary>(CAST<String>("abcd" AS Binary) AS Variant))
1388+
optimized expr : true
1389+
output type : Boolean
1390+
output domain : {TRUE}
1391+
output : true
1392+
1393+
1394+
ast : is_date(to_date('2025-10-11')::variant)
1395+
raw expr : is_date(CAST(to_date('2025-10-11') AS Variant))
1396+
checked expr : is_date<Variant>(CAST<Date>(CAST<String>("2025-10-11" AS Date) AS Variant))
1397+
optimized expr : true
1398+
output type : Boolean
1399+
output domain : {TRUE}
1400+
output : true
1401+
1402+
1403+
ast : is_timestamp(to_timestamp('2025-05-01 10:00:00')::variant)
1404+
raw expr : is_timestamp(CAST(to_timestamp('2025-05-01 10:00:00') AS Variant))
1405+
checked expr : is_timestamp<Variant>(CAST<Timestamp>(CAST<String>("2025-05-01 10:00:00" AS Timestamp) AS Variant))
1406+
optimized expr : true
1407+
output type : Boolean
1408+
output domain : {TRUE}
1409+
output : true
1410+
1411+
1412+
ast : is_interval(to_interval('1 year 2 month')::variant)
1413+
raw expr : is_interval(CAST(to_interval('1 year 2 month') AS Variant))
1414+
checked expr : is_interval<Variant>(CAST<Interval>(CAST<String>("1 year 2 month" AS Interval) AS Variant))
1415+
optimized expr : true
1416+
output type : Boolean
1417+
output domain : {TRUE}
1418+
output : true
1419+
1420+
13491421
ast : is_null_value(parse_json(s))
13501422
raw expr : is_null_value(parse_json(s::String))
13511423
checked expr : is_null_value<Variant>(CAST<String>(s AS Variant))

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,18 @@ fn test_as_type(file: &mut impl Write) {
510510
run_ast(file, "as_array(parse_json('{\"a\":\"b\"}'))", &[]);
511511
run_ast(file, "as_object(parse_json('[1,2,3]'))", &[]);
512512
run_ast(file, "as_object(parse_json('{\"a\":\"b\"}'))", &[]);
513+
run_ast(file, "as_binary(to_binary('abcd')::variant)", &[]);
514+
run_ast(file, "as_date(to_date('2025-10-11')::variant)", &[]);
515+
run_ast(
516+
file,
517+
"as_timestamp(to_timestamp('2025-05-01 10:00:00')::variant)",
518+
&[],
519+
);
520+
run_ast(
521+
file,
522+
"as_interval(to_interval('1 year 2 month')::variant)",
523+
&[],
524+
);
513525

514526
let columns = &[(
515527
"s",
@@ -546,6 +558,18 @@ fn test_is_type(file: &mut impl Write) {
546558
run_ast(file, "is_array(parse_json('{\"a\":\"b\"}'))", &[]);
547559
run_ast(file, "is_object(parse_json('[1,2,3]'))", &[]);
548560
run_ast(file, "is_object(parse_json('{\"a\":\"b\"}'))", &[]);
561+
run_ast(file, "is_binary(to_binary('abcd')::variant)", &[]);
562+
run_ast(file, "is_date(to_date('2025-10-11')::variant)", &[]);
563+
run_ast(
564+
file,
565+
"is_timestamp(to_timestamp('2025-05-01 10:00:00')::variant)",
566+
&[],
567+
);
568+
run_ast(
569+
file,
570+
"is_interval(to_interval('1 year 2 month')::variant)",
571+
&[],
572+
);
549573

550574
let columns = &[(
551575
"s",

src/query/service/src/servers/mysql/writers/query_result_writer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl<'a, W: AsyncWrite + Send + Unpin> DFQueryResultWriter<'a, W> {
191191
DataType::Geometry => Ok(ColumnType::MYSQL_TYPE_GEOMETRY),
192192
DataType::Geography => Ok(ColumnType::MYSQL_TYPE_GEOMETRY),
193193
DataType::Decimal(_) => Ok(ColumnType::MYSQL_TYPE_DECIMAL),
194+
DataType::Interval => Ok(ColumnType::MYSQL_TYPE_VARCHAR),
194195
_ => Err(ErrorCode::Unimplemented(format!(
195196
"Unsupported column type:{:?}",
196197
field.data_type()

0 commit comments

Comments
 (0)