From cd876bdf01d45699b05c096f9623ad55b964f200 Mon Sep 17 00:00:00 2001 From: Emil Sadek Date: Mon, 29 Dec 2025 23:28:31 -0800 Subject: [PATCH 1/5] Add CrateDB examples --- README.md | 1 + cpp/README.md | 1 + cpp/postgresql/README.md | 1 + cpp/postgresql/cratedb/CMakeLists.txt | 43 ++++++++++ cpp/postgresql/cratedb/Makefile | 24 ++++++ cpp/postgresql/cratedb/README.md | 98 +++++++++++++++++++++++ cpp/postgresql/cratedb/main.cpp | 110 ++++++++++++++++++++++++++ go/README.md | 1 + go/postgresql/README.md | 1 + go/postgresql/cratedb/README.md | 66 ++++++++++++++++ go/postgresql/cratedb/main.go | 74 +++++++++++++++++ python/README.md | 1 + python/postgresql/README.md | 1 + python/postgresql/cratedb/README.md | 65 +++++++++++++++ python/postgresql/cratedb/main.py | 34 ++++++++ r/README.md | 1 + r/postgresql/README.md | 1 + r/postgresql/cratedb/README.md | 71 +++++++++++++++++ r/postgresql/cratedb/main.R | 38 +++++++++ rust/Cargo.toml | 14 +++- rust/README.md | 1 + rust/postgresql/README.md | 1 + rust/postgresql/cratedb/Cargo.toml | 24 ++++++ rust/postgresql/cratedb/README.md | 65 +++++++++++++++ rust/postgresql/cratedb/src/main.rs | 53 +++++++++++++ 25 files changed, 787 insertions(+), 3 deletions(-) create mode 100644 cpp/postgresql/cratedb/CMakeLists.txt create mode 100644 cpp/postgresql/cratedb/Makefile create mode 100644 cpp/postgresql/cratedb/README.md create mode 100644 cpp/postgresql/cratedb/main.cpp create mode 100644 go/postgresql/cratedb/README.md create mode 100644 go/postgresql/cratedb/main.go create mode 100644 python/postgresql/cratedb/README.md create mode 100644 python/postgresql/cratedb/main.py create mode 100644 r/postgresql/cratedb/README.md create mode 100644 r/postgresql/cratedb/main.R create mode 100644 rust/postgresql/cratedb/Cargo.toml create mode 100644 rust/postgresql/cratedb/README.md create mode 100644 rust/postgresql/cratedb/src/main.rs diff --git a/README.md b/README.md index e90092c..9c21b30 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ Simple examples showing how to use ADBC to connect, run a query, and return the - MySQL - PostgreSQL - Citus + - CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/cpp/README.md b/cpp/README.md index e2ca8c3..b206663 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -37,6 +37,7 @@ Simple C++ examples showing how to use ADBC to connect, run a query, and return - MySQL - PostgreSQL - Citus + - CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/cpp/postgresql/README.md b/cpp/postgresql/README.md index 0ff491d..6f8d0fa 100644 --- a/cpp/postgresql/README.md +++ b/cpp/postgresql/README.md @@ -23,6 +23,7 @@ This directory contains examples showing how to use ADBC to connect C++ applicat Any open source tool or vendor product that implements PostgreSQL frontend/backend protocol should work with the ADBC driver for PostgreSQL. The examples included here focus on the following systems: - Citus +- CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/cpp/postgresql/cratedb/CMakeLists.txt b/cpp/postgresql/cratedb/CMakeLists.txt new file mode 100644 index 0000000..56e91ec --- /dev/null +++ b/cpp/postgresql/cratedb/CMakeLists.txt @@ -0,0 +1,43 @@ +# Copyright 2025 Columnar Technologies Inc. +# +# Licensed 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. + +cmake_minimum_required(VERSION 3.15) +project(cratedb_demo CXX) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Use conda environment paths +if(DEFINED ENV{CONDA_PREFIX}) + list(APPEND CMAKE_PREFIX_PATH "$ENV{CONDA_PREFIX}") +endif() + +find_package(Arrow REQUIRED) + +add_executable(cratedb_demo main.cpp) + +if(DEFINED ENV{CONDA_PREFIX}) + target_include_directories(cratedb_demo PRIVATE $ENV{CONDA_PREFIX}/include) + target_link_directories(cratedb_demo PRIVATE $ENV{CONDA_PREFIX}/lib) +endif() + +target_link_libraries(cratedb_demo + adbc_driver_manager + Arrow::arrow_shared +) + +target_compile_options(cratedb_demo PRIVATE + -Wall + -Werror +) diff --git a/cpp/postgresql/cratedb/Makefile b/cpp/postgresql/cratedb/Makefile new file mode 100644 index 0000000..8e945b8 --- /dev/null +++ b/cpp/postgresql/cratedb/Makefile @@ -0,0 +1,24 @@ +# Copyright 2025 Columnar Technologies Inc. +# +# Licensed 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. + +CXXFLAGS = -std=c++17 -Wall -Werror -I$(CONDA_PREFIX)/include +LIBS = -L$(CONDA_PREFIX)/lib -ladbc_driver_manager -larrow -Wl,-rpath,$(CONDA_PREFIX)/lib + +cratedb_demo: main.cpp + $(CXX) $(CXXFLAGS) -o cratedb_demo main.cpp $(LIBS) + +clean: + rm -f cratedb_demo + +.PHONY: clean diff --git a/cpp/postgresql/cratedb/README.md b/cpp/postgresql/cratedb/README.md new file mode 100644 index 0000000..03b3b99 --- /dev/null +++ b/cpp/postgresql/cratedb/README.md @@ -0,0 +1,98 @@ + + +# Connecting C++ and CrateDB with ADBC + +## Instructions + +> [!TIP] +> If you already have a CrateDB instance running, skip the steps to set up and clean up CrateDB. + +### Prerequisites + +1. [Install dbc](https://docs.columnar.tech/dbc/getting_started/installation/) + +2. [Install miniforge](https://github.com/conda-forge/miniforge) + +3. Create and activate a new environment with the required C++ libraries: + + ```sh + mamba create -n adbc-cpp -c conda-forge cmake compilers libadbc-driver-manager libarrow + + # Initialize mamba in your shell if not already done + eval "$(mamba shell hook --shell zsh)" + mamba activate adbc-cpp + ``` + + (`cmake` is only needed if you use CMake to build the C++ program below.) + +### Set up CrateDB + +1. [Install Docker](https://docs.docker.com/get-started/get-docker/) + +2. Start a CrateDB instance: + + ```sh + docker run -d --rm --name cratedb -p 4200:4200 -p 5432:5432 crate -Cdiscovery.type=single-node + ``` + +### Connect to CrateDB + +1. Install the PostgreSQL ADBC driver: + + ```sh + dbc install --level user postgresql + ``` + +2. Customize the C++ program `main.cpp` as needed + - Change the connection arguments in the `AdbcDatabaseSetOption()` calls + - Format `uri` according to the [connection URI format used by PostgreSQL](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS), or keep it as is + - If you changed which database you're connecting to, also change the SQL SELECT statement in `AdbcStatementSetSqlQuery()` + +3. Build and run the C++ program: + + Using Make: + ```sh + make + ./cratedb_demo + ``` + + Or using CMake: + ```sh + cmake -B build + cmake --build build + ./build/cratedb_demo + ``` + +### Clean up + +1. Clean build artifacts: + + Using Make: + ```sh + make clean + ``` + + Using CMake: + ```sh + rm -rf build + ``` + +2. Stop the Docker container running CrateDB: + + ```sh + docker stop cratedb + ``` diff --git a/cpp/postgresql/cratedb/main.cpp b/cpp/postgresql/cratedb/main.cpp new file mode 100644 index 0000000..da1a09e --- /dev/null +++ b/cpp/postgresql/cratedb/main.cpp @@ -0,0 +1,110 @@ +// Copyright 2025 Columnar Technologies Inc. +// +// Licensed 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. + +// For EXIT_SUCCESS +#include +// For strerror +#include +#include + +#include +#include +#include +#include + +// Error-checking helper for ADBC calls. +// Assumes that there is an AdbcError named `error` in scope. +#define CHECK_ADBC(EXPR) \ + if (AdbcStatusCode status = (EXPR); status != ADBC_STATUS_OK) { \ + if (error.message != nullptr) { \ + std::cerr << error.message << std::endl; \ + } \ + return EXIT_FAILURE; \ + } + +// Error-checking helper for ArrowArrayStream. +#define CHECK_STREAM(STREAM, EXPR) \ + if (int status = (EXPR); status != 0) { \ + std::cerr << "(" << std::strerror(status) << "): "; \ + const char *message = (STREAM).get_last_error(&(STREAM)); \ + if (message != nullptr) { \ + std::cerr << message << std::endl; \ + } else { \ + std::cerr << "(no error message)" << std::endl; \ + } \ + return EXIT_FAILURE; \ + } + +int main() { + AdbcError error = {}; + + AdbcDatabase database = {}; + CHECK_ADBC(AdbcDatabaseNew(&database, &error)); + + CHECK_ADBC(AdbcDatabaseSetOption(&database, "driver", "postgresql", &error)); + CHECK_ADBC(AdbcDatabaseSetOption(&database, "uri", + "postgresql://crate@localhost:5432/crate", &error)); + CHECK_ADBC(AdbcDriverManagerDatabaseSetLoadFlags( + &database, ADBC_LOAD_FLAG_DEFAULT, &error)); + CHECK_ADBC(AdbcDatabaseInit(&database, &error)); + + AdbcConnection connection = {}; + CHECK_ADBC(AdbcConnectionNew(&connection, &error)); + CHECK_ADBC(AdbcConnectionInit(&connection, &database, &error)); + + AdbcStatement statement = {}; + CHECK_ADBC(AdbcStatementNew(&connection, &statement, &error)); + + struct ArrowArrayStream stream = {}; + int64_t rows_affected = -1; + + CHECK_ADBC( + AdbcStatementSetOption(&statement, "adbc.postgresql.use_copy", "false", &error)); + CHECK_ADBC( + AdbcStatementSetSqlQuery(&statement, "SELECT version()", &error)); + CHECK_ADBC( + AdbcStatementExecuteQuery(&statement, &stream, &rows_affected, &error)); + + // Import stream as record batch reader + auto maybe_reader = arrow::ImportRecordBatchReader(&stream); + if (!maybe_reader.ok()) { + std::cerr << "Failed to import record batch reader: " + << maybe_reader.status().message() << std::endl; + return 1; + } + + auto reader = maybe_reader.ValueOrDie(); + + while (true) { + auto maybe_batch = reader->Next(); + if (!maybe_batch.ok()) { + std::cerr << "Error reading batch: " << maybe_batch.status().message() + << std::endl; + return 1; + } + + auto batch = maybe_batch.ValueOrDie(); + if (!batch) { + break; + } + + std::cout << batch->ToString() << std::endl; + } + + CHECK_ADBC(AdbcStatementRelease(&statement, &error)); + CHECK_ADBC(AdbcConnectionRelease(&connection, &error)); + CHECK_ADBC(AdbcDatabaseRelease(&database, &error)); + + return EXIT_SUCCESS; +} diff --git a/go/README.md b/go/README.md index 49e3693..733a818 100644 --- a/go/README.md +++ b/go/README.md @@ -38,6 +38,7 @@ Simple Go examples showing how to use ADBC to connect, run a query, and return t - MySQL - PostgreSQL - Citus + - CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/go/postgresql/README.md b/go/postgresql/README.md index 66f6478..f0f1053 100644 --- a/go/postgresql/README.md +++ b/go/postgresql/README.md @@ -23,6 +23,7 @@ This directory contains examples showing how to use ADBC to connect Go applicati Any open source tool or vendor product that implements PostgreSQL frontend/backend protocol should work with the ADBC driver for PostgreSQL. The examples included here focus on the following systems: - Citus +- CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/go/postgresql/cratedb/README.md b/go/postgresql/cratedb/README.md new file mode 100644 index 0000000..4f69ac3 --- /dev/null +++ b/go/postgresql/cratedb/README.md @@ -0,0 +1,66 @@ + + +# Connecting Go and CrateDB with ADBC + +## Instructions + +> [!TIP] +> If you already have a CrateDB instance running, skip the steps to set up and clean up CrateDB. + +### Prerequisites + +1. [Install Go](https://go.dev/doc/install) + +2. [Install dbc](https://docs.columnar.tech/dbc/getting_started/installation/) + +### Set up CrateDB + +1. [Install Docker](https://docs.docker.com/get-started/get-docker/) + +2. Start a CrateDB instance: + + ```sh + docker run -d --rm --name cratedb -p 4200:4200 -p 5432:5432 crate -Cdiscovery.type=single-node + ``` + +### Connect to CrateDB + +1. Install the PostgreSQL ADBC driver: + + ```sh + dbc install postgresql + ``` + +2. Customize the Go program `main.go` as needed + - Change the connection arguments in the `NewDatabase()` call + - Format `uri` according to the [connection URI format used by PostgreSQL](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS), or keep it as is + - If you changed which database you're connecting to, also change the SQL SELECT statement in `stmt.SetSqlQuery()` + +3. Run the Go program: + + ```sh + go mod tidy + go run main.go + ``` + +### Clean up + +Stop the Docker container running CrateDB: + +```sh +docker stop cratedb +``` diff --git a/go/postgresql/cratedb/main.go b/go/postgresql/cratedb/main.go new file mode 100644 index 0000000..6e3a963 --- /dev/null +++ b/go/postgresql/cratedb/main.go @@ -0,0 +1,74 @@ +// Copyright 2025 Columnar Technologies Inc. +// +// Licensed 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. + +package main + +import ( + "context" + "fmt" + "log" + + "github.com/apache/arrow-adbc/go/adbc/drivermgr" +) + +func main() { + var drv drivermgr.Driver + + db, err := drv.NewDatabase(map[string]string{ + "driver": "postgresql", + "uri": "postgresql://crate@localhost:5432/crate", + }) + if err != nil { + log.Fatal(err) + } + defer db.Close() + + conn, err := db.Open(context.Background()) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + stmt, err := conn.NewStatement() + if err != nil { + log.Fatal(err) + } + defer stmt.Close() + + err = stmt.SetOption("adbc.postgresql.use_copy", "false") + if err != nil { + log.Fatal(err) + } + + err = stmt.SetSqlQuery("SELECT version()") + if err != nil { + log.Fatal(err) + } + + stream, _, err := stmt.ExecuteQuery(context.Background()) + if err != nil { + log.Fatal(err) + } + defer stream.Release() + + // Read all record batches from the stream + for stream.Next() { + batch := stream.RecordBatch() + fmt.Println(batch) + } + + if err := stream.Err(); err != nil { + log.Fatal(err) + } +} diff --git a/python/README.md b/python/README.md index 713cecb..06afd3a 100644 --- a/python/README.md +++ b/python/README.md @@ -38,6 +38,7 @@ Simple Python examples showing how to use ADBC to connect, run a query, and retu - MySQL - PostgreSQL - Citus + - CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/python/postgresql/README.md b/python/postgresql/README.md index 1f9425b..bdfa4ac 100644 --- a/python/postgresql/README.md +++ b/python/postgresql/README.md @@ -23,6 +23,7 @@ This directory contains examples showing how to use ADBC to connect Python appli Any open source tool or vendor product that implements PostgreSQL frontend/backend protocol should work with the ADBC driver for PostgreSQL. The examples included here focus on the following systems: - Citus +- CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/python/postgresql/cratedb/README.md b/python/postgresql/cratedb/README.md new file mode 100644 index 0000000..13ecd48 --- /dev/null +++ b/python/postgresql/cratedb/README.md @@ -0,0 +1,65 @@ + + +# Connecting Python and CrateDB with ADBC + +## Instructions + +> [!TIP] +> If you already have a CrateDB instance running, skip the steps to set up and clean up CrateDB. + +### Prerequisites + +1. [Install uv](https://docs.astral.sh/uv/getting-started/installation/) + +2. [Install dbc](https://docs.columnar.tech/dbc/getting_started/installation/) + +### Set up CrateDB + +1. [Install Docker](https://docs.docker.com/get-started/get-docker/) + +2. Start a CrateDB instance: + + ```sh + docker run -d --rm --name cratedb -p 4200:4200 -p 5432:5432 crate -Cdiscovery.type=single-node + ``` + +### Connect to CrateDB + +1. Install the PostgreSQL ADBC driver: + + ```sh + dbc install postgresql + ``` + +2. Customize the Python script `main.py` as needed + - Change the connection arguments in `db_kwargs` + - Format `uri` according to the [connection URI format used by PostgreSQL](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS), or keep it as is + - If you changed which database you're connecting to, also change the SQL SELECT statement in `cursor.execute()` + +3. Run the Python script: + + ```sh + uv run main.py + ``` + +### Clean up + +Stop the Docker container running CrateDB: + +```sh +docker stop cratedb +``` diff --git a/python/postgresql/cratedb/main.py b/python/postgresql/cratedb/main.py new file mode 100644 index 0000000..b5e4ea3 --- /dev/null +++ b/python/postgresql/cratedb/main.py @@ -0,0 +1,34 @@ +# Copyright 2025 Columnar Technologies Inc. +# +# Licensed 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. + +# /// script +# requires-python = ">=3.9" +# dependencies = ["adbc-driver-manager>=1.8.0", "pyarrow>=20.0.0"] +# /// + +from adbc_driver_manager import dbapi + +with ( + dbapi.connect( + driver="postgresql", + db_kwargs={ + "uri": "postgresql://crate@localhost:5432/crate", + }, + ) as connection, + connection.cursor(adbc_stmt_kwargs={"adbc.postgresql.use_copy": False}) as cursor, +): + cursor.execute("SELECT version()") + table = cursor.fetch_arrow_table() + +print(table) diff --git a/r/README.md b/r/README.md index 6c4c72c..1e70106 100644 --- a/r/README.md +++ b/r/README.md @@ -38,6 +38,7 @@ Simple R examples showing how to use ADBC to connect, run a query, and return th - MySQL - PostgreSQL - Citus + - CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/r/postgresql/README.md b/r/postgresql/README.md index ceceacb..f00f880 100644 --- a/r/postgresql/README.md +++ b/r/postgresql/README.md @@ -23,6 +23,7 @@ This directory contains examples showing how to use ADBC to connect R applicatio Any open source tool or vendor product that implements PostgreSQL frontend/backend protocol should work with the ADBC driver for PostgreSQL. The examples included here focus on the following systems: - Citus +- CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/r/postgresql/cratedb/README.md b/r/postgresql/cratedb/README.md new file mode 100644 index 0000000..37a4cc5 --- /dev/null +++ b/r/postgresql/cratedb/README.md @@ -0,0 +1,71 @@ + + +# Connecting R and CrateDB with ADBC + +## Instructions + +> [!TIP] +> If you already have a CrateDB instance running, skip the steps to set up and clean up CrateDB. + +### Prerequisites + +1. [Install R](https://www.r-project.org/) + +2. [Install dbc](https://docs.columnar.tech/dbc/getting_started/installation/) + +3. Install R packages `adbcdrivermanager`, `arrow`, and `tibble`: + + ```r + install.packages(c("adbcdrivermanager", "arrow", "tibble")) + ``` + +### Set up CrateDB + +1. [Install Docker](https://docs.docker.com/get-started/get-docker/) + +2. Start a CrateDB instance: + + ```sh + docker run -d --rm --name cratedb -p 4200:4200 -p 5432:5432 crate -Cdiscovery.type=single-node + ``` + +### Connect to CrateDB + +1. Install the PostgreSQL ADBC driver: + + ```sh + dbc install postgresql + ``` + +2. Customize the R script `main.R` as needed + - Change the connection arguments in `adbc_database_init()` + - Format `uri` according to the [connection URI format used by PostgreSQL](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS), or keep it as is + - If you changed which database you're connecting to, also change the SQL SELECT statement in `read_adbc()` + +3. Run the R script: + + ```sh + Rscript main.R + ``` + +### Clean up + +Stop the Docker container running CrateDB: + +```sh +docker stop cratedb +``` diff --git a/r/postgresql/cratedb/main.R b/r/postgresql/cratedb/main.R new file mode 100644 index 0000000..dce1554 --- /dev/null +++ b/r/postgresql/cratedb/main.R @@ -0,0 +1,38 @@ +# Copyright 2025 Columnar Technologies Inc. +# +# Licensed 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. + +library(adbcdrivermanager) + +drv <- adbc_driver("postgresql") + +db <- adbc_database_init( + drv, + uri = "postgresql://crate@localhost:5432/crate" +) + +con <- adbc_connection_init(db) + +stmt <- adbc_statement_init(con) +adbc_statement_set_options( + stmt, + list( + "adbc.postgresql.use_copy" = "false" + ) +) +adbc_statement_set_sql_query(stmt, "SELECT version()") + +adbc_statement_execute_query(stmt) |> + tibble::as_tibble() # or: + # arrow::as_arrow_table() # to keep result in Arrow format + # arrow::as_record_batch_reader() # for larger results diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 3a1eee0..8d0be7b 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -19,11 +19,19 @@ members = [ "duckdb", "flightsql/dremio", "flightsql/gizmosql", + "flightsql/starrocks", "mssql", - "mysql", - "postgresql", + "mysql/mariadb", + "mysql/mysql", + "postgresql/citus", + "postgresql/cratedb", + "postgresql/neon", + "postgresql/paradedb", + "postgresql/postgresql", + "postgresql/yellowbrick", + "postgresql/yugabytedb", "redshift", "snowflake", "sqlite", "trino", -] +] \ No newline at end of file diff --git a/rust/README.md b/rust/README.md index cc72b57..d8c3a02 100644 --- a/rust/README.md +++ b/rust/README.md @@ -38,6 +38,7 @@ Simple Rust examples showing how to use ADBC to connect, run a query, and return - MySQL - PostgreSQL - Citus + - CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/rust/postgresql/README.md b/rust/postgresql/README.md index 5c87723..66ebc4a 100644 --- a/rust/postgresql/README.md +++ b/rust/postgresql/README.md @@ -23,6 +23,7 @@ This directory contains examples showing how to use ADBC to connect Rust applica Any open source tool or vendor product that implements PostgreSQL frontend/backend protocol should work with the ADBC driver for PostgreSQL. The examples included here focus on the following systems: - Citus +- CrateDB - Neon - ParadeDB - PostgreSQL diff --git a/rust/postgresql/cratedb/Cargo.toml b/rust/postgresql/cratedb/Cargo.toml new file mode 100644 index 0000000..2bb290a --- /dev/null +++ b/rust/postgresql/cratedb/Cargo.toml @@ -0,0 +1,24 @@ +# Copyright 2025 Columnar Technologies Inc. +# +# Licensed 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. + +[package] +name = "cratedb" +version = "0.1.0" +edition = "2024" + +[dependencies] +adbc_core = "0.21.0" +adbc_driver_manager = "0.21.0" +arrow = { version = "57.0.0", features = ["prettyprint"] } +arrow-array = "57.0.0" diff --git a/rust/postgresql/cratedb/README.md b/rust/postgresql/cratedb/README.md new file mode 100644 index 0000000..52a1066 --- /dev/null +++ b/rust/postgresql/cratedb/README.md @@ -0,0 +1,65 @@ + + +# Connecting Rust and CrateDB with ADBC + +## Instructions + +> [!TIP] +> If you already have a CrateDB instance running, skip the steps to set up and clean up CrateDB. + +### Prerequisites + +1. [Install Rust](https://www.rust-lang.org/tools/install) + +2. [Install dbc](https://docs.columnar.tech/dbc/getting_started/installation/) + +### Set up CrateDB + +1. [Install Docker](https://docs.docker.com/get-started/get-docker/) + +2. Start a CrateDB instance: + + ```sh + docker run -d --rm --name cratedb -p 4200:4200 -p 5432:5432 crate -Cdiscovery.type=single-node + ``` + +### Connect to CrateDB + +1. Install the PostgreSQL ADBC driver: + + ```sh + dbc install postgresql + ``` + +2. Customize `src/main.rs` as needed + - Change the connection arguments in `opts` + - Format `OptionDatabase::Uri` according to the [connection URI format used by PostgreSQL](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS), or keep it as is + - If you changed which database you're connecting to, also change the SQL SELECT statement in `statement.set_sql_query()` + +3. Run the Rust program: + + ```sh + cargo run + ``` + +### Clean up + +Stop the Docker container running CrateDB: + +```sh +docker stop cratedb +``` diff --git a/rust/postgresql/cratedb/src/main.rs b/rust/postgresql/cratedb/src/main.rs new file mode 100644 index 0000000..fc545be --- /dev/null +++ b/rust/postgresql/cratedb/src/main.rs @@ -0,0 +1,53 @@ +// Copyright 2025 Columnar Technologies Inc. +// +// Licensed 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. + +use adbc_core::options::{AdbcVersion, OptionDatabase, OptionStatement, OptionValue}; +use adbc_core::{Connection, Database, Driver, LOAD_FLAG_DEFAULT, Optionable, Statement}; +use adbc_driver_manager::ManagedDriver; +use arrow::util::pretty; +use arrow_array::RecordBatch; + +fn main() { + let mut driver = ManagedDriver::load_from_name( + "postgresql", + None, + AdbcVersion::default(), + LOAD_FLAG_DEFAULT, + None, + ) + .expect("Failed to load driver"); + + let opts = [( + OptionDatabase::Uri, + "postgresql://crate@localhost:5432/crate".into(), + )]; + let db = driver + .new_database_with_opts(opts) + .expect("Failed to create database handle"); + + let mut conn = db.new_connection().expect("Failed to create connection"); + + let mut statement: adbc_driver_manager::ManagedStatement = conn.new_statement().unwrap(); + statement + .set_option( + OptionStatement::Other("adbc.postgresql.use_copy".into()), + OptionValue::String("false".into()), + ) + .unwrap(); + statement.set_sql_query("SELECT version()").unwrap(); + let reader = statement.execute().unwrap(); + let batches: Vec = reader.collect::>().unwrap(); + + pretty::print_batches(&batches).expect("Failed to print batches"); +} From ee08b30bb16c0b23e738f87b10ac771573111715 Mon Sep 17 00:00:00 2001 From: Ian Cook Date: Tue, 6 Jan 2026 10:59:07 -0500 Subject: [PATCH 2/5] Add newline at end of Cargo.toml --- rust/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/Cargo.toml b/rust/Cargo.toml index c56bee3..038ac21 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -36,4 +36,5 @@ members = [ "snowflake", "sqlite", "trino", -] \ No newline at end of file +] + From 33cec5f383c5b07354e8c2ba1b0851ef0b6cc53d Mon Sep 17 00:00:00 2001 From: Ian Cook Date: Wed, 7 Jan 2026 09:28:37 -0500 Subject: [PATCH 3/5] Format Python example --- python/postgresql/cratedb/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/postgresql/cratedb/main.py b/python/postgresql/cratedb/main.py index b5e4ea3..23582f5 100644 --- a/python/postgresql/cratedb/main.py +++ b/python/postgresql/cratedb/main.py @@ -22,11 +22,11 @@ with ( dbapi.connect( driver="postgresql", - db_kwargs={ - "uri": "postgresql://crate@localhost:5432/crate", - }, + db_kwargs={"uri": "postgresql://crate@localhost:5432/crate"}, ) as connection, - connection.cursor(adbc_stmt_kwargs={"adbc.postgresql.use_copy": False}) as cursor, + connection.cursor( + adbc_stmt_kwargs={"adbc.postgresql.use_copy": False} + ) as cursor, ): cursor.execute("SELECT version()") table = cursor.fetch_arrow_table() From 8d9a24bf4bb227a9785ff41687c76893c9e196c8 Mon Sep 17 00:00:00 2001 From: Ian Cook Date: Wed, 7 Jan 2026 09:30:06 -0500 Subject: [PATCH 4/5] Add trailing comma --- python/postgresql/cratedb/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/postgresql/cratedb/main.py b/python/postgresql/cratedb/main.py index 23582f5..7a1067d 100644 --- a/python/postgresql/cratedb/main.py +++ b/python/postgresql/cratedb/main.py @@ -25,7 +25,7 @@ db_kwargs={"uri": "postgresql://crate@localhost:5432/crate"}, ) as connection, connection.cursor( - adbc_stmt_kwargs={"adbc.postgresql.use_copy": False} + adbc_stmt_kwargs={"adbc.postgresql.use_copy": False}, ) as cursor, ): cursor.execute("SELECT version()") From 217a1c0b236e4aed015c4bb4b773afa014cae20c Mon Sep 17 00:00:00 2001 From: Ian Cook Date: Wed, 7 Jan 2026 09:47:12 -0500 Subject: [PATCH 5/5] Add Java README in lieu of full example --- java/postgresql/cratedb/README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 java/postgresql/cratedb/README.md diff --git a/java/postgresql/cratedb/README.md b/java/postgresql/cratedb/README.md new file mode 100644 index 0000000..903d082 --- /dev/null +++ b/java/postgresql/cratedb/README.md @@ -0,0 +1,23 @@ + + +# Connecting Java and CrateDB with ADBC + +## Not Yet Implemented + +A Java example for CrateDB is not yet available due to an unimplemented feature in the ADBC Java driver manager. + +See [apache/arrow-adbc#3868](https://github.com/apache/arrow-adbc/issues/3868) for details and progress on this issue.