Skip to content

[WIP] feat: Add combined_logical_filter operator with AND/OR support#914

Open
yxdyc wants to merge 1 commit intomainfrom
feat/combined-logical-filter
Open

[WIP] feat: Add combined_logical_filter operator with AND/OR support#914
yxdyc wants to merge 1 commit intomainfrom
feat/combined-logical-filter

Conversation

@yxdyc
Copy link
Collaborator

@yxdyc yxdyc commented Feb 13, 2026

Summary:

  • Add CombinedLogicalFilter operator for explicit filter composition
  • Support AND/OR logical operations between multiple filters
  • Enable OR operations that were impossible with sequential filters
  • Optimize performance: single batch stats computation, no intermediate data reduction
  • Extend FusedFilter to support logical_op parameter (AND/OR)
  • Add comprehensive tests covering AND, OR, edge cases, and error handling
  • Add detailed documentation with performance comparisons and use cases

TODO
[] Check and improve unit-test

- Add CombinedLogicalFilter operator for explicit filter composition
- Support AND/OR logical operations between multiple filters
- Enable OR operations that were impossible with sequential filters
- Optimize performance: single batch stats computation, no intermediate data reduction
- Extend FusedFilter to support logical_op parameter (AND/OR)
- Add comprehensive tests covering AND, OR, edge cases, and error handling
- Add detailed documentation with performance comparisons and use cases

Key advantages:
1. OR operation support (previously impossible with sequential filters)
2. Performance optimization (reduced data reorganization, optimized stats computation)
3. Explicit logical relationships (self-documenting configuration)
4. Complementary to FusedFilter (automatic optimization + explicit control)
@yxdyc yxdyc requested a review from HYLcool February 13, 2026 07:03
@yxdyc yxdyc added the dj:op issues/PRs about some specific OPs label Feb 13, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @yxdyc, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a powerful new CombinedLogicalFilter operator that significantly enhances data filtering capabilities. It allows users to explicitly combine multiple filter conditions using both AND and OR logical operations, addressing limitations of sequential filtering which only supported implicit AND logic. This new operator also optimizes performance by processing all combined filters in a single batch, reducing intermediate data reduction and reorganization. The changes include the new operator's implementation, updates to the FusedFilter to support logical operations, and comprehensive documentation and tests.

Highlights

  • New CombinedLogicalFilter Operator: Introduced a new CombinedLogicalFilter operator that allows explicit composition of multiple filter operators using AND/OR logical operations.
  • Enabled OR Operations: The new operator enables OR logical operations between filters, which was previously impossible with sequential filtering, offering greater flexibility in data selection.
  • Performance Optimization: Optimized filtering performance by computing statistics for all combined filters in a single batch, reducing intermediate data reduction and reorganization.
  • Enhanced FusedFilter: Extended the existing FusedFilter to support a logical_op parameter, allowing it to also combine filters with AND/OR logic, though automatic fusion defaults to AND.
  • Comprehensive Documentation and Tests: Added detailed documentation explaining the operator's design, usage, performance advantages, and real-world use cases, along with comprehensive unit tests.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • data_juicer/ops/filter/init.py
    • Imported CombinedLogicalFilter.
    • Registered CombinedLogicalFilter in the __all__ list.
  • data_juicer/ops/filter/combined_logical_filter.py
    • Added a new CombinedLogicalFilter class.
    • Implemented __init__, compute_stats_batched, process_batched, compute_stats_single, and process_single methods.
    • Included logic for combining multiple filters with 'and' or 'or' operations.
    • Added validation for filter_ops and logical_op parameters.
  • data_juicer/ops/op_fusion.py
    • Formatted ALL_INTER_VARS list for readability.
    • Refactored speed calculation for fused_filter_speed to handle zero speed sums.
    • Added op_names variable for clearer string joining in fused_filter_name.
    • Modified FusedFilter constructor to accept a logical_op parameter.
    • Updated FusedFilter docstring to clarify its default AND logic and suggest combined_logical_filter for OR.
    • Adjusted FusedFilter.process_batched to apply the specified logical_op (AND/OR).
    • Improved readability and consistency in GeneralFusedOP initialization and docstrings.
    • Enhanced isinstance check for av.container.InputContainer in GeneralFusedOP.process_batched.
  • docs/Operators.md
    • Updated the total count of filter operators from 56 to 57.
    • Added an entry for combined_logical_filter with its description and link to documentation.
  • docs/operators/filter/combined_logical_filter.md
    • Added new documentation file for combined_logical_filter.
    • Detailed parameter configuration, effect demonstration with AND/OR examples, design rationale, performance advantages, notes, Ray compatibility, and real-world use cases.
  • tests/ops/filter/test_combined_logical_filter.py
    • Added a new test file for CombinedLogicalFilter.
    • Included test cases for AND operation, OR operation, single filter usage, default AND behavior, empty filter_ops, invalid logical_op, case-insensitive logical_op, and multiple filters with AND/OR.
