@@ -217,6 +217,41 @@ fn get_casted_value(
217
217
}
218
218
}
219
219
220
+ /// Rewrites the NULL expression (1st argument) with an expression
221
+ /// which is the same data type as the default value (3rd argument).
222
+ /// Also rewrites the return type with the same data type as the
223
+ /// default value.
224
+ ///
225
+ /// If a default value is not provided, or it is NULL the original
226
+ /// expression (1st argument) and return type is returned without
227
+ /// any modifications.
228
+ fn rewrite_null_expr_and_data_type (
229
+ args : & [ Arc < dyn PhysicalExpr > ] ,
230
+ expr_type : & DataType ,
231
+ ) -> Result < ( Arc < dyn PhysicalExpr > , DataType ) > {
232
+ assert ! ( !args. is_empty( ) ) ;
233
+ let expr = Arc :: clone ( & args[ 0 ] ) ;
234
+
235
+ // The input expression and the return is type is unchanged
236
+ // when the input expression is not NULL.
237
+ if !expr_type. is_null ( ) {
238
+ return Ok ( ( expr, expr_type. clone ( ) ) ) ;
239
+ }
240
+
241
+ get_scalar_value_from_args ( args, 2 ) ?
242
+ . and_then ( |value| {
243
+ ScalarValue :: try_from ( value. data_type ( ) . clone ( ) )
244
+ . map ( |sv| {
245
+ Ok ( (
246
+ Arc :: new ( Literal :: new ( sv) ) as Arc < dyn PhysicalExpr > ,
247
+ value. data_type ( ) . clone ( ) ,
248
+ ) )
249
+ } )
250
+ . ok ( )
251
+ } )
252
+ . unwrap_or ( Ok ( ( expr, expr_type. clone ( ) ) ) )
253
+ }
254
+
220
255
fn create_built_in_window_expr (
221
256
fun : & BuiltInWindowFunction ,
222
257
args : & [ Arc < dyn PhysicalExpr > ] ,
@@ -252,31 +287,35 @@ fn create_built_in_window_expr(
252
287
}
253
288
}
254
289
BuiltInWindowFunction :: Lag => {
255
- let arg = Arc :: clone ( & args[ 0 ] ) ;
290
+ // rewrite NULL expression and the return datatype
291
+ let ( arg, out_data_type) =
292
+ rewrite_null_expr_and_data_type ( args, out_data_type) ?;
256
293
let shift_offset = get_scalar_value_from_args ( args, 1 ) ?
257
294
. map ( get_signed_integer)
258
295
. map_or ( Ok ( None ) , |v| v. map ( Some ) ) ?;
259
296
let default_value =
260
- get_casted_value ( get_scalar_value_from_args ( args, 2 ) ?, out_data_type) ?;
297
+ get_casted_value ( get_scalar_value_from_args ( args, 2 ) ?, & out_data_type) ?;
261
298
Arc :: new ( lag (
262
299
name,
263
- out_data_type . clone ( ) ,
300
+ default_value . data_type ( ) . clone ( ) ,
264
301
arg,
265
302
shift_offset,
266
303
default_value,
267
304
ignore_nulls,
268
305
) )
269
306
}
270
307
BuiltInWindowFunction :: Lead => {
271
- let arg = Arc :: clone ( & args[ 0 ] ) ;
308
+ // rewrite NULL expression and the return datatype
309
+ let ( arg, out_data_type) =
310
+ rewrite_null_expr_and_data_type ( args, out_data_type) ?;
272
311
let shift_offset = get_scalar_value_from_args ( args, 1 ) ?
273
312
. map ( get_signed_integer)
274
313
. map_or ( Ok ( None ) , |v| v. map ( Some ) ) ?;
275
314
let default_value =
276
- get_casted_value ( get_scalar_value_from_args ( args, 2 ) ?, out_data_type) ?;
315
+ get_casted_value ( get_scalar_value_from_args ( args, 2 ) ?, & out_data_type) ?;
277
316
Arc :: new ( lead (
278
317
name,
279
- out_data_type . clone ( ) ,
318
+ default_value . data_type ( ) . clone ( ) ,
280
319
arg,
281
320
shift_offset,
282
321
default_value,
0 commit comments