alamb commented on code in PR #10746:
URL: https://github.com/apache/datafusion/pull/10746#discussion_r1632035977
##########
datafusion/sql/src/expr/mod.rs:
##########
@@ -274,6 +276,34 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
if let Some(format) = format {
return not_impl_err!("CAST with format is not supported:
{format}");
}
+ // <https://github.com/apache/datafusion/issues/10743> in this
issue, when we try to
+ // use SELECT '12345'::VARCHAR(2) to truncate a string, we
would like to throw an arrow since the length
+ // of the current string is longer than the given type.
+ // The reason to do the judgment here is that when we using
sqlparser to transfer 12345'::VARCHAR(2) to AST, it
+ // would generate a AST like
+ // ```
+ // Cast {
+ // kind: DoubleColon,
+ // expr: Value(SingleQuotedString("12345")),
+ // data_type: Varchar(Some(IntegerLength { length: 2,
unit: None })),
+ // format: None
+ // }
+ // ```
+ // but the arrow Datatype did not contain the length
information, so we need to the judgement here
+ if let ast::DataType::Varchar(Some(IntegerLength { length, ..
})) =
+ data_type
+ {
+ match &*expr {
+ SQLExpr::Value(Value::SingleQuotedString(s))
+ if s.len() > (length).try_into().unwrap() =>
Review Comment:
Or since we are returning an error, we could just erorr
something like this (untested)
```rust
let length: usize = length.try_into().map_err(|e| datafusion_plan_err!("Can
not convert {e} to length")?;
if s.len() > length {
...
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]