Skip to content

Commit

Permalink
feat: schema to use a JSON format and query should be generated in ba…
Browse files Browse the repository at this point in the history
…ckend
  • Loading branch information
sanjibansg committed Oct 5, 2023
1 parent 13ba068 commit 3490705
Show file tree
Hide file tree
Showing 4 changed files with 362 additions and 48 deletions.
20 changes: 18 additions & 2 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from fastapi.openapi.utils import get_openapi
from fastapi_health import health

import json

from duckdb import DuckDBPyConnection

import substrait_validator as sv
Expand All @@ -14,7 +16,6 @@
ParseFromDuckDB,
)

1
from loguru import logger


Expand Down Expand Up @@ -83,12 +84,27 @@ async def ValidateFile(file: UploadFile = File(), override_levels: list[int] = F
status_code=500, detail="Substrait Validator Internal Error: " + str(e)
)


@app.post("/execute/duckdb/")
def ExecuteBackend(data: dict, db_conn: DuckDBPyConnection = Depends(get_duck_conn)):
response = ExecuteDuckDb(data, db_conn)
return response

@app.post("/add_schema/")
def AddSchema(data: dict, db_conn: DuckDBPyConnection = Depends(get_duck_conn)):
schema = data["schema"]
query = "CREATE TEMP TABLE "
json_data = json.loads(schema["schema"])
query += json_data["table"] + "("
for field in json_data["fields"]:
query += field["name"] + " "
query += field["type"] + " "
for props in field["properties"]:
query += props + " "
query += ", "
query += ");"
response = ExecuteDuckDb(query, db_conn)
return response


@app.post("/parse/", status_code=status.HTTP_200_OK)
def ParseToSubstrait(data: dict, db_conn: DuckDBPyConnection = Depends(get_duck_conn)):
Expand Down
27 changes: 24 additions & 3 deletions api/backend/duckdb.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import duckdb
from pydantic import BaseModel
from fastapi import HTTPException

from loguru import logger
Expand All @@ -8,12 +7,34 @@
class DuckDBConnection:
def __init__(self):
self.conn_pool = []

query_lineitem = '''CREATE TABLE IF NOT EXISTS lineitem(
l_orderkey INTEGER NOT NULL,
l_partkey INTEGER NOT NULL,
l_suppkey INTEGER NOT NULL,
l_linenumber INTEGER NOT NULL,
l_quantity INTEGER NOT NULL,
l_extendedprice DECIMAL(15,2) NOT NULL,
l_discount DECIMAL(15,2) NOT NULL,
l_tax DECIMAL(15,2) NOT NULL,
l_returnflag VARCHAR NOT NULL,
l_linestatus VARCHAR NOT NULL,
l_shipdate DATE NOT NULL,
l_commitdate DATE NOT NULL,
l_receiptdate DATE NOT NULL,
l_shipinstruct VARCHAR NOT NULL,
l_shipmode VARCHAR NOT NULL,
l_comment VARCHAR NOT NULL);'''
conn = duckdb.connect("duck.db")
conn.execute(query=query_lineitem)

for i in range(5):
conn = duckdb.connect("duck.db")
conn.install_extension("substrait")
conn.load_extension("substrait")
self.conn_pool.append(conn)


def check_pool(self):
if len(self.conn_pool) == 0:
print("creating new conn objects")
Expand Down Expand Up @@ -44,9 +65,9 @@ def CheckDuckDBConnection(con):
return status


def ExecuteDuckDb(data, con):
def ExecuteDuckDb(query, con):
try:
con.execute(query=data["query"])
con.execute(query = query)
return {"message": "DuckDB Operation successful"}
except Exception as e:
raise HTTPException(
Expand Down
7 changes: 4 additions & 3 deletions api/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from fastapi.testclient import TestClient
from io import BytesIO
import pytest
import requests
from app import app

Expand Down Expand Up @@ -51,7 +50,8 @@ def test_duckdb_execute():
res = client.post(
"/execute/duckdb/",
json={
"query": "CREATE TABLE IF NOT EXISTS weather(city VARCHAR, temp_lo INTEGER);",
"query": '''CREATE TABLE IF NOT EXISTS
weather(city VARCHAR, temp_lo INTEGER);''',
},
)
print(res.json())
Expand All @@ -65,7 +65,8 @@ def test_parse_to_substrait():
client.post(
"/execute/duckdb/",
json={
"query": "CREATE TABLE IF NOT EXISTS test_fiddle(id INTEGER NOT NULL, key INTEGER NOT NULL);",
"query": '''CREATE TABLE IF NOT EXISTS test_fiddle(
id INTEGER NOT NULL, key INTEGER NOT NULL);''',
},
)
response = client.post(
Expand Down
Loading

0 comments on commit 3490705

Please sign in to comment.