Skip to content

chore(query): auto cast int to string for concat functions #17891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/query/functions/src/cast_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ pub fn register(registry: &mut FunctionRegistry) {

for func_name in ALL_STRING_FUNC_NAMES {
registry.register_additional_cast_rules(func_name, GENERAL_CAST_RULES.iter().cloned());
registry.register_additional_cast_rules(func_name, CAST_FROM_STRING_RULES.iter().cloned());
if ["concat", "concat_ws"].contains(func_name) {
registry.register_additional_cast_rules(
func_name,
get_cast_int_to_string_rules().into_iter(),
)
} else {
registry
.register_additional_cast_rules(func_name, CAST_FROM_STRING_RULES.iter().cloned());
}
registry.register_additional_cast_rules(func_name, CAST_FROM_VARIANT_RULES());
registry.register_additional_cast_rules(func_name, CAST_INT_TO_UINT64.iter().cloned());
}
Expand Down Expand Up @@ -366,3 +374,10 @@ pub const CAST_INT_TO_UINT64: AutoCastRules = &[
DataType::Number(NumberDataType::UInt64),
),
];

pub fn get_cast_int_to_string_rules() -> Vec<(DataType, DataType)> {
ALL_NUMERICS_TYPES
.iter()
.map(|ty| (DataType::Number(*ty), DataType::String))
.collect()
}
36 changes: 36 additions & 0 deletions src/query/functions/src/scalars/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,42 @@ pub fn register(registry: &mut FunctionRegistry) {
}
}),
);

registry.register_passthrough_nullable_2_arg::<ArrayType<GenericType<0>>, ArrayType<GenericType<0>>, ArrayType<GenericType<0>> , _, _>(
"array_intersection",
|_, _, _| FunctionDomain::MayThrow,
vectorize_with_builder_2_arg::<ArrayType<GenericType<0>>, ArrayType<GenericType<0>>, ArrayType<GenericType<0>> >(
|left, right, output, _| {
let mut set: StackHashSet<u128, 16> = StackHashSet::with_capacity(left.len());
let builder = &mut output.builder;
for val in left.iter() {
if val == ScalarRef::Null {
continue;
}
let mut hasher = SipHasher24::new();
val.hash(&mut hasher);
let hash128 = hasher.finish128();
let key = hash128.into();
let _ = set.set_insert(key);
}

for val in right.iter() {
if val == ScalarRef::Null {
continue;
}
let mut hasher = SipHasher24::new();
val.hash(&mut hasher);
let hash128 = hasher.finish128();
let key = hash128.into();

if set.contains(&key) {
builder.push(val);
}
}
output.commit_row()
},
),
);
}

fn register_array_aggr(registry: &mut FunctionRegistry) {
Expand Down
10 changes: 8 additions & 2 deletions src/query/functions/tests/it/scalars/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn test_array() {
test_array_append(file);
test_array_indexof(file);
test_array_unique(file);
test_array_distinct(file);
test_array_distinct_intersection(file);
test_array_sum(file);
test_array_avg(file);
test_array_count(file);
Expand Down Expand Up @@ -316,7 +316,7 @@ fn test_array_unique(file: &mut impl Write) {
]);
}

fn test_array_distinct(file: &mut impl Write) {
fn test_array_distinct_intersection(file: &mut impl Write) {
run_ast(file, "array_distinct([])", &[]);
run_ast(file, "array_distinct([1, 1, 2, 2, 3, NULL])", &[]);
run_ast(
Expand All @@ -331,6 +331,12 @@ fn test_array_distinct(file: &mut impl Write) {
("c", Int16Type::from_data(vec![3i16, 1, 3, 4])),
("d", Int16Type::from_data(vec![4i16, 2, 3, 4])),
]);

run_ast(
file,
"array_intersection(['a', NULL, 'a', 'b', NULL, 'c', 'd'], ['a', 'd'])",
&[],
);
}

fn test_array_sum(file: &mut impl Write) {
Expand Down
1 change: 1 addition & 0 deletions src/query/functions/tests/it/scalars/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ fn test_trim(file: &mut impl Write) {
}

fn test_concat(file: &mut impl Write) {
run_ast(file, "concat('5', 3, 4)", &[]);
run_ast(file, "concat('5', '3', '4')", &[]);
run_ast(file, "concat(NULL, '3', '4')", &[]);
run_ast(
Expand Down
9 changes: 9 additions & 0 deletions src/query/functions/tests/it/scalars/testdata/array.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,15 @@ evaluation (internal):
+--------+--------------------------------------------------------------------------------------+


ast : array_intersection(['a', NULL, 'a', 'b', NULL, 'c', 'd'], ['a', 'd'])
raw expr : array_intersection(array('a', NULL, 'a', 'b', NULL, 'c', 'd'), array('a', 'd'))
checked expr : array_intersection<T0=String NULL><Array(T0), Array(T0)>(array<T0=String NULL><T0, T0, T0, T0, T0, T0, T0>(CAST<String>("a" AS String NULL), CAST<NULL>(NULL AS String NULL), CAST<String>("a" AS String NULL), CAST<String>("b" AS String NULL), CAST<NULL>(NULL AS String NULL), CAST<String>("c" AS String NULL), CAST<String>("d" AS String NULL)), CAST<Array(String)>(array<T0=String><T0, T0>("a", "d") AS Array(String NULL)))
optimized expr : ['a', 'd']
output type : Array(String NULL)
output domain : [{"a"..="d"}]
output : ['a', 'd']


ast : array_sum([])
raw expr : array_sum(array())
checked expr : array_sum<Array(Nothing)>(array<>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ Functions overloads:
0 array_indexof(NULL, NULL) :: NULL
1 array_indexof(Array(T0), T0) :: UInt64
2 array_indexof(Array(T0) NULL, T0 NULL) :: UInt64 NULL
0 array_intersection(Array(T0), Array(T0)) :: Array(T0)
1 array_intersection(Array(T0) NULL, Array(T0) NULL) :: Array(T0) NULL
0 array_kurtosis FACTORY
0 array_max FACTORY
0 array_median FACTORY
Expand Down
9 changes: 9 additions & 0 deletions src/query/functions/tests/it/scalars/testdata/string.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,15 @@ evaluation (internal):
+--------+----------------------------+


ast : concat('5', 3, 4)
raw expr : concat('5', 3, 4)
checked expr : concat<String, String, String>("5", CAST<UInt8>(3_u8 AS String), CAST<UInt8>(4_u8 AS String))
optimized expr : "534"
output type : String
output domain : {"534"..="534"}
output : '534'


ast : concat('5', '3', '4')
raw expr : concat('5', '3', '4')
checked expr : concat<String, String, String>("5", "3", "4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ alter table t2 modify column b drop stored
statement ok
create table t3(a string, b string as (concat(a, '-')) stored)

statement error 1117
statement ok
alter table t3 modify column a float

statement ok
Expand Down
Loading