Skip to content

Commit 85f4771

Browse files
authored
feat(query): to_binary function support variant, bitmap, geometry, geography types (#17026)
* feat(query): `to_binary` function support variant, bitmap, geometry, geography types * add tests * remove some dict tests
1 parent 3a9f404 commit 85f4771

File tree

9 files changed

+353
-30
lines changed

9 files changed

+353
-30
lines changed

src/common/column/src/binary/fmt.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,3 @@ impl Debug for BinaryColumn {
3939
.finish()
4040
}
4141
}
42-
43-
// impl Debug for BinaryColumn {
44-
// fn fmt(&self, f: &mut Formatter) -> Result {
45-
// let writer = |f: &mut Formatter, index| write_value(self, index, f);
46-
// write!(f, "BinaryColumn")?;
47-
// write_vec(f, writer, None, self.len(), "None", false)
48-
// }
49-
// }

src/query/functions/src/scalars/binary.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ use databend_common_expression::types::string::StringColumnBuilder;
2525
use databend_common_expression::types::AnyType;
2626
use databend_common_expression::types::BinaryType;
2727
use databend_common_expression::types::Bitmap;
28+
use databend_common_expression::types::BitmapType;
2829
use databend_common_expression::types::DataType;
30+
use databend_common_expression::types::GeographyType;
31+
use databend_common_expression::types::GeometryType;
2932
use databend_common_expression::types::NumberDataType;
3033
use databend_common_expression::types::NumberType;
3134
use databend_common_expression::types::StringType;
3235
use databend_common_expression::types::UInt8Type;
36+
use databend_common_expression::types::VariantType;
3337
use databend_common_expression::vectorize_1_arg;
3438
use databend_common_expression::Column;
3539
use databend_common_expression::EvalContext;
@@ -63,6 +67,90 @@ pub fn register(registry: &mut FunctionRegistry) {
6367
error_to_null(eval_binary_to_string),
6468
);
6569

