{"id":13501278,"url":"https://github.com/ekzhang/inline-sql","last_synced_at":"2025-04-13T05:05:23.246Z","repository":{"id":61087984,"uuid":"546753300","full_name":"ekzhang/inline-sql","owner":"ekzhang","description":"🪄 Inline SQL in any Python program","archived":false,"fork":false,"pushed_at":"2024-02-17T00:50:09.000Z","size":37,"stargazers_count":423,"open_issues_count":0,"forks_count":8,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-13T05:04:46.613Z","etag":null,"topics":["dsl","duckdb","olap","python","query-language","sql"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ekzhang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-10-06T15:32:20.000Z","updated_at":"2025-04-09T16:36:38.000Z","dependencies_parsed_at":"2024-10-25T18:32:43.042Z","dependency_job_id":"c81176ec-4e73-46e8-a240-72d828a38dc4","html_url":"https://github.com/ekzhang/inline-sql","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"cc98c84049ef8eb6c2aad5d1c518d778a7534bad"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekzhang%2Finline-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekzhang%2Finline-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekzhang%2Finline-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ekzhang%2Finline-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ekzhang","download_url":"https://codeload.github.com/ekzhang/inline-sql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248665748,"owners_count":21142123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dsl","duckdb","olap","python","query-language","sql"],"created_at":"2024-07-31T22:01:31.388Z","updated_at":"2025-04-13T05:05:23.220Z","avatar_url":"https://github.com/ekzhang.png","language":"Python","readme":"# Inline SQL\n\n[![PyPI - Version](https://img.shields.io/pypi/v/inline-sql.svg)](https://pypi.org/project/inline-sql)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/inline-sql.svg)](https://pypi.org/project/inline-sql)\n\nA simple embedded language for running inline SQL in Python programs.\n\n```python\nfrom inline_sql import sql, sql_val\n\nassert sql_val^ \"SELECT 1 + 1\" == 2\n\nx = 5\nassert sql_val^ \"SELECT $x * 2\" == 10\n\ndf = sql^ \"SELECT * FROM (VALUES (1, 10), (2, 20)) df (x, y)\"\nassert sql_val^ \"SELECT SUM(x) + SUM(y) FROM df\" == 33\n```\n\nOperations in the `inline_sql` library run directly inside your process. You can query local datasets (pandas frames), CSV files, and even interpolate variables seamlessly. This is implemented as a small wrapper around [DuckDB](https://duckdb.org/), so it is [extremely fast](https://duckdb.org/2021/05/14/sql-on-pandas.html).\n\n## Installation\n\nSupports Python 3.7+, tested on all major operating systems.\n\n```console\npip install inline-sql\n```\n\n## Usage\n\nThe exported `sql` and `sql_val` variables are magic objects that can be used to run queries. Queries can read from local dataframes by name, and they can embed parameters using dollar-sign notation.\n\n```python\n\u003e\u003e\u003e from inline_sql import sql, sql_val\n\n\u003e\u003e\u003e sql_val^ \"SELECT 1 + 1\"\n2\n\n\u003e\u003e\u003e x = 5\n\n\u003e\u003e\u003e sql_val^ \"SELECT 2 * $x\"\n10\n\n\u003e\u003e\u003e sql^ \"SELECT * FROM 'disasters.csv' LIMIT 5\"\n                  Entity  Year   Deaths\n0  All natural disasters  1900  1267360\n1  All natural disasters  1901   200018\n2  All natural disasters  1902    46037\n3  All natural disasters  1903     6506\n4  All natural disasters  1905    22758\n\n\u003e\u003e\u003e disasters = sql^ \"SELECT * FROM 'disasters.csv'\"\n\n\u003e\u003e\u003e def total_deaths(entity: str) -\u003e float:\n...     return sql_val^ \"SELECT SUM(deaths) FROM disasters WHERE Entity = $entity\"\n...\n\n\u003e\u003e\u003e total_deaths(\"Drought\")\n11731294.0\n\n\u003e\u003e\u003e total_deaths(\"Earthquake\")\n2576801.0\n```\n\nYou can run any SQL query as described in the [DuckDB documentation](https://duckdb.org/docs/guides/).\n\n## Library Use\n\nYou can use `inline_sql` as a library. Since results from queries are ordinary `pandas.DataFrame` objects, they work in functions and application code. Here's a longer example:\n\n```python\nimport pandas as pd\nfrom inline_sql import sql, sql_val\n\n\ndef head_data(count: int) -\u003e pd.DataFrame:\n    return sql^ \"SELECT * FROM 'cars.csv' LIMIT $count\"\n\n\ncars = head_data(50)\n\norigin_counts = sql^ \"\"\"\n    SELECT origin, COUNT() FROM cars\n    GROUP BY origin\n    ORDER BY count DESC\n\"\"\"\nprint(origin_counts)\n\nmost_common = origin_counts.origin[0]\nprint(sql_val^ \"\"\"\n    SELECT AVG(horsepower) FROM cars\n    WHERE origin = $most_common\n\"\"\")\n```\n\nIn general, `sql_val` is used to run scalar queries, while `sql` is used to run queries that return tables.\n\n## Acknowledgements\n\nCreated by Eric Zhang ([@ekzhang1](https://twitter.com/ekzhang1)). Licensed under the [MIT license](LICENSE).\n","funding_links":[],"categories":["Python","sql"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekzhang%2Finline-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fekzhang%2Finline-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fekzhang%2Finline-sql/lists"}