Skip to content

Enhance interpolation documentation and update mocked queries#989

Merged
cching95 merged 8 commits intodevelopfrom
feature/00977
Apr 16, 2026
Merged

Enhance interpolation documentation and update mocked queries#989
cching95 merged 8 commits intodevelopfrom
feature/00977

Conversation

@Amber-Rigg
Copy link
Copy Markdown
Contributor

PR Summary: Refactor Interpolation Query Architecture - Time Gradient Buffering

Overview

Refactored the interpolation query builder to implement time gradient buffering for accurate boundary-aware linear interpolation. This change improves accuracy at query boundaries by automatically expanding the data range based on the time interval unit.

Key Changes

  1. Core Architecture Changes (_time_series_query_builder.py)
    New Functions Added:

_build_raw_query_for_interpolate() - Raw query with time gradient buffering

Automatically expands query range by time-based buffer (±5 min for minutes, ±60 sec for seconds, ±1 hour for hours, ±1 day for days)
Uses timestampadd() instead of plain to_timestamp() for start/end dates
_build_interpolate_intervals_cte() - Generates regular time intervals using sequence()

_build_interpolate_fill_intervals_cte() - Unions raw data with intervals using SortKey (0 for real, 1 for desired)

_build_interpolate_calculate_cte() - Applies LAG/LEAD window functions to capture previous/next real data points

_build_interpolate_interpolate_cte() - Performs linear interpolation using unix_millis() for precision

_build_interpolate_uom_cte() - Joins interpolated data with metadata for Unit of Measure

_build_interpolate_query() - Orchestrates all CTEs together

Updates to _interpolation_query():

Changed from _build_raw_query() to _build_raw_query_for_interpolate()
Removed old resample/fill_intervals approach
Simplified query structure: raw → intervals → fill_intervals → interpolate_calculate → interpolate → uom
Removed manual UoM query building (now integrated into main flow)

  1. Query Logic Changes
    Old Structure:

Used window() function for time grouping
Complex FULL OUTER JOIN between calculate and fill_intervals CTEs
Separate handling of UoM
New Structure:

No window() function
5 dedicated CTEs with clear separation of concerns
Linear interpolation formula: value = prev_value + ((next_value - prev_value) * (current_time - prev_time) / (next_time - prev_time))
Uses unix_millis() for millisecond precision
Time gradient buffering ensures boundary accuracy
UoM integrated into the main CTE chain

  1. Test Mock Updates ([sdk_test_objects.py](vscode-file://vscode-
    Updated all 4 interpolate mock queries:

INTERPOLATE_MOCKED_QUERY
INTERPOLATE_MOCKED_QUERY_CHECK_TAGS
INTERPOLATE_MOCKED_QUERY_PIVOT
INTERPOLATE_MOCKED_QUERY_UOM
Changes:

Replaced window() function with time gradient buffering: timestampadd(minute, -5, ...) to timestampadd(minute, 5, ...)
Updated raw CTE to use 'Good' AS Status instead of actual status column
Restructured CTEs to follow new architecture
Reflected new SortKey-based approach for fill_intervals

  1. API Documentation
    Added comprehensive warning section:

Files Modified

[_time_series_query_builder.py](major refactor)
[interpolate.py] (documentation)
[sdk_test_objects.py]((test mocks)

- Added detailed warning in `interpolate.py` regarding time gradient buffering for accurate boundary interpolation.
- Updated mocked SQL queries in `sdk_test_objects.py` to include a ±5 minute buffer for the requested date range, ensuring accurate interpolation.
- Adjusted the structure of the mocked queries to reflect the new logic for handling event times and values.

Signed-off-by: Amber-Rigg <amber.l.rigg25@gmail.com>
@Amber-Rigg Amber-Rigg self-assigned this Apr 14, 2026
@Amber-Rigg Amber-Rigg added the bug Something isn't working label Apr 14, 2026
…e return values

Signed-off-by: Amber-Rigg <amber.l.rigg25@gmail.com>
Signed-off-by: Amber-Rigg <amber.l.rigg25@gmail.com>
…ordering

Signed-off-by: Amber-Rigg <amber.l.rigg25@gmail.com>
…ter and update handling in related functions

Signed-off-by: Amber-Rigg <amber.l.rigg25@gmail.com>
…e parameter

Signed-off-by: Amber-Rigg <amber.l.rigg25@gmail.com>
Signed-off-by: Amber-Rigg <amber.l.rigg25@gmail.com>
cching95
cching95 previously approved these changes Apr 16, 2026
… sparse data

Signed-off-by: Amber-Rigg <amber.l.rigg25@gmail.com>
@cching95 cching95 merged commit e29560c into develop Apr 16, 2026
17 checks passed
@cching95 cching95 deleted the feature/00977 branch April 16, 2026 09:53
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
15.2% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants