Skip to content

Commit

Permalink
docs: add Flight SQL driver recipes (apache#1547)
Browse files Browse the repository at this point in the history
Fixes apache#935.
  • Loading branch information
lidavidm authored Feb 13, 2024
1 parent d462c51 commit 8596879
Show file tree
Hide file tree
Showing 9 changed files with 231 additions and 34 deletions.
48 changes: 16 additions & 32 deletions .github/workflows/native-unix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,14 @@ jobs:
name: driver-manager-${{ matrix.os }}
path: ~/local

- name: Build Python Driver Manager
- name: Build
shell: bash -l {0}
env:
BUILD_ALL: "1"
ADBC_USE_ASAN: "0"
ADBC_USE_UBSAN: "0"
run: |
env BUILD_ALL=0 BUILD_DRIVER_MANAGER=1 ./ci/scripts/python_build.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
./ci/scripts/python_build.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
- name: Build Panic Dummy
shell: bash -l {0}
run: |
Expand All @@ -472,10 +476,6 @@ jobs:
export PANICDUMMY_LIBRARY_PATH=$(pwd)/go/adbc/pkg/libadbc_driver_panicdummy.so
fi
env BUILD_ALL=0 BUILD_DRIVER_MANAGER=1 ./ci/scripts/python_test.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
- name: Build Python Driver Flight SQL
shell: bash -l {0}
run: |
env BUILD_ALL=0 BUILD_DRIVER_FLIGHTSQL=1 ./ci/scripts/python_build.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
- name: Test Python Driver Flight SQL
shell: bash -l {0}
run: |
Expand All @@ -493,26 +493,14 @@ jobs:
export ADBC_TEST_FLIGHTSQL_URI=grpc://localhost:41414
env BUILD_ALL=0 BUILD_DRIVER_FLIGHTSQL=1 ./ci/scripts/python_test.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
kill %1
- name: Build Python Driver PostgreSQL
shell: bash -l {0}
run: |
env BUILD_ALL=0 BUILD_DRIVER_POSTGRESQL=1 ./ci/scripts/python_build.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
- name: Test Python Driver PostgreSQL
shell: bash -l {0}
run: |
env BUILD_ALL=0 BUILD_DRIVER_POSTGRESQL=1 ./ci/scripts/python_test.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
- name: Build Python Driver SQLite
shell: bash -l {0}
run: |
env BUILD_ALL=0 BUILD_DRIVER_SQLITE=1 ./ci/scripts/python_build.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
- name: Test Python Driver SQLite
shell: bash -l {0}
run: |
env BUILD_ALL=0 BUILD_DRIVER_SQLITE=1 ./ci/scripts/python_test.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
- name: Build Python Driver Snowflake
shell: bash -l {0}
run: |
env BUILD_ALL=0 BUILD_DRIVER_SNOWFLAKE=1 ./ci/scripts/python_build.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
- name: Test Python Driver Snowflake
shell: bash -l {0}
run: |
Expand All @@ -527,19 +515,6 @@ jobs:
runs-on: ${{ matrix.os }}
needs:
- drivers-build-conda
services:
postgres:
image: postgres
env:
POSTGRES_DB: postgres
POSTGRES_PASSWORD: password
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
strategy:
matrix:
os: ["ubuntu-latest"]
Expand Down Expand Up @@ -579,8 +554,12 @@ jobs:

- name: Build Python
shell: bash -l {0}
env:
BUILD_ALL: "1"
ADBC_USE_ASAN: "0"
ADBC_USE_UBSAN: "0"
run: |
env BUILD_ALL=1 ./ci/scripts/python_build.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
./ci/scripts/python_build.sh "$(pwd)" "$(pwd)/build" "$HOME/local"
# Docs requires Python packages since it runs doctests
- name: Build Docs
shell: bash -l {0}
Expand All @@ -601,5 +580,10 @@ jobs:
shell: bash -l {0}
env:
ADBC_POSTGRESQL_TEST_URI: "postgres://localhost:5432/postgres?user=postgres&password=password"
ADBC_DREMIO_FLIGHTSQL_URI: "grpc+tcp://localhost:32010"
ADBC_DREMIO_FLIGHTSQL_USER: "dremio"
ADBC_DREMIO_FLIGHTSQL_PASS: "dremio123"
ADBC_SQLITE_FLIGHTSQL_URI: "grpc+tcp://localhost:8080"
run: |
docker compose up --detach --wait dremio dremio-init flightsql-sqlite-test postgres-test
pytest -vvs docs/source/tests/
12 changes: 11 additions & 1 deletion ci/scripts/python_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,24 @@ set -e
: ${BUILD_DRIVER_SQLITE:=${BUILD_ALL}}
: ${BUILD_DRIVER_SNOWFLAKE:=${BUILD_ALL}}

: ${ADBC_USE_ASAN:=ON}
: ${ADBC_USE_UBSAN:=ON}

: ${ADBC_CMAKE_ARGS:=""}
: ${CMAKE_BUILD_TYPE:=Debug}

main() {
local -r source_dir="${1}"
local -r build_dir="${2}"

set -x

cmake -S "${source_dir}/c" -B ${build_dir} \
cmake -S "${source_dir}/c" \
-B ${build_dir} \
${ADBC_CMAKE_ARGS} \
-DCMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE}" \
-DADBC_USE_ASAN="${ADBC_USE_ASAN}" \
-DADBC_USE_UBSAN="${ADBC_USE_UBSAN}" \
-DADBC_DRIVER_MANAGER=${BUILD_DRIVER_MANAGER} \
-DADBC_DRIVER_FLIGHTSQL=${BUILD_DRIVER_FLIGHTSQL} \
-DADBC_DRIVER_POSTGRESQL=${BUILD_DRIVER_POSTGRESQL} \
Expand Down
6 changes: 5 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ services:
image: condaforge/mambaforge:latest
volumes:
- .:/adbc:delegated
environment:
ADBC_USE_ASAN: "0"
ADBC_USE_UBSAN: "0"
CGO_ENABLED: "1"
command: |
/bin/bash -c 'git config --global --add safe.directory /adbc && source /opt/conda/etc/profile.d/conda.sh && mamba create -y -n adbc -c conda-forge go --file /adbc/ci/conda_env_cpp.txt --file /adbc/ci/conda_env_docs.txt --file /adbc/ci/conda_env_java.txt --file /adbc/ci/conda_env_python.txt && conda activate adbc && env ADBC_USE_ASAN=0 ADBC_USE_UBSAN=0 /adbc/ci/scripts/cpp_build.sh /adbc /adbc/build && env CGO_ENABLED=1 /adbc/ci/scripts/go_build.sh /adbc /adbc/build && /adbc/ci/scripts/python_build.sh /adbc /adbc/build && /adbc/ci/scripts/r_build.sh /adbc && /adbc/ci/scripts/docs_build.sh /adbc'
/bin/bash -c 'git config --global --add safe.directory /adbc && source /opt/conda/etc/profile.d/conda.sh && mamba create -y -n adbc -c conda-forge go --file /adbc/ci/conda_env_cpp.txt --file /adbc/ci/conda_env_docs.txt --file /adbc/ci/conda_env_java.txt --file /adbc/ci/conda_env_python.txt && conda activate adbc && /adbc/ci/scripts/cpp_build.sh /adbc /adbc/build && /adbc/ci/scripts/go_build.sh /adbc /adbc/build && /adbc/ci/scripts/python_build.sh /adbc /adbc/build && /adbc/ci/scripts/r_build.sh /adbc && /adbc/ci/scripts/docs_build.sh /adbc'
############################ Java JARs ######################################

Expand Down
2 changes: 2 additions & 0 deletions docs/source/driver/flight_sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ the :cpp:class:`AdbcDatabase`.
) as conn:
pass
For more examples, see :doc:`../python/recipe/flight_sql`.

.. tab-item:: Go
:sync: go

Expand Down
58 changes: 58 additions & 0 deletions docs/source/python/recipe/flight_sql.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing,
.. software distributed under the License is distributed on an
.. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
.. KIND, either express or implied. See the License for the
.. specific language governing permissions and limitations
.. under the License.
==================
Flight SQL Recipes
==================

Some of these recipes are written againt a demo Flight SQL service backed by
SQLite. You can run it yourself as follows:

.. code-block:: shell
$ go install github.com/apache/arrow/go/v${ARROW_MAJOR_VERSION}/arrow/flight/flightsql/example/cmd/sqlite_flightsql_server@latest
$ sqlite_flightsql_server -host 0.0.0.0 -port 8080
Other recipes work using the OSS version of Dremio_:

.. code-block:: shell
$ docker run -p 9047:9047 -p 31010:31010 -p 45678:45678 dremio/dremio-oss
If you have the ADBC repository checked out and Docker Compose installed, you
can use our configuration to run both services:

.. code-block:: shell
$ docker compose up --detach --wait dremio dremio-init flightsql-sqlite-test
.. _Dremio: https://www.dremio.com/

Connect to an unsecured Flight SQL service
------------------------------------------

.. recipe:: flightsql_sqlite_connect.py

Connect to a Flight SQL service with username and password
----------------------------------------------------------

