forked from apache/datafusion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit '4109f581ce9bca956e01f13ff16b30d59720e96b' into chunchun…
…/update-df-june-week-3
- Loading branch information
Showing
77 changed files
with
3,352 additions
and
2,113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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. | ||
|
||
//! Shows an example of a custom session context that unions the input plan with itself. | ||
//! To run this example, use `cargo run --example cli-session-context` from within the `datafusion-cli` directory. | ||
use std::sync::Arc; | ||
|
||
use datafusion::{ | ||
dataframe::DataFrame, | ||
error::DataFusionError, | ||
execution::{context::SessionState, TaskContext}, | ||
logical_expr::{LogicalPlan, LogicalPlanBuilder}, | ||
prelude::SessionContext, | ||
}; | ||
use datafusion_cli::{ | ||
cli_context::CliSessionContext, exec::exec_from_repl, print_options::PrintOptions, | ||
}; | ||
use object_store::ObjectStore; | ||
|
||
/// This is a toy example of a custom session context that unions the input plan with itself. | ||
struct MyUnionerContext { | ||
ctx: SessionContext, | ||
} | ||
|
||
impl Default for MyUnionerContext { | ||
fn default() -> Self { | ||
Self { | ||
ctx: SessionContext::new(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl CliSessionContext for MyUnionerContext { | ||
fn task_ctx(&self) -> Arc<TaskContext> { | ||
self.ctx.task_ctx() | ||
} | ||
|
||
fn session_state(&self) -> SessionState { | ||
self.ctx.state() | ||
} | ||
|
||
fn register_object_store( | ||
&self, | ||
url: &url::Url, | ||
object_store: Arc<dyn ObjectStore>, | ||
) -> Option<Arc<dyn ObjectStore + 'static>> { | ||
self.ctx.register_object_store(url, object_store) | ||
} | ||
|
||
fn register_table_options_extension_from_scheme(&self, _scheme: &str) { | ||
unimplemented!() | ||
} | ||
|
||
async fn execute_logical_plan( | ||
&self, | ||
plan: LogicalPlan, | ||
) -> Result<DataFrame, DataFusionError> { | ||
let new_plan = LogicalPlanBuilder::from(plan.clone()) | ||
.union(plan.clone())? | ||
.build()?; | ||
|
||
self.ctx.execute_logical_plan(new_plan).await | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
/// Runs the example. | ||
pub async fn main() { | ||
let mut my_ctx = MyUnionerContext::default(); | ||
|
||
let mut print_options = PrintOptions { | ||
format: datafusion_cli::print_format::PrintFormat::Automatic, | ||
quiet: false, | ||
maxrows: datafusion_cli::print_options::MaxRows::Unlimited, | ||
color: true, | ||
}; | ||
|
||
exec_from_repl(&mut my_ctx, &mut print_options) | ||
.await | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// 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::sync::Arc; | ||
|
||
use datafusion::{ | ||
dataframe::DataFrame, | ||
error::DataFusionError, | ||
execution::{context::SessionState, TaskContext}, | ||
logical_expr::LogicalPlan, | ||
prelude::SessionContext, | ||
}; | ||
use object_store::ObjectStore; | ||
|
||
use crate::object_storage::{AwsOptions, GcpOptions}; | ||
|
||
#[async_trait::async_trait] | ||
/// The CLI session context trait provides a way to have a session context that can be used with datafusion's CLI code. | ||
pub trait CliSessionContext { | ||
/// Get an atomic reference counted task context. | ||
fn task_ctx(&self) -> Arc<TaskContext>; | ||
|
||
/// Get the session state. | ||
fn session_state(&self) -> SessionState; | ||
|
||
/// Register an object store with the session context. | ||
fn register_object_store( | ||
&self, | ||
url: &url::Url, | ||
object_store: Arc<dyn ObjectStore>, | ||
) -> Option<Arc<dyn ObjectStore + 'static>>; | ||
|
||
/// Register table options extension from scheme. | ||
fn register_table_options_extension_from_scheme(&self, scheme: &str); | ||
|
||
/// Execute a logical plan and return a DataFrame. | ||
async fn execute_logical_plan( | ||
&self, | ||
plan: LogicalPlan, | ||
) -> Result<DataFrame, DataFusionError>; | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl CliSessionContext for SessionContext { | ||
fn task_ctx(&self) -> Arc<TaskContext> { | ||
self.task_ctx() | ||
} | ||
|
||
fn session_state(&self) -> SessionState { | ||
self.state() | ||
} | ||
|
||
fn register_object_store( | ||
&self, | ||
url: &url::Url, | ||
object_store: Arc<dyn ObjectStore>, | ||
) -> Option<Arc<dyn ObjectStore + 'static>> { | ||
self.register_object_store(url, object_store) | ||
} | ||
|
||
fn register_table_options_extension_from_scheme(&self, scheme: &str) { | ||
match scheme { | ||
// For Amazon S3 or Alibaba Cloud OSS | ||
"s3" | "oss" | "cos" => { | ||
// Register AWS specific table options in the session context: | ||
self.register_table_options_extension(AwsOptions::default()) | ||
} | ||
// For Google Cloud Storage | ||
"gs" | "gcs" => { | ||
// Register GCP specific table options in the session context: | ||
self.register_table_options_extension(GcpOptions::default()) | ||
} | ||
// For unsupported schemes, do nothing: | ||
_ => {} | ||
} | ||
} | ||
|
||
async fn execute_logical_plan( | ||
&self, | ||
plan: LogicalPlan, | ||
) -> Result<DataFrame, DataFusionError> { | ||
self.execute_logical_plan(plan).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.