-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
113 additions
and
9 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ members = [ | |
"examples", | ||
"sources/sql", | ||
"sources/flight-sql", | ||
"sql-writer", | ||
] | ||
|
||
[patch.crates-io] | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "datafusion-sql-writer" | ||
version.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
readme.workspace = true | ||
|
||
[lib] | ||
name = "datafusion_sql_writer" | ||
path = "src/lib.rs" | ||
|
||
[dependencies] | ||
datafusion.workspace = true | ||
# derive_builder = "0.13.0" | ||
|
||
[dev-dependencies] | ||
tokio = "1.35.1" |
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,35 @@ | ||
use std::sync::Arc; | ||
|
||
use datafusion::{ | ||
common::Column, | ||
logical_expr::{BinaryExpr, Operator}, | ||
prelude::Expr, | ||
sql::{sqlparser::dialect::GenericDialect, TableReference}, | ||
}; | ||
use datafusion_sql_writer::from_df_epr; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// Example expression | ||
let expr = Expr::BinaryExpr(BinaryExpr { | ||
left: Box::new(Expr::Column(Column { | ||
relation: Some(TableReference::bare("table_a")), | ||
name: "id".to_string(), | ||
})), | ||
op: Operator::Gt, | ||
right: Box::new(Expr::Column(Column { | ||
relation: Some(TableReference::bare("table_b")), | ||
name: "b".to_string(), | ||
})), | ||
}); | ||
|
||
// datafusion::Expr -> sqlparser::ast | ||
let dialect = Arc::new(GenericDialect {}); | ||
let ast = from_df_epr(&expr, dialect)?; | ||
|
||
// Get SQL string by formatting the AST | ||
let sql = format!("{}", ast); | ||
|
||
println!("{sql}"); | ||
|
||
Ok(()) | ||
} |
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,41 @@ | ||
use std::sync::Arc; | ||
|
||
use datafusion::{ | ||
execution::context::SessionContext, sql::sqlparser::dialect::GenericDialect, | ||
test_util::TestTableFactory, | ||
}; | ||
use datafusion_sql_writer::from_df_plan; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// Example query | ||
let query = "select ta.id, tb.value from table_a ta join table_b tb on ta.id = tb.id;"; | ||
|
||
// Create the DataFusion plan | ||
let dialect = Arc::new(GenericDialect {}); | ||
let ctx = mock_ctx().await; | ||
let plan = ctx.sql(query).await.unwrap().into_unoptimized_plan(); | ||
|
||
// datafusion::LogicalPlan -> sqlparser::ast | ||
let ast = from_df_plan(&plan, dialect)?; | ||
|
||
// Get SQL string by formatting the AST | ||
let sql = format!("{}", ast); | ||
|
||
println!("{sql}"); | ||
Ok(()) | ||
} | ||
|
||
async fn mock_ctx() -> SessionContext { | ||
let mut state = SessionContext::new().state(); | ||
state | ||
.table_factories_mut() | ||
.insert("MOCKTABLE".to_string(), Arc::new(TestTableFactory {})); | ||
let ctx = SessionContext::new_with_state(state); | ||
|
||
ctx.sql("CREATE EXTERNAL TABLE table_a (id integer, value string) STORED AS MOCKTABLE LOCATION 'mock://path';").await.unwrap(); | ||
ctx.sql("CREATE EXTERNAL TABLE table_b (id integer, value string) STORED AS MOCKTABLE LOCATION 'mock://path';").await.unwrap(); | ||
ctx.sql("CREATE EXTERNAL TABLE table_c (id integer, value string) STORED AS MOCKTABLE LOCATION 'mock://path';").await.unwrap(); | ||
|
||
ctx | ||
} |
File renamed without changes.
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