Activity
  • The pull request is currently a Work In Progress ([WIP]).
  • The author plans to check and improve unit tests.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new CombinedLogicalFilter operator, which allows combining multiple filters using explicit AND/OR logic. This is a great feature, as it enables more complex filtering logic (like OR operations) that was not possible with simple sequential filters. The implementation is well-structured, with good documentation and comprehensive unit tests.

I've made a few suggestions:

  • A potential bug fix in compute_stats_single for handling batched-only sub-operators.
  • A refactoring suggestion for FusedFilter.process_batched to improve readability and fix a potential bug.
  • A couple of minor corrections in the new documentation file for clarity and consistency.

Overall, this is a high-quality contribution that significantly enhances the filtering capabilities of the framework.

Comment on lines +195 to +203
else:
# For batched-only operators, we cannot compute stats for
# a single sample. This is a limitation - batched-only
# operators should implement compute_stats_single or we need
# to create a minimal batch. For now, we skip stats
# computation for batched-only operators in single mode.
# This is acceptable because process_single will handle the
# fallback.
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

high

In compute_stats_single, for batched-only sub-operators, you're currently skipping statistics computation (pass). This will likely cause process_single to fail later, as it calls op.process_batched which expects pre-computed statistics.

To fix this, you should compute stats for these operators by creating a minimal batch and calling their compute_stats_batched method, similar to the fallback logic in process_single.

Comment on lines 203 to 213
res = None
for op in self.fused_filters:
this_res = np.array(list(op.process_batched(samples)))
if res is not None:
res = np.logical_and(res, this_res)
if self.logical_op == "and":
res = np.logical_and(res, this_res)
else: # or
res = np.logical_or(res, this_res)
else:
res = this_res
return res
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This implementation has two issues:

  1. It returns None if self.fused_filters is empty, which will cause a TypeError for the caller expecting an iterable of booleans.
  2. The if res is not None: check inside the loop can be avoided by initializing res with the result of the first filter before the loop.

Here is a suggested refactoring that addresses both points for improved correctness and readability.

Suggested change
res = None
for op in self.fused_filters:
this_res = np.array(list(op.process_batched(samples)))
if res is not None:
res = np.logical_and(res, this_res)
if self.logical_op == "and":
res = np.logical_and(res, this_res)
else: # or
res = np.logical_or(res, this_res)
else:
res = this_res
return res
if not self.fused_filters:
num_samples = len(samples[list(samples.keys())[0]])
return [True] * num_samples
res = np.array(list(self.fused_filters[0].process_batched(samples)))
op_func = np.logical_and if self.logical_op == "and" else np.logical_or
for op in self.fused_filters[1:]:
this_res = np.array(list(op.process_batched(samples)))
res = op_func(res, this_res)
return res

logical_op: 'or' # ✅ Now possible!
```
- ✅ **Explicit logic**: AND/OR relationship is clearly specified.
- ✅ **OR support**: Can now implement OR operations that sequential version impossible.
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This sentence is grammatically a bit awkward. Consider rephrasing for better clarity.

Suggested change
-**OR support**: Can now implement OR operations that sequential version impossible.
-**OR support**: Can now implement OR operations, which was impossible with the sequential version.


**Why this was impossible before**: Sequential filters can only implement AND logic. The first filter would remove all samples outside its range, leaving nothing for the second filter to process.

**为什么Default Sequential Version 无法实现**:串联过滤器只能实现 AND 逻辑。第一个过滤器会移除其范围外的所有样本,第二个过滤器无法处理。
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This heading mixes Chinese and English (Default Sequential Version). For better readability, consider using a full Chinese phrase.

Suggested change
**为什么Default Sequential Version 无法实现**串联过滤器只能实现 AND 逻辑。第一个过滤器会移除其范围外的所有样本,第二个过滤器无法处理。
**为什么串联过滤器无法实现**

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

Labels

dj:op issues/PRs about some specific OPs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant