sgrebnov commented on code in PR #13132:
URL: https://github.com/apache/datafusion/pull/13132#discussion_r1817953084
##########
datafusion/sql/src/unparser/utils.rs:
##########
@@ -239,6 +239,87 @@ pub(crate) fn unproject_sort_expr(
Ok(sort_expr)
}
+/// Iterates through the children of a [LogicalPlan] to find a TableScan node
before encountering
+/// a Projection or any unexpected node that indicates the presence of a
Projection (SELECT) in the plan.
+/// If a TableScan node is found, returns the TableScan node without filters,
along with the collected filters separately.
+/// If the plan contains a Projection, returns None.
+///
+/// Note: If a table alias is present, TableScan filters are rewritten to
reference the alias.
+///
+/// LogicalPlan example:
+/// Filter: ta.j1_id < 5
+/// Alias: ta
+/// TableScan: j1, j1_id > 10
+///
+/// Will return LogicalPlan below:
+/// Alias: ta
+/// TableScan: j1
+/// And filters: [ta.j1_id < 5, ta.j1_id > 10]
+pub(crate) fn try_transform_to_simple_table_scan_with_filters(
+ plan: &LogicalPlan,
+) -> Result<Option<(LogicalPlan, Vec<Expr>)>> {
+ let mut filters: Vec<Expr> = vec![];
+ let mut plan_stack = vec![plan];
+ let mut table_alias = None;
+
+ while let Some(current_plan) = plan_stack.pop() {
+ match current_plan {
+ LogicalPlan::SubqueryAlias(alias) => {
+ table_alias = Some(alias.alias.clone());
+ plan_stack.push(alias.input.as_ref());
+ }
+ LogicalPlan::Filter(filter) => {
+ filters.push(filter.predicate.clone());
+ plan_stack.push(filter.input.as_ref());
+ }
+ LogicalPlan::TableScan(table_scan) => {
+ let table_schema = table_scan.source.schema();
+ // optional rewriter if table has an alias
+ let mut filter_alias_rewriter =
+ table_alias.as_ref().map(|alias_name| TableAliasRewriter {
+ table_schema: &table_schema,
+ alias_name: alias_name.clone(),
+ });
+
+ // rewrite filters to use table alias if present
+ let table_scan_filters = table_scan
Review Comment:
`unparse_table_scan_pushdown` has similar filters rewriting logic (other
logic is different as it adds filters directly to new plan) - this could be
refactored to re-use some common rewrite logic
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]