Skip to content
This repository was archived by the owner on Dec 25, 2019. It is now read-only.

Commit 2e53850

Browse files
committed
Merge branch 'master' into materialize-master
2 parents e4bf262 + abf68c6 commit 2e53850

9 files changed

Lines changed: 144 additions & 86 deletions

File tree

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ script:
4949
- travis-cargo clippy -- --all-targets --all-features -- -D warnings
5050
- travis-cargo build
5151
- travis-cargo test
52-
- travis-cargo --only nightly fmt -- -- --check --config-path <(echo 'license_template_path = "HEADER"')
52+
- travis-cargo test -- all-features
53+
- cargo +nightly fmt -- --check --config-path <(echo 'license_template_path = "HEADER"')
5354

5455
after_success:
5556
- cargo coveralls --verbose

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ name = "sqlparser"
1919
path = "src/lib.rs"
2020

2121
[dependencies]
22-
bigdecimal = "0.1.0"
22+
bigdecimal = { version = "0.1.0", optional = true }
2323
log = "0.4.5"
2424

2525
[dev-dependencies]

src/ast/ddl.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
113
//! AST types specific to CREATE/ALTER variants of [Statement]
214
//! (commonly referred to as Data Definition Language, or DDL)
315
use super::{display_comma_separated, DataType, Expr, Ident, ObjectName};

src/ast/value.rs

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

13+
#[cfg(feature = "bigdecimal")]
1314
use bigdecimal::BigDecimal;
1415
use std::fmt;
1516

@@ -32,10 +33,11 @@ impl fmt::Display for ValueError {
3233
/// Primitive SQL values such as number and string
3334
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3435
pub enum Value {
35-
/// Unsigned integer value
36-
Long(u64),
37-
/// Unsigned decimal fraction
38-
Decimal(BigDecimal),
36+
/// Numeric literal
37+
#[cfg(not(feature = "bigdecimal"))]
38+
Number(String),
39+
#[cfg(feature = "bigdecimal")]
40+
Number(BigDecimal),
3941
/// 'string value'
4042
SingleQuotedString(String),
4143
/// N'string value'
@@ -70,8 +72,7 @@ impl fmt::Display for Value {
7072
#[allow(clippy::unneeded_field_pattern)] // want to be warned if we add another field
7173
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
7274
match self {
73-
Value::Long(v) => write!(f, "{}", v),
74-
Value::Decimal(v) => write!(f, "{}", v),
75+
Value::Number(v) => write!(f, "{}", v),
7576
Value::SingleQuotedString(v) => write!(f, "'{}'", escape_single_quote_string(v)),
7677
Value::NationalStringLiteral(v) => write!(f, "N'{}'", v),
7778
Value::HexStringLiteral(v) => write!(f, "X'{}'", v),

src/parser.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
//! SQL Parser
1414
15-
use bigdecimal::BigDecimal;
1615
use log::debug;
1716

1817
use super::ast::*;
@@ -1422,13 +1421,12 @@ impl Parser {
14221421
return parser_err!(format!("No value parser for keyword {}", k.keyword));
14231422
}
14241423
},
1425-
Token::Number(ref n) if n.contains('.') => match n.parse::<BigDecimal>() {
1426-
Ok(n) => Ok(Value::Decimal(n)),
1427-
Err(e) => parser_err!(format!("Could not parse '{}' as decimal: {}", n, e)),
1428-
},
1429-
Token::Number(ref n) => match n.parse::<u64>() {
1430-
Ok(n) => Ok(Value::Long(n)),
1431-
Err(e) => parser_err!(format!("Could not parse '{}' as u64: {}", n, e)),
1424+
// The call to n.parse() returns a bigdecimal when the
1425+
// bigdecimal feature is enabled, and is otherwise a no-op
1426+
// (i.e., it returns the input string).
1427+
Token::Number(ref n) => match n.parse() {
1428+
Ok(n) => Ok(Value::Number(n)),
1429+
Err(e) => parser_err!(format!("Could not parse '{}' as number: {}", n, e)),
14321430
},
14331431
Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
14341432
Token::NationalStringLiteral(ref s) => {
@@ -1441,6 +1439,16 @@ impl Parser {
14411439
}
14421440
}
14431441

1442+
pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
1443+
match self.parse_value()? {
1444+
v @ Value::Number(_) => Ok(v),
1445+
_ => {
1446+
self.prev_token();
1447+
self.expected("literal number", self.peek_token())
1448+
}
1449+
}
1450+
}
1451+
14441452
/// Parse an unsigned literal integer/long
14451453
pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
14461454
match self.next_token() {
@@ -1847,7 +1855,7 @@ impl Parser {
18471855
modes: self.parse_transaction_modes()?,
18481856
})
18491857
} else {
1850-
self.expected("variable name", self.peek_token())
1858+
self.expected("equals sign or TO", self.peek_token())
18511859
}
18521860
}
18531861

@@ -2187,16 +2195,13 @@ impl Parser {
21872195
if self.parse_keyword("ALL") {
21882196
Ok(None)
21892197
} else {
2190-
self.parse_literal_uint()
2191-
.map(|n| Some(Expr::Value(Value::Long(n))))
2198+
Ok(Some(Expr::Value(self.parse_number_value()?)))
21922199
}
21932200
}
21942201

21952202
/// Parse an OFFSET clause
21962203
pub fn parse_offset(&mut self) -> Result<Expr, ParserError> {
2197-
let value = self
2198-
.parse_literal_uint()
2199-
.map(|n| Expr::Value(Value::Long(n)))?;
2204+
let value = Expr::Value(self.parse_number_value()?);
22002205
self.expect_one_of_keywords(&["ROW", "ROWS"])?;
22012206
Ok(value)
22022207
}

src/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,7 @@ pub fn expr_from_projection(item: &SelectItem) -> &Expr {
136136
_ => panic!("Expected UnnamedExpr"),
137137
}
138138
}
139+
140+
pub fn number(n: &'static str) -> Value {
141+
Value::Number(n.parse().unwrap())
142+
}

src/tokenizer.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,5 +789,4 @@ mod tests {
789789
//println!("------------------------------");
790790
assert_eq!(expected, actual);
791791
}
792-
793792
}

0 commit comments

Comments
 (0)