Skip to content
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
24 changes: 17 additions & 7 deletions datafusion/sql/src/expr/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,21 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
}

if self.options.parse_float_as_decimal {
parse_decimal(
let decimal = parse_decimal(
unsigned_number,
negative,
self.options.trim_decimal_literal_trailing_zeros,
)
);
match decimal {
Err(DataFusionError::NotImplemented(_))
if unsigned_number.contains(['.', 'e', 'E']) =>
{
parse_float(&signed_number)
}
result => result,
}
} else {
signed_number.parse::<f64>().map(lit).map_err(|_| {
DataFusionError::from(ParserError(format!(
"Cannot parse {signed_number} as f64"
)))
})
parse_float(&signed_number)
}
}

Expand Down Expand Up @@ -379,6 +383,12 @@ fn bigint_to_i256(v: &BigInt) -> Option<i256> {
}
}

fn parse_float(signed_number: &str) -> Result<Expr> {
signed_number.parse::<f64>().map(lit).map_err(|_| {
DataFusionError::from(ParserError(format!("Cannot parse {signed_number} as f64")))
})
}

fn parse_decimal(
unsigned_number: &str,
negative: bool,
Expand Down
34 changes: 33 additions & 1 deletion datafusion/sql/tests/sql_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ use std::vec;

use arrow::datatypes::{TimeUnit::Nanosecond, *};
use common::MockContextProvider;
use datafusion_common::{DFSchema, DataFusionError, Result, assert_contains};
use datafusion_common::{
DFSchema, DataFusionError, Result, ScalarValue, assert_contains,
};
use datafusion_expr::{
ColumnarValue, CreateIndex, DdlStatement, Expr, HigherOrderFunctionArgs,
HigherOrderReturnFieldArgs, HigherOrderSignature, HigherOrderUDF, HigherOrderUDFImpl,
Expand Down Expand Up @@ -194,6 +196,36 @@ fn parse_decimals_9() {
);
}

#[test]
fn parse_decimal_out_of_range_float_falls_back_to_float64() {
let sql = "SELECT 1.7976931348623157e+308, 2.2250738585072014e-308";
let options = parse_decimals_parser_options();
let plan = logical_plan_with_options(sql, options).unwrap();
let LogicalPlan::Projection(projection) = plan else {
panic!("expected a projection")
};
let values = projection
.expr
.iter()
.map(|expr| match expr {
Expr::Literal(ScalarValue::Float64(Some(value)), _) => value.to_bits(),
_ => panic!("expected Float64 literals, got {expr}"),
})
.collect::<Vec<_>>();
assert_eq!(values, [f64::MAX.to_bits(), f64::MIN_POSITIVE.to_bits()]);
}

#[test]
fn parse_decimal_out_of_range_integer_remains_an_error() {
let sql = format!("SELECT {}", "1".repeat(77));
let options = parse_decimals_parser_options();
let error = logical_plan_with_options(&sql, options).unwrap_err();
assert_contains!(
error.to_string(),
"Decimal precision 77 exceeds the maximum supported precision: 76"
);
}

#[test]
fn parse_decimals_trim_insignificant_trailing_zeros() {
let sql = "SELECT 10.00, 10.10, 0.00100, 100.0001, 1.2300e2";
Expand Down
Loading