Skip to content
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

fix: Parse scientific notation correctly #2865

Merged
merged 1 commit into from
Jun 19, 2023
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
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ will become the public version at the next release._
As part of this, we've also formalized tuples as containing both individual
items (`select {foo, baz}`), and assignments (`select {foo=bar, baz=fuz}`).

- Some significant (breaking) changes regarding SQL dialect.
- Some significant changes regarding SQL dialects:

- Operators and functions can be defined on per-dialect basis. (@aljazerzen,
#2681)
Expand Down Expand Up @@ -80,10 +80,8 @@ will become the public version at the next release._
- "Relation Literals", which create inline relations / tables from data in the
query:

' # TODO: add `from`?

```prql no-eval
[{a=5, b=false}, {a=6, b=true}]
from [{a=5, b=false}, {a=6, b=true}]
filter b == true
select a
```
Expand All @@ -92,6 +90,8 @@ will become the public version at the next release._

(@aljazerzen, #2605)

<!-- TODO: should we have a section for "Library changes? There's a missing category between "Language features" and "Internal changes" -->

- We've changed how we handle colors. We now use the
`[anstream](https://github.com/rust-cli/anstyle)` library in `prqlc` &
`prql-compiler`.
Expand All @@ -110,6 +110,9 @@ will become the public version at the next release._

**Fixes**:

- Numbers expressed with scientific notation — `1e9` — are now handled correctly
by the compiler (@max-sixty).

**Documentation**:

**Web**:
Expand Down
2 changes: 1 addition & 1 deletion prql-compiler/src/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn ident_part() -> impl Parser<char, String, Error = Cheap<char>> {
}

fn literal() -> impl Parser<char, Literal, Error = Cheap<char>> {
let exp = just('e').or(just('E')).ignore_then(
let exp = just('e').or(just('E')).chain(
just('+')
.or(just('-'))
.or_not()
Expand Down
17 changes: 12 additions & 5 deletions prql-compiler/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,13 +894,20 @@ Canada
"###);

// Underscores at the beginning are parsed as ident
parse_expr("_2").unwrap().kind.into_ident().unwrap();
parse_expr("_").unwrap().kind.into_ident().unwrap();
assert!(parse_expr("_2").unwrap().kind.into_ident().is_ok());
assert!(parse_expr("_").unwrap().kind.into_ident().is_ok());

// We don't allow empty fractions.
parse_expr(r#"add 1. 2"#).unwrap_err();
// We don't allow trailing periods
assert!(parse_expr(r#"add 1. 2"#).is_err());

assert!(parse_expr("_2.3").is_err());

assert_yaml_snapshot!(parse_expr(r#"2e3"#).unwrap(), @r###"
---
Literal:
Float: 2000
"###);

parse_expr("_2.3").unwrap_err();
// expr_of_string("2_").unwrap_err(); // TODO
// expr_of_string("2.3_").unwrap_err(); // TODO
}
Expand Down
5 changes: 4 additions & 1 deletion web/book/src/syntax/numbers.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# Numbers

Numbers can contain underscores between numbers; which can make reading large
numbers easier:
numbers easier.

PRQL currently converts scientific notation into their full form.

```prql
from numbers
select {
small = 1.000_000_1,
big = 5_000_000,
huge = 5e9,
}
```
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
---
source: web/book/tests/documentation/book.rs
expression: "from numbers\nselect {\n small = 1.000_000_1,\n big = 5_000_000,\n}\n"
expression: "from numbers\nselect {\n small = 1.000_000_1,\n big = 5_000_000,\n huge = 5e9,\n}\n"
---
SELECT
1.0000001 AS small,
5000000 AS big
5000000 AS big,
5000000000.0 AS huge
FROM
numbers