Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cldellow/ersatz-table-valued-functions
A bad idea
https://github.com/cldellow/ersatz-table-valued-functions
sqlglot sqlite
Last synced: 20 days ago
JSON representation
A bad idea
- Host: GitHub
- URL: https://github.com/cldellow/ersatz-table-valued-functions
- Owner: cldellow
- License: apache-2.0
- Created: 2023-01-21T17:45:49.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-22T18:09:28.000Z (almost 2 years ago)
- Last Synced: 2024-10-04T22:16:36.442Z (about 1 month ago)
- Topics: sqlglot, sqlite
- Language: Python
- Homepage:
- Size: 11.7 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ersatz-table-valued-functions
[![PyPI](https://img.shields.io/pypi/v/ersatz-table-valued-functions.svg)](https://pypi.org/project/ersatz-table-valued-functions/)
[![Changelog](https://img.shields.io/github/v/release/cldellow/ersatz-table-valued-functions?include_prereleases&label=changelog)](https://github.com/cldellow/ersatz-table-valued-functions/releases)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/cldellow/ersatz-table-valued-functions/blob/main/LICENSE)A bad idea.
**ersatz** *(adj.)*: made or used as a substitute, typically an inferior one, for something else.
See also [datasette-ersatz-tablue-valued-functions](https://github.com/cldellow/datasette-ersatz-table-valued-functions/).
## Installation
Install this library using `pip`:
pip install ersatz-table-valued-functions
## Usage
This library lets you rewrite queries like:
```sql
SELECT root, square FROM tbl_squares(3)
```into queries like:
```sql
WITH _ersatz_1 AS (
SELECT
JSON_EXTRACT(value, '$[0]') AS "root",
JSON_EXTRACT(value, '$[1]') AS "square"
FROM JSON_EACH((SELECT tbl_squares(3)))
)
SELECT root, square FROM _ersatz_1
```That is: it translates a query that _looks_ like it needs a table-valued function
into one that uses a scalar-valued function that returns a JSON 2D array.To use it:
```python
from ersatz_table_valued_functions import rewriterewrite('SELECT root, square FROM tbl_squares(3)', { 'TBL_SQUARES': ['root', 'square'] })
```## Development
To contribute to this library, first checkout the code. Then create a new virtual environment:
cd ersatz-table-valued-functions
python -m venv venv
source venv/bin/activateNow install the dependencies and test dependencies:
pip install -e '.[test]'
To run the tests:
pytest