70+
registry.register_passthrough_nullable_1_arg::<VariantType, BinaryType, _, _>(
71+
"to_binary",
72+
|_, _| FunctionDomain::Full,
73+
|val, _| match val {
74+
Value::Scalar(val) => Value::Scalar(val.to_vec()),
75+
Value::Column(col) => Value::Column(col),
76+
},
77+
);
78+
79+
registry.register_combine_nullable_1_arg::<VariantType, BinaryType, _, _>(
80+
"try_to_binary",
81+
|_, _| FunctionDomain::Full,
82+
|val, _| match val {
83+
Value::Scalar(val) => Value::Scalar(Some(val.to_vec())),
84+
Value::Column(col) => {
85+
let validity = Bitmap::new_constant(true, col.len());
86+
Value::Column(NullableColumn::new(col, validity))
87+
}
88+
},
89+
);
90+
91+
registry.register_passthrough_nullable_1_arg::<BitmapType, BinaryType, _, _>(
92+
"to_binary",
93+
|_, _| FunctionDomain::Full,
94+
|val, _| match val {
95+
Value::Scalar(val) => Value::Scalar(val.to_vec()),
96+
Value::Column(col) => Value::Column(col),
97+
},
98+
);
99+
100+
registry.register_combine_nullable_1_arg::<BitmapType, BinaryType, _, _>(
101+
"try_to_binary",
102+
|_, _| FunctionDomain::Full,
103+
|val, _| match val {
104+
Value::Scalar(val) => Value::Scalar(Some(val.to_vec())),
105+
Value::Column(col) => {
106+
let validity = Bitmap::new_constant(true, col.len());
107+
Value::Column(NullableColumn::new(col, validity))
108+
}
109+
},
110+
);
111+
112+
registry.register_passthrough_nullable_1_arg::<GeometryType, BinaryType, _, _>(
113+
"to_binary",
114+
|_, _| FunctionDomain::Full,
115+
|val, _| match val {
116+
Value::Scalar(val) => Value::Scalar(val.to_vec()),
117+
Value::Column(col) => Value::Column(col),
118+
},
119+
);
120+
121+
registry.register_combine_nullable_1_arg::<GeometryType, BinaryType, _, _>(
122+
"try_to_binary",
123+
|_, _| FunctionDomain::Full,
124+
|val, _| match val {
125+
Value::Scalar(val) => Value::Scalar(Some(val.to_vec())),
126+
Value::Column(col) => {
127+
let validity = Bitmap::new_constant(true, col.len());
128+
Value::Column(NullableColumn::new(col, validity))
129+
}
130+
},
131+
);
132+
133+
registry.register_passthrough_nullable_1_arg::<GeographyType, BinaryType, _, _>(
134+
"to_binary",
135+
|_, _| FunctionDomain::Full,
136+
|val, _| match val {
137+
Value::Scalar(val) => Value::Scalar(val.0.to_vec()),
138+
Value::Column(col) => Value::Column(col.0),
139+
},
140+
);
141+
142+
registry.register_combine_nullable_1_arg::<GeographyType, BinaryType, _, _>(
143+
"try_to_binary",
144+
|_, _| FunctionDomain::Full,
145+
|val, _| match val {
146+
Value::Scalar(val) => Value::Scalar(Some(val.0.to_vec())),
147+
Value::Column(col) => {
148+
let validity = Bitmap::new_constant(true, col.len());
149+
Value::Column(NullableColumn::new(col.0, validity))
150+
}
151+
},
152+
);
153+
66154
registry.register_passthrough_nullable_1_arg::<StringType, BinaryType, _, _>(
67155
"to_binary",
68156
|_, _| FunctionDomain::Full,

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ fn test_binary() {
3333
for is_try in [false, true] {
3434
test_from_base64(file, is_try);
3535
test_from_hex(file, is_try);
36+
test_to_binary(file, is_try);
3637
}
3738
}
3839

@@ -91,3 +92,42 @@ fn test_from_base64(file: &mut impl Write, is_try: bool) {
9192
)]);
9293
run_ast(file, format!("{prefix}from_base64('!@#')"), &[]);
9394
}
95+
96+
fn test_to_binary(file: &mut impl Write, is_try: bool) {
97+
let prefix = if is_try { "TRY_" } else { "" };
98+
99+
run_ast(
100+
file,
101+
format!("{prefix}to_binary(parse_json('{{\"k1\":\"val\",\"k2\":100}}'))"),
102+
&[],
103+
);
104+
run_ast(file, format!("{prefix}to_binary(parse_json('10'))"), &[]);
105+
run_ast(file, format!("{prefix}to_binary(parse_json('123456'))"), &[
106+
]);
107+
run_ast(
108+
file,
109+
format!("{prefix}to_binary(parse_json('\"abcd\"'))"),
110+
&[],
111+
);
112+
run_ast(file, format!("{prefix}to_binary(to_bitmap('1,2,3'))"), &[]);
113+
run_ast(
114+
file,
115+
format!("{prefix}to_binary(to_bitmap('100,25,50,700'))"),
116+
&[],
117+
);
118+
run_ast(
119+
file,
120+
format!("{prefix}to_binary(st_geometryfromwkt('SRID=4326;POINT(1.0 2.0)'))"),
121+
&[],
122+
);
123+
run_ast(
124+
file,
125+
format!("{prefix}to_binary(st_geometryfromwkb(unhex('0101000020797f000066666666a9cb17411f85ebc19e325641')))"),
126+
&[],
127+
);
128+
run_ast(
129+
file,
130+
format!("{prefix}to_binary(st_geographyfromewkt('SRID=4326;POINT(-122.35 37.55)'))"),
131+
&[],
132+
);
133+
}

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

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ output : 5
99

1010
ast : length(to_binary(NULL))
1111
raw expr : length(to_binary(NULL))
12-
checked expr : length<Binary NULL>(to_binary<String NULL>(CAST(NULL AS String NULL)))
12+
checked expr : length<Binary NULL>(to_binary<Variant NULL>(CAST(NULL AS Variant NULL)))
1313
optimized expr : NULL
1414
output type : UInt64 NULL
1515
output domain : {NULL}
@@ -203,6 +203,87 @@ evaluation (internal):
203203
+--------+------------------------------------------------+
204204

205205

