Skip to content

Apache DataFusion SQL Query Engine

fix: Panic non-integer for the second argument of `nth_value` function (#12076)

Alex Huang9d076bde4fb0
diff --git a/datafusion/physical-plan/src/windows/mod.rs b/datafusion/physical-plan/src/windows/mod.rs
index f938f4410a..63f4ffcfaa 100644
--- a/datafusion/physical-plan/src/windows/mod.rs
+++ b/datafusion/physical-plan/src/windows/mod.rs
@@ -30,7 +30,9 @@ use crate::{
use arrow::datatypes::Schema;
use arrow_schema::{DataType, Field, SchemaRef};
-use datafusion_common::{exec_err, DataFusionError, Result, ScalarValue};
+use datafusion_common::{
+ exec_datafusion_err, exec_err, DataFusionError, Result, ScalarValue,
+};
use datafusion_expr::{
BuiltInWindowFunction, PartitionEvaluator, WindowFrame, WindowFunctionDefinition,
WindowUDF,
@@ -284,7 +286,9 @@ fn create_built_in_window_expr(
args[1]
.as_any()
.downcast_ref::<Literal>()
- .unwrap()
+ .ok_or_else(|| {
+ exec_datafusion_err!("Expected a signed integer literal for the second argument of nth_value, got {}", args[1])
+ })?
.value()
.clone(),
)?;
diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.slt
index 78055f8c1c..5bf5cf8328 100644
--- a/datafusion/sqllogictest/test_files/window.slt
+++ b/datafusion/sqllogictest/test_files/window.slt
@@ -4861,3 +4861,16 @@ select a, row_number(a) over (order by b) as rn from t;
statement ok
drop table t;
+
+statement ok
+DROP TABLE t1;
+
+# https://github.com/apache/datafusion/issues/12073
+statement ok
+CREATE TABLE t1(v1 BIGINT);
+
+query error DataFusion error: Execution error: Expected a signed integer literal for the second argument of nth_value, got v1@0
+SELECT NTH_VALUE('+Inf'::Double, v1) OVER (PARTITION BY v1) FROM t1;
+
+statement ok
+DROP TABLE t1;
\ No newline at end of file