apache / apache/datafusion-sqlparser-rs

support scientific notation when parsing numbers

未关闭
#610 2 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Rust
星标
3.5k
派生
772
平均合并
4 天 9 小时
30 天内合并 PR
17

描述

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

```sql
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:
```rust
let v = ["1", "1e3", "1e", ".5e2", "1e-1"];
for s in v {
let _ = s
.parse::()
.map(|i| println!("parsed {} as i64: {}", s, i))
.or_else(|_| {
s.parse::()
.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.

贡献指南

这个仓库没有索引到贡献指南

调研方向

该 issue 没有指定文件或测试。首先定位 SQL lexer 或数值 token 解析的入口点,然后重现列出的输入,并为科学计数法添加覆盖,包括末尾标识符和指数符号。完成标准是每个示例都生成所要求的 Number 或 identifier token,同时不改变 `1e.5` 现有的语法错误。

由索引模型根据 Issue 内容生成。

评估

技术栈
rust, sql
领域
compilers, databases
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。