feat: support filtering on table calculations#24
Open
jpetey75 wants to merge 1 commit into
Open
Conversation
Add a TableCalculation type and TableCalculationFilter so queries can filter
on calculated values. Table-calc filters serialize under
`filters.tableCalculations` (a sibling of `filters.dimensions`), matching the
Lightdash `Filters` type; the calculation itself serializes as a
`SqlTableCalculation` (`{name, displayName, sql}`).
- `TableCalculation(name, sql, display_name=None)` with the same comparison
operators / helper methods as `Dimension`.
- `TableCalculationFilter` accepts a `TableCalculation` or a calc name string.
- `CompositeFilter.to_dict()` routes dimension vs table-calc rules into their
respective keys; the `tableCalculations` key is omitted when unused so
existing dimension-only payloads are byte-for-byte unchanged.
- `Query.table_calculations()` builder method + `model.query(table_calculations=...)`.
- Extracted a shared `_FieldFilterMixin` for the `&`/`|` operators instead of
duplicating them across filter classes.
Shapes verified against lightdash/common source types and the live API's
filter-rule parser.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #21
Problem
The SDK's filter system was dimension-only — there was no way to filter on table calculations, even though the API supports it via
filters.tableCalculations.Change
TableCalculation(name, sql, display_name=None)— a SQL table calculation, with the same comparison operators (==,>,between, etc.) and helpers asDimension.TableCalculationFilter— accepts aTableCalculationor a calc-name string; serializes as{target: {fieldId}, operator, values}.CompositeFilter.to_dict()now routes dimension vs table-calc rules intofilters.dimensions/filters.tableCalculationsrespectively. ThetableCalculationskey is omitted when unused, so existing dimension-only payloads are unchanged.Query.table_calculations(...)builder method +model.query(table_calculations=...)kwarg._FieldFilterMixinfor the&/|operators rather than duplicating them across filter classes.Verification
Shapes were checked against the authoritative
lightdash/commonsource types rather than guessed:Filters = { dimensions?, metrics?, tableCalculations? }, each aFilterGroup→ confirms thefilters.tableCalculationsrouting.FilterRule = { target: {fieldId}, operator, values }(server injectsid) → confirms the rule shape. A live API call returning a filter-rule parse confirmed the API accepts exactly this shape.SqlTableCalculation = { name, displayName, sql }→ confirmsTableCalculation.to_dict().Caveat: I could not produce a fully-green end-to-end run via the available tooling (the MCP query tool doesn't register custom SQL table calculations into the executed query). The serialization is confirmed by source + the API's filter parser; a final confidence check would be running the SDK against a live instance with an API token.
Tests
27 new tests in
tests/test_table_calculations.py(calc serialization, operators,filters.tableCalculationsrouting, mixed dimension+calc filters, builder integration). Full unit suite green (81 passed).🤖 Generated with Claude Code