Skip to content

Conversation

@kosiew
Copy link
Contributor

@kosiew kosiew commented Mar 13, 2025

Which issue does this PR close?

Closes #1057.

Rationale for this change

This PR improves the WindowUDF.udwf function by making it more flexible and user-friendly. It introduces function overloads and allows usage as both a function and a decorator, improving usability and code clarity. Additionally, it expands test coverage to ensure correctness and robustness.

What changes are included in this PR?

Added function overloads to WindowUDF.udwf to support both function and decorator usage.

Refactored udwf to separate core logic into _create_window_udf and _create_window_udf_decorator for better maintainability.

Introduced helper methods _get_default_name and _normalize_input_types to simplify input handling.

Updated the test_udwf.py test suite to:

Add new test cases for function and decorator usage.

Introduce additional fixture-based DataFrame test setups.

Cover error cases, argument variations, and SQL registration scenarios.

Are there any user-facing changes?

Yes:

Users can now define WindowUDF functions using either a traditional function call or as a decorator.

Improved error handling and better default behavior when inferring names.

New test cases ensure correctness across different usage scenarios.

These changes are backward-compatible and enhance the developer experience when defining user-defined window functions.

kosiew added 12 commits March 13, 2025 17:37
- Added `create_udwf` static method to `WindowUDF` class, allowing users to create User-Defined Window Functions (UDWF) as both a function and a decorator.
- Updated type hinting for `_R` using `TypeAlias` for better clarity.
- Enhanced documentation with usage examples for both function and decorator styles, improving usability and understanding.
…aluator

- Removed multiple exponential smoothing classes to streamline the code.
- Introduced SimpleWindowCount class for basic row counting functionality.
- Updated test cases to validate the new SimpleWindowCount evaluator.
- Refactored fixture and test functions for clarity and consistency.
- Enhanced error handling in UDWF creation tests.
- Renamed `df` fixture to `complex_window_df` for clarity.
- Renamed `simple_df` fixture to `count_window_df` to better reflect its purpose.
- Updated test functions to use the new fixture names, enhancing readability and maintainability.
- Changed udwf1 to use BiasedNumbers instead of bias_10.
- Added udwf2 to call udwf with bias_10.
- Introduced udwf3 to demonstrate a lambda function returning BiasedNumbers(20).
…urn type handling

- Eliminated the state_type parameter from the udwf method to simplify the function signature.
- Updated return type handling in the _function and _decorator methods to use a generic type _R for better type flexibility.
- Enhanced the decorator to wrap the original function, allowing for improved argument handling and expression return.
kosiew added 4 commits March 13, 2025 18:15
- Changed the type hint for the return type in the _create_window_udf_decorator method to use pa.DataType directly instead of a TypeVar.
- Simplified the handling of input types by removing redundant checks and directly using the input types list.
- Removed unnecessary comments and cleaned up the code for better readability.
- Updated the test for udwf to use parameterized tests for better coverage and maintainability.
@kosiew kosiew marked this pull request as ready for review March 13, 2025 11:01
kosiew added 2 commits March 14, 2025 17:42
…r WindowEvaluator and improving import organization"

This reverts commit 16dbe5f.
Copy link
Member

@timsaucer timsaucer left a comment

Choose a reason for hiding this comment

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

Thank you for another excellent contribution!

@timsaucer timsaucer merged commit 2f52688 into apache:main Mar 15, 2025
17 checks passed
@kosiew
Copy link
Contributor Author

kosiew commented Mar 17, 2025

@timsaucer
Thanks for reviewing and merging this.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add decorator for udwf

2 participants