-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a SQL server speaking the presto protocol (#56)
* Implement a SQL server speaking the presto protocol This server can be used to run dask-sql in a standalone application. It is e.g. possible to run it in a dask-cluster and answer SQL queries from external. * Stylefix * Fixes to docker build setup
- Loading branch information
1 parent
a33dbfb
commit 71ada13
Showing
17 changed files
with
505 additions
and
475 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
[run] | ||
omit = tests/* | ||
dask_sql/server/* | ||
branch = True | ||
|
||
[report] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Dockerfile for dask-sql running the SQL server | ||
# For more information, see https://dask-sql.readthedocs.io/. | ||
FROM continuumio/miniconda3:4.8.2 | ||
LABEL author "Nils Braun <[email protected]>" | ||
|
||
# Install dependencies for dask-sql | ||
COPY conda.yaml /opt/dask_sql/ | ||
RUN /opt/conda/bin/conda install \ | ||
--file /opt/dask_sql/conda.yaml \ | ||
-c conda-forge | ||
|
||
# Build the java libraries | ||
COPY setup.py /opt/dask_sql/ | ||
COPY .git /opt/dask_sql/.git | ||
COPY planner /opt/dask_sql/planner | ||
RUN cd /opt/dask_sql/ \ | ||
&& python setup.py java | ||
|
||
# Install the python library | ||
COPY dask_sql /opt/dask_sql/dask_sql | ||
RUN cd /opt/dask_sql/ \ | ||
&& pip install -e . | ||
|
||
# Set the script to execute | ||
EXPOSE 8080 | ||
ENV JAVA_HOME /opt/conda | ||
ENTRYPOINT [ "/opt/conda/bin/python", "/opt/dask_sql/dask_sql/server/app.py" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ maven>=3.6.0 | |
pytest>=6.0.1 | ||
pytest-cov>=2.10.1 | ||
sphinx>=3.2.1 | ||
fastapi>=0.61.1 | ||
uvicorn>=0.11.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from .context import Context | ||
from .server.app import run_server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from argparse import ArgumentParser | ||
|
||
from dask_sql.server.responses import DataResults, QueryResults, ErrorResults | ||
from fastapi import FastAPI, Request | ||
import uvicorn | ||
|
||
from dask_sql import Context | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/v1/empty") | ||
async def empty(request: Request): | ||
""" | ||
Helper endpoint returning an empty | ||
result. | ||
""" | ||
return QueryResults(request=request) | ||
|
||
|
||
@app.post("/v1/statement") | ||
async def query(request: Request): | ||
""" | ||
Main endpoint returning query results | ||
in the presto on wire format. | ||
""" | ||
try: | ||
sql = (await request.body()).decode().strip() | ||
df = request.app.c.sql(sql) | ||
|
||
return DataResults(df, request=request) | ||
except Exception as e: | ||
return ErrorResults(e, request=request) | ||
|
||
|
||
def run_server( | ||
context: Context = None, host: str = "0.0.0.0", port: int = 8080 | ||
): # pragma: no cover | ||
""" | ||
Run a HTTP server for answering SQL queries using ``dask-sql``. | ||
It uses the `Presto Wire Protocol <https://github.com/prestodb/presto/wiki/HTTP-Protocol>`_ | ||
for communication. | ||
This means, it has a single POST endpoint `v1/statement`, which answers | ||
SQL queries (as string in the body) with the output as a JSON | ||
(in the format described in the documentation above). | ||
Every SQL expression that ``dask-sql`` understands can be used here. | ||
Note: | ||
The presto protocol also includes some statistics on the query | ||
in the response. | ||
These statistics are currently only filled with placeholder variables. | ||
Args: | ||
context (:obj:`dask_sql.Context`): If set, use this context instead of an empty one. | ||
host (:obj:`str`): The host interface to listen on (defaults to all interfaces) | ||
port (:obj:`int`): The port to listen on (defaults to 8080) | ||
Example: | ||
It is possible to run an SQL server by using the CLI script in ``dask_sql.server.app`` | ||
or by calling this function directly in your user code: | ||
.. code-block:: python | ||
from dask_sql import run_server | ||
# Create your pre-filled context | ||
c = Context() | ||
... | ||
run_server(context=c) | ||
After starting the server, it is possible to send queries to it, e.g. with the | ||
`presto CLI <https://prestosql.io/docs/current/installation/cli.html>`_ | ||
or via sqlalchemy (e.g. using the `PyHive <https://github.com/dropbox/PyHive#sqlalchemy>`_ package): | ||
.. code-block:: python | ||
from sqlalchemy.engine import create_engine | ||
engine = create_engine('presto://localhost:8080/') | ||
import pandas as pd | ||
pd.read_sql_query("SELECT 1 + 1", con=engine) | ||
Of course, it is also possible to call the usual ``CREATE TABLE`` | ||
commands. | ||
""" | ||
if context is None: | ||
context = Context() | ||
|
||
app.c = context | ||
|
||
uvicorn.run(app, host=host, port=port) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = ArgumentParser() | ||
parser.add_argument( | ||
"--host", | ||
default="0.0.0.0", | ||
help="The host interface to listen on (defaults to all interfaces)", | ||
) | ||
parser.add_argument( | ||
"--port", default=8080, help="The port to listen on (defaults to 8080)" | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
run_server(host=args.host, port=args.port) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.