@@ -204,12 +204,19 @@ pub fn add_offset_to_physical_sort_exprs(
204204mod tests {
205205 use super :: * ;
206206
207- use crate :: expressions:: { Column , Literal } ;
207+ use crate :: expressions:: { BinaryExpr , Column , Literal } ;
208208 use crate :: physical_expr:: {
209209 physical_exprs_bag_equal, physical_exprs_contains, physical_exprs_equal,
210210 } ;
211+ use datafusion_physical_expr_common:: physical_expr:: is_volatile;
211212
212- use datafusion_common:: ScalarValue ;
213+ use arrow:: datatypes:: { DataType , Schema } ;
214+ use arrow:: record_batch:: RecordBatch ;
215+ use datafusion_common:: { Result , ScalarValue } ;
216+ use datafusion_expr:: ColumnarValue ;
217+ use datafusion_expr:: Operator ;
218+ use std:: any:: Any ;
219+ use std:: fmt;
213220
214221 #[ test]
215222 fn test_physical_exprs_contains ( ) {
@@ -322,4 +329,120 @@ mod tests {
322329 assert ! ( physical_exprs_bag_equal( list3. as_slice( ) , list3. as_slice( ) ) ) ;
323330 assert ! ( physical_exprs_bag_equal( list4. as_slice( ) , list4. as_slice( ) ) ) ;
324331 }
332+
333+ #[ test]
334+ fn test_is_volatile_default_behavior ( ) {
335+ // Test that default PhysicalExpr implementations are not volatile
336+ let literal =
337+ Arc :: new ( Literal :: new ( ScalarValue :: Int32 ( Some ( 42 ) ) ) ) as Arc < dyn PhysicalExpr > ;
338+ let column = Arc :: new ( Column :: new ( "test" , 0 ) ) as Arc < dyn PhysicalExpr > ;
339+
340+ // Test is_volatile_node() - should return false by default
341+ assert ! ( !literal. is_volatile_node( ) ) ;
342+ assert ! ( !column. is_volatile_node( ) ) ;
343+
344+ // Test is_volatile() - should return false for non-volatile expressions
345+ assert ! ( !is_volatile( & literal) ) ;
346+ assert ! ( !is_volatile( & column) ) ;
347+ }
348+
349+ /// Mock volatile PhysicalExpr for testing purposes
350+ #[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
351+ struct MockVolatileExpr {
352+ volatile : bool ,
353+ }
354+
355+ impl MockVolatileExpr {
356+ fn new ( volatile : bool ) -> Self {
357+ Self { volatile }
358+ }
359+ }
360+
361+ impl fmt:: Display for MockVolatileExpr {
362+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
363+ write ! ( f, "MockVolatile({})" , self . volatile)
364+ }
365+ }
366+
367+ impl PhysicalExpr for MockVolatileExpr {
368+ fn as_any ( & self ) -> & dyn Any {
369+ self
370+ }
371+
372+ fn data_type ( & self , _input_schema : & Schema ) -> Result < DataType > {
373+ Ok ( DataType :: Boolean )
374+ }
375+
376+ fn nullable ( & self , _input_schema : & Schema ) -> Result < bool > {
377+ Ok ( false )
378+ }
379+
380+ fn evaluate ( & self , _batch : & RecordBatch ) -> Result < ColumnarValue > {
381+ Ok ( ColumnarValue :: Scalar ( ScalarValue :: Boolean ( Some (
382+ self . volatile ,
383+ ) ) ) )
384+ }
385+
386+ fn children ( & self ) -> Vec < & Arc < dyn PhysicalExpr > > {
387+ vec ! [ ]
388+ }
389+
390+ fn with_new_children (
391+ self : Arc < Self > ,
392+ _children : Vec < Arc < dyn PhysicalExpr > > ,
393+ ) -> Result < Arc < dyn PhysicalExpr > > {
394+ Ok ( self )
395+ }
396+
397+ fn is_volatile_node ( & self ) -> bool {
398+ self . volatile
399+ }
400+
401+ fn fmt_sql ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
402+ write ! ( f, "mock_volatile({})" , self . volatile)
403+ }
404+ }
405+
406+ #[ test]
407+ fn test_nested_expression_volatility ( ) {
408+ // Test that is_volatile() recursively detects volatility in expression trees
409+
410+ // Create a volatile mock expression
411+ let volatile_expr =
412+ Arc :: new ( MockVolatileExpr :: new ( true ) ) as Arc < dyn PhysicalExpr > ;
413+ assert ! ( volatile_expr. is_volatile_node( ) ) ;
414+ assert ! ( is_volatile( & volatile_expr) ) ;
415+
416+ // Create a non-volatile mock expression
417+ let stable_expr = Arc :: new ( MockVolatileExpr :: new ( false ) ) as Arc < dyn PhysicalExpr > ;
418+ assert ! ( !stable_expr. is_volatile_node( ) ) ;
419+ assert ! ( !is_volatile( & stable_expr) ) ;
420+
421+ // Create a literal (non-volatile)
422+ let literal =
423+ Arc :: new ( Literal :: new ( ScalarValue :: Int32 ( Some ( 42 ) ) ) ) as Arc < dyn PhysicalExpr > ;
424+ assert ! ( !literal. is_volatile_node( ) ) ;
425+ assert ! ( !is_volatile( & literal) ) ;
426+
427+ // Test composite expression: volatile_expr AND literal
428+ // The BinaryExpr itself is not volatile, but contains a volatile child
429+ let composite_expr = Arc :: new ( BinaryExpr :: new (
430+ Arc :: clone ( & volatile_expr) ,
431+ Operator :: And ,
432+ Arc :: clone ( & literal) ,
433+ ) ) as Arc < dyn PhysicalExpr > ;
434+
435+ assert ! ( !composite_expr. is_volatile_node( ) ) ; // BinaryExpr itself is not volatile
436+ assert ! ( is_volatile( & composite_expr) ) ; // But it contains a volatile child
437+
438+ // Test composite expression with all non-volatile children
439+ let stable_composite = Arc :: new ( BinaryExpr :: new (
440+ Arc :: clone ( & stable_expr) ,
441+ Operator :: And ,
442+ Arc :: clone ( & literal) ,
443+ ) ) as Arc < dyn PhysicalExpr > ;
444+
445+ assert ! ( !stable_composite. is_volatile_node( ) ) ;
446+ assert ! ( !is_volatile( & stable_composite) ) ; // No volatile children
447+ }
325448}
0 commit comments