18
18
//! Rewrites for using Array Functions
19
19
20
20
use crate :: array_has:: array_has_all;
21
- use crate :: concat:: { array_append, array_concat, array_prepend} ;
22
21
use datafusion_common:: config:: ConfigOptions ;
23
22
use datafusion_common:: tree_node:: Transformed ;
24
- use datafusion_common:: utils :: list_ndims ;
23
+ use datafusion_common:: DFSchema ;
25
24
use datafusion_common:: Result ;
26
- use datafusion_common:: { Column , DFSchema } ;
27
25
use datafusion_expr:: expr:: ScalarFunction ;
28
26
use datafusion_expr:: expr_rewriter:: FunctionRewrite ;
29
27
use datafusion_expr:: { BinaryExpr , Expr , Operator } ;
@@ -39,7 +37,7 @@ impl FunctionRewrite for ArrayFunctionRewriter {
39
37
fn rewrite (
40
38
& self ,
41
39
expr : Expr ,
42
- schema : & DFSchema ,
40
+ _schema : & DFSchema ,
43
41
_config : & ConfigOptions ,
44
42
) -> Result < Transformed < Expr > > {
45
43
let transformed = match expr {
@@ -61,91 +59,6 @@ impl FunctionRewrite for ArrayFunctionRewriter {
61
59
Transformed :: yes ( array_has_all ( * right, * left) )
62
60
}
63
61
64
- // Column cases:
65
- // 1) array_prepend/append/concat || column
66
- Expr :: BinaryExpr ( BinaryExpr { left, op, right } )
67
- if op == Operator :: StringConcat
68
- && is_one_of_func (
69
- & left,
70
- & [ "array_append" , "array_prepend" , "array_concat" ] ,
71
- )
72
- && as_col ( & right) . is_some ( ) =>
73
- {
74
- let c = as_col ( & right) . unwrap ( ) ;
75
- let d = schema. field_from_column ( c) ?. data_type ( ) ;
76
- let ndim = list_ndims ( d) ;
77
- match ndim {
78
- 0 => Transformed :: yes ( array_append ( * left, * right) ) ,
79
- _ => Transformed :: yes ( array_concat ( vec ! [ * left, * right] ) ) ,
80
- }
81
- }
82
- // 2) select column1 || column2
83
- Expr :: BinaryExpr ( BinaryExpr { left, op, right } )
84
- if op == Operator :: StringConcat
85
- && as_col ( & left) . is_some ( )
86
- && as_col ( & right) . is_some ( ) =>
87
- {
88
- let c1 = as_col ( & left) . unwrap ( ) ;
89
- let c2 = as_col ( & right) . unwrap ( ) ;
90
- let d1 = schema. field_from_column ( c1) ?. data_type ( ) ;
91
- let d2 = schema. field_from_column ( c2) ?. data_type ( ) ;
92
- let ndim1 = list_ndims ( d1) ;
93
- let ndim2 = list_ndims ( d2) ;
94
- match ( ndim1, ndim2) {
95
- ( 0 , _) => Transformed :: yes ( array_prepend ( * left, * right) ) ,
96
- ( _, 0 ) => Transformed :: yes ( array_append ( * left, * right) ) ,
97
- _ => Transformed :: yes ( array_concat ( vec ! [ * left, * right] ) ) ,
98
- }
99
- }
100
-
101
- // Chain concat operator (a || b) || array,
102
- // (array_concat, array_append, array_prepend) || array -> array concat
103
- Expr :: BinaryExpr ( BinaryExpr { left, op, right } )
104
- if op == Operator :: StringConcat
105
- && is_one_of_func (
106
- & left,
107
- & [ "array_append" , "array_prepend" , "array_concat" ] ,
108
- )
109
- && is_func ( & right, "make_array" ) =>
110
- {
111
- Transformed :: yes ( array_concat ( vec ! [ * left, * right] ) )
112
- }
113
-
114
- // Chain concat operator (a || b) || scalar,
115
- // (array_concat, array_append, array_prepend) || scalar -> array append
116
- Expr :: BinaryExpr ( BinaryExpr { left, op, right } )
117
- if op == Operator :: StringConcat
118
- && is_one_of_func (
119
- & left,
120
- & [ "array_append" , "array_prepend" , "array_concat" ] ,
121
- ) =>
122
- {
123
- Transformed :: yes ( array_append ( * left, * right) )
124
- }
125
-
126
- // array || array -> array concat
127
- Expr :: BinaryExpr ( BinaryExpr { left, op, right } )
128
- if op == Operator :: StringConcat
129
- && is_func ( & left, "make_array" )
130
- && is_func ( & right, "make_array" ) =>
131
- {
132
- Transformed :: yes ( array_concat ( vec ! [ * left, * right] ) )
133
- }
134
-
135
- // array || scalar -> array append
136
- Expr :: BinaryExpr ( BinaryExpr { left, op, right } )
137
- if op == Operator :: StringConcat && is_func ( & left, "make_array" ) =>
138
- {
139
- Transformed :: yes ( array_append ( * left, * right) )
140
- }
141
-
142
- // scalar || array -> array prepend
143
- Expr :: BinaryExpr ( BinaryExpr { left, op, right } )
144
- if op == Operator :: StringConcat && is_func ( & right, "make_array" ) =>
145
- {
146
- Transformed :: yes ( array_prepend ( * left, * right) )
147
- }
148
-
149
62
_ => Transformed :: no ( expr) ,
150
63
} ;
151
64
Ok ( transformed)
@@ -161,21 +74,3 @@ fn is_func(expr: &Expr, func_name: &str) -> bool {
161
74
162
75
func. name ( ) == func_name
163
76
}
164
-
165
- /// Returns true if expr is a function call with one of the specified names
166
- fn is_one_of_func ( expr : & Expr , func_names : & [ & str ] ) -> bool {
167
- let Expr :: ScalarFunction ( ScalarFunction { func, args : _ } ) = expr else {
168
- return false ;
169
- } ;
170
-
171
- func_names. contains ( & func. name ( ) )
172
- }
173
-
174
- /// returns Some(col) if this is Expr::Column
175
- fn as_col ( expr : & Expr ) -> Option < & Column > {
176
- if let Expr :: Column ( c) = expr {
177
- Some ( c)
178
- } else {
179
- None
180
- }
181
- }
0 commit comments