@@ -188,7 +188,7 @@ impl<S: SimplifyInfo> ExprSimplifier<S> {
188
188
/// assert_eq!(expr, b_lt_2);
189
189
/// ```
190
190
pub fn simplify ( & self , expr : Expr ) -> Result < Expr > {
191
- Ok ( self . simplify_with_cycle_count ( expr) ?. 0 )
191
+ Ok ( self . simplify_with_cycle_count_transformed ( expr) ?. 0 . data )
192
192
}
193
193
194
194
/// Like [Self::simplify], simplifies this [`Expr`] as much as possible, evaluating
@@ -198,7 +198,34 @@ impl<S: SimplifyInfo> ExprSimplifier<S> {
198
198
///
199
199
/// See [Self::simplify] for details and usage examples.
200
200
///
201
+ #[ deprecated(
202
+ since = "48.0.0" ,
203
+ note = "Use `simplify_with_cycle_count_transformed` instead"
204
+ ) ]
205
+ #[ allow( unused_mut) ]
201
206
pub fn simplify_with_cycle_count ( & self , mut expr : Expr ) -> Result < ( Expr , u32 ) > {
207
+ let ( transformed, cycle_count) =
208
+ self . simplify_with_cycle_count_transformed ( expr) ?;
209
+ Ok ( ( transformed. data , cycle_count) )
210
+ }
211
+
212
+ /// Like [Self::simplify], simplifies this [`Expr`] as much as possible, evaluating
213
+ /// constants and applying algebraic simplifications. Additionally returns a `u32`
214
+ /// representing the number of simplification cycles performed, which can be useful for testing
215
+ /// optimizations.
216
+ ///
217
+ /// # Returns
218
+ ///
219
+ /// A tuple containing:
220
+ /// - The simplified expression wrapped in a `Transformed<Expr>` indicating if changes were made
221
+ /// - The number of simplification cycles that were performed
222
+ ///
223
+ /// See [Self::simplify] for details and usage examples.
224
+ ///
225
+ pub fn simplify_with_cycle_count_transformed (
226
+ & self ,
227
+ mut expr : Expr ,
228
+ ) -> Result < ( Transformed < Expr > , u32 ) > {
202
229
let mut simplifier = Simplifier :: new ( & self . info ) ;
203
230
let mut const_evaluator = ConstEvaluator :: try_new ( self . info . execution_props ( ) ) ?;
204
231
let mut shorten_in_list_simplifier = ShortenInListSimplifier :: new ( ) ;
@@ -212,6 +239,7 @@ impl<S: SimplifyInfo> ExprSimplifier<S> {
212
239
// simplifications can enable new constant evaluation
213
240
// see `Self::with_max_cycles`
214
241
let mut num_cycles = 0 ;
242
+ let mut has_transformed = false ;
215
243
loop {
216
244
let Transformed {
217
245
data, transformed, ..
@@ -221,13 +249,18 @@ impl<S: SimplifyInfo> ExprSimplifier<S> {
221
249
. transform_data ( |expr| expr. rewrite ( & mut guarantee_rewriter) ) ?;
222
250
expr = data;
223
251
num_cycles += 1 ;
252
+ // Track if any transformation occurred
253
+ has_transformed = has_transformed || transformed;
224
254
if !transformed || num_cycles >= self . max_simplifier_cycles {
225
255
break ;
226
256
}
227
257
}
228
258
// shorten inlist should be started after other inlist rules are applied
229
259
expr = expr. rewrite ( & mut shorten_in_list_simplifier) . data ( ) ?;
230
- Ok ( ( expr, num_cycles) )
260
+ Ok ( (
261
+ Transformed :: new_transformed ( expr, has_transformed) ,
262
+ num_cycles,
263
+ ) )
231
264
}
232
265
233
266
/// Apply type coercion to an [`Expr`] so that it can be
@@ -392,15 +425,15 @@ impl<S: SimplifyInfo> ExprSimplifier<S> {
392
425
/// let expr = col("a").is_not_null();
393
426
///
394
427
/// // When using default maximum cycles, 2 cycles will be performed.
395
- /// let (simplified_expr, count) = simplifier.simplify_with_cycle_count (expr.clone()).unwrap();
396
- /// assert_eq!(simplified_expr, lit(true));
428
+ /// let (simplified_expr, count) = simplifier.simplify_with_cycle_count_transformed (expr.clone()).unwrap();
429
+ /// assert_eq!(simplified_expr.data , lit(true));
397
430
/// // 2 cycles were executed, but only 1 was needed
398
431
/// assert_eq!(count, 2);
399
432
///
400
433
/// // Only 1 simplification pass is necessary here, so we can set the maximum cycles to 1.
401
- /// let (simplified_expr, count) = simplifier.with_max_cycles(1).simplify_with_cycle_count (expr.clone()).unwrap();
434
+ /// let (simplified_expr, count) = simplifier.with_max_cycles(1).simplify_with_cycle_count_transformed (expr.clone()).unwrap();
402
435
/// // Expression has been rewritten to: (c = a AND b = 1)
403
- /// assert_eq!(simplified_expr, lit(true));
436
+ /// assert_eq!(simplified_expr.data , lit(true));
404
437
/// // Only 1 cycle was executed
405
438
/// assert_eq!(count, 1);
406
439
///
@@ -3329,7 +3362,8 @@ mod tests {
3329
3362
let simplifier = ExprSimplifier :: new (
3330
3363
SimplifyContext :: new ( & execution_props) . with_schema ( schema) ,
3331
3364
) ;
3332
- simplifier. simplify_with_cycle_count ( expr)
3365
+ let ( expr, count) = simplifier. simplify_with_cycle_count_transformed ( expr) ?;
3366
+ Ok ( ( expr. data , count) )
3333
3367
}
3334
3368
3335
3369
fn simplify_with_cycle_count ( expr : Expr ) -> ( Expr , u32 ) {
0 commit comments