Skip to content

Commit 95972bc

Browse files
feat[vortex-array]: added debug to expr/array reduce rules (#5442)
Signed-off-by: Joe Isaacs <[email protected]>
1 parent 63b759f commit 95972bc

File tree

18 files changed

+163
-123
lines changed

18 files changed

+163
-123
lines changed

vortex-array/src/array/session/rewrite.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
use std::fmt::Debug;
45
use std::marker::PhantomData;
56
use std::sync::Arc;
67

@@ -11,17 +12,17 @@ use crate::EncodingId;
1112
use crate::array::ArrayRef;
1213
use crate::array::transform::context::ArrayRuleContext;
1314
use crate::array::transform::rules::{
14-
AnyParent, ArrayParentMatcher, ArrayParentReduceRule, ArrayReduceRule,
15+
AnyArrayParent, ArrayParentMatcher, ArrayParentReduceRule, ArrayReduceRule,
1516
};
1617
use crate::vtable::VTable;
1718

1819
/// Dynamic trait for array reduce rules
19-
pub trait DynArrayReduceRule: Send + Sync {
20+
pub trait DynArrayReduceRule: Debug + Send + Sync {
2021
fn reduce(&self, array: &ArrayRef, ctx: &ArrayRuleContext) -> VortexResult<Option<ArrayRef>>;
2122
}
2223

2324
/// Dynamic trait for array parent reduce rules
24-
pub trait DynArrayParentReduceRule: Send + Sync {
25+
pub trait DynArrayParentReduceRule: Debug + Send + Sync {
2526
fn reduce_parent(
2627
&self,
2728
array: &ArrayRef,
@@ -37,12 +38,30 @@ struct ArrayReduceRuleAdapter<V: VTable, R> {
3738
_phantom: PhantomData<V>,
3839
}
3940

41+
impl<V: VTable, R: Debug> Debug for ArrayReduceRuleAdapter<V, R> {
42+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43+
f.debug_struct("ArrayReduceRuleAdapter")
44+
.field("rule", &self.rule)
45+
.finish()
46+
}
47+
}
48+
4049
/// Adapter for ArrayParentReduceRule
4150
struct ArrayParentReduceRuleAdapter<Child: VTable, Parent: ArrayParentMatcher, R> {
4251
rule: R,
4352
_phantom: PhantomData<(Child, Parent)>,
4453
}
4554

55+
impl<Child: VTable, Parent: ArrayParentMatcher, R: Debug> Debug
56+
for ArrayParentReduceRuleAdapter<Child, Parent, R>
57+
{
58+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59+
f.debug_struct("ArrayParentReduceRuleAdapter")
60+
.field("rule", &self.rule)
61+
.finish()
62+
}
63+
}
64+
4665
impl<V, R> DynArrayReduceRule for ArrayReduceRuleAdapter<V, R>
4766
where
4867
V: VTable,
@@ -81,7 +100,7 @@ where
81100

82101
/// Inner struct that holds all the rule registries.
83102
/// Wrapped in a single Arc by ArrayRewriteRuleRegistry for efficient cloning.
84-
#[derive(Default)]
103+
#[derive(Default, Debug)]
85104
struct ArrayRewriteRuleRegistryInner {
86105
/// Reduce rules indexed by encoding ID
87106
reduce_rules: DashMap<EncodingId, Vec<Arc<dyn DynArrayReduceRule>>>,
@@ -91,25 +110,6 @@ struct ArrayRewriteRuleRegistryInner {
91110
any_parent_rules: DashMap<EncodingId, Vec<Arc<dyn DynArrayParentReduceRule>>>,
92111
}
93112

94-
impl std::fmt::Debug for ArrayRewriteRuleRegistryInner {
95-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96-
f.debug_struct("ArrayRewriteRuleRegistryInner")
97-
.field(
98-
"reduce_rules",
99-
&format!("{} encodings", self.reduce_rules.len()),
100-
)
101-
.field(
102-
"parent_rules",
103-
&format!("{} pairs", self.parent_rules.len()),
104-
)
105-
.field(
106-
"any_parent_rules",
107-
&format!("{} encodings", self.any_parent_rules.len()),
108-
)
109-
.finish()
110-
}
111-
}
112-
113113
/// Registry of array rewrite rules.
114114
///
115115
/// Stores rewrite rules indexed by the encoding ID they apply to.
@@ -135,8 +135,7 @@ impl ArrayRewriteRuleRegistry {
135135
pub fn register_reduce_rule<V, R>(&self, encoding: &V::Encoding, rule: R)
136136
where
137137
V: VTable,
138-
R: 'static,
139-
R: ArrayReduceRule<V>,
138+
R: ArrayReduceRule<V> + 'static,
140139
{
141140
let adapter = ArrayReduceRuleAdapter {
142141
rule,
@@ -159,8 +158,7 @@ impl ArrayRewriteRuleRegistry {
159158
) where
160159
Child: VTable,
161160
Parent: VTable,
162-
R: 'static,
163-
R: ArrayParentReduceRule<Child, Parent>,
161+
R: ArrayParentReduceRule<Child, Parent> + 'static,
164162
{
165163
let adapter = ArrayParentReduceRuleAdapter {
166164
rule,
@@ -179,8 +177,7 @@ impl ArrayRewriteRuleRegistry {
179177
pub fn register_any_parent_rule<Child, R>(&self, child_encoding: &Child::Encoding, rule: R)
180178
where
181179
Child: VTable,
182-
R: 'static,
183-
R: ArrayParentReduceRule<Child, AnyParent>,
180+
R: ArrayParentReduceRule<Child, AnyArrayParent> + 'static,
184181
{
185182
let adapter = ArrayParentReduceRuleAdapter {
186183
rule,

vortex-array/src/array/transform/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ mod tests;
99

1010
pub use context::ArrayRuleContext;
1111
pub use optimizer::ArrayOptimizer;
12-
pub use rules::{AnyParent, ArrayParentMatcher, ArrayParentReduceRule, ArrayReduceRule};
12+
pub use rules::{AnyArrayParent, ArrayParentMatcher, ArrayParentReduceRule, ArrayReduceRule};

vortex-array/src/array/transform/optimizer.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,7 @@ impl ArrayOptimizer {
8989
Some(&parent_id),
9090
|rules| -> VortexResult<Option<ArrayRef>> {
9191
for rule in rules {
92-
println!("apply rule");
9392
if let Some(new_array) = rule.reduce_parent(child, &array, idx, ctx)? {
94-
println!(
95-
"matched new: {}, old: {}",
96-
new_array.display_tree(),
97-
array.display_tree()
98-
);
9993
return Ok(Some(new_array));
10094
}
10195
}

vortex-array/src/array/transform/rules.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
use std::fmt::Debug;
5+
46
use vortex_error::VortexResult;
57

68
use crate::array::ArrayRef;
@@ -16,9 +18,10 @@ pub trait ArrayParentMatcher: Send + Sync + 'static {
1618
}
1719

1820
/// Matches any parent type (wildcard matcher)
19-
pub struct AnyParent;
21+
#[derive(Debug)]
22+
pub struct AnyArrayParent;
2023

21-
impl ArrayParentMatcher for AnyParent {
24+
impl ArrayParentMatcher for AnyArrayParent {
2225
type View<'a> = &'a ArrayRef;
2326

2427
fn try_match(parent: &ArrayRef) -> Option<Self::View<'_>> {
@@ -36,7 +39,7 @@ impl<V: VTable> ArrayParentMatcher for V {
3639
}
3740

3841
/// A rewrite rule that transforms arrays based on the array itself and its children
39-
pub trait ArrayReduceRule<V: VTable>: Send + Sync {
42+
pub trait ArrayReduceRule<V: VTable>: Debug + Send + Sync + 'static {
4043
/// Attempt to rewrite this array.
4144
///
4245
/// Returns:
@@ -47,7 +50,9 @@ pub trait ArrayReduceRule<V: VTable>: Send + Sync {
4750
}
4851

4952
/// A rewrite rule that transforms arrays based on parent context
50-
pub trait ArrayParentReduceRule<Child: VTable, Parent: ArrayParentMatcher>: Send + Sync {
53+
pub trait ArrayParentReduceRule<Child: VTable, Parent: ArrayParentMatcher>:
54+
Debug + Send + Sync + 'static
55+
{
5156
/// Attempt to rewrite this child array given information about its parent.
5257
///
5358
/// Returns:

vortex-array/src/array/transform/tests.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::expr::transform::ExprOptimizer;
1818
use crate::validity::Validity;
1919

2020
/// Test rule that unwraps single-chunk ChunkedArrays
21+
#[derive(Debug, Default)]
2122
struct UnwrapSingleChunkRule;
2223

2324
impl ArrayReduceRule<ChunkedVTable> for UnwrapSingleChunkRule {
@@ -42,8 +43,9 @@ fn test_unwrap_single_chunk_rule() -> VortexResult<()> {
4243
let primitive = PrimitiveArray::from_iter([1i32, 2, 3]).into_array();
4344
let chunked = ChunkedArray::from_iter([primitive.clone()]);
4445

45-
let rule = UnwrapSingleChunkRule;
46-
let result = rule.reduce(&chunked, &ctx)?.vortex_expect("transformed");
46+
let result = UnwrapSingleChunkRule
47+
.reduce(&chunked, &ctx)?
48+
.vortex_expect("transformed");
4749

4850
assert!(Arc::ptr_eq(&primitive, &result));
4951
Ok(())
@@ -60,8 +62,7 @@ fn test_unwrap_single_chunk_rule_no_op() -> VortexResult<()> {
6062
PrimitiveArray::from_iter([3i32, 4]).into_array(),
6163
]);
6264

63-
let rule = UnwrapSingleChunkRule;
64-
let result = rule.reduce(&chunked, &ctx)?;
65+
let result = UnwrapSingleChunkRule.reduce(&chunked, &ctx)?;
6566

6667
assert!(result.is_none());
6768
Ok(())
@@ -72,7 +73,10 @@ fn test_reduce_rules_traverse_whole_tree() -> VortexResult<()> {
7273
let array_session = ArraySession::default();
7374
let expr_session = ExprSession::default();
7475

75-
array_session.register_reduce_rule::<ChunkedVTable, _>(&ChunkedEncoding, UnwrapSingleChunkRule);
76+
array_session.register_reduce_rule::<ChunkedVTable, UnwrapSingleChunkRule>(
77+
&ChunkedEncoding,
78+
UnwrapSingleChunkRule,
79+
);
7680

7781
let expr_optimizer = ExprOptimizer::new(&expr_session);
7882
let optimizer = array_session.optimizer(expr_optimizer);
@@ -121,6 +125,7 @@ fn test_reduce_rules_traverse_whole_tree() -> VortexResult<()> {
121125
}
122126

123127
// Odd rule for testing
128+
#[derive(Debug, Default)]
124129
struct ConstantInStructRule;
125130

126131
impl ArrayParentReduceRule<ConstantVTable, StructVTable> for ConstantInStructRule {
@@ -161,7 +166,7 @@ fn test_parent_rules_traverse_whole_tree() -> VortexResult<()> {
161166
let array_session = ArraySession::default();
162167
let expr_session = ExprSession::default();
163168

164-
array_session.register_parent_rule::<ConstantVTable, StructVTable, _>(
169+
array_session.register_parent_rule::<ConstantVTable, StructVTable, ConstantInStructRule>(
165170
&ConstantEncoding,
166171
&crate::arrays::StructEncoding,
167172
ConstantInStructRule,

vortex-array/src/arrays/bool/vtable/operator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ impl OperatorVTable<BoolVTable> for BoolVTable {
3737
///
3838
/// When a BoolArray is wrapped by a MaskedArray, this rule merges the mask's validity
3939
/// with the BoolArray's existing validity, eliminating the need for the MaskedArray wrapper.
40+
#[derive(Default, Debug)]
4041
pub struct BoolMaskedValidityRule;
4142

4243
impl ArrayParentReduceRule<BoolVTable, MaskedVTable> for BoolMaskedValidityRule {

vortex-array/src/arrays/decimal/vtable/operator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl OperatorVTable<DecimalVTable> for DecimalVTable {
4343
///
4444
/// When a DecimalArray is wrapped by a MaskedArray, this rule merges the mask's validity
4545
/// with the DecimalArray's existing validity, eliminating the need for the MaskedArray wrapper.
46+
#[derive(Default, Debug)]
4647
pub struct DecimalMaskedValidityRule;
4748

4849
impl ArrayParentReduceRule<DecimalVTable, MaskedVTable> for DecimalMaskedValidityRule {

vortex-array/src/arrays/expr/vtable/operator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::vtable::OperatorVTable;
1212
impl OperatorVTable<ExprVTable> for ExprVTable {}
1313

1414
/// Rule to optimize expressions within ExprArrays.
15+
#[derive(Default, Debug)]
1516
pub struct ExprOptimizationRule;
1617

1718
impl ArrayReduceRule<ExprVTable> for ExprOptimizationRule {

vortex-array/src/arrays/primitive/vtable/operator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl OperatorVTable<PrimitiveVTable> for PrimitiveVTable {
4242
///
4343
/// When a PrimitiveArray is wrapped by a MaskedArray, this rule merges the mask's validity
4444
/// with the PrimitiveArray's existing validity, eliminating the need for the MaskedArray wrapper.
45+
#[derive(Default, Debug)]
4546
pub struct PrimitiveMaskedValidityRule;
4647

4748
impl ArrayParentReduceRule<PrimitiveVTable, MaskedVTable> for PrimitiveMaskedValidityRule {

vortex-array/src/arrays/struct_/vtable/operator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ impl OperatorVTable<StructVTable> for StructVTable {
4747
///
4848
/// This optimization pushes expression evaluation down to individual struct fields, enabling
4949
/// better field-level optimizations and potentially avoiding materialization of unused fields.
50+
#[derive(Default, Debug)]
5051
pub struct StructExprPartitionRule;
5152

5253
impl ArrayParentReduceRule<StructVTable, ExprVTable> for StructExprPartitionRule {

0 commit comments

Comments
 (0)