Skip to content

Commit

Permalink
Merge pull request #5 from qidi1/rename_sql_parser
Browse files Browse the repository at this point in the history
change sql_parser to parser
  • Loading branch information
qidi1 authored Sep 8, 2023
2 parents 2409c8b + 99a043b commit 619835e
Show file tree
Hide file tree
Showing 55 changed files with 129 additions and 109 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@ pip install sqlgpt-parser
### Parser SQL

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

## 词法解析与语法解析

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

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

`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` 三个文件。
`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` 三个文件。

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

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

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

sql = "DELETE FROM t WHERE a=1"
result = mysql_parser.parse(sql)
Expand Down
6 changes: 3 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](../../sqlgpt_parser/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/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](../../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`.
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`.

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,7 @@ 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 sqlgpt_parser.sql_parser.mysql_parser import parser as mysql_parser
from sqlgpt_parser.parser.mysql_parser import parser as mysql_parser

sql = "DELETE FROM t WHERE a=1"
result = mysql_parser.parse(sql)
Expand Down
8 changes: 4 additions & 4 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.1a2"
version = "0.0.1a3"
authors = [
{ name="luliwjc", email="[email protected]" },
{ name="Ifffff", email="[email protected]" },
Expand Down Expand Up @@ -37,7 +37,7 @@ dependencies = [
[tool.black]
skip-string-normalization = 1
force-exclude = '''
src/sql_parser/mysql_parser/parser_table.py
| src/sql_parser/oceanbase_parser/parser_table.py
| src/sql_parser/odps_parser/parser_table.py
sqlgpt_parser/parser/mysql_parser/parser_table.py
| sqlgpt_parser/parser/oceanbase_parser/parser_table.py
| sqlgpt_parser/parser/odps_parser/parser_table.py
'''
12 changes: 12 additions & 0 deletions sqlgpt_parser/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# coding=utf-8
"""
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
"""
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
14 changes: 7 additions & 7 deletions sqlgpt_parser/format/formatter.py
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 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
from sqlgpt_parser.parser.tree import JsonTableColumn
from sqlgpt_parser.parser.tree.explain import ExplainFormat, ExplainType
from sqlgpt_parser.parser.tree.grouping import SimpleGroupBy
from sqlgpt_parser.parser.tree.join_criteria import JoinOn, JoinUsing, NaturalJoin
from sqlgpt_parser.parser.tree.table import Table, TableSubquery
from sqlgpt_parser.parser.tree.visitor import AstVisitor
from sqlgpt_parser.parser.parser_utils import FIELD_REFERENCE_PREFIX


class Formatter(AstVisitor):
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 sqlgpt_parser.sql_parser.mysql_parser.reserved import (
from sqlgpt_parser.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 sqlgpt_parser.sql_parser.tree.window import (
from sqlgpt_parser.parser.tree.window import (
FrameBound,
FrameClause,
FrameExpr,
Expand All @@ -23,9 +23,9 @@
WindowSpec,
)

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

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

tokens = tokens

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

from sqlgpt_parser.sql_parser.oceanbase_parser.reserved import reserved, nonreserved
from sqlgpt_parser.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 sqlgpt_parser.sql_parser.tree.window import (
from sqlgpt_parser.parser.tree.window import (
FrameBound,
FrameClause,
FrameExpr,
Expand All @@ -23,9 +23,9 @@
)

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

tokens = tokens

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
"""
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import re
from ply import lex

from sqlgpt_parser.sql_parser.odps_parser.reserved import reserved, nonreserved
from sqlgpt_parser.parser.odps_parser.reserved import reserved, nonreserved

tokens = (
[
Expand Down
Loading

0 comments on commit 619835e

Please sign in to comment.