Skip to content

Conversation

@alamb
Copy link
Contributor

@alamb alamb commented Feb 13, 2024

Which issue does this PR close?

Part of #8045
See also #9100

Rationale for this change

The high level rationale is described in #8045

In this PR I am trying to set up the pattern for how functions can be moved to the datafusion-functions crate so we can continue the wholesale movement.

I specifically wanted core to unblock the movement of arrow_cast -- #9143 (comment) from @brayanjuls

What changes are included in this PR?

  1. Move isnan function to datafusion-functions crate, math_expressions
  2. Move 'nullif' function to datafusion-functions crate, core_expressions

Are these changes tested?

Yes, covered by existing tests and new CI test

Are there any user-facing changes?

I don't think so

@github-actions github-actions bot added logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Feb 13, 2024
Gcd,
/// lcm, Least common multiple
Lcm,
/// isnan
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One benefit is that the implementation of these functions are consolidated, rather than having them spread all over

use arrow::datatypes::DataType;
//! "core" DataFusion functions
mod nullif;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add new functions, we can add the appropriate module and entry in this file

signature: Signature,
}

/// Currently supported types by the nullif function.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is just moved from various other places in the codebase. There is no new logic

/// $RETURN_TYPE: the type of array to return
/// $FUNC: the function to apply to each element of $ARG
///
macro_rules! make_function_scalar_inputs_return_type {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is copied (with more documentation) from datafusion-physical_expr. Once we move all the functions we can remove the original copy

Ok(DataType::Boolean)
}

fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise all this code is moved from elsewhere, it is not new

expr::{self, InList, Sort, WindowFunction},
factorial, find_in_set, flatten, floor, from_unixtime, gcd, gen_range, initcap,
instr, isnan, iszero, lcm, left, levenshtein, ln, log, log10, log2,
instr, iszero, lcm, left, levenshtein, ln, log, log10, log2,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the expr_fn's are still available (via datafusion_functions exports)

statement error
SELECT nullif(1);
----
DataFusion error: Failed to coerce arguments for NULLIF
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I improved the error message slightly as well

@github-actions github-actions bot added the sql SQL Planner label Feb 13, 2024
@alamb alamb force-pushed the alamb/extract_is_nan branch from a355d4a to 8e5f3ad Compare February 13, 2024 14:55
planner.statement_to_plan(ast.pop_front().unwrap())
}

fn make_udf(name: &'static str, args: Vec<DataType>, return_type: DataType) -> ScalarUDF {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sql integration test needs function definitions, to resolve function names, but doesn't actually invoke them

@alamb alamb marked this pull request as ready for review February 13, 2024 17:14
/// $ARG: ArrayRef
/// $NAME: name of the argument (for error messages)
/// $ARRAY_TYPE: the type of array to cast the argument to
macro_rules! downcast_arg {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember to move those downcast macros :)

Copy link
Contributor

@comphead comphead left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks @alamb

@comphead comphead merged commit e1f7b24 into apache:main Feb 14, 2024
@alamb
Copy link
Contributor Author

alamb commented Feb 14, 2024

Thank you @comphead 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate logical-expr Logical plan and expressions physical-expr Changes to the physical-expr crates sql SQL Planner sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants