Skip to content

Commit 619835e

Browse files
authored
Merge pull request #5 from qidi1/rename_sql_parser
change sql_parser to parser
2 parents 2409c8b + 99a043b commit 619835e

55 files changed

Lines changed: 129 additions & 109 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,24 @@ pip install sqlgpt-parser
1111
### Parser SQL
1212

1313
```python
14-
>>> from sqlgpt_parser.sql_parser.mysql_parser import parser as mysql_parser
15-
>>> mysql_parser.parse("select * from t")
16-
Query(query_body=QuerySpecification(select=Select(distinct=False, select_items=[SingleColumn(expression=QualifiedNameReference(name=QualifiedName.of("*")))]), from_=Table(name=QualifiedName.of("t"), for_update=False), order_by=[], limit=0, offset=0, for_update=False, nowait_or_wait=False), order_by=[], limit=0, offset=0)
17-
>>> from sqlgpt_parser.sql_parser.oceanbase_parser import parser as oceanbase_parser
18-
>>> oceanbase_parser.parse("select * from t")
19-
Query(query_body=QuerySpecification(select=Select(distinct=False, select_items=[SingleColumn(expression=QualifiedNameReference(name=QualifiedName.of("*")))]), from_=Table(name=QualifiedName.of("t"), for_update=False), order_by=[], limit=0, offset=0, for_update=False, nowait_or_wait=False), order_by=[], limit=0, offset=0)
20-
>>> from sqlgpt_parser.sql_parser.odps_parser import parser as odps_parser
21-
>>> odps_parser.parse("select * from t")
22-
Query(query_body=QuerySpecification(select=Select(distinct=False, select_items=[SingleColumn(expression=QualifiedNameReference(name=QualifiedName.of("*")))]), from_=Table(name=QualifiedName.of("t"), for_update=False), order_by=[], limit=0, offset=0, for_update=False, nowait_or_wait=False), order_by=[], limit=0, offset=0)
14+
>> > from sqlgpt_parser.parser.mysql_parser import parser as mysql_parser
15+
>> > mysql_parser.parse("select * from t")
16+
Query(query_body=QuerySpecification(select=Select(distinct=False, select_items=[
17+
SingleColumn(expression=QualifiedNameReference(name=QualifiedName.of("*")))]),
18+
from_=Table(name=QualifiedName.of("t"), for_update=False), order_by=[], limit=0,
19+
offset=0, for_update=False, nowait_or_wait=False), order_by=[], limit=0, offset=0)
20+
>> > from sqlgpt_parser.parser.oceanbase_parser import parser as oceanbase_parser
21+
>> > oceanbase_parser.parse("select * from t")
22+
Query(query_body=QuerySpecification(select=Select(distinct=False, select_items=[
23+
SingleColumn(expression=QualifiedNameReference(name=QualifiedName.of("*")))]),
24+
from_=Table(name=QualifiedName.of("t"), for_update=False), order_by=[], limit=0,
25+
offset=0, for_update=False, nowait_or_wait=False), order_by=[], limit=0, offset=0)
26+
>> > from sqlgpt_parser.parser.odps_parser import parser as odps_parser
27+
>> > odps_parser.parse("select * from t")
28+
Query(query_body=QuerySpecification(select=Select(distinct=False, select_items=[
29+
SingleColumn(expression=QualifiedNameReference(name=QualifiedName.of("*")))]),
30+
from_=Table(name=QualifiedName.of("t"), for_update=False), order_by=[], limit=0,
31+
offset=0, for_update=False, nowait_or_wait=False), order_by=[], limit=0, offset=0)
2332
```
2433

2534
### Format SQL

docs/docs-ch/SQL Parser 开发指南.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
`parser``sqlgpt-parser` 的基础模块,它将 SQL 语句按照预定义 SQL 语法规则解析,从文本转换成抽象语法树(`AST`)。
44

5-
`sqlgpt-parser``parser` 基于 [PLY](https://github.com/dabeaz/ply) 编写。PLY 是一个用于构建词法和语法分析器的 Python 工具。它能够根据指定的模式对输入的文本进行分析,它会在程序运行之前,自动编译项目 [sql-parser](../../sqlgpt_parser/sql_parser/) 文件夹下的词法规则和语法规则文件,生成可执行代码。
5+
`sqlgpt-parser``parser` 基于 [PLY](https://github.com/dabeaz/ply) 编写。PLY 是一个用于构建词法和语法分析器的 Python 工具。它能够根据指定的模式对输入的文本进行分析,它会在程序运行之前,自动编译项目 [sql-parser](../../sqlgpt_parser/parser/) 文件夹下的词法规则和语法规则文件,生成可执行代码。
66

77
## 词法解析与语法解析
88

@@ -161,7 +161,7 @@ def p_expr_paren(p):
161161

162162
## `sqlgpt-parser``parser` 实现
163163

164-
`sqlgpt-parser` 中共有三个 SQL 语法解析器 ,分别在 [mysql_parser](../../sqlgpt_parser/sql_parser/mysql_parser)[oceanbase_parser](../../sqlgpt_parser/sql_parser/oceanbase_parser)[odps_parser](../../sqlgpt_parser/sql_parser/odps_parser) 文件夹下,三个文件夹都包含`lexer.py``reserved.py``parser.py` 三个文件。
164+
`sqlgpt-parser` 中共有三个 SQL 语法解析器 ,分别在 [mysql_parser](../../sqlgpt_parser/parser/mysql_parser)[oceanbase_parser](../../sqlgpt_parser/parser/oceanbase_parser)[odps_parser](../../sqlgpt_parser/parser/odps_parser) 文件夹下,三个文件夹都包含`lexer.py``reserved.py``parser.py` 三个文件。
165165

166166
`lexer.py``reserved.py` 文件都用于词法解析。`reserved.py` 中定义了 SQL 的关键字,关键字定义在`reserved``nonreserved` 两个变量中 ,`reserved` 里面包含了所有 `sql` 中不可用作列名、表名或者别名的关键字,`nonreserved` 则是可以用作列名、表名或者别名的关键字。
167167

@@ -260,7 +260,7 @@ def p_delete(p):
260260
完成语法规则的编写后就可以使用,该语法规则解析 `SQL` 语句,以 `mysql_parser` 为例。
261261

262262
```python
263-
from sqlgpt_parser.sql_parser.mysql_parser import parser as mysql_parser
263+
from sqlgpt_parser.parser.mysql_parser import parser as mysql_parser
264264

265265
sql = "DELETE FROM t WHERE a=1"
266266
result = mysql_parser.parse(sql)

docs/docs-en/SQL Parser Development Guide.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The `parser` module is the foundational module of `sqlgpt-parser`. It parses SQL statements according to predefined SQL grammar rules, converting them from text into an abstract syntax tree (`AST`).
44

5-
The `parser` module in `sqlgpt-parser` is written using [PLY](https://github.com/dabeaz/ply). PLY is a Python tool for building lexical and parsing analyzers. It can analyze input text based on specified patterns and automatically compile the lexical and grammar rule files in the [sql-parser](../../sqlgpt_parser/sql_parser/) folder of the project before the program runs, generating executable code.
5+
The `parser` module in `sqlgpt-parser` is written using [PLY](https://github.com/dabeaz/ply). PLY is a Python tool for building lexical and parsing analyzers. It can analyze input text based on specified patterns and automatically compile the lexical and grammar rule files in the [sql-parser](../../sqlgpt_parser/parser/) folder of the project before the program runs, generating executable code.
66

77
## Lexical Analysis and Syntax Analysis
88

@@ -158,7 +158,7 @@ PLY uses the notation `p[position]` to access the stack, where `p[0]` correspond
158158

159159
## Implementation of the `parser` for `sqlgpt-parser`
160160

161-
There are a total of three SQL parsers in `sqlgpt-parser`, located in the [mysql_parser](../../sqlgpt_parser/sql_parser/mysql_parser), [oceanbase_parser](../../sqlgpt_parser/sql_parser/oceanbase_parser), and [odps_parser](../../sqlgpt_parser/sql_parser/odps_parser) folders. Each of these folders contains three files: `lexer.py`, `reserved.py`, and `parser.py`.
161+
There are a total of three SQL parsers in `sqlgpt-parser`, located in the [mysql_parser](../../sqlgpt_parser/parser/mysql_parser), [oceanbase_parser](../../sqlgpt_parser/parser/oceanbase_parser), and [odps_parser](../../sqlgpt_parser/parser/odps_parser) folders. Each of these folders contains three files: `lexer.py`, `reserved.py`, and `parser.py`.
162162

163163
The `lexer.py` and `reserved.py` files are both used for lexical analysis. In `reserved.py`, SQL keywords are defined and stored in two variables: `reserved` and `nonreserved`. The `reserved` variable contains all the keywords that cannot be used as column names, table names, or aliases in SQL. On the other hand, `nonreserved` contains keywords that can be used as column names, table names, or aliases.
164164

@@ -245,7 +245,7 @@ The comment in `p_delete` corresponds to the syntax rule for the `DELETE` statem
245245
Once the grammar rules are written, they can be used to parse SQL statements. Taking `mysql_parser` as an example, you can use this grammar rule for parsing SQL statements.
246246

247247
```python
248-
from sqlgpt_parser.sql_parser.mysql_parser import parser as mysql_parser
248+
from sqlgpt_parser.parser.mysql_parser import parser as mysql_parser
249249

250250
sql = "DELETE FROM t WHERE a=1"
251251
result = mysql_parser.parse(sql)

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"
44

55
[project]
66
name = "sqlgpt-parser"
7-
version = "0.0.1a2"
7+
version = "0.0.1a3"
88
authors = [
99
{ name="luliwjc", email="[email protected]" },
1010
{ name="Ifffff", email="[email protected]" },
@@ -37,7 +37,7 @@ dependencies = [
3737
[tool.black]
3838
skip-string-normalization = 1
3939
force-exclude = '''
40-
src/sql_parser/mysql_parser/parser_table.py
41-
| src/sql_parser/oceanbase_parser/parser_table.py
42-
| src/sql_parser/odps_parser/parser_table.py
40+
sqlgpt_parser/parser/mysql_parser/parser_table.py
41+
| sqlgpt_parser/parser/oceanbase_parser/parser_table.py
42+
| sqlgpt_parser/parser/odps_parser/parser_table.py
4343
'''

sqlgpt_parser/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# coding=utf-8
2+
"""
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
"""
12+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)

sqlgpt_parser/format/formatter.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
distributed under the License is distributed on an "AS IS" BASIS,
1010
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1111
"""
12-
from sqlgpt_parser.sql_parser.tree import JsonTableColumn
13-
from sqlgpt_parser.sql_parser.tree.explain import ExplainFormat, ExplainType
14-
from sqlgpt_parser.sql_parser.tree.grouping import SimpleGroupBy
15-
from sqlgpt_parser.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
16-
from sqlgpt_parser.sql_parser.tree.table import Table, TableSubquery
17-
from sqlgpt_parser.sql_parser.tree.visitor import AstVisitor
18-
from sqlgpt_parser.sql_parser.parser_utils import FIELD_REFERENCE_PREFIX
12+
from sqlgpt_parser.parser.tree import JsonTableColumn
13+
from sqlgpt_parser.parser.tree.explain import ExplainFormat, ExplainType
14+
from sqlgpt_parser.parser.tree.grouping import SimpleGroupBy
15+
from sqlgpt_parser.parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
16+
from sqlgpt_parser.parser.tree.table import Table, TableSubquery
17+
from sqlgpt_parser.parser.tree.visitor import AstVisitor
18+
from sqlgpt_parser.parser.parser_utils import FIELD_REFERENCE_PREFIX
1919

2020

2121
class Formatter(AstVisitor):
File renamed without changes.
File renamed without changes.

sqlgpt_parser/sql_parser/mysql_parser/lexer.py renamed to sqlgpt_parser/parser/mysql_parser/lexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import re
1414
from ply import lex
1515

16-
from sqlgpt_parser.sql_parser.mysql_parser.reserved import (
16+
from sqlgpt_parser.parser.mysql_parser.reserved import (
1717
reversed,
1818
nonreserved,
1919
not_keyword_token,

0 commit comments

Comments
 (0)