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

WIP(iox-10577): patched df upgrade 202-04-09 #10

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
66f4fcb
Use `struct` instead of `named_struct` when there are no aliases (#9897)
alamb Apr 2, 2024
f0eec34
coercion vec[Dictionary, Utf8] to Dictionary for coalesce function (#…
Lordworms Apr 5, 2024
e8de1c6
fix NamedStructField should be rewritten in OperatorToFunction in sub…
alamb Apr 10, 2024
aaa8735
Merge commit 'dfd4442da8332116c7e1f9fcd9de7c6da856442b' into chunchun…
appletreeisyellow Apr 22, 2024
7c8d77a
Merge commit '2f550032140d42d1ee6d8ed86f7790766fa7302e' into chunchun…
appletreeisyellow Apr 22, 2024
555c388
Merge commit '202f415811c0a559dde63108be967855844c14cb' into chunchun…
appletreeisyellow Apr 22, 2024
a689b9d
chore: merge main 3ae029988754c3fd3eb000abd4b76e643b9cbc7b
appletreeisyellow Apr 22, 2024
59cb935
Merge commit '1dd535451198777693ac962a21b79a680bd4b7e8' into chunchun…
appletreeisyellow Apr 22, 2024
d51293b
Merge commit '2b0a7db0ce64950864e07edaddfa80756fe0ffd5' into chunchun…
appletreeisyellow Apr 22, 2024
eedf46d
Merge commit '6d81c410ad5917de01451d819620eb79bebd7d60' into chunchun…
appletreeisyellow Apr 22, 2024
f7e4aa1
Merge commit 'f7b4ed0ae1382fc10498c053597c202974753514' into chunchun…
appletreeisyellow Apr 23, 2024
aacf194
Merge commit '215f30f74a12e91fd7dca0d30e37014c8c3493ed' into chunchun…
appletreeisyellow Apr 23, 2024
e2773fd
Merge commit '8c9e5678228557aff370b137e9029462230df68a' into chunchun…
appletreeisyellow Apr 23, 2024
5b989ed
Merge commit '78f8ef16b89ffa38f80e8c7e3f21948602901787' into chunchun…
appletreeisyellow Apr 23, 2024
059ce1a
Merge commit 'fe71aa242d44ad337aba1e373b336da234ed4f8c' into chunchun…
appletreeisyellow Apr 23, 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
131 changes: 89 additions & 42 deletions datafusion/optimizer/src/analyzer/function_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ use super::AnalyzerRule;
use datafusion_common::config::ConfigOptions;
use datafusion_common::tree_node::{Transformed, TreeNodeRewriter};
use datafusion_common::{DFSchema, Result};
use datafusion_expr::expr::{Exists, InSubquery};
use datafusion_expr::expr_rewriter::{rewrite_preserving_name, FunctionRewrite};
use datafusion_expr::utils::merge_schema;
use datafusion_expr::{Expr, LogicalPlan};
use datafusion_expr::{Expr, LogicalPlan, Subquery};
use std::sync::Arc;

/// Analyzer rule that invokes [`FunctionRewrite`]s on expressions
Expand All @@ -45,54 +46,66 @@ impl AnalyzerRule for ApplyFunctionRewrites {
}

fn analyze(&self, plan: LogicalPlan, options: &ConfigOptions) -> Result<LogicalPlan> {
self.analyze_internal(&plan, options)
analyze_internal(&plan, &self.function_rewrites, options)
}
}

impl ApplyFunctionRewrites {
fn analyze_internal(
&self,
plan: &LogicalPlan,
options: &ConfigOptions,
) -> Result<LogicalPlan> {
// optimize child plans first
let new_inputs = plan
.inputs()
.iter()
.map(|p| self.analyze_internal(p, options))
.collect::<Result<Vec<_>>>()?;

// get schema representing all available input fields. This is used for data type
// resolution only, so order does not matter here
let mut schema = merge_schema(new_inputs.iter().collect());

if let LogicalPlan::TableScan(ts) = plan {
let source_schema = DFSchema::try_from_qualified_schema(
ts.table_name.clone(),
&ts.source.schema(),
)?;
schema.merge(&source_schema);
}
fn analyze_internal(
plan: &LogicalPlan,
function_rewrites: &[Arc<dyn FunctionRewrite + Send + Sync>],
options: &ConfigOptions,
) -> Result<LogicalPlan> {
// optimize child plans first
let new_inputs = plan
.inputs()
.iter()
.map(|p| analyze_internal(p, function_rewrites, options))
.collect::<Result<Vec<_>>>()?;

let mut expr_rewrite = OperatorToFunctionRewriter {
function_rewrites: &self.function_rewrites,
options,
schema: &schema,
};
// get schema representing all available input fields. This is used for data type
// resolution only, so order does not matter here
let mut schema = merge_schema(new_inputs.iter().collect());

let new_expr = plan
.expressions()
.into_iter()
.map(|expr| {
// ensure names don't change:
// https://github.com/apache/arrow-datafusion/issues/3555
rewrite_preserving_name(expr, &mut expr_rewrite)
})
.collect::<Result<Vec<_>>>()?;

plan.with_new_exprs(new_expr, new_inputs)
if let LogicalPlan::TableScan(ts) = plan {
let source_schema = DFSchema::try_from_qualified_schema(
ts.table_name.clone(),
&ts.source.schema(),
)?;
schema.merge(&source_schema);
}

let mut expr_rewrite = OperatorToFunctionRewriter {
function_rewrites,
options,
schema: &schema,
};

let new_expr = plan
.expressions()
.into_iter()
.map(|expr| {
// ensure names don't change:
// https://github.com/apache/arrow-datafusion/issues/3555
rewrite_preserving_name(expr, &mut expr_rewrite)
})
.collect::<Result<Vec<_>>>()?;

plan.with_new_exprs(new_expr, new_inputs)
}

fn rewrite_subquery(
mut subquery: Subquery,
function_rewrites: &[Arc<dyn FunctionRewrite + Send + Sync>],
options: &ConfigOptions,
) -> Result<Subquery> {
subquery.subquery = Arc::new(analyze_internal(
&subquery.subquery,
function_rewrites,
options,
)?);
Ok(subquery)
}

struct OperatorToFunctionRewriter<'a> {
function_rewrites: &'a [Arc<dyn FunctionRewrite + Send + Sync>],
options: &'a ConfigOptions,
Expand All @@ -113,6 +126,40 @@ impl<'a> TreeNodeRewriter for OperatorToFunctionRewriter<'a> {
expr = result.data
}

// recurse into subqueries if needed
let expr = match expr {
Expr::ScalarSubquery(subquery) => Expr::ScalarSubquery(rewrite_subquery(
subquery,
self.function_rewrites,
self.options,
)?),

Expr::Exists(Exists { subquery, negated }) => Expr::Exists(Exists {
subquery: rewrite_subquery(
subquery,
self.function_rewrites,
self.options,
)?,
negated,
}),

Expr::InSubquery(InSubquery {
expr,
subquery,
negated,
}) => Expr::InSubquery(InSubquery {
expr,
subquery: rewrite_subquery(
subquery,
self.function_rewrites,
self.options,
)?,
negated,
}),

expr => expr,
};

Ok(if transformed {
Transformed::yes(expr)
} else {
Expand Down
55 changes: 55 additions & 0 deletions datafusion/sqllogictest/test_files/subquery.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,58 @@ logical_plan
Projection: t.a / Int64(2)Int64(2)t.a AS t.a / Int64(2), t.a / Int64(2)Int64(2)t.a AS t.a / Int64(2) + Int64(1)
--Projection: t.a / Int64(2) AS t.a / Int64(2)Int64(2)t.a
----TableScan: t projection=[a]

###
## Ensure that operators are rewritten in subqueries
###

statement ok
create table foo(x int) as values (1);

# Show input data
query ?
select struct(1, 'b')
----
{c0: 1, c1: b}


query T
select (select struct(1, 'b')['c1']);
----
b

query T
select 'foo' || (select struct(1, 'b')['c1']);
----
foob

query I
SELECT * FROM (VALUES (1), (2))
WHERE column1 IN (SELECT struct(1, 'b')['c0']);
----
1

# also add an expression so the subquery is the output expr
query I
SELECT * FROM (VALUES (1), (2))
WHERE 1+2 = 3 AND column1 IN (SELECT struct(1, 'b')['c0']);
----
1


query I
SELECT * FROM foo
WHERE EXISTS (SELECT * FROM (values (1)) WHERE column1 = foo.x AND struct(1, 'b')['c0'] = 1);
----
1

# also add an expression so the subquery is the output expr
query I
SELECT * FROM foo
WHERE 1+2 = 3 AND EXISTS (SELECT * FROM (values (1)) WHERE column1 = foo.x AND struct(1, 'b')['c0'] = 1);
----
1


statement ok
drop table foo;
Loading