Skip to content

Commit e2c3f09

Browse files
committed
chore(query): add function v2 repeat
1 parent af7b1cb commit e2c3f09

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/query/functions-v2/src/scalars/string.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use common_expression::types::GenericMap;
2424
use common_expression::types::NumberType;
2525
use common_expression::types::StringType;
2626
use common_expression::vectorize_with_builder_1_arg;
27+
use common_expression::vectorize_with_builder_2_arg;
2728
use common_expression::FunctionProperty;
2829
use common_expression::FunctionRegistry;
2930
use common_expression::Value;
@@ -511,6 +512,26 @@ pub fn register(registry: &mut FunctionRegistry) {
511512
),
512513
);
513514

515+
const MAX_REPEAT_TIMES: u64 = 1000000;
516+
registry.register_passthrough_nullable_2_arg::<StringType, NumberType<u64>, StringType, _, _>(
517+
"repeat",
518+
FunctionProperty::default(),
519+
|_, _| None,
520+
vectorize_with_builder_2_arg::<StringType, NumberType<u64>, StringType>(
521+
|a, times, builder| {
522+
if times > MAX_REPEAT_TIMES {
523+
return Err(format!(
524+
"Too many times to repeat: ({}), maximum is: {}",
525+
times, MAX_REPEAT_TIMES
526+
));
527+
}
528+
(0..times).for_each(|_| builder.put_slice(a));
529+
builder.commit_row();
530+
Ok(())
531+
},
532+
),
533+
);
534+
514535
registry.register_passthrough_nullable_1_arg::<StringType, StringType, _, _>(
515536
"unhex",
516537
FunctionProperty::default(),

src/query/functions-v2/tests/it/scalars/string.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ fn test_string() {
5555
test_char(file);
5656
test_soundex(file);
5757
test_ord(file);
58+
test_repeat(file);
5859
}
5960

6061
fn test_upper(file: &mut impl Write) {
@@ -686,3 +687,10 @@ fn test_ord(file: &mut impl Write) {
686687
run_ast(file, "ord('早ab')", &[]);
687688
run_ast(file, "ord('💖')", &[]);
688689
}
690+
691+
fn test_repeat(file: &mut impl Write) {
692+
run_ast(file, "repeat('3', NULL)", &[]);
693+
run_ast(file, "repeat('3', 5)", &[]);
694+
let table = [("a", DataType::String, Column::from_data(&["a", "b", "c"]))];
695+
run_ast(file, "repeat(a, 3)", &table);
696+
}

src/query/functions-v2/tests/it/scalars/testdata/string.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,3 +2723,44 @@ output domain : Unknown
27232723
output : 4036989590
27242724

27252725

2726+
ast : repeat('3', NULL)
2727+
raw expr : repeat("3", NULL)
2728+
checked expr : repeat<String NULL, NULL>(CAST("3" AS String NULL), NULL)
2729+
optimized expr : NULL
2730+
output type : NULL
2731+
output domain : {NULL}
2732+
output : NULL
2733+
2734+
2735+
ast : repeat('3', 5)
2736+
raw expr : repeat("3", 5_u8)
2737+
checked expr : repeat<String, UInt64>("3", CAST(5_u8 AS UInt64))
2738+
optimized expr : "33333"
2739+
output type : String
2740+
output domain : Unknown
2741+
output : "33333"
2742+
2743+
2744+
ast : repeat(a, 3)
2745+
raw expr : repeat(ColumnRef(0)::String, 3_u8)
2746+
checked expr : repeat<String, UInt64>(ColumnRef(0), CAST(3_u8 AS UInt64))
2747+
optimized expr : repeat<String, UInt64>(ColumnRef(0), 3_u64)
2748+
evaluation:
2749+
+--------+-------------+---------+
2750+
| | a | Output |
2751+
+--------+-------------+---------+
2752+
| Type | String | String |
2753+
| Domain | {"a"..="c"} | Unknown |
2754+
| Row 0 | "a" | "aaa" |
2755+
| Row 1 | "b" | "bbb" |
2756+
| Row 2 | "c" | "ccc" |
2757+
+--------+-------------+---------+
2758+
evaluation (internal):
2759+
+--------+------------------------------------------------------------------------------------+
2760+
| Column | Data |
2761+
+--------+------------------------------------------------------------------------------------+
2762+
| a | StringColumn { data: [97, 98, 99], offsets: [0, 1, 2, 3] } |
2763+
| Output | StringColumn { data: [97, 97, 97, 98, 98, 98, 99, 99, 99], offsets: [0, 3, 6, 9] } |
2764+
+--------+------------------------------------------------------------------------------------+
2765+
2766+

0 commit comments

Comments
 (0)