Skip to content

feat(python): support FIELD predicates in table queries - #1166

Closed
ColinLeeo wants to merge 2 commits into
apache:developfrom
ColinLeeo:fix/tsfile-208
Closed

ColinLeeo wants to merge 2 commits into
apache:developfrom
ColinLeeo:fix/tsfile-208

Conversation

@ColinLeeo

Copy link
Copy Markdown
Contributor

Summary

Python table-model queries could only filter TAG columns (TagFilter); FIELD predicates were not exposed, even though the C++ aligned field filter already existed. This adds a Python FieldFilter API and pushes it down through the C wrapper into the table query executor.

API

field_eq / field_neq / field_lt / field_lteq / field_gt / field_gteq / field_between / field_not_between / field_is_null / field_is_not_null, composable with &, |, ~, and passed as field_filter= to TsFileReader.query_table (row and batch modes) and query_table_by_row. FIELD and TAG filters compose.

Changes

  • python/tsfile/field_filter.py plus exports in python/tsfile/__init__.py.
  • C wrapper: tsfile_field_filter_* create/and/or/not/free and tsfile_query_table_with_filters / ..._by_row_with_filters; the filter handle is deep-cloned per query and ownership is handed to the executor, which deletes it on every early-return path.
  • Cython plumbing in tsfile_reader.pyx / tsfile_py_cpp.pyx; result sets keep their handles alive and free them after the native result set.
  • Semantics: SQL three-valued logic for NOT and composites; NULL never satisfies a comparison or its negation; a double literal narrows to INT64 Java-style; TAG and FIELD filters compose; a negative batch_size now behaves the same with and without a filter.
  • Docs: skills/tsfile/references/properties-filters.md and python.md.

Verification

  • New C-wrapper tests and Python tests (test_field_filter.py, test_field_filter_query.py) cover comparisons, BETWEEN / NOT BETWEEN, composite expressions, NULL semantics, type/column errors, and TAG + FIELD composition.
  • Independent read-only review verified pushdown on all three entry points, ownership/no leaks, and runtime NULL/type semantics; the delta review of the batch_size fix concluded CLOSE.
  • Commits: 4e47594a, 603c8734.

Python table-model queries only accepted TAG predicates; FIELD value
filtering existed only inside the C++ table query executor.

- cwrapper: add a FieldFilterHandle API over AlignedFieldFilter
  (create checked/and/or/not/free) plus tsfile_query_table_with_filters
  and tsfile_reader_query_table_by_row_with_filters. Handles are
  borrowed and deep-copied per query, so callers keep ownership.
- TsFileReader: query/queryByRow overloads taking a FIELD filter; the
  executor owns it once invoked and the reader deletes it on the
  validation paths that return earlier.
- AlignedFieldFilter: add clone() for the copy-per-query contract.
- Python: new field_filter module (field_eq/field_neq/ordered,
  field_between/field_not_between, field_is_null/field_is_not_null,
  compose with &, |, ~); query_table and query_table_by_row accept
  field_filter=. Values normalize as bool/int64/double/string, with
  TypeMismatchError, ColumnNotExistError and InvalidArgumentError for
  bad column/type/operator combinations; None is rejected in favour of
  the IS NULL predicates.
- Tests: cwrapper C API coverage (selection, composition, reuse,
  errors, by-row paging) and Python DSL plus end-to-end query tests
  (scalar, Arrow batch, by-row, NULL semantics, type errors).
- Skills: document FIELD filters for Python, C and C++.

The CLI keeps --tag-filter only: this change adds a Python-side query
option and does not alter existing CLI-visible behavior, so no CLI
command/option synchronization is applicable.
Address the review findings on 4e47594:

- Accept batch_size < 0 as row-by-row mode in
  tsfile_query_table_with_filters and
  tsfile_reader_query_table_by_row_with_filters. This matches
  query_table without a FIELD filter and the documented promise that
  batch_size <= 0 selects row-by-row mode.
- Document start_time/end_time/offset/limit/batch_size on the new C
  query entry points.
- Drop the unreachable null check around AlignedFieldFilter::clone();
  allocation failure propagates as an exception and maps to E_OOM.
- Tests: negative batch_size parity, INT32 and FLOAT column literals,
  NOT of AND/OR compounds (C++ and Python), FLOAT narrowing in the C
  API.
@ColinLeeo ColinLeeo closed this Dec 1, 2026
@ColinLeeo
ColinLeeo deleted the fix/tsfile-208 branch December 1, 2026 02:50
@ColinLeeo

Copy link
Copy Markdown
Contributor Author

Superseded by #1169 after the branch was renamed.

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.

1 participant