Skip to main content

datafusion_functions/core/
file_row_index.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Implementation of the `file_row_index` scalar function.
19
20use arrow::datatypes::DataType;
21use datafusion_common::utils::take_function_args;
22use datafusion_common::{Result, exec_err};
23use datafusion_doc::Documentation;
24use datafusion_expr::{
25    ColumnarValue, ExpressionPlacement, ScalarFunctionArgs, ScalarUDFImpl, Signature,
26    Volatility,
27};
28use datafusion_macros::user_doc;
29
30/// Scalar UDF implementation for `file_row_index()`.
31///
32/// File sources that can expose per-file row indexes rewrite this placeholder
33/// function into a source-provided physical expression. Direct evaluation
34/// returns an error because there is no file context outside a scan.
35#[user_doc(
36    doc_section(label = "Other Functions"),
37    description = r#"Returns the zero-based row offset within the source file
38that produced the current row.
39
40The value is scoped to one file, so rows from different files in the same scan
41can have the same row index. This function is intended to be rewritten at
42file-scan time. If the input file is not known (for example, if this function
43is evaluated outside a file scan, or was not pushed down into one), direct
44evaluation returns an error.
45"#,
46    syntax_example = "file_row_index()",
47    sql_example = r#"```sql
48SELECT file_row_index() FROM t;
49```"#
50)]
51#[derive(Debug, PartialEq, Eq, Hash)]
52pub struct FileRowIndexFunc {
53    signature: Signature,
54}
55
56impl Default for FileRowIndexFunc {
57    fn default() -> Self {
58        Self::new()
59    }
60}
61
62impl FileRowIndexFunc {
63    pub fn new() -> Self {
64        Self {
65            signature: Signature::nullary(Volatility::Volatile),
66        }
67    }
68}
69
70impl ScalarUDFImpl for FileRowIndexFunc {
71    fn name(&self) -> &str {
72        "file_row_index"
73    }
74
75    fn signature(&self) -> &Signature {
76        &self.signature
77    }
78
79    fn return_type(&self, args: &[DataType]) -> Result<DataType> {
80        let [] = take_function_args(self.name(), args)?;
81        Ok(DataType::Int64)
82    }
83
84    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
85        let [] = take_function_args(self.name(), args.args)?;
86        exec_err!("file_row_index() is source dependent and cannot be evaluated directly")
87    }
88
89    fn placement(&self, _args: &[ExpressionPlacement]) -> ExpressionPlacement {
90        ExpressionPlacement::MoveTowardsLeafNodes
91    }
92
93    fn documentation(&self) -> Option<&Documentation> {
94        self.doc()
95    }
96}