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

Python: Improve project setup and test runner conveniences after unboxing. Add CI configuration. Prepare packaging. #10

Merged
merged 6 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 2

updates:

- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "weekly"

- package-ecosystem: "pip"
directory: "/cratedb_sqlparse_py"
schedule:
interval: "weekly"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
88 changes: 88 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
name: Python Tests

on:
pull_request: ~
push:
branches: [ main ]

# Allow job to be triggered manually.
workflow_dispatch:

# Run job each night after CrateDB nightly has been published.
schedule:
- cron: '0 3 * * *'

# Cancel in-progress jobs when pushing to the same branch.
concurrency:
cancel-in-progress: true
group: ${{ github.workflow }}-${{ github.ref }}

# Select Python grammar.
defaults:
run:
working-directory: cratedb_sqlparse_py

jobs:

tests:

runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ["ubuntu-latest"]
python-version: ["3.8", "3.12"]

env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python-version }}

# https://docs.github.com/en/actions/using-containerized-services/about-service-containers
services:
cratedb:
image: crate/crate:nightly
ports:
- 4200:4200
- 5432:5432
env:
CRATE_HEAP_SIZE: 4g

name: Python ${{ matrix.python-version }} on OS ${{ matrix.os }}
steps:

- name: Acquire sources
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
architecture: x64
cache: 'pip'
cache-dependency-path: 'pyproject.toml'

- name: Setup project
run: |

# `setuptools 0.64.0` adds support for editable install hooks (PEP 660).
# https://github.com/pypa/setuptools/blob/main/CHANGES.rst#v6400
pip install "setuptools>=64" --upgrade

# Install package in editable mode.
pip install --use-pep517 --prefer-binary --editable='.[develop,generate,test]'

- name: Run linter and software tests
run: |
poe check

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
files: ./coverage.xml
flags: unittests
env_vars: OS,PYTHON
name: codecov-umbrella
fail_ci_if_error: true
8 changes: 0 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,3 @@ cython_debug/
*.tokens
*.interp
*.g4


*SqlBaseLexer.js
*SqlBaseParser.js

*SqlBaseLexer.py
*SqlBaseParser.py
*SqlBaseParserListener.py
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## Unreleased

## 2024/05/17 0.0.1
- Initial release
39 changes: 16 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# cratedb-sqlparse

`Antlr4` is a parser generator for reading, processing and executing text, there are several
target languages (Java, Python, JavaScript, Dart...) available. CrateDB uses the Java target.
target languages (Java, Python, JavaScript, Dart) available. CrateDB uses the Java target.

The repository holds libraries/packages created from some of those available languages, so
far: `Python` and `JavaScript`.
More might be added if needed in the future.
far: `Python` and `JavaScript`. More might be added if needed in the future.

These libraries allow you to parse Crate's SQL dialect without sending it to a CrateDB instance.

Expand Down Expand Up @@ -50,29 +49,23 @@ exceptions as error listener, dollar strings and any new one. See past commits t
implemented in Python and Javascript, remember that CrateDB's SQLParser written in Java is the most
complete and the default reference.

## Building locally & using a different CrateDB version.
## Building locally & using a different CrateDB version

The generated parser is not uploaded to the repository since it's huge, to use the package locally or
to build a different version use the build script.

#### Clone the project
`git clone [email protected]:crate/cratedb-sqlparse.git`

#### Install the dependencies
`pip install antlr4-python3-runtime requests`

#### Run the build script
`python3 setup_grammar.py`
### Acquire sources
```shell
git clone [email protected]:crate/cratedb-sqlparse.git
cd cratedb-sqlparse
```

### Install dependencies
```
pip install -r requirements.txt
```

> At the end of the build script `setup_grammar.py` the target and the CrateDB version can be modified.
>
> ```python
> if __name__ == '__main__':
> version = '5.6.4'
> target = Antlr4Target.python
> download_cratedb_grammar(version)
> compile_grammar(target)
> patch_lexer(target)
> set_version(target, version)
>```
### Generate grammar files
```shell
poe generate
```
3 changes: 3 additions & 0 deletions cratedb_sqlparse_py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*/generated_parser/SqlBaseLexer.py
*/generated_parser/SqlBaseParser.py
*/generated_parser/SqlBaseParserListener.py
13 changes: 12 additions & 1 deletion cratedb_sqlparse_py/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# cratedb_sqlparse
# CrateDB SQL Parser for Python

This package provides utilities to validate and split SQL statements specifically designed for CrateDB.

Expand Down Expand Up @@ -36,3 +36,14 @@ print(select_query.tree)
sqlparse('SUUULECT * FROM sys.shards')
# cratedb_sqlparse.parser.parser.ParsingException: line1:0 mismatched input 'SUUULECT' expecting {'SELECT', 'DEALLOCATE', ...}
```


## Development
```shell
git clone https://github.com/crate/cratedb-sqlparse
cd cratedb-sqlparse/cratedb_sqlparse_py
python3 -m venv .venv
source .venv/bin/activate
pip install --editable='.[develop,generate,release,test]'
poe check
```
4 changes: 3 additions & 1 deletion cratedb_sqlparse_py/cratedb_sqlparse/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

from antlr4 import InputStream, CommonTokenStream, Token
from antlr4.error.ErrorListener import ErrorListener

Expand Down Expand Up @@ -90,7 +92,7 @@ def __repr__(self):
return f'{self.__class__.__qualname__}<{self.query if len(self.query) < 15 else self.query[:15] + "..."}>'


def sqlparse(query: str) -> list[Statement]:
def sqlparse(query: str) -> List[Statement]:
"""
Parses a string into SQL `Statement`.
"""
Expand Down
Loading