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

refactor: use native execs instead of custom execs #1262

Merged
merged 8 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/datasources/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ tracing = "0.1"
uuid = "1.4.0"
url.workspace = true
webpki-roots = "0.24.0"
dashmap = "5.4.0"

# SSH tunnels
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies]
Expand Down
104 changes: 104 additions & 0 deletions crates/datasources/src/object_store/csv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//! Helpers for handling csv files.

use std::any::Any;
use std::sync::Arc;

use async_trait::async_trait;
use datafusion::arrow::datatypes::SchemaRef as ArrowSchemaRef;
use datafusion::datasource::file_format::csv::CsvFormat;
use datafusion::datasource::file_format::file_type::FileCompressionType;
use datafusion::datasource::file_format::FileFormat;
use datafusion::datasource::object_store::ObjectStoreUrl;
use datafusion::datasource::TableProvider;
use datafusion::error::Result as DatafusionResult;
use datafusion::execution::context::SessionState;
use datafusion::logical_expr::TableType;
use datafusion::physical_plan::file_format::{CsvExec as DfCsvExec, FileScanConfig};
use datafusion::physical_plan::{ExecutionPlan, Statistics};
use datafusion::prelude::{Expr, SessionContext};

use crate::object_store::errors::Result;
use crate::object_store::TableAccessor;

/// Table provider for csv table
pub struct CsvTableProvider<T>
where
T: TableAccessor,
{
pub(crate) accessor: T,
/// Schema for csv file
pub(crate) arrow_schema: ArrowSchemaRef,
}

impl<T> CsvTableProvider<T>
where
T: TableAccessor,
{
pub async fn from_table_accessor(accessor: T) -> Result<CsvTableProvider<T>> {
let store = accessor.store();
let location = [accessor.object_meta().as_ref().clone()];
// TODO infer schema without generating unused session context/state
let csv_format = CsvFormat::default();
let session_ctx = SessionContext::new();
let state = session_ctx.state();
let arrow_schema = csv_format.infer_schema(&state, store, &location).await?;
Ok(CsvTableProvider {
accessor,
arrow_schema,
})
}
}

#[async_trait]
impl<T> TableProvider for CsvTableProvider<T>
where
T: TableAccessor + 'static,
{
fn as_any(&self) -> &dyn Any {
self
}

fn schema(&self) -> ArrowSchemaRef {
self.arrow_schema.clone()
}

fn table_type(&self) -> TableType {
TableType::View
}

async fn scan(
&self,
_ctx: &SessionState,
projection: Option<&Vec<usize>>,
_filters: &[Expr],
limit: Option<usize>,
) -> DatafusionResult<Arc<dyn ExecutionPlan>> {
let file = self.accessor.object_meta().as_ref().clone();
let base_url = self.accessor.base_path();

let base_config = FileScanConfig {
object_store_url: ObjectStoreUrl::parse(base_url)
.unwrap_or_else(|_| ObjectStoreUrl::local_filesystem()),
file_schema: self.arrow_schema.clone(),
file_groups: vec![vec![file.into()]],
statistics: Statistics::default(),
projection: projection.cloned(),
limit,
table_partition_cols: Vec::new(),
output_ordering: Vec::new(),
infinite_source: false,
};
// Assume csv has a header
let has_header = true;
let exec = DfCsvExec::new(
base_config,
has_header,
DEFAULT_DELIMITER,
DEFAULT_FILE_COMPRESSION_TYPE,
);
Ok(Arc::new(exec))
}
}

const DEFAULT_DELIMITER: u8 = b',';
const DEFAULT_FILE_COMPRESSION_TYPE: FileCompressionType = FileCompressionType::UNCOMPRESSED;
220 changes: 0 additions & 220 deletions crates/datasources/src/object_store/csv/csv_helper.rs

This file was deleted.

Loading