206+
ast : to_binary(parse_json('{"k1":"val","k2":100}'))
207+
raw expr : to_binary(parse_json('{"k1":"val","k2":100}'))
208+
checked expr : to_binary<Variant>(parse_json<String>("{\"k1\":\"val\",\"k2\":100}"))
209+
optimized expr : 40000002100000021000000210000003200000026B316B3276616C5064
210+
output type : Binary
211+
output domain : Undefined
212+
output : 40000002100000021000000210000003200000026B316B3276616C5064
213+
214+
215+
ast : to_binary(parse_json('10'))
216+
raw expr : to_binary(parse_json('10'))
217+
checked expr : to_binary<Variant>(parse_json<String>("10"))
218+
optimized expr : 2000000020000002500A
219+
output type : Binary
220+
output domain : Undefined
221+
output : 2000000020000002500A
222+
223+
224+
ast : to_binary(parse_json('123456'))
225+
raw expr : to_binary(parse_json('123456'))
226+
checked expr : to_binary<Variant>(parse_json<String>("123456"))
227+
optimized expr : 2000000020000005500001E240
228+
output type : Binary
229+
output domain : Undefined
230+
output : 2000000020000005500001E240
231+
232+
233+
ast : to_binary(parse_json('"abcd"'))
234+
raw expr : to_binary(parse_json('"abcd"'))
235+
checked expr : to_binary<Variant>(parse_json<String>("\"abcd\""))
236+
optimized expr : 200000001000000461626364
237+
output type : Binary
238+
output domain : Undefined
239+
output : 200000001000000461626364
240+
241+
242+
ast : to_binary(to_bitmap('1,2,3'))
243+
raw expr : to_binary(to_bitmap('1,2,3'))
244+
checked expr : to_binary<Bitmap>(to_bitmap<String>("1,2,3"))
245+
optimized expr : 0100000000000000000000003A300000010000000000020010000000010002000300
246+
output type : Binary
247+
output domain : Undefined
248+
output : 0100000000000000000000003A300000010000000000020010000000010002000300
249+
250+
251+
ast : to_binary(to_bitmap('100,25,50,700'))
252+
raw expr : to_binary(to_bitmap('100,25,50,700'))
253+
checked expr : to_binary<Bitmap>(to_bitmap<String>("100,25,50,700"))
254+
optimized expr : 0100000000000000000000003A300000010000000000030010000000190032006400BC02
255+
output type : Binary
256+
output domain : Undefined
257+
output : 0100000000000000000000003A300000010000000000030010000000190032006400BC02
258+
259+
260+
ast : to_binary(st_geometryfromwkt('SRID=4326;POINT(1.0 2.0)'))
261+
raw expr : to_binary(st_geometryfromwkt('SRID=4326;POINT(1.0 2.0)'))
262+
checked expr : to_binary<Geometry>(st_geometryfromwkt<String>("SRID=4326;POINT(1.0 2.0)"))
263+
optimized expr : 0101000020E6100000000000000000F03F0000000000000040
264+
output type : Binary
265+
output domain : Undefined
266+
output : 0101000020E6100000000000000000F03F0000000000000040
267+
268+
269+
ast : to_binary(st_geometryfromwkb(unhex('0101000020797f000066666666a9cb17411f85ebc19e325641')))
270+
raw expr : to_binary(st_geometryfromwkb(unhex('0101000020797f000066666666a9cb17411f85ebc19e325641')))
271+
checked expr : to_binary<Geometry>(st_geometryfromwkb<Binary>(from_hex<String>("0101000020797f000066666666a9cb17411f85ebc19e325641")))
272+
optimized expr : 0101000020797F000066666666A9CB17411F85EBC19E325641
273+
output type : Binary
274+
output domain : Undefined
275+
output : 0101000020797F000066666666A9CB17411F85EBC19E325641
276+
277+
278+
ast : to_binary(st_geographyfromewkt('SRID=4326;POINT(-122.35 37.55)'))
279+
raw expr : to_binary(st_geographyfromewkt('SRID=4326;POINT(-122.35 37.55)'))
280+
checked expr : to_binary<Geography>(st_geographyfromewkt<String>("SRID=4326;POINT(-122.35 37.55)"))
281+
optimized expr : 0101000020E61000006666666666965EC06666666666C64240
282+
output type : Binary
283+
output domain : Undefined
284+
output : 0101000020E61000006666666666965EC06666666666C64240
285+
286+
206287
ast : TRY_from_base64('QWJj')::String
207288
raw expr : CAST(TRY_from_base64('QWJj') AS String)
208289
checked expr : CAST(try_from_base64<String>("QWJj") AS String)
@@ -291,3 +372,84 @@ evaluation (internal):
291372
+--------+------------------------------------------------+
292373

293374

