Skip to content

Don't lose precision when parsing decimal fractions #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2019
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ script:
- travis-cargo clippy -- --all-targets --all-features -- -D warnings
- travis-cargo build
- travis-cargo test
- travis-cargo test -- all-features
- cargo +nightly fmt -- --check --config-path <(echo 'license_template_path = "HEADER"')

after_success:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ name = "sqlparser"
path = "src/lib.rs"

[dependencies]
bigdecimal = { version = "0.1.0", optional = true }
log = "0.4.5"
ordered-float = "1.0.2"

[dev-dependencies]
simple_logger = "1.0.1"
Expand Down
15 changes: 8 additions & 7 deletions src/ast/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use ordered_float::OrderedFloat;
#[cfg(feature = "bigdecimal")]
use bigdecimal::BigDecimal;
use std::fmt;

/// Primitive SQL values such as number and string
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Value {
/// Unsigned integer value
Long(u64),
/// Unsigned floating point value
Double(OrderedFloat<f64>),
/// Numeric literal
#[cfg(not(feature = "bigdecimal"))]
Number(String),
#[cfg(feature = "bigdecimal")]
Number(BigDecimal),
/// 'string value'
SingleQuotedString(String),
/// N'string value'
Expand Down Expand Up @@ -60,8 +62,7 @@ pub enum Value {
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Value::Long(v) => write!(f, "{}", v),
Value::Double(v) => write!(f, "{}", v),
Value::Number(v) => write!(f, "{}", v),
Value::SingleQuotedString(v) => write!(f, "'{}'", escape_single_quote_string(v)),
Value::NationalStringLiteral(v) => write!(f, "N'{}'", v),
Value::HexStringLiteral(v) => write!(f, "X'{}'", v),
Expand Down
30 changes: 18 additions & 12 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,13 +1183,12 @@ impl Parser {
return parser_err!(format!("No value parser for keyword {}", k.keyword));
}
},
Token::Number(ref n) if n.contains('.') => match n.parse::<f64>() {
Ok(n) => Ok(Value::Double(n.into())),
Err(e) => parser_err!(format!("Could not parse '{}' as f64: {}", n, e)),
},
Token::Number(ref n) => match n.parse::<u64>() {
Ok(n) => Ok(Value::Long(n)),
Err(e) => parser_err!(format!("Could not parse '{}' as u64: {}", n, e)),
// The call to n.parse() returns a bigdecimal when the
// bigdecimal feature is enabled, and is otherwise a no-op
// (i.e., it returns the input string).
Token::Number(ref n) => match n.parse() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused by this at first, as the IDE suggested that parse() returns a String. Perhaps add a comment that this is for feature=bigdecimal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, done!

Ok(n) => Ok(Value::Number(n)),
Err(e) => parser_err!(format!("Could not parse '{}' as number: {}", n, e)),
},
Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
Token::NationalStringLiteral(ref s) => {
Expand All @@ -1202,6 +1201,16 @@ impl Parser {
}
}

pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
match self.parse_value()? {
v @ Value::Number(_) => Ok(v),
_ => {
self.prev_token();
self.expected("literal number", self.peek_token())
}
}
}

/// Parse an unsigned literal integer/long
pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
match self.next_token() {
Expand Down Expand Up @@ -1863,16 +1872,13 @@ impl Parser {
if self.parse_keyword("ALL") {
Ok(None)
} else {
self.parse_literal_uint()
.map(|n| Some(Expr::Value(Value::Long(n))))
Ok(Some(Expr::Value(self.parse_number_value()?)))
}
}

/// Parse an OFFSET clause
pub fn parse_offset(&mut self) -> Result<Expr, ParserError> {
let value = self
.parse_literal_uint()
.map(|n| Expr::Value(Value::Long(n)))?;
let value = Expr::Value(self.parse_number_value()?);
self.expect_one_of_keywords(&["ROW", "ROWS"])?;
Ok(value)
}
Expand Down
4 changes: 4 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,7 @@ pub fn expr_from_projection(item: &SelectItem) -> &Expr {
_ => panic!("Expected UnnamedExpr"),
}
}

pub fn number(n: &'static str) -> Value {
Value::Number(n.parse().unwrap())
}
Loading