Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rust/driver/snowflake): add adbc_snowflake crate with Go driver wrapper #2207

Merged
merged 24 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7dd0c67
feat(rust/driver/snowflake): add `adbc_snowflake` crate with Go drive…
mbrobbel Oct 1, 2024
0ed54ab
Rename `rust/drivers` to `rust/driver`
mbrobbel Oct 1, 2024
ea19114
Add required permissions for `native-unix` workflow
mbrobbel Oct 1, 2024
691785a
Also add `contents: read` to permissions for `native-unix` workflow
mbrobbel Oct 1, 2024
a213419
Merge branch 'main' into rust/snowflake
mbrobbel Oct 8, 2024
88d8809
Merge branch 'main' into rust/snowflake
mbrobbel Oct 9, 2024
23b995f
Merge branch 'main' into rust/snowflake
mbrobbel Nov 14, 2024
b541e07
Also move `datafusion` to `driver`
mbrobbel Nov 14, 2024
277bbc9
Fix unused warning
mbrobbel Nov 14, 2024
4e6d9ce
Add configuration builders
mbrobbel Nov 19, 2024
e6f3638
Add missing license headers
mbrobbel Nov 19, 2024
b6e7cd1
Fix clippy warning
mbrobbel Nov 19, 2024
c686f2e
Add `bundled` and `linked` features to control linking
mbrobbel Nov 19, 2024
032cb92
Use testing database in CI
mbrobbel Nov 19, 2024
25a86c9
Merge branch 'main' into rust/snowflake
mbrobbel Nov 19, 2024
80898f1
Add go lib dir to search path
mbrobbel Nov 19, 2024
cf54213
Disable integration tests for now
mbrobbel Nov 19, 2024
7b3ed83
Ignore load dynamic driver test when `linked` feature is not set
mbrobbel Nov 20, 2024
af70f82
Set `ADBC_SNOWFLAKE_GO_LIB_DIR` for all steps
mbrobbel Nov 20, 2024
041f72d
Add `adbc_snowflake` to release script
mbrobbel Nov 20, 2024
0229b41
Add some get info methods and tests
mbrobbel Nov 20, 2024
2b5a8b9
Add a few more tests
mbrobbel Nov 20, 2024
61bc558
Add some docs
mbrobbel Nov 21, 2024
029ccae
Mark doctests that require a Snowflake account as `no_run`
mbrobbel Nov 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ jobs:
run: |
rustup toolchain install stable --no-self-update
rustup default stable
- name: Get required Go version
run: |
(. ../.env && echo "GO_VERSION=${GO}") >> $GITHUB_ENV
- uses: actions/setup-go@v5
with:
go-version: "${{ env.GO_VERSION }}"
check-latest: true
cache: true
cache-dependency-path: go/adbc/go.sum
- uses: actions/download-artifact@v4
with:
name: driver-manager-${{ matrix.os }}
Expand Down
9 changes: 9 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# under the License.

[workspace]
members = ["core", "drivers/*"]
members = ["core", "driver/*"]
resolver = "2"

[workspace.package]
Expand Down
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions rust/driver/snowflake/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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.

[package]
name = "adbc_snowflake"
description = "Snowflake Arrow Database Connectivity (ADBC) driver"
version.workspace = true
edition.workspace = true
authors.workspace = true
license.workspace = true

[dependencies]
adbc_core = { path = "../../core", features = ["driver_manager"] }
arrow-array.workspace = true
arrow-schema.workspace = true
20 changes: 20 additions & 0 deletions rust/driver/snowflake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!---
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.
-->

# Snowflake driver for Arrow Database Connectivity (ADBC)
57 changes: 57 additions & 0 deletions rust/driver/snowflake/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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.

use std::{env, error::Error, path::PathBuf, process::Command};

fn main() -> Result<(), Box<dyn Error>> {
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
let go_dir = manifest_dir.ancestors().nth(3).unwrap().join("go");
let go_pkg = go_dir.join("adbc/pkg/snowflake");

let out_dir = PathBuf::from(env::var("OUT_DIR")?);
let archive = out_dir.join("libsnowflake.a");

// Build the Go driver
let status = Command::new("go")
.current_dir(go_pkg.as_path())
.arg("build")
.arg("-tags")
.arg("driverlib")
.arg("-buildmode=c-archive")
.arg("-o")
.arg(&archive)
.arg(".")
.status()?;
assert!(status.success(), "Go build failed");

// Rebuild when the Go pkg changes.
println!("cargo:rerun-if-changed={}", go_pkg.display());

// Link the driver statically.
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static=snowflake");

// Link other dependencies.
println!("cargo:rustc-link-lib=resolv");
#[cfg(target_os = "macos")]
{
println!("cargo:rustc-link-lib=framework=CoreFoundation");
println!("cargo:rustc-link-lib=framework=Security");
}

Ok(())
}
131 changes: 131 additions & 0 deletions rust/driver/snowflake/src/connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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.