.. recipe:: flightsql_dremio_connect.py

Set timeouts and other options
------------------------------

.. recipe:: flightsql_sqlite_options.py
46 changes: 46 additions & 0 deletions docs/source/python/recipe/flightsql_dremio_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# RECIPE STARTS HERE

#: Dremio requires a username and password. To connect to a Flight SQL
#: service with authentication, provide the options at connection time.

import os

import adbc_driver_flightsql.dbapi
import adbc_driver_manager

uri = os.environ["ADBC_DREMIO_FLIGHTSQL_URI"]
username = os.environ["ADBC_DREMIO_FLIGHTSQL_USER"]
password = os.environ["ADBC_DREMIO_FLIGHTSQL_PASS"]
conn = adbc_driver_flightsql.dbapi.connect(
uri,
db_kwargs={
adbc_driver_manager.DatabaseOptions.USERNAME.value: username,
adbc_driver_manager.DatabaseOptions.PASSWORD.value: password,
},
)

#: We can then execute a simple query.

with conn.cursor() as cur:
cur.execute("SELECT 1")

assert cur.fetchone() == (1,)

conn.close()
36 changes: 36 additions & 0 deletions docs/source/python/recipe/flightsql_sqlite_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# RECIPE STARTS HERE

#: To connect to an unsecured Flight SQL service, just provide the URI.

import os

import adbc_driver_flightsql.dbapi

uri = os.environ["ADBC_SQLITE_FLIGHTSQL_URI"]
conn = adbc_driver_flightsql.dbapi.connect(uri)

#: We can then execute a simple query.

with conn.cursor() as cur:
cur.execute("SELECT 1")

assert cur.fetchone() == (1,)

conn.close()
56 changes: 56 additions & 0 deletions docs/source/python/recipe/flightsql_sqlite_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# RECIPE STARTS HERE

#: The Flight SQL driver supports various options.

import os

import adbc_driver_flightsql.dbapi
from adbc_driver_flightsql import ConnectionOptions, DatabaseOptions

uri = os.environ["ADBC_SQLITE_FLIGHTSQL_URI"]
#: We can enable cookie support, which some server implementations require.
conn = adbc_driver_flightsql.dbapi.connect(
uri,
db_kwargs={DatabaseOptions.WITH_COOKIE_MIDDLEWARE.value: "true"},
)

#: Other options are set on the connection or statement.

#: For example, we can add a custom header to all outgoing requests.
custom_header = f"{ConnectionOptions.RPC_CALL_HEADER_PREFIX.value}x-custom-header"
conn.adbc_connection.set_options(**{custom_header: "value"})

#: We can also set timeouts. These are in floating-point seconds.
conn.adbc_connection.set_options(
**{
ConnectionOptions.TIMEOUT_FETCH.value: 30.0,
ConnectionOptions.TIMEOUT_QUERY.value: 30.0,
ConnectionOptions.TIMEOUT_UPDATE.value: 30.0,
}
)

#: These options will apply to all cursors we create.

with conn.cursor() as cur:
cur.execute("SELECT 1")

assert cur.fetchone() == (1,)

conn.close()
1 change: 1 addition & 0 deletions docs/source/python/recipe/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ Python.
:maxdepth: 2

driver_manager
flight_sql
postgresql

0 comments on commit 8596879

Please sign in to comment.