Skip to content

Commit 28efc88

Browse files
authored
Preserve the filter order when eliminating duplicated filter (#56)
1 parent 06969ee commit 28efc88

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

datafusion/sql/src/unparser/utils.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::{cmp::Ordering, collections::HashSet, sync::Arc, vec};
18+
use std::{cmp::Ordering, sync::Arc, vec};
1919

2020
use datafusion_common::{
2121
internal_err,
@@ -102,7 +102,7 @@ pub(crate) fn find_unnest_node_within_select(
102102
/// TableScan: j1
103103
/// And filters: [ta.j1_id < 5, ta.j1_id > 10]
104104
pub (crate) fn try_transform_to_simple_table_scan_with_filters(plan: &LogicalPlan) -> Option<(LogicalPlan, Vec<Expr>)> {
105-
let mut filters: HashSet<Expr> = HashSet::new();
105+
let mut filters: Vec<Expr> = Vec::new();
106106
let mut plan_stack = vec![plan];
107107
let mut table_alias = None;
108108

@@ -113,7 +113,9 @@ pub (crate) fn try_transform_to_simple_table_scan_with_filters(plan: &LogicalPla
113113
plan_stack.push(alias.input.as_ref());
114114
}
115115
LogicalPlan::Filter(filter) => {
116-
filters.insert(filter.predicate.clone());
116+
if !filters.contains(&filter.predicate) {
117+
filters.push(filter.predicate.clone());
118+
}
117119
plan_stack.push(filter.input.as_ref());
118120
}
119121
LogicalPlan::TableScan(table_scan) => {
@@ -139,7 +141,9 @@ pub (crate) fn try_transform_to_simple_table_scan_with_filters(plan: &LogicalPla
139141
}).collect::<Result<Vec<_>, DataFusionError>>().ok()?;
140142

141143
for table_scan_filter in table_scan_filters {
142-
filters.insert(table_scan_filter);
144+
if !filters.contains(&table_scan_filter) {
145+
filters.push(table_scan_filter);
146+
}
143147
}
144148

145149
let mut builder = LogicalPlanBuilder::scan(
@@ -153,7 +157,6 @@ pub (crate) fn try_transform_to_simple_table_scan_with_filters(plan: &LogicalPla
153157
}
154158

155159
let plan = builder.build().ok()?;
156-
let filters: Vec<Expr> = filters.into_iter().collect();
157160

158161
return Some((plan, filters));
159162

0 commit comments

Comments
 (0)