Skip to main content

There will be many times when we need to run SQL queries via python code. Often this is hadeled by copying our sql into our python code as a string and executed it using jaydebeapi. This works well enough but makes version control a pain if we are editing sql files and then copying them into our python, especially if we are outsourcing the actual development of the queries to another team.

SqlParser is a Python package that wraps around jaydebeapi that seeks to address this issue by automatically parsing our SQL queries based on a few simple annotations that get added when a query is being developed.

This is currently only tested for teradata but it should work with any database supported by jaydebeapi.

SQL Annotation

There are three types of sql "Blocks" that can be created using the following annotations: --:--SETUP These are queries that get run first to maybe create some intermediate table. No data is returned from these --:--DATA: output_filename These are queries that extract data, and get saved under theme given inlace of "output_filename" --:--CLEANUP Finally these are queries that get run last two do any cleanup that needs to be done, maybe to remove and intermediate table.

You can have more than one of each block and within each Block, you can have multiple commands and each should be terminated by a semicolon (;). Note: Data blocks should only contain one command. Here is a simple example using all three types of block but note you will often only need a "DATA" block:

Example Annotated SQL

--:--SETUP
CREATE  table temporary_table as
Select * from table_a a
Join table_b  b
on a.key = b.key;

CREATE  table temporary_table2 as
Select * from new_table_a a
Join table_b  b
on a.key = b.key;

--:--DATA: tracked
Select * from temporaty_table
Where package_type = “tracked”;

--:--DATA: sd_agg
Select route_id, count(barcode)
From temporary_table
Where package_type = “special_delivery”
groupby 1;


--:--CLEANUP
DROP table temporary_table;
DROP table temporary_table2;

Using the Parser

Once you have annotated your sql you can easily use the SqlParser to parse and execute your commands. Here is a simple example: SqlParser Example

import glob
import yaml
from sqlparser import SqlParser


#load credentials
with open('credentials.yaml', 'r') as file:
    creds = yaml.load(file)

#create a parser and point it to where you want the sql extracts to be stored
parser = SqlParser(output_basepath="/path/to/store/files/", sep="|")

#set the address and credentials
parser.set_address("//00.000.00.000/DBS_PORT=0000") #replace address with ip and port of your database
parser.set_credentials(creds['td_username'], creds['td_password'])
parser.open_connection()

#run all the queries you want executed
queries = glob.glob("path/to/annotated/files/*.sql")
for query in queries:
    parser.parse_sql(path=query)
    parser.run_all()

Release files for sqlparser 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for sqlparser 0.0.9
File Interpreter ABI Platform
sqlparser-0.0.9-py3-none-any.whl Python 3 none any Details

Release files / sqlparser-0.0.9-py3-none-any.whl

Download URL sqlparser-0.0.9-py3-none-any.whl
Size 21.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
790e6df69bd66103a37bf27c703ad6748939496ef7cd59eb16b20ff77b150998
BLAKE2b-256 checksum
How to use checksums
74d917967b031764371e517a6209646a1872a8c91af2fd8f3d57c68bf1783eaa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.14.2 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.2

Release history Release notifications | RSS feed

This release

0.0.9 This release

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page