-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Add new input_file_name UDF for file-backed scans
#22978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // 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. | ||
|
|
||
| //! [`InputFileNameFunc`]: Implementation of the `input_file_name` function. | ||
|
|
||
| use arrow::datatypes::DataType; | ||
| use datafusion_common::{exec_err, utils::take_function_args}; | ||
| use datafusion_doc::Documentation; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, ExpressionPlacement, ScalarFunctionArgs, ScalarUDFImpl, Signature, | ||
| Volatility, | ||
| }; | ||
| use datafusion_macros::user_doc; | ||
|
|
||
| #[user_doc( | ||
| doc_section(label = "Other Functions"), | ||
| description = r#"Returns the path of the input file that produced the current row. | ||
|
|
||
| Note: file paths/URIs may be sensitive metadata depending on your environment. | ||
|
|
||
| This function is intended to be rewritten at file-scan time (when the file is | ||
| known). If the input file is not known (for example, if this function is | ||
| evaluated outside a file scan, or was not pushed down into one), direct evaluation returns an error. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should be returning an error or try be more permissive and return null/empty string for this case 🤔 For reference, Spark seems to return an empty string: >>> spark.sql("select input_file_name()").show()
+-----------------+
|input_file_name()|
+-----------------+
| |
+-----------------+
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @AdamGS 's implementation of I don't have a strong preference either way, as long as we have test coverage for the behavior |
||
| "#, | ||
| syntax_example = "input_file_name()", | ||
| sql_example = r#"```sql | ||
| SELECT input_file_name() FROM t; | ||
| ```"# | ||
| )] | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct InputFileNameFunc { | ||
| signature: Signature, | ||
| } | ||
|
|
||
| impl Default for InputFileNameFunc { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl InputFileNameFunc { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| signature: Signature::nullary(Volatility::Volatile), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for InputFileNameFunc { | ||
| fn name(&self) -> &str { | ||
| "input_file_name" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| &self.signature | ||
| } | ||
|
|
||
| fn return_type(&self, arg_types: &[DataType]) -> datafusion_common::Result<DataType> { | ||
| let [] = take_function_args(self.name(), arg_types)?; | ||
| Ok(DataType::Utf8) | ||
| } | ||
|
|
||
| fn invoke_with_args( | ||
| &self, | ||
| args: ScalarFunctionArgs, | ||
| ) -> datafusion_common::Result<ColumnarValue> { | ||
| let [] = take_function_args(self.name(), args.args)?; | ||
|
|
||
| exec_err!( | ||
| "input_file_name() is source dependent and cannot be evaluated directly" | ||
| ) | ||
| } | ||
|
|
||
| fn placement(&self, _args: &[ExpressionPlacement]) -> ExpressionPlacement { | ||
| ExpressionPlacement::MoveTowardsLeafNodes | ||
| } | ||
|
|
||
| fn documentation(&self) -> Option<&Documentation> { | ||
| self.doc() | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It occurs to me that we might want to add some documentation (as a follow on PR) to the TableProvider about functions that might need special handling (e.g. input_file_name, input_row_number, get_field)
Otherwise people implementing table providers might not know it would be helpful