Skip to content

Commit e02d3e6

Browse files
authored
Merge branch 'main' into windows_avg
2 parents 8f19afd + 74c4c64 commit e02d3e6

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

datafusion/functions-window/src/nth_value.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,22 @@ impl WindowUDFImpl for NthValue {
308308
}
309309

310310
fn field(&self, field_args: WindowUDFFieldArgs) -> Result<FieldRef> {
311-
let return_type = field_args
312-
.input_fields()
313-
.first()
314-
.map(|f| f.data_type())
315-
.cloned()
316-
.unwrap_or(DataType::Null);
317-
318-
Ok(Field::new(field_args.name(), return_type, true).into())
311+
let input_field =
312+
field_args
313+
.input_fields()
314+
.first()
315+
.cloned()
316+
.unwrap_or_else(|| {
317+
Arc::new(Field::new(field_args.name(), DataType::Null, true))
318+
});
319+
320+
// Clone the input field to preserve metadata, update name and nullability
321+
Ok(input_field
322+
.as_ref()
323+
.clone()
324+
.with_name(field_args.name())
325+
.with_nullable(true)
326+
.into())
319327
}
320328

321329
fn reverse_expr(&self) -> ReversedUDWF {

datafusion/sqllogictest/test_files/metadata.slt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,5 +472,51 @@ select arrow_metadata(with_metadata(id, 'unit', ''), 'unit') from table_with_met
472472
----
473473
(empty)
474474

475+
# Regression test: window functions should preserve field metadata
476+
# Test FIRST_VALUE window function preserves metadata
477+
query IT
478+
select
479+
first_value(id) over (order by id asc nulls last) as fv,
480+
arrow_metadata(first_value(id) over (order by id asc nulls last), 'metadata_key') as meta
481+
from table_with_metadata limit 1;
482+
----
483+
1 the id field
484+
485+
# Test LAST_VALUE window function preserves metadata
486+
query IT
487+
select
488+
last_value(id) over (order by id asc nulls last rows between unbounded preceding and unbounded following) as lv,
489+
arrow_metadata(last_value(id) over (order by id asc nulls last rows between unbounded preceding and unbounded following), 'metadata_key') as meta
490+
from table_with_metadata limit 1;
491+
----
492+
NULL the id field
493+
494+
# Test NTH_VALUE window function preserves metadata
495+
query IT
496+
select
497+
nth_value(id, 2) over (order by id asc nulls last) as nv,
498+
arrow_metadata(nth_value(id, 2) over (order by id asc nulls last), 'metadata_key') as meta
499+
from table_with_metadata limit 1;
500+
----
501+
NULL the id field
502+
503+
# Test LEAD window function preserves metadata
504+
query IT
505+
select
506+
lead(id) over (order by id asc nulls last) as ld,
507+
arrow_metadata(lead(id) over (order by id asc nulls last), 'metadata_key') as meta
508+
from table_with_metadata limit 1;
509+
----
510+
3 the id field
511+
512+
# Test LAG window function preserves metadata
513+
query IT
514+
select
515+
lag(id) over (order by id asc nulls last) as lg,
516+
arrow_metadata(lag(id) over (order by id asc nulls last), 'metadata_key') as meta
517+
from table_with_metadata limit 1;
518+
----
519+
NULL the id field
520+
475521
statement ok
476522
drop table table_with_metadata;

0 commit comments

Comments
 (0)