Skip to content

Commit 44cd5f0

Browse files
committed
Don't lose precision when parsing decimal fractions
The SQL standard requires that numeric literals with a decimal point, like 1.23, are represented exactly, up to some precision. That means that parsing these literals into f64s is invalid, as it is impossible to represent many decimal numbers exactly in binary floating point (for example, 0.3). This commit parses all numeric literals into a new `Value` variant `Number(String)`, removing the old `Long(u64)` and `Double(f64)` variants. This is slightly less convenient for downstream consumers, but far more flexible, as numbers that do not fit into a u64 and f64 are now representable.
1 parent 35a2009 commit 44cd5f0

File tree

5 files changed

+82
-77
lines changed

5 files changed

+82
-77
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ path = "src/lib.rs"
2020

2121
[dependencies]
2222
log = "0.4.5"
23-
ordered-float = "1.0.2"
2423

2524
[dev-dependencies]
2625
simple_logger = "1.0.1"

src/ast/value.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13-
use ordered_float::OrderedFloat;
1413
use std::fmt;
1514

1615
/// Primitive SQL values such as number and string
1716
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1817
pub enum Value {
19-
/// Unsigned integer value
20-
Long(u64),
21-
/// Unsigned floating point value
22-
Double(OrderedFloat<f64>),
18+
/// Numeric literal
19+
Number(String),
2320
/// 'string value'
2421
SingleQuotedString(String),
2522
/// N'string value'
@@ -60,8 +57,7 @@ pub enum Value {
6057
impl fmt::Display for Value {
6158
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6259
match self {
63-
Value::Long(v) => write!(f, "{}", v),
64-
Value::Double(v) => write!(f, "{}", v),
60+
Value::Number(v) => write!(f, "{}", v),
6561
Value::SingleQuotedString(v) => write!(f, "'{}'", escape_single_quote_string(v)),
6662
Value::NationalStringLiteral(v) => write!(f, "N'{}'", v),
6763
Value::HexStringLiteral(v) => write!(f, "X'{}'", v),

src/parser.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,14 +1183,7 @@ impl Parser {
11831183
return parser_err!(format!("No value parser for keyword {}", k.keyword));
11841184
}
11851185
},
1186-
Token::Number(ref n) if n.contains('.') => match n.parse::<f64>() {
1187-
Ok(n) => Ok(Value::Double(n.into())),
1188-
Err(e) => parser_err!(format!("Could not parse '{}' as f64: {}", n, e)),
1189-
},
1190-
Token::Number(ref n) => match n.parse::<u64>() {
1191-
Ok(n) => Ok(Value::Long(n)),
1192-
Err(e) => parser_err!(format!("Could not parse '{}' as u64: {}", n, e)),
1193-
},
1186+
Token::Number(ref n) => Ok(Value::Number(n.to_string())),
11941187
Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
11951188
Token::NationalStringLiteral(ref s) => {
11961189
Ok(Value::NationalStringLiteral(s.to_string()))
@@ -1864,15 +1857,15 @@ impl Parser {
18641857
Ok(None)
18651858
} else {
18661859
self.parse_literal_uint()
1867-
.map(|n| Some(Expr::Value(Value::Long(n))))
1860+
.map(|n| Some(Expr::Value(Value::Number(n.to_string()))))
18681861
}
18691862
}
18701863

18711864
/// Parse an OFFSET clause
18721865
pub fn parse_offset(&mut self) -> Result<Expr, ParserError> {
18731866
let value = self
18741867
.parse_literal_uint()
1875-
.map(|n| Expr::Value(Value::Long(n)))?;
1868+
.map(|n| Expr::Value(Value::Number(n.to_string())))?;
18761869
self.expect_one_of_keywords(&["ROW", "ROWS"])?;
18771870
Ok(value)
18781871
}

0 commit comments

Comments
 (0)