Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-toth committed Jun 22, 2024
1 parent ccc92b9 commit b23529a
Showing 1 changed file with 17 additions and 40 deletions.
57 changes: 17 additions & 40 deletions datafusion/optimizer/src/common_subexpr_eliminate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ struct Identifier<'n> {
}

impl<'n> Identifier<'n> {
pub fn new(expr: &'n Expr, random_state: &RandomState) -> Self {
fn new(expr: &'n Expr, random_state: &RandomState) -> Self {
let mut hasher = random_state.build_hasher();
expr.hash_node(&mut hasher);
let hash = hasher.finish();
Self { hash, expr }
}

pub fn combine(mut self, other: Option<Self>) -> Self {
fn combine(mut self, other: Option<Self>) -> Self {
other.map_or(self, |other_id| {
self.hash = combine_hashes(self.hash, other_id.hash);
self
Expand All @@ -76,12 +76,6 @@ impl Hash for Identifier<'_> {
}
}

impl From<Identifier<'_>> for String {
fn from(id: Identifier<'_>) -> Self {
format!("common_{}", id.hash)
}
}

/// A cache that contains the postorder index and the identifier of expression tree nodes
/// by the preorder index of the nodes.
///
Expand Down Expand Up @@ -1554,42 +1548,37 @@ mod test {
Ok(())
}

fn test_identifier(hash: u64, expr: &Expr) -> Identifier {
Identifier {
hash,
expr,
}
}

#[test]
fn redundant_project_fields() {
let table_scan = test_table_scan().unwrap();
let c_plus_a = col("c") + col("a");
let b_plus_a = col("b") + col("a");
let common_exprs_1 = CommonExprs::from([
(
Identifier {
hash: 0,
expr: &c_plus_a,
},
test_identifier(0, &c_plus_a),
(c_plus_a.clone(), format!("{CSE_PREFIX}_1")),
),
(
Identifier {
hash: 1,
expr: &b_plus_a,
},
test_identifier(1, &b_plus_a),
(b_plus_a.clone(), format!("{CSE_PREFIX}_2")),
),
]);
let c_plus_a_2 = col(format!("{CSE_PREFIX}_1"));
let b_plus_a_2 = col(format!("{CSE_PREFIX}_2"));
let common_exprs_2 = CommonExprs::from([
(
Identifier {
hash: 3,
expr: &c_plus_a_2,
},
test_identifier(3, &c_plus_a_2),
(c_plus_a_2.clone(), format!("{CSE_PREFIX}_3")),
),
(
Identifier {
hash: 4,
expr: &b_plus_a_2,
},
test_identifier(4, &b_plus_a_2),
(b_plus_a_2.clone(), format!("{CSE_PREFIX}_4")),
),
]);
Expand All @@ -1615,35 +1604,23 @@ mod test {
let b_plus_a = col("test1.b") + col("test1.a");
let common_exprs_1 = CommonExprs::from([
(
Identifier {
hash: 0,
expr: &c_plus_a,
},
test_identifier(0, &c_plus_a),
(c_plus_a.clone(), format!("{CSE_PREFIX}_1")),
),
(
Identifier {
hash: 1,
expr: &b_plus_a,
},
test_identifier(1, &b_plus_a),
(b_plus_a.clone(), format!("{CSE_PREFIX}_2")),
),
]);
let c_plus_a_2 = col(format!("{CSE_PREFIX}_1"));
let b_plus_a_2 = col(format!("{CSE_PREFIX}_2"));
let common_exprs_2 = CommonExprs::from([
(
Identifier {
hash: 3,
expr: &c_plus_a_2,
},
test_identifier(3, &c_plus_a_2),
(c_plus_a_2.clone(), format!("{CSE_PREFIX}_3")),
),
(
Identifier {
hash: 4,
expr: &b_plus_a_2,
},
test_identifier(4, &b_plus_a_2),
(b_plus_a_2.clone(), format!("{CSE_PREFIX}_4")),
),
]);
Expand Down

0 comments on commit b23529a

Please sign in to comment.