[PR #23086] Fix Empty Collection WHERE Filter Issue #30155

Closed
opened 2026-02-21 20:46:57 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/23086

State: closed
Merged: Yes


Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

This hotfix addresses a critical database query issue where empty collections passed to WHERE IN clauses would result in invalid SQL conditions (WHERE false). The problem occurs when filtering datasets, documents, or segments with empty ID lists, potentially causing query failures or unexpected behavior.

Fix #23085

Changes Made:

  1. Dataset Filtering: Added validation checks in get_datasets() method for permitted_dataset_ids and tag_ids parameters
  2. Document Operations: Enhanced get_datasets_by_ids() and delete_documents() methods with empty collection guards
  3. Segment Operations: Implemented safety checks in delete_segments(), update_segments_status(), and get_segments() methods
  4. Status Filtering: Added validation for status_list parameter in segment queries

Root Cause:

When collections like permitted_dataset_ids, tag_ids, document_ids, or segment_ids are empty, the resulting SQL query contains WHERE column IN () which translates to WHERE false, causing potential database errors or returning no results unexpectedly.

Solution:

Added explicit checks using if collection and len(collection) > 0: pattern before applying WHERE IN filters, with appropriate fallback behavior (returning empty results or early returns).

Technical Details

Files Modified:

  • Dataset service methods for permission-based filtering
  • Document management operations
  • Segment CRUD operations
  • Query builders with IN clause filters

Impact:

  • Improved Stability: Prevents SQL errors from empty WHERE IN clauses
  • Better Performance: Avoids unnecessary database queries when collections are empty
  • Enhanced Reliability: Ensures consistent behavior across different user permission levels

Screenshots

Before After
SQL errors or unexpected WHERE false conditions when collections are empty Proper validation with early returns and safe query execution

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Testing Recommendations

  1. Test dataset filtering with users having no permitted datasets
  2. Verify document operations with empty document ID lists
  3. Test segment operations with empty segment collections
  4. Validate tag-based filtering with empty tag lists
  5. Ensure proper behavior across different user roles (OWNER, DATASET_OPERATOR, etc.)
**Original Pull Request:** https://github.com/langgenius/dify/pull/23086 **State:** closed **Merged:** Yes --- > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 2. Ensure there is an associated issue and you have been assigned to it > 3. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary This hotfix addresses a critical database query issue where empty collections passed to WHERE IN clauses would result in invalid SQL conditions (`WHERE false`). The problem occurs when filtering datasets, documents, or segments with empty ID lists, potentially causing query failures or unexpected behavior. Fix #23085 ### Changes Made: 1. **Dataset Filtering**: Added validation checks in `get_datasets()` method for `permitted_dataset_ids` and `tag_ids` parameters 2. **Document Operations**: Enhanced `get_datasets_by_ids()` and `delete_documents()` methods with empty collection guards 3. **Segment Operations**: Implemented safety checks in `delete_segments()`, `update_segments_status()`, and `get_segments()` methods 4. **Status Filtering**: Added validation for `status_list` parameter in segment queries ### Root Cause: When collections like `permitted_dataset_ids`, `tag_ids`, `document_ids`, or `segment_ids` are empty, the resulting SQL query contains `WHERE column IN ()` which translates to `WHERE false`, causing potential database errors or returning no results unexpectedly. ### Solution: Added explicit checks using `if collection and len(collection) > 0:` pattern before applying WHERE IN filters, with appropriate fallback behavior (returning empty results or early returns). ## Technical Details ### Files Modified: - Dataset service methods for permission-based filtering - Document management operations - Segment CRUD operations - Query builders with IN clause filters ### Impact: - **Improved Stability**: Prevents SQL errors from empty WHERE IN clauses - **Better Performance**: Avoids unnecessary database queries when collections are empty - **Enhanced Reliability**: Ensures consistent behavior across different user permission levels ## Screenshots | Before | After | |--------|-------| | SQL errors or unexpected `WHERE false` conditions when collections are empty | Proper validation with early returns and safe query execution | ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods ## Testing Recommendations 1. Test dataset filtering with users having no permitted datasets 2. Verify document operations with empty document ID lists 3. Test segment operations with empty segment collections 4. Validate tag-based filtering with empty tag lists 5. Ensure proper behavior across different user roles (OWNER, DATASET_OPERATOR, etc.)
yindo added the pull-request label 2026-02-21 20:46:57 -05:00
yindo closed this issue 2026-02-21 20:46:57 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30155