375+
ast : TRY_to_binary(parse_json('{"k1":"val","k2":100}'))
376+
raw expr : TRY_to_binary(parse_json('{"k1":"val","k2":100}'))
377+
checked expr : try_to_binary<Variant>(parse_json<String>("{\"k1\":\"val\",\"k2\":100}"))
378+
optimized expr : 40000002100000021000000210000003200000026B316B3276616C5064
379+
output type : Binary NULL
380+
output domain : Undefined
381+
output : 40000002100000021000000210000003200000026B316B3276616C5064
382+
383+
384+
ast : TRY_to_binary(parse_json('10'))
385+
raw expr : TRY_to_binary(parse_json('10'))
386+
checked expr : try_to_binary<Variant>(parse_json<String>("10"))
387+
optimized expr : 2000000020000002500A
388+
output type : Binary NULL
389+
output domain : Undefined
390+
output : 2000000020000002500A
391+
392+
393+
ast : TRY_to_binary(parse_json('123456'))
394+
raw expr : TRY_to_binary(parse_json('123456'))
395+
checked expr : try_to_binary<Variant>(parse_json<String>("123456"))
396+
optimized expr : 2000000020000005500001E240
397+
output type : Binary NULL
398+
output domain : Undefined
399+
output : 2000000020000005500001E240
400+
401+
402+
ast : TRY_to_binary(parse_json('"abcd"'))
403+
raw expr : TRY_to_binary(parse_json('"abcd"'))
404+
checked expr : try_to_binary<Variant>(parse_json<String>("\"abcd\""))
405+
optimized expr : 200000001000000461626364
406+
output type : Binary NULL
407+
output domain : Undefined
408+
output : 200000001000000461626364
409+
410+
411+
ast : TRY_to_binary(to_bitmap('1,2,3'))
412+
raw expr : TRY_to_binary(to_bitmap('1,2,3'))
413+
checked expr : try_to_binary<Bitmap>(to_bitmap<String>("1,2,3"))
414+
optimized expr : 0100000000000000000000003A300000010000000000020010000000010002000300
415+
output type : Binary NULL
416+
output domain : Undefined
417+
output : 0100000000000000000000003A300000010000000000020010000000010002000300
418+
419+
420+
ast : TRY_to_binary(to_bitmap('100,25,50,700'))
421+
raw expr : TRY_to_binary(to_bitmap('100,25,50,700'))
422+
checked expr : try_to_binary<Bitmap>(to_bitmap<String>("100,25,50,700"))
423+
optimized expr : 0100000000000000000000003A300000010000000000030010000000190032006400BC02
424+
output type : Binary NULL
425+
output domain : Undefined
426+
output : 0100000000000000000000003A300000010000000000030010000000190032006400BC02
427+
428+
429+
ast : TRY_to_binary(st_geometryfromwkt('SRID=4326;POINT(1.0 2.0)'))
430+
raw expr : TRY_to_binary(st_geometryfromwkt('SRID=4326;POINT(1.0 2.0)'))
431+
checked expr : try_to_binary<Geometry>(st_geometryfromwkt<String>("SRID=4326;POINT(1.0 2.0)"))
432+
optimized expr : 0101000020E6100000000000000000F03F0000000000000040
433+
output type : Binary NULL
434+
output domain : Undefined
435+
output : 0101000020E6100000000000000000F03F0000000000000040
436+
437+
438+
ast : TRY_to_binary(st_geometryfromwkb(unhex('0101000020797f000066666666a9cb17411f85ebc19e325641')))
439+
raw expr : TRY_to_binary(st_geometryfromwkb(unhex('0101000020797f000066666666a9cb17411f85ebc19e325641')))
440+
checked expr : try_to_binary<Geometry>(st_geometryfromwkb<Binary>(from_hex<String>("0101000020797f000066666666a9cb17411f85ebc19e325641")))
441+
optimized expr : 0101000020797F000066666666A9CB17411F85EBC19E325641
442+
output type : Binary NULL
443+
output domain : Undefined
444+
output : 0101000020797F000066666666A9CB17411F85EBC19E325641
445+
446+
447+
ast : TRY_to_binary(st_geographyfromewkt('SRID=4326;POINT(-122.35 37.55)'))
448+
raw expr : TRY_to_binary(st_geographyfromewkt('SRID=4326;POINT(-122.35 37.55)'))
449+
checked expr : try_to_binary<Geography>(st_geographyfromewkt<String>("SRID=4326;POINT(-122.35 37.55)"))
450+
optimized expr : 0101000020E61000006666666666965EC06666666666C64240
451+
output type : Binary NULL
452+
output domain : Undefined
453+
output : 0101000020E61000006666666666965EC06666666666C64240
454+
455+

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4118,7 +4118,7 @@ output : C39FF09F9880E5B1B1
41184118

41194119
ast : TRY_CAST(NULL AS BINARY)
41204120
raw expr : TRY_CAST(NULL AS Binary)
4121-
checked expr : try_to_binary<String NULL>(CAST(NULL AS String NULL))
4121+
checked expr : try_to_binary<Variant NULL>(CAST(NULL AS Variant NULL))
41224122
optimized expr : NULL
41234123
output type : Binary NULL
41244124
output domain : {NULL}
@@ -4229,7 +4229,7 @@ output : 'ß😀山'
42294229

42304230
ast : TRY_CAST(TRY_CAST(NULL AS BINARY) AS STRING)
42314231
raw expr : TRY_CAST(TRY_CAST(NULL AS Binary) AS String)
4232-
checked expr : try_to_string<Binary NULL>(try_to_binary<String NULL>(CAST(NULL AS String NULL)))
4232+
checked expr : try_to_string<Binary NULL>(try_to_binary<Variant NULL>(CAST(NULL AS Variant NULL)))
42334233
optimized expr : NULL
42344234
output type : String NULL
42354235
output domain : {NULL}

0 commit comments

Comments
 (0)