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

support scientific notation when parsing numbers #610

Open
kmitchener opened this issue Sep 11, 2022 · 2 comments
Open

support scientific notation when parsing numbers #610

kmitchener opened this issue Sep 11, 2022 · 2 comments

Comments

@kmitchener
Copy link

kmitchener commented Sep 11, 2022

select 1e3 currently identifies 1 as a number and e3 as alias. Instead, it should return 1e3 as part of the string Number.

select 1e3; -- returns now e3 as identifier and 1 as number, but should return 1e3 as number
select 1e3a; -- should return 1e3 as number, and a as identifier
select 1e; -- should return 1e as the number (currently returns 1 as number, e as identifier)
select 1e.5; -- correctly returns syntax error
select .5e2; -- should return .5e2 as number, but currently returns .5 as number, e2 as identifier

Since the user of the library will get back a string representing the number anyway and must parse it, we could recognize the e notation and leave it as part of the string in Number.

For example, Rust can parse scientific notation to f64:

    let v = ["1", "1e3", "1e", ".5e2", "1e-1"];
    for s in v {
        let _ = s
            .parse::<i64>()
            .map(|i| println!("parsed {} as i64: {}", s, i))
            .or_else(|_| {
                s.parse::<f64>()
                    .map(|f| println!("parsed {} as f64: {}", s, f))
            })
            .map_err(|_| println!("couldn't parse {}", s));
    }
parsed 1 as i64: 1
parsed 1e3 as f64: 1000
couldn't parse 1e
parsed .5e2 as f64: 50
parsed 1e-1 as f64: 0.1

Only problem is that 1e parses to 1 in Postgres, but it wouldn't in this case without special handling by the parsers. In any case, this is an argument in favor of returning scientific notation as part of the existing Number enum value.

@kmitchener
Copy link
Author

The Hive dialect in sqlparser-rs lets it consider the 1e3 in select 1e3 as an identifier. Is that actually true? @andygrove, do you have access to a Hive SQL system and can confirm that behavior?

@kmitchener
Copy link
Author

1E+20 is a valid form as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant