Skip to content

Commit

Permalink
Merge pull request #4 from qidi1/change_path
Browse files Browse the repository at this point in the history
rename src to sql_parser
  • Loading branch information
qidi1 authored Sep 8, 2023
2 parents c2962ef + cdc4e40 commit 2409c8b
Show file tree
Hide file tree
Showing 60 changed files with 109 additions and 139 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ pip install sqlgpt-parser
### Parser SQL

```python
>>> from sql_parser.mysql_parser import parser as mysql_parser
>>> from sqlgpt_parser.sql_parser.mysql_parser import parser as mysql_parser
>>> mysql_parser.parse("select * from t")
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)
>>> from sql_parser.oceanbase_parser import parser as oceanbase_parser
>>> from sqlgpt_parser.sql_parser.oceanbase_parser import parser as oceanbase_parser
>>> oceanbase_parser.parse("select * from t")
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)
>>> from sql_parser.odps_parser import parser as odps_parser
>>> from sqlgpt_parser.sql_parser.odps_parser import parser as odps_parser
>>> odps_parser.parse("select * from t")
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)
```

### Format SQL
```python
>>> from sql_parser.format.formatter import format_sql
>>> from sql_parser.mysql_parser import parser
>>> from sqlgpt_parser.format.formatter import format_sql
>>> from sqlgpt_parser.mysql_parser import parser
>>> result=parser.parse("select * from t")
>>> format_sql(result)
'SELECT\n *\nFROM\n t'
Expand Down
5 changes: 3 additions & 2 deletions docs/docs-ch/Parser Module Development Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ The comment in `p_delete` corresponds to the syntax rule for the `DELETE` statem
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.

```python
from src.parser.mysql_parser.parser import parser as mysql_parser
from src.parser.mysql_parser.lexer import lexer as mysql_lexer
from sqlgpt_parser.parser.mysql_parser.parser import parser as mysql_parser
from sqlgpt_parser.parser.mysql_parser.lexer import lexer as mysql_lexer

sql = "DELETE FROM t WHERE a=1"
result = mysql_parser.parse(sql, lexer=mysql_lexer)
```
Expand Down
7 changes: 4 additions & 3 deletions docs/docs-ch/SQL Parser 开发指南.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

## 词法解析与语法解析

Expand Down Expand Up @@ -161,7 +161,7 @@ def p_expr_paren(p):

## `sqlgpt-parser``parser` 实现

`sqlgpt-parser` 中共有三个 SQL 语法解析器 ,分别在 [mysql_parser](../../src/sql_parser/mysql_parser)[oceanbase_parser](../../src/sql_parser/oceanbase_parser)[odps_parser](../../src/sql_parser/odps_parser) 文件夹下,三个文件夹都包含`lexer.py``reserved.py``parser.py` 三个文件。
`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` 三个文件。

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

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

```python
from src.sql_parser.mysql_parser import parser as mysql_parser
from sqlgpt_parser.sql_parser.mysql_parser import parser as mysql_parser

sql = "DELETE FROM t WHERE a=1"
result = mysql_parser.parse(sql)
```
Expand Down
7 changes: 4 additions & 3 deletions docs/docs-en/SQL Parser Development Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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`).

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](../../src/sql_parser/) folder of the project before the program runs, generating executable code.
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.

## Lexical Analysis and Syntax Analysis

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

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

There are a total of three SQL parsers in `sqlgpt-parser`, located in the [mysql_parser](../../src/sql_parser/mysql_parser), [oceanbase_parser](../../src/sql_parser/oceanbase_parser), and [odps_parser](../../src/sql_parser/odps_parser) folders. Each of these folders contains three files: `lexer.py`, `reserved.py`, and `parser.py`.
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`.

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.

Expand Down Expand Up @@ -245,7 +245,8 @@ The comment in `p_delete` corresponds to the syntax rule for the `DELETE` statem
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.

```python
from src.sql_parser.mysql_parser import parser as mysql_parser
from sqlgpt_parser.sql_parser.mysql_parser import parser as mysql_parser

sql = "DELETE FROM t WHERE a=1"
result = mysql_parser.parse(sql)
```
Expand Down
4 changes: 1 addition & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi"

[project]
name = "sqlgpt-parser"
version = "0.0.1a1"
version = "0.0.1a2"
authors = [
{ name="luliwjc", email="[email protected]" },
{ name="Ifffff", email="[email protected]" },
Expand All @@ -24,8 +24,6 @@ dependencies = [
"ply == 3.11",
]

[tool.flit.module]
name = "sql_parser"
[project.urls]
"Homepage" = "https://github.com/eosphoros-ai/sqlgpt-parser"
"Bug Tracker" = "https://github.com/eosphoros-ai/sqlgpt-parser/issues"
Expand Down
Empty file added sqlgpt_parser/__init__.py
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
"""
from src.sql_parser.tree import JsonTableColumn
from src.sql_parser.tree.explain import ExplainFormat, ExplainType
from src.sql_parser.tree.grouping import SimpleGroupBy
from src.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from src.sql_parser.tree.table import Table, TableSubquery
from src.sql_parser.tree.visitor import AstVisitor
from src.sql_parser.parser_utils import FIELD_REFERENCE_PREFIX
from sqlgpt_parser.sql_parser.tree import JsonTableColumn
from sqlgpt_parser.sql_parser.tree.explain import ExplainFormat, ExplainType
from sqlgpt_parser.sql_parser.tree.grouping import SimpleGroupBy
from sqlgpt_parser.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from sqlgpt_parser.sql_parser.tree.table import Table, TableSubquery
from sqlgpt_parser.sql_parser.tree.visitor import AstVisitor
from sqlgpt_parser.sql_parser.parser_utils import FIELD_REFERENCE_PREFIX


class Formatter(AstVisitor):
Expand Down
5 changes: 1 addition & 4 deletions src/__init__.py → sqlgpt_parser/sql_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,4 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
"""

import sys

sys.path.insert(0, '..')
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import re
from ply import lex

from src.sql_parser.mysql_parser.reserved import (
from sqlgpt_parser.sql_parser.mysql_parser.reserved import (
reversed,
nonreserved,
not_keyword_token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import types, threading

from src.sql_parser.tree.window import (
from sqlgpt_parser.sql_parser.tree.window import (
FrameBound,
FrameClause,
FrameExpr,
Expand All @@ -23,9 +23,9 @@
WindowSpec,
)

from src.sql_parser.tree.with_stmt import CommonTableExpr, With, WithHasQuery
from src.sql_parser.tree.index_type import IndexType
from src.sql_parser.tree.expression import (
from sqlgpt_parser.sql_parser.tree.with_stmt import CommonTableExpr, With, WithHasQuery
from sqlgpt_parser.sql_parser.tree.index_type import IndexType
from sqlgpt_parser.sql_parser.tree.expression import (
AggregateFunc,
ArithmeticBinaryExpression,
ArithmeticUnaryExpression,
Expand Down Expand Up @@ -60,9 +60,9 @@
WhenClause,
JsonTableColumn,
)
from src.sql_parser.tree.grouping import SimpleGroupBy
from src.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from src.sql_parser.tree.literal import (
from sqlgpt_parser.sql_parser.tree.grouping import SimpleGroupBy
from sqlgpt_parser.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from sqlgpt_parser.sql_parser.tree.literal import (
BooleanLiteral,
DateLiteral,
DoubleLiteral,
Expand All @@ -73,21 +73,21 @@
DefaultLiteral,
ErrorLiteral,
)
from src.sql_parser.tree.node import Node
from src.sql_parser.tree.qualified_name import QualifiedName
from src.sql_parser.tree.query_specification import QuerySpecification
from src.sql_parser.tree.relation import AliasedRelation, Join
from src.sql_parser.tree.select import Select
from src.sql_parser.tree.select_item import Partition, SingleColumn
from src.sql_parser.tree.set_operation import Except, Intersect, Union
from src.sql_parser.tree.sort_item import ByItem, PartitionByClause, SortItem
from src.sql_parser.tree.statement import Delete, Insert, Query, Update
from src.sql_parser.tree.table import Table
from src.sql_parser.tree.values import Values
from src.sql_parser.tree.field_type import UNSPECIFIEDLENGTH, FieldType, SQLType
from sqlgpt_parser.sql_parser.tree.node import Node
from sqlgpt_parser.sql_parser.tree.qualified_name import QualifiedName
from sqlgpt_parser.sql_parser.tree.query_specification import QuerySpecification
from sqlgpt_parser.sql_parser.tree.relation import AliasedRelation, Join
from sqlgpt_parser.sql_parser.tree.select import Select
from sqlgpt_parser.sql_parser.tree.select_item import Partition, SingleColumn
from sqlgpt_parser.sql_parser.tree.set_operation import Except, Intersect, Union
from sqlgpt_parser.sql_parser.tree.sort_item import ByItem, PartitionByClause, SortItem
from sqlgpt_parser.sql_parser.tree.statement import Delete, Insert, Query, Update
from sqlgpt_parser.sql_parser.tree.table import Table
from sqlgpt_parser.sql_parser.tree.values import Values
from sqlgpt_parser.sql_parser.tree.field_type import UNSPECIFIEDLENGTH, FieldType, SQLType

from ply import yacc
from src.sql_parser.mysql_parser.lexer import tokens, lexer
from sqlgpt_parser.sql_parser.mysql_parser.lexer import tokens, lexer

tokens = tokens

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import re
from ply import lex

from src.sql_parser.odps_parser.reserved import reserved, nonreserved
from sqlgpt_parser.sql_parser.oceanbase_parser.reserved import reserved, nonreserved

tokens = (
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from __future__ import print_function

import types, threading
from src.sql_parser.tree.window import (
from sqlgpt_parser.sql_parser.tree.window import (
FrameBound,
FrameClause,
FrameExpr,
Expand All @@ -23,9 +23,9 @@
)

from ply import yacc
from src.sql_parser.tree.index_type import IndexType
from src.sql_parser.oceanbase_parser.lexer import tokens, lexer
from src.sql_parser.tree.expression import (
from sqlgpt_parser.sql_parser.tree.index_type import IndexType
from sqlgpt_parser.sql_parser.oceanbase_parser.lexer import tokens, lexer
from sqlgpt_parser.sql_parser.tree.expression import (
AggregateFunc,
ArithmeticBinaryExpression,
ArithmeticUnaryExpression,
Expand Down Expand Up @@ -60,9 +60,9 @@
WhenClause,
JsonTableColumn,
)
from src.sql_parser.tree.grouping import SimpleGroupBy
from src.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from src.sql_parser.tree.literal import (
from sqlgpt_parser.sql_parser.tree.grouping import SimpleGroupBy
from sqlgpt_parser.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from sqlgpt_parser.sql_parser.tree.literal import (
BooleanLiteral,
DateLiteral,
DoubleLiteral,
Expand All @@ -73,19 +73,19 @@
DefaultLiteral,
ErrorLiteral,
)
from src.sql_parser.tree.node import Node
from src.sql_parser.tree.qualified_name import QualifiedName
from src.sql_parser.tree.query_specification import QuerySpecification
from src.sql_parser.tree.relation import AliasedRelation, Join
from src.sql_parser.tree.select import Select
from src.sql_parser.tree.select_item import Partition, SingleColumn
from src.sql_parser.tree.set_operation import Except, Intersect, Union
from src.sql_parser.tree.sort_item import ByItem, PartitionByClause, SortItem
from src.sql_parser.tree.statement import Delete, Insert, Query, Update
from src.sql_parser.tree.table import Table
from src.sql_parser.tree.values import Values
from src.sql_parser.tree.field_type import UNSPECIFIEDLENGTH, FieldType, SQLType
from src.sql_parser.tree.with_stmt import With, CommonTableExpr, WithHasQuery
from sqlgpt_parser.sql_parser.tree.node import Node
from sqlgpt_parser.sql_parser.tree.qualified_name import QualifiedName
from sqlgpt_parser.sql_parser.tree.query_specification import QuerySpecification
from sqlgpt_parser.sql_parser.tree.relation import AliasedRelation, Join
from sqlgpt_parser.sql_parser.tree.select import Select
from sqlgpt_parser.sql_parser.tree.select_item import Partition, SingleColumn
from sqlgpt_parser.sql_parser.tree.set_operation import Except, Intersect, Union
from sqlgpt_parser.sql_parser.tree.sort_item import ByItem, PartitionByClause, SortItem
from sqlgpt_parser.sql_parser.tree.statement import Delete, Insert, Query, Update
from sqlgpt_parser.sql_parser.tree.table import Table
from sqlgpt_parser.sql_parser.tree.values import Values
from sqlgpt_parser.sql_parser.tree.field_type import UNSPECIFIEDLENGTH, FieldType, SQLType
from sqlgpt_parser.sql_parser.tree.with_stmt import With, CommonTableExpr, WithHasQuery

tokens = tokens

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import re
from ply import lex

from src.sql_parser.oceanbase_parser.reserved import reserved, nonreserved
from sqlgpt_parser.sql_parser.odps_parser.reserved import reserved, nonreserved

tokens = (
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from __future__ import print_function

import types, threading
from src.sql_parser.tree.window import (
from sqlgpt_parser.sql_parser.tree.window import (
FrameBound,
FrameClause,
FrameExpr,
Expand All @@ -24,9 +24,9 @@

from ply import yacc

from src.sql_parser.tree.index_type import IndexType
from src.sql_parser.odps_parser.lexer import tokens, lexer
from src.sql_parser.tree.expression import (
from sqlgpt_parser.sql_parser.tree.index_type import IndexType
from sqlgpt_parser.sql_parser.odps_parser.lexer import tokens, lexer
from sqlgpt_parser.sql_parser.tree.expression import (
AggregateFunc,
ArithmeticBinaryExpression,
ArithmeticUnaryExpression,
Expand Down Expand Up @@ -61,9 +61,9 @@
WhenClause,
JsonTableColumn,
)
from src.sql_parser.tree.grouping import SimpleGroupBy
from src.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from src.sql_parser.tree.literal import (
from sqlgpt_parser.sql_parser.tree.grouping import SimpleGroupBy
from sqlgpt_parser.sql_parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from sqlgpt_parser.sql_parser.tree.literal import (
BooleanLiteral,
DateLiteral,
DoubleLiteral,
Expand All @@ -74,19 +74,19 @@
DefaultLiteral,
ErrorLiteral,
)
from src.sql_parser.tree.node import Node
from src.sql_parser.tree.qualified_name import QualifiedName
from src.sql_parser.tree.query_specification import QuerySpecification
from src.sql_parser.tree.relation import AliasedRelation, Join
from src.sql_parser.tree.select import Select
from src.sql_parser.tree.select_item import Partition, SingleColumn
from src.sql_parser.tree.set_operation import Except, Intersect, Union
from src.sql_parser.tree.sort_item import ByItem, PartitionByClause, SortItem
from src.sql_parser.tree.statement import Delete, Insert, Query, Update
from src.sql_parser.tree.table import Table
from src.sql_parser.tree.values import Values
from src.sql_parser.tree.field_type import UNSPECIFIEDLENGTH, FieldType, SQLType
from src.sql_parser.tree.with_stmt import With, CommonTableExpr, WithHasQuery
from sqlgpt_parser.sql_parser.tree.node import Node
from sqlgpt_parser.sql_parser.tree.qualified_name import QualifiedName
from sqlgpt_parser.sql_parser.tree.query_specification import QuerySpecification
from sqlgpt_parser.sql_parser.tree.relation import AliasedRelation, Join
from sqlgpt_parser.sql_parser.tree.select import Select
from sqlgpt_parser.sql_parser.tree.select_item import Partition, SingleColumn
from sqlgpt_parser.sql_parser.tree.set_operation import Except, Intersect, Union
from sqlgpt_parser.sql_parser.tree.sort_item import ByItem, PartitionByClause, SortItem
from sqlgpt_parser.sql_parser.tree.statement import Delete, Insert, Query, Update
from sqlgpt_parser.sql_parser.tree.table import Table
from sqlgpt_parser.sql_parser.tree.values import Values
from sqlgpt_parser.sql_parser.tree.field_type import UNSPECIFIEDLENGTH, FieldType, SQLType
from sqlgpt_parser.sql_parser.tree.with_stmt import With, CommonTableExpr, WithHasQuery

tokens = tokens

Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 2409c8b

Please sign in to comment.