Skip to content

Commit f7df2fe

Browse files
authored
Merge pull request #7571 from sundy-li/function-v2-repeat
feat(query): add function v2 repeat
2 parents 39112ff + f622dc4 commit f7df2fe

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-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;
@@ -534,6 +535,26 @@ pub fn register(registry: &mut FunctionRegistry) {
534535
),
535536
);
536537

538+
const MAX_REPEAT_TIMES: u64 = 1000000;
539+
registry.register_passthrough_nullable_2_arg::<StringType, NumberType<u64>, StringType, _, _>(
540+
"repeat",
541+
FunctionProperty::default(),
542+
|_, _| None,
543+
vectorize_with_builder_2_arg::<StringType, NumberType<u64>, StringType>(
544+
|a, times, builder| {
545+
if times > MAX_REPEAT_TIMES {
546+
return Err(format!(
547+
"Too many times to repeat: ({}), maximum is: {}",
548+
times, MAX_REPEAT_TIMES
549+
));
550+
}
551+
(0..times).for_each(|_| builder.put_slice(a));
552+
builder.commit_row();
553+
Ok(())
554+
},
555+
),
556+
);
557+
537558
registry.register_passthrough_nullable_1_arg::<StringType, StringType, _, _>(
538559
"unhex",
539560
FunctionProperty::default(),

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

Lines changed: 9 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
test_insert(file);
5960
}
6061

@@ -688,6 +689,14 @@ fn test_ord(file: &mut impl Write) {
688689
run_ast(file, "ord('💖')", &[]);
689690
}
690691

692+
fn test_repeat(file: &mut impl Write) {
693+
run_ast(file, "repeat('3', NULL)", &[]);
694+
run_ast(file, "repeat('3', 5)", &[]);
695+
run_ast(file, "repeat('3', 1000001)", &[]);
696+
let table = [("a", DataType::String, Column::from_data(&["a", "b", "c"]))];
697+
run_ast(file, "repeat(a, 3)", &table);
698+
}
699+
691700
fn test_insert(file: &mut impl Write) {
692701
run_ast(file, "insert('Quadratic', 3, 4, 'What', 4)", &[]);
693702
run_ast(file, "insert('Quadratic', 3, 4)", &[]);

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,55 @@ 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+
error:
2745+
--> SQL:1:1
2746+
|
2747+
1 | repeat('3', 1000001)
2748+
| ^^^^^^^^^^^^^^^^^^^^ Too many times to repeat: (1000001), maximum is: 1000000
2749+
2750+
2751+
2752+
ast : repeat(a, 3)
2753+
raw expr : repeat(ColumnRef(0)::String, 3_u8)
2754+
checked expr : repeat<String, UInt64>(ColumnRef(0), CAST(3_u8 AS UInt64))
2755+
optimized expr : repeat<String, UInt64>(ColumnRef(0), 3_u64)
2756+
evaluation:
2757+
+--------+-------------+---------+
2758+
| | a | Output |
2759+
+--------+-------------+---------+
2760+
| Type | String | String |
2761+
| Domain | {"a"..="c"} | Unknown |
2762+
| Row 0 | "a" | "aaa" |
2763+
| Row 1 | "b" | "bbb" |
2764+
| Row 2 | "c" | "ccc" |
2765+
+--------+-------------+---------+
2766+
evaluation (internal):
2767+
+--------+------------------------------------------------------------------------------------+
2768+
| Column | Data |
2769+
+--------+------------------------------------------------------------------------------------+
2770+
| a | StringColumn { data: [97, 98, 99], offsets: [0, 1, 2, 3] } |
2771+
| Output | StringColumn { data: [97, 97, 97, 98, 98, 98, 99, 99, 99], offsets: [0, 3, 6, 9] } |
2772+
+--------+------------------------------------------------------------------------------------+
2773+
2774+
27262775
error:
27272776
--> SQL:1:1
27282777
|

0 commit comments

Comments
 (0)