Repository navigation
feat(spark): implement Spark-compatible log1p #344
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
Open
kazantsev-maksim
wants to merge
6
commits into
apache:main
Choose a base branch
from
kazantsev-maksim:log1p
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
87eeadc
impl log1p
f2bb093
Merge remote-tracking branch 'origin/main' into log1p
25cb61d
Merge remote-tracking branch 'origin/main' into log1p
348f9d6
Merge remote-tracking branch 'origin/main' into log1p
9355c28
address comments
1f66c82
address comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership | ||
| // SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements | ||
|
|
||
| use crate::function::implicit_cast::{SparkInputType, SparkInputTypes}; | ||
| use arrow::array::{ArrayRef, AsArray, Float64Array}; | ||
| use arrow::datatypes::{DataType, Float64Type}; | ||
| use datafusion_common::config::ConfigOptions; | ||
| use datafusion_common::utils::take_function_args; | ||
| use datafusion_common::{Result, ScalarValue, internal_err}; | ||
| use datafusion_expr::{ | ||
| ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDF, ScalarUDFImpl, | ||
| Signature, Volatility, | ||
| }; | ||
| use datafusion_macros::user_doc; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Spark-compatible `log1p` expression. | ||
| /// | ||
| /// <https://spark.apache.org/docs/latest/api/sql/index.html#log1p> | ||
| #[user_doc( | ||
| doc_section(label = "Math Functions"), | ||
| description = "Returns `ln(1 + expr)` as a DOUBLE, computed accurately when `expr` is \ | ||
| close to 0. Values less than or equal to -1 return NULL. NULL input returns NULL.", | ||
| syntax_example = "log1p(expr)", | ||
| sql_example = r#"```sql | ||
| > SELECT log1p(0); | ||
| 0 | ||
| > SELECT log1p(-1); | ||
| NULL | ||
| ```"#, | ||
| argument( | ||
| name = "expr", | ||
| description = "Numeric expression evaluated as DOUBLE. Values <= -1 and NULL return NULL." | ||
| ) | ||
| )] | ||
| #[derive(Debug, PartialEq, Eq, Hash)] | ||
| pub struct SparkLog1p { | ||
| input_types: SparkInputTypes, | ||
| } | ||
|
|
||
| impl Default for SparkLog1p { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| impl SparkLog1p { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| input_types: SparkInputTypes::new( | ||
| vec![SparkInputType::Double], | ||
| Volatility::Immutable, | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ScalarUDFImpl for SparkLog1p { | ||
| fn name(&self) -> &str { | ||
| "log1p" | ||
| } | ||
|
|
||
| fn signature(&self) -> &Signature { | ||
| self.input_types.signature() | ||
| } | ||
|
|
||
| fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { | ||
| self.input_types.coerce(self.name(), arg_types) | ||
| } | ||
|
|
||
| fn with_updated_config(&self, config: &ConfigOptions) -> Option<ScalarUDF> { | ||
| let input_types = self.input_types.with_config(config)?; | ||
| Some(ScalarUDF::from(Self { input_types })) | ||
| } | ||
|
|
||
| fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { | ||
| Ok(DataType::Float64) | ||
| } | ||
|
|
||
| fn documentation(&self) -> Option<&Documentation> { | ||
| self.doc() | ||
| } | ||
|
|
||
| fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { | ||
| let [arg] = take_function_args(self.name(), args.args)?; | ||
|
|
||
| match arg { | ||
| ColumnarValue::Scalar(ScalarValue::Float64(value)) => { | ||
| Ok(ColumnarValue::Scalar(ScalarValue::Float64( | ||
| value.and_then(log1p_or_null), | ||
| ))) | ||
| } | ||
| ColumnarValue::Array(array) => { | ||
| let result: Float64Array = array | ||
| .as_primitive::<Float64Type>() | ||
| .iter() | ||
| .map(|value| value.and_then(log1p_or_null)) | ||
| .collect(); | ||
|
|
||
| Ok(ColumnarValue::Array(Arc::new(result) as ArrayRef)) | ||
| } | ||
| other => internal_err!( | ||
| "Unexpected argument type for log1p after coercion: {}", | ||
| other.data_type() | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn log1p_or_null(value: f64) -> Option<f64> { | ||
| if value <= -1.0 { | ||
| None | ||
| } else { | ||
| Some(value.ln_1p()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could you add a few cases for the inputs Spark rejects, like
log1p(true),log1p(DATE '2020-01-01')andlog1p(X'01'), asquery error? A malformed string likelog1p('abc')would also be useful. It returns NULL with ANSI off and raisesCAST_INVALID_INPUTwith ANSI on, so it needs the ANSI wrapper described intest_files/README.md.