jcsherin commented on code in PR #12030:
URL: https://github.com/apache/datafusion/pull/12030#discussion_r1720164721


##########
datafusion/functions-window/src/row_number.rs:
##########
@@ -0,0 +1,183 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Defines physical expression for `row_number` that can evaluated at runtime 
during query execution
+
+use std::any::Any;
+use std::fmt::Debug;
+use std::ops::Range;
+
+use datafusion_common::arrow::array::ArrayRef;
+use datafusion_common::arrow::array::UInt64Array;
+use datafusion_common::arrow::compute::SortOptions;
+use datafusion_common::arrow::datatypes::DataType;
+use datafusion_common::{Result, ScalarValue};
+use datafusion_expr::expr::WindowFunction;
+use datafusion_expr::{Expr, PartitionEvaluator, Signature, Volatility, 
WindowUDFImpl};
+
+/// Create a [`WindowFunction`](Expr::WindowFunction) expression for
+/// `row_number` user-defined window function.
+pub fn row_number(args: Vec<Expr>) -> Expr {
+    Expr::WindowFunction(WindowFunction::new(row_number_udwf(), args))
+}
+
+/// Singleton instance of `row_number`, ensures the UDWF is only created once.
+#[allow(non_upper_case_globals)]
+static STATIC_RowNumber: 
std::sync::OnceLock<std::sync::Arc<datafusion_expr::WindowUDF>> =
+    std::sync::OnceLock::new();
+
+/// Returns a [`WindowUDF`](datafusion_expr::WindowUDF) for `row_number`
+/// user-defined window function.
+pub fn row_number_udwf() -> std::sync::Arc<datafusion_expr::WindowUDF> {
+    STATIC_RowNumber
+        .get_or_init(|| {
+            
std::sync::Arc::new(datafusion_expr::WindowUDF::from(RowNumber::default()))
+        })
+        .clone()
+}
+
+/// row_number expression
+#[derive(Debug)]
+struct RowNumber {
+    signature: Signature,
+}
+
+impl RowNumber {
+    /// Create a new `row_number` function
+    fn new() -> Self {
+        Self {
+            signature: Signature::any(0, Volatility::Immutable),
+        }
+    }
+}
+
+impl Default for RowNumber {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl WindowUDFImpl for RowNumber {
+    fn as_any(&self) -> &dyn Any {
+        self
+    }
+
+    fn name(&self) -> &str {
+        "row_number"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {

Review Comment:
   I think this makes sense.
   
   All the existing built-in window functions define nullable within the 
`BuiltInWindowFunctionExpr::field` implementation. 
   
   Therefore we can define field like this (without `FieldArgs` struct):
   ```rust
   fn field(&self, arg_types: &[DataType]) -> Result<Field>`
   ```
   
   This will also eliminate `data_type` field from `WindowUDFExpr` :
   
https://github.com/apache/datafusion/blob/2a16704db7af0045d465cda39b90d1a17e68dbe8/datafusion/physical-plan/src/windows/mod.rs#L342-L351
   
   Then the `field` method implementation:
   
https://github.com/apache/datafusion/blob/2a16704db7af0045d465cda39b90d1a17e68dbe8/datafusion/physical-plan/src/windows/mod.rs#L353-L361
   
   Will instead be this:
   ```rust
   impl BuiltInWindowFunctionExpr for WindowUDFExpr {
   
       fn field(&self) -> Result<Field> { self.fun.field() }
   
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to