Skip to content

Commit

Permalink
feat(frontend): redirect cast char to varchar (#20532)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzl25 authored Feb 19, 2025
1 parent 7167d86 commit 1a047ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions e2e_test/batch/functions/cast.slt.part
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,19 @@ select date(1);

query error unexpected arguments number
select date();

query ?
select cast('a' as char(1));
----
a

# since we don't support char type, cast to char(2) will be the same as cast to varchar, so its behavior is the same as cast to varchar and not the same as postgresql.
query ?
select 'a'::char(2) = 'a '::char(2);
----
f

query ?
select 'a'::varchar = 'a '::varchar;
----
f
14 changes: 14 additions & 0 deletions src/frontend/src/binder/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,20 @@ impl Binder {
Err(ErrorCode::BindError(format!("Can't cast {} to regproc", lhs_ty)).into())
}
}
// Redirect cast char to varchar to make system like Metabase happy.
// Char is not supported in RisingWave, but some ecosystem tools like Metabase will use it.
// Notice that the behavior of `char` and `varchar` is different in PostgreSQL.
// The following sql result should be different in PostgreSQL:
// ```
// select 'a'::char(2) = 'a '::char(2);
// ----------
// t
//
// select 'a'::varchar = 'a '::varchar;
// ----------
// f
// ```
AstDataType::Char(_) => self.bind_cast_inner(expr, DataType::Varchar),
_ => self.bind_cast_inner(expr, bind_data_type(&data_type)?),
}
}
Expand Down

0 comments on commit 1a047ba

Please sign in to comment.