Skip to content

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

Draft
ColinLeeo wants to merge 10 commits into
apache:developfrom
ColinLeeo:colin/feat-python-field-filter
Draft

ColinLeeo wants to merge 10 commits into
apache:developfrom
ColinLeeo:colin/feat-python-field-filter

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.
- Reject numeric literals whose Java-style narrowing would change the
  predicate on integral columns (fractional, NaN/inf, out-of-range) with
  E_TYPE_NOT_MATCH / TypeMismatchError instead of silently narrowing.
- Validate creation literals on a throwaway clone so reusable handles keep
  the caller literal and bind against the query-time schema.
- Align FIELD and/or/not ownership with the TAG API: inputs are consumed;
  wrong-typed or too-deep inputs are rejected and consumed.
- Cap filter trees at 512 levels in the C API, query wrapper and Cython
  builder to stop unbounded recursion.
- Make TableQueryExecutor adopt time/field filters with RAII so a throwing
  allocation cannot leak them; hoist table-name lowering out of the
  release window.
- Zero-initialize Cython column-name arrays and free handles through the
  virtual Filter destructor.
- Validate empty projections and negative offsets uniformly in
  query_table / query_table_by_row.
- Add regression tests (Python and C wrapper) and update the header, DSL
  and skill docs.
… query time

- Reject an aliased operand pair (same handle twice) and consume the
  non-NULL operand on a NULL-sibling call, matching the documented
  consumption contract.
- Validate literal exactness against the queried schema in the table query
  executor, so a handle reused with a different schema is rejected instead
  of being silently narrowed.
- Keep negation valid for a maximum-depth tree (negation preserves height)
  and charge BETWEEN's synthetic native level in the Cython builder so the
  boundary raises ValueError instead of MemoryError.
- Guard the TimeBetween filter with RAII and materialise the table name
  before cloning, closing the remaining exception windows.
- Use len() for the empty-projection check so numpy arrays and pandas
  Index work; document the new validation, depth and ownership rules.
- Add regression tests: aliasing, NULL sibling, 512/513 depth boundary,
  INT32/INT64 literal boundaries, cross-device OFFSET, cache-bypass proof,
  Arrow OR composition, and the by-row validation paths.
…validation

- Unwrap consecutive NOT filters iteratively with parity reduction, so the
  Python builder counts native tree height (negation preserves it) and a
  maximum-depth tree stays negatable; BETWEEN keeps its extra native level.
- check_exact_literals now treats TIMESTAMP/DATE source literals like
  INT64/INT32 and reports E_COLUMN_NOT_EXIST / E_INVALID_ARG for missing or
  non-FIELD columns; the executor validates structure before literals.
- TableQueryExecutor::query_on_tree adopts its time filter with RAII and
  returns on unsupported ordering instead of falling through with a null
  reader.
- Accept arbitrary column-name sequences (numpy arrays, pandas Index,
  generators) by dropping the Cython list annotation and materialising once;
  free both tree-by-row arrays when one allocation fails.
- Document the 512-level ValueError, query-time column validation, bool and
  string literal binding, string_value requirements, error-code lists and the
  per-function depth rules; add unit and e2e tests for exactness, wrong-family
  handles, depth boundaries, negation and sequence inputs.
…s/tests

- Add AlignedFieldFilter::height() and reject caller-built FIELD trees
  taller than 512 in TableQueryExecutor::query, so the C++ API cannot
  overflow the recursive clone()/bind() traversals.
- Guard the NOT unwrap against cyclic filter objects by tracking visited
  nodes and raising ValueError instead of looping forever.
- Accept arbitrary column-name sequences in query_table_on_tree,
  query_tree_by_row and query_timeseries as well (materialise once).
- Keep the tree-model time filter guarded until the executor adopts it.
- Correct the NULL wording for NOT(IS NOT NULL), the TAG-filter lifetime
  note, err_code direction, the Python height wording and the
  check_exact_literals comment scope.
- Strengthen tests: selective depth predicate and BETWEEN acceptance
  boundary, captured composition results, or/family and right-operand
  depth cases, TIMESTAMP/FLOAT literal acceptance, by-row sequence inputs.
@ColinLeeo
ColinLeeo marked this pull request as draft December 2, 2026 03:40
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