Skip to content
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

fix(function): fix incorrect return datatype of function if #5980

Merged
merged 10 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions common/datablocks/src/data_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub struct DataBlock {
impl DataBlock {
#[inline]
pub fn create(schema: DataSchemaRef, columns: Vec<ColumnRef>) -> Self {
debug_assert!(
schema.fields().iter().zip(columns.iter()).all( |(f, c) | f.data_type() == &c.data_type() );
);
DataBlock { schema, columns }
}

Expand Down
12 changes: 7 additions & 5 deletions common/functions/src/scalars/conditionals/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ impl IfFunction {
&self,
cond_col: &ColumnRef,
columns: &ColumnsWithField,
_func_ctx: &FunctionContext,
func_ctx: &FunctionContext,
) -> Result<ColumnRef> {
debug_assert!(cond_col.is_const());
// whether nullable or not, we can use viewer to make it
let cond_viewer = bool::try_create_viewer(cond_col)?;
if cond_viewer.value_at(0) {
return Ok(columns[0].column().clone());
let c = if cond_viewer.value_at(0) {
columns[0].clone()
} else {
return Ok(columns[1].column().clone());
}
columns[1].clone()
};

cast_column_field(&c, c.data_type(), &self.least_supertype, func_ctx)
}

// lhs is const column and:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ NULL
NULL
5
6
NULL 0
0 NULL
6 changes: 5 additions & 1 deletion tests/suites/0_stateless/02_function/02_0010_function_if.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ select if(number>0, 'Z+', 'zero') from numbers(3) order by number;
select if(number<1, true, null) from numbers(3) order by number;
select typeof(if(number % 3 = 0, to_uint32(1), to_int64(3))) from numbers(10) limit 1;
select typeof(if(number % 3 = 0, to_uint32(1), to_float32(3))) from numbers(10) limit 1;
SELECT if (number % 3 = 1, null, number) as a FROM numbers(7) order by number;
SELECT if (number % 3 = 1, null, number) as a FROM numbers(7) order by number;

-- constant
select if(true, null, number), if(false, null, number) from numbers(1);
select if(true, number, null), if(false, number, null) from numbers(1);