Skip to content

Commit

Permalink
impl vtab arrow (#140)
Browse files Browse the repository at this point in the history
* impl vtab arrow

Change-Id: I2044c36db7d7d145f5def782eb19c45d75ba4747

* fix clippy

Change-Id: I3ec9f35a646d23e875807cc7e883e1d3c6b30e80

* don't call func

* fmt
  • Loading branch information
wangfenjin authored Mar 22, 2023
1 parent 0c89e7f commit f8b45b5
Show file tree
Hide file tree
Showing 35 changed files with 703 additions and 210 deletions.
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ httpfs = ["libduckdb-sys/httpfs", "bundled"]
json = ["libduckdb-sys/json", "bundled"]
parquet = ["libduckdb-sys/parquet", "bundled"]
vtab = []
excel = ["vtab", "calamine"]
vtab-full = ["excel"]
vtab-excel = ["vtab", "calamine"]
vtab-arrow = ["vtab", "num"]
vtab-full = ["vtab-excel", "vtab-arrow"]
extensions-full = ["httpfs", "json", "parquet", "vtab-full"]
buildtime_bindgen = ["libduckdb-sys/buildtime_bindgen"]
modern-full = [
Expand Down Expand Up @@ -59,6 +60,7 @@ rust_decimal = "1.14"
strum = { version = "0.24", features = ["derive"] }
r2d2 = { version = "0.8.9", optional = true }
calamine = { version = "0.19.1", optional = true }
num = { version = "0.4", optional = true, default-features = false, features = ["std"] }

[dev-dependencies]
doc-comment = "0.3"
Expand Down
6 changes: 2 additions & 4 deletions examples/appender.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
extern crate duckdb;
use std::convert::TryFrom;

use duckdb::DropBehavior;
use duckdb::{params, Connection, Result};
use duckdb::{params, Connection, DropBehavior, Result};

fn main() -> Result<()> {
//let mut db = Connection::open("10m.db")?;
Expand Down Expand Up @@ -69,8 +68,7 @@ fn firstn(n: i32) -> impl std::iter::Iterator<Item = User> {
}

// Modified from https://github.com/avinassh/fast-sqlite3-inserts/blob/master/src/bin/common.rs
use rand::prelude::SliceRandom;
use rand::Rng;
use rand::{prelude::SliceRandom, Rng};

#[inline]
fn get_random_age() -> i8 {
Expand Down
7 changes: 4 additions & 3 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

extern crate duckdb;

use duckdb::arrow::record_batch::RecordBatch;
use duckdb::arrow::util::pretty::print_batches;
use duckdb::{params, Connection, Result};
use duckdb::{
arrow::{record_batch::RecordBatch, util::pretty::print_batches},
params, Connection, Result,
};

#[derive(Debug)]
struct Person {
Expand Down
7 changes: 4 additions & 3 deletions examples/parquet.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extern crate duckdb;
use duckdb::arrow::record_batch::RecordBatch;
use duckdb::arrow::util::pretty::print_batches;
use duckdb::{Connection, Result};
use duckdb::{
arrow::{record_batch::RecordBatch, util::pretty::print_batches},
Connection, Result,
};

fn main() -> Result<()> {
let db = Connection::open_in_memory()?;
Expand Down
16 changes: 7 additions & 9 deletions libduckdb-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::env;
use std::path::Path;
use std::{env, path::Path};

#[cfg(feature = "httpfs")]
mod openssl;
Expand Down Expand Up @@ -39,8 +38,10 @@ fn main() {

#[cfg(feature = "bundled")]
mod build_bundled {
use std::collections::{HashMap, HashSet};
use std::path::Path;
use std::{
collections::{HashMap, HashSet},
path::Path,
};

use crate::win_target;

Expand Down Expand Up @@ -199,8 +200,7 @@ mod build_linked {
use super::bindings;

use super::{env_prefix, is_compiler, lib_name, win_target, HeaderLocation};
use std::env;
use std::path::Path;
use std::{env, path::Path};

pub fn main(_out_dir: &str, out_path: &Path) {
// We need this to config the LD_LIBRARY_PATH
Expand Down Expand Up @@ -307,9 +307,7 @@ mod build_linked {
mod bindings {
use super::HeaderLocation;

use std::fs::OpenOptions;
use std::io::Write;
use std::path::Path;
use std::{fs::OpenOptions, io::Write, path::Path};

pub fn write_to_out_dir(header: HeaderLocation, out_path: &Path) {
let header: String = header.into();
Expand Down
8 changes: 5 additions & 3 deletions libduckdb-sys/openssl/find_normal.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{
ffi::OsString,
path::{Path, PathBuf},
process::Command,
};

use super::env;

Expand Down
10 changes: 6 additions & 4 deletions libduckdb-sys/openssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ extern crate pkg_config;
#[cfg(target_env = "msvc")]
extern crate vcpkg;

use std::collections::HashSet;
use std::env;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::{
collections::HashSet,
env,
ffi::OsString,
path::{Path, PathBuf},
};
mod cfgs;

mod find_normal;
Expand Down
9 changes: 5 additions & 4 deletions libduckdb-sys/openssl/run_bindgen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use bindgen::callbacks::{MacroParsingBehavior, ParseCallbacks};
use bindgen::RustTarget;
use std::env;
use std::path::PathBuf;
use bindgen::{
callbacks::{MacroParsingBehavior, ParseCallbacks},
RustTarget,
};
use std::{env, path::PathBuf};

const INCLUDES: &str = "
#include <openssl/aes.h>
Expand Down
3 changes: 1 addition & 2 deletions libduckdb-sys/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::duckdb_state;
use std::error;
use std::fmt;
use std::{error, fmt};

/// Error Codes
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down
20 changes: 12 additions & 8 deletions libduckdb-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ mod error;
#[cfg(test)]
mod tests {
use super::*;
use std::convert::TryFrom;
use std::ffi::{CStr, CString};
use std::mem;
use std::os::raw::c_char;
use std::ptr;
use std::{
convert::TryFrom,
ffi::{CStr, CString},
mem,
os::raw::c_char,
ptr,
};

use arrow::array::{Array, ArrayData, Int32Array, StructArray};
use arrow::datatypes::DataType;
use arrow::ffi::{ArrowArray, FFI_ArrowArray, FFI_ArrowSchema};
use arrow::{
array::{Array, ArrayData, Int32Array, StructArray},
datatypes::DataType,
ffi::{ArrowArray, FFI_ArrowArray, FFI_ArrowSchema},
};

unsafe fn print_int_result(mut result: duckdb_result) {
for i in 0..duckdb_column_count(&mut result) {
Expand Down
18 changes: 8 additions & 10 deletions src/appender.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use super::ffi;
use super::{AppenderParams, Connection, Result, ValueRef};
use std::ffi::c_void;
use std::fmt;
use std::iter::IntoIterator;
use std::os::raw::c_char;

use crate::error::result_from_duckdb_appender;
use crate::types::{TimeUnit, ToSql, ToSqlOutput};
use crate::Error;
use super::{ffi, AppenderParams, Connection, Result, ValueRef};
use std::{ffi::c_void, fmt, iter::IntoIterator, os::raw::c_char};

use crate::{
error::result_from_duckdb_appender,
types::{TimeUnit, ToSql, ToSqlOutput},
Error,
};

/// Appender for fast import data
pub struct Appender<'conn> {
Expand Down
6 changes: 4 additions & 2 deletions src/arrow_batch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::arrow::{datatypes::SchemaRef, record_batch::RecordBatch};
use super::Statement;
use super::{
arrow::{datatypes::SchemaRef, record_batch::RecordBatch},
Statement,
};

/// An handle for the resulting RecordBatch of a query.
#[must_use = "Arrow is lazy and will do nothing unless consumed"]
Expand Down
11 changes: 6 additions & 5 deletions src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Prepared statements cache for faster execution.

use crate::raw_statement::RawStatement;
use crate::{Connection, Result, Statement};
use crate::{raw_statement::RawStatement, Connection, Result, Statement};
use hashlink::LruCache;
use std::cell::RefCell;
use std::ops::{Deref, DerefMut};
use std::sync::Arc;
use std::{
cell::RefCell,
ops::{Deref, DerefMut},
sync::Arc,
};

impl Connection {
/// Prepare a SQL statement for execution, returning a previously prepared
Expand Down
11 changes: 3 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use super::ffi;
use super::Result;
use super::{ffi, Result};
use crate::error::Error;
use std::default::Default;
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr;
use std::{default::Default, ffi::CString, os::raw::c_char, ptr};

use strum::{Display, EnumString};

Expand Down Expand Up @@ -140,8 +136,7 @@ impl Drop for Config {

#[cfg(test)]
mod test {
use crate::types::Value;
use crate::{Config, Connection, Result};
use crate::{types::Value, Config, Connection, Result};

#[test]
fn test_default_config() -> Result<()> {
Expand Down
13 changes: 5 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use super::Result;
use crate::ffi;
use crate::types::FromSqlError;
use crate::types::Type;
use std::error;
use std::ffi::CStr;
use std::fmt;
use std::path::PathBuf;
use std::str;
use crate::{
ffi,
types::{FromSqlError, Type},
};
use std::{error, ffi::CStr, fmt, path::PathBuf, str};

/// Enum listing possible errors from duckdb.
#[derive(Debug)]
Expand Down
22 changes: 12 additions & 10 deletions src/inner_connection.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::ffi::{c_void, CStr, CString};
use std::mem;
use std::os::raw::c_char;
use std::ptr;
use std::str;
use std::{
ffi::{c_void, CStr, CString},
mem,
os::raw::c_char,
ptr, str,
};

use super::ffi;
use super::{Appender, Config, Connection, Result};
use crate::error::{result_from_duckdb_appender, result_from_duckdb_arrow, result_from_duckdb_prepare, Error};
use crate::raw_statement::RawStatement;
use crate::statement::Statement;
use super::{ffi, Appender, Config, Connection, Result};
use crate::{
error::{result_from_duckdb_appender, result_from_duckdb_arrow, result_from_duckdb_prepare, Error},
raw_statement::RawStatement,
statement::Statement,
};

pub struct InnerConnection {
pub db: ffi::duckdb_database,
Expand Down
65 changes: 30 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,35 @@

pub use libduckdb_sys as ffi;

use std::cell::RefCell;
use std::convert;
use std::default::Default;
use std::ffi::CString;
use std::fmt;
use std::path::{Path, PathBuf};
use std::result;
use std::str;

use crate::cache::StatementCache;
use crate::inner_connection::InnerConnection;
use crate::raw_statement::RawStatement;
use crate::types::ValueRef;

pub use crate::appender::Appender;
pub use crate::appender_params::{appender_params_from_iter, AppenderParams, AppenderParamsFromIter};
pub use crate::arrow_batch::Arrow;
pub use crate::cache::CachedStatement;
pub use crate::column::Column;
pub use crate::config::{AccessMode, Config, DefaultNullOrder, DefaultOrder};
pub use crate::error::Error;
pub use crate::ffi::ErrorCode;
pub use crate::params::{params_from_iter, Params, ParamsFromIter};
use std::{
cell::RefCell,
convert,
default::Default,
ffi::CString,
fmt,
path::{Path, PathBuf},
result, str,
};

use crate::{cache::StatementCache, inner_connection::InnerConnection, raw_statement::RawStatement, types::ValueRef};

#[cfg(feature = "r2d2")]
pub use crate::r2d2::DuckdbConnectionManager;
pub use crate::row::{AndThenRows, Map, MappedRows, Row, RowIndex, Rows};
pub use crate::statement::Statement;
pub use crate::transaction::{DropBehavior, Savepoint, Transaction, TransactionBehavior};
pub use crate::types::ToSql;
pub use crate::{
appender::Appender,
appender_params::{appender_params_from_iter, AppenderParams, AppenderParamsFromIter},
arrow_batch::Arrow,
cache::CachedStatement,
column::Column,
config::{AccessMode, Config, DefaultNullOrder, DefaultOrder},
error::Error,
ffi::ErrorCode,
params::{params_from_iter, Params, ParamsFromIter},
row::{AndThenRows, Map, MappedRows, Row, RowIndex, Rows},
statement::Statement,
transaction::{DropBehavior, Savepoint, Transaction, TransactionBehavior},
types::ToSql,
};

// re-export dependencies from arrow-rs to minimise version maintenance for crate users
pub use arrow;
Expand Down Expand Up @@ -546,12 +546,9 @@ doc_comment::doctest!("../README.md");
#[cfg(test)]
mod test {
use super::*;
use std::error::Error as StdError;
use std::fmt;
use std::{error::Error as StdError, fmt};

use arrow::array::Int32Array;
use arrow::datatypes::DataType;
use arrow::record_batch::RecordBatch;
use arrow::{array::Int32Array, datatypes::DataType, record_batch::RecordBatch};
use fallible_iterator::FallibleIterator;

// this function is never called, but is still type checked; in
Expand Down Expand Up @@ -677,9 +674,7 @@ mod test {
#[cfg(unix)]
#[test]
fn test_invalid_unicode_file_names() -> Result<()> {
use std::ffi::OsStr;
use std::fs::File;
use std::os::unix::ffi::OsStrExt;
use std::{ffi::OsStr, fs::File, os::unix::ffi::OsStrExt};
let temp_dir = tempfile::tempdir().unwrap();

let path = temp_dir.path();
Expand Down
Loading

0 comments on commit f8b45b5

Please sign in to comment.