viirya commented on code in PR #4102:
URL: https://github.com/apache/arrow-datafusion/pull/4102#discussion_r1014482609
##########
datafusion/sql/src/planner.rs:
##########
@@ -2671,6 +2694,49 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
Ok(lit(ScalarValue::new_list(Some(values), data_type)))
}
}
+
+ /// Parse number in sql string, convert to Expr::Literal
+ fn parse_sql_number(&self, n: &str) -> Result<Expr> {
+ if let Some(_) = n.find('E') {
+ // not implemented yet
+ // https://github.com/apache/arrow-datafusion/issues/3448
+ Err(DataFusionError::NotImplemented(
+ "sql numeric literals in scientific notation are not supported"
+ .to_string(),
+ ))
+ } else if let Ok(n) = n.parse::<i64>() {
+ Ok(lit(n))
+ } else if self.options.parse_float_as_decimal {
+ if let Some(i) = n.find('.') {
+ let p = n.len() - 1;
+ let s = n.len() - i - 1;
+ let str = n.replace('.', "");
+ let n = str.parse::<i128>().map_err(|_| {
+ DataFusionError::from(ParserError(format!(
+ "Cannot parse {} as i128 when building decimal. Input
token: {}",
+ str, n
+ )))
+ })?;
+ Ok(Expr::Literal(ScalarValue::Decimal128(
+ Some(n),
+ p as u8,
+ s as u8,
+ )))
+ } else {
+ let number = n.parse::<i128>().map_err(|_| {
+ DataFusionError::from(ParserError(format!(
+ "Cannot parse {} as i128 when building decimal",
+ n
+ )))
+ })?;
+ Ok(Expr::Literal(ScalarValue::Decimal128(Some(number), 38, 0)))
Review Comment:
Can we get the minimum precision to handle this number?
```
scala> sql("select 10000000000000000000")
res10: org.apache.spark.sql.DataFrame = [10000000000000000000: decimal(20,0)]
```
--
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]