Skip to content

Commit 9e7e5e2

Browse files
committed
added
1 parent 9bbeef4 commit 9e7e5e2

File tree

4 files changed

+12
-48
lines changed

4 files changed

+12
-48
lines changed

datafusion/functions-window/src/nth_value.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ pub fn last_value(arg: datafusion_expr::Expr) -> datafusion_expr::Expr {
7171

7272
/// Create an expression to represent the `nth_value` window function
7373
///
74-
pub fn nth_value(arg: datafusion_expr::Expr, n: Option<i64>) -> datafusion_expr::Expr {
75-
let n_lit = n.map(|v| v.lit()).unwrap_or(ScalarValue::Null.lit());
76-
nth_value_udwf().call(vec![arg, n_lit])
74+
pub fn nth_value(arg: datafusion_expr::Expr, n: i64) -> datafusion_expr::Expr {
75+
nth_value_udwf().call(vec![arg, n.lit()])
7776
}
7877

7978
/// Tag to differentiate special use cases of the NTH_VALUE built-in window function.

datafusion/proto/tests/cases/roundtrip_logical_plan.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use datafusion::functions_aggregate::expr_fn::{
4848
};
4949
use datafusion::functions_aggregate::min_max::max_udaf;
5050
use datafusion::functions_nested::map::map;
51+
use datafusion::functions_window;
5152
use datafusion::functions_window::expr_fn::{
5253
cume_dist, dense_rank, lag, lead, ntile, percent_rank, rank, row_number,
5354
};
@@ -911,6 +912,9 @@ async fn roundtrip_expr_api() -> Result<()> {
911912
count_distinct(lit(1)),
912913
first_value(lit(1), None),
913914
first_value(lit(1), Some(vec![lit(2).sort(true, true)])),
915+
functions_window::nth_value::first_value(lit(1)),
916+
functions_window::nth_value::last_value(lit(1)),
917+
functions_window::nth_value::nth_value(lit(1), 1),
914918
avg(lit(1.5)),
915919
covar_samp(lit(1.5), lit(2.2)),
916920
covar_pop(lit(1.5), lit(2.2)),

datafusion/proto/tests/cases/roundtrip_physical_plan.rs

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ use datafusion::datasource::physical_plan::{
4747
};
4848
use datafusion::execution::FunctionRegistry;
4949
use datafusion::functions_aggregate::sum::sum_udaf;
50-
use datafusion::functions_window::nth_value::first_value_udwf;
5150
use datafusion::logical_expr::{create_udf, JoinType, Operator, Volatility};
5251
use datafusion::physical_expr::expressions::Literal;
53-
use datafusion::physical_expr::window::{BuiltInWindowExpr, SlidingAggregateWindowExpr};
52+
use datafusion::physical_expr::window::SlidingAggregateWindowExpr;
5453
use datafusion::physical_expr::{
5554
LexOrdering, LexRequirement, PhysicalSortRequirement, ScalarFunctionExpr,
5655
};
@@ -74,9 +73,7 @@ use datafusion::physical_plan::repartition::RepartitionExec;
7473
use datafusion::physical_plan::sorts::sort::SortExec;
7574
use datafusion::physical_plan::union::{InterleaveExec, UnionExec};
7675
use datafusion::physical_plan::unnest::{ListUnnest, UnnestExec};
77-
use datafusion::physical_plan::windows::{
78-
create_udwf_window_expr, PlainAggregateWindowExpr, WindowAggExec,
79-
};
76+
use datafusion::physical_plan::windows::{PlainAggregateWindowExpr, WindowAggExec};
8077
use datafusion::physical_plan::{ExecutionPlan, Partitioning, PhysicalExpr, Statistics};
8178
use datafusion::prelude::SessionContext;
8279
use datafusion::scalar::ScalarValue;
@@ -88,11 +85,9 @@ use datafusion_common::stats::Precision;
8885
use datafusion_common::{
8986
internal_err, not_impl_err, DataFusionError, Result, UnnestOptions,
9087
};
91-
use datafusion_expr::WindowFunctionDefinition::WindowUDF;
9288
use datafusion_expr::{
9389
Accumulator, AccumulatorFactoryFunction, AggregateUDF, ColumnarValue, ScalarUDF,
9490
Signature, SimpleAggregateUDF, WindowFrame, WindowFrameBound,
95-
WindowFunctionDefinition,
9691
};
9792
use datafusion_functions_aggregate::average::avg_udaf;
9893
use datafusion_functions_aggregate::nth_value::nth_value_udaf;
@@ -101,7 +96,6 @@ use datafusion_proto::physical_plan::{
10196
AsExecutionPlan, DefaultPhysicalExtensionCodec, PhysicalExtensionCodec,
10297
};
10398
use datafusion_proto::protobuf;
104-
use datafusion_proto::protobuf::logical_expr_node::ExprType::WindowExpr;
10599

106100
/// Perform a serde roundtrip and assert that the string representation of the before and after plans
107101
/// are identical. Note that this often isn't sufficient to guarantee that no information is
@@ -275,35 +269,6 @@ fn roundtrip_window() -> Result<()> {
275269
let field_b = Field::new("b", DataType::Int64, false);
276270
let schema = Arc::new(Schema::new(vec![field_a, field_b]));
277271

278-
let window_frame = WindowFrame::new_bounds(
279-
datafusion_expr::WindowFrameUnits::Range,
280-
WindowFrameBound::Preceding(ScalarValue::Int64(None)),
281-
WindowFrameBound::CurrentRow,
282-
);
283-
284-
let first_value_window = create_udwf_window_expr(
285-
&first_value_udwf(),
286-
&[col("a", &schema)?],
287-
schema.as_ref(),
288-
"FIRST_VALUE(a) PARTITION BY [b] ORDER BY [a ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW".to_string(),
289-
false,
290-
)?;
291-
// "FIRST_VALUE(a) PARTITION BY [b] ORDER BY [a ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW",
292-
let builtin_window_expr = Arc::new(BuiltInWindowExpr::new(
293-
first_value_window,
294-
&[col("b", &schema)?],
295-
&LexOrdering {
296-
inner: vec![PhysicalSortExpr {
297-
expr: col("a", &schema)?,
298-
options: SortOptions {
299-
descending: false,
300-
nulls_first: false,
301-
},
302-
}],
303-
},
304-
Arc::new(window_frame),
305-
));
306-
307272
let plain_aggr_window_expr = Arc::new(PlainAggregateWindowExpr::new(
308273
AggregateExprBuilder::new(
309274
avg_udaf(),
@@ -341,11 +306,7 @@ fn roundtrip_window() -> Result<()> {
341306
let input = Arc::new(EmptyExec::new(schema.clone()));
342307

343308
roundtrip_test(Arc::new(WindowAggExec::try_new(
344-
vec![
345-
plain_aggr_window_expr,
346-
sliding_aggr_window_expr,
347-
builtin_window_expr,
348-
],
309+
vec![plain_aggr_window_expr, sliding_aggr_window_expr],
349310
input,
350311
vec![col("b", &schema)?],
351312
)?))

datafusion/sqllogictest/test_files/errors.slt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ regr_slope(c11, '2') over () as min1
119119
from aggregate_test_100
120120
order by c9
121121

122-
# WindowFunction with BuiltInWindowFunction wrong signature
122+
# WindowFunction wrong signature
123123
statement error
124124
select
125125
c9,
@@ -132,17 +132,17 @@ DataFusion error: Error during planning: Error during planning: Coercion from [I
132132
nth_value()
133133
nth_value(Any)
134134
nth_value(Any, Any)
135-
from aggregate_test_100
136-
order by c9
137135

138136

137+
# nth_value with wrong name
139138
statement error DataFusion error: Error during planning: Invalid function 'nth_vlue'.\nDid you mean 'nth_value'?
140139
SELECT
141140
NTH_VLUE(c4, 2) OVER()
142141
FROM aggregate_test_100
143142
ORDER BY c9
144143
LIMIT 5;
145144

145+
# first_value with wrong name
146146
statement error DataFusion error: Error during planning: Invalid function 'frst_value'.\nDid you mean 'first_value'?
147147
SELECT
148148
FRST_VALUE(c4, 2) OVER()

0 commit comments

Comments
 (0)