use std::collections::HashSet;

use adbc_core::{
driver_manager::ManagedConnection,
error::Result,
options::{InfoCode, OptionConnection, OptionValue},
Connection, Optionable,
};
use arrow_array::RecordBatchReader;
use arrow_schema::Schema;

use crate::SnowflakeStatement;

/// Snowflake ADBC Connection.
pub struct SnowflakeConnection(pub(crate) ManagedConnection);

impl Optionable for SnowflakeConnection {
type Option = OptionConnection;

fn set_option(&mut self, key: Self::Option, value: OptionValue) -> Result<()> {
self.0.set_option(key, value)
}

fn get_option_string(&self, key: Self::Option) -> Result<String> {
self.0.get_option_string(key)
}

fn get_option_bytes(&self, key: Self::Option) -> Result<Vec<u8>> {
self.0.get_option_bytes(key)
}

fn get_option_int(&self, key: Self::Option) -> Result<i64> {
self.0.get_option_int(key)
}

fn get_option_double(&self, key: Self::Option) -> Result<f64> {
self.0.get_option_double(key)
}
}
Comment on lines +43 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar enough with Rust, but can't these just be inherited somehow from a base type instead of having to implement them here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we should be using traits for this. Discussed before here: #1725 (comment).

I'll try to put up a PR to change these option structs to traits following the proposal in #1725 (comment).


impl Connection for SnowflakeConnection {
type StatementType = SnowflakeStatement;

fn new_statement(&mut self) -> Result<Self::StatementType> {
self.0.new_statement().map(SnowflakeStatement)
}

fn cancel(&mut self) -> Result<()> {
self.0.cancel()
}

fn get_info(&self, codes: Option<HashSet<InfoCode>>) -> Result<impl RecordBatchReader + Send> {
self.0.get_info(codes)
}

fn get_objects(
&self,
depth: adbc_core::options::ObjectDepth,
catalog: Option<&str>,
db_schema: Option<&str>,
table_name: Option<&str>,
table_type: Option<Vec<&str>>,
column_name: Option<&str>,
) -> Result<impl RecordBatchReader + Send> {
self.0.get_objects(
depth,
catalog,
db_schema,
table_name,
table_type,
column_name,
)
}

fn get_table_schema(
&self,
catalog: Option<&str>,
db_schema: Option<&str>,
table_name: &str,
) -> Result<Schema> {
self.0.get_table_schema(catalog, db_schema, table_name)
}

fn get_table_types(&self) -> Result<impl RecordBatchReader + Send> {
self.0.get_table_types()
}

fn get_statistic_names(&self) -> Result<impl RecordBatchReader + Send> {
self.0.get_statistic_names()
}

fn get_statistics(
&self,
catalog: Option<&str>,
db_schema: Option<&str>,
table_name: Option<&str>,
approximate: bool,
) -> Result<impl RecordBatchReader + Send> {
self.0
.get_statistics(catalog, db_schema, table_name, approximate)
}

fn commit(&mut self) -> Result<()> {
self.0.commit()
}

fn rollback(&mut self) -> Result<()> {
self.0.rollback()
}

fn read_partition(&self, partition: impl AsRef<[u8]>) -> Result<impl RecordBatchReader + Send> {
self.0.read_partition(partition)
}
}
69 changes: 69 additions & 0 deletions rust/driver/snowflake/src/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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.

use adbc_core::{
driver_manager::ManagedDatabase,
error::Result,
options::{OptionConnection, OptionDatabase, OptionValue},
Database, Optionable,
};

use crate::SnowflakeConnection;

/// Snowflake ADBC Database.
pub struct SnowflakeDatabase(pub(crate) ManagedDatabase);

impl Optionable for SnowflakeDatabase {
type Option = OptionDatabase;

fn set_option(&mut self, key: Self::Option, value: OptionValue) -> Result<()> {
self.0.set_option(key, value)
}

fn get_option_string(&self, key: Self::Option) -> Result<String> {
self.0.get_option_string(key)
}

fn get_option_bytes(&self, key: Self::Option) -> Result<Vec<u8>> {
self.0.get_option_bytes(key)
}

fn get_option_int(&self, key: Self::Option) -> Result<i64> {
self.0.get_option_int(key)
}

fn get_option_double(&self, key: Self::Option) -> Result<f64> {
self.0.get_option_double(key)
}
}

impl Database for SnowflakeDatabase {
type ConnectionType = SnowflakeConnection;

fn new_connection(&mut self) -> Result<Self::ConnectionType> {
self.0.new_connection().map(SnowflakeConnection)
}

fn new_connection_with_opts(
&mut self,
opts: impl IntoIterator<Item = (OptionConnection, OptionValue)>,
) -> Result<Self::ConnectionType> {
self.0
.new_connection_with_opts(opts)
.map(SnowflakeConnection)
}
}
Loading
Loading