Skip to content

SQLPatternMatcher rejects valid queries when scrape_interval > 1s #201

@milindsrivastava1997

Description

@milindsrivastava1997

Bug

In flatten_query_info, scraped_intervals is a dimensionless ratio (duration_s / scrape_interval_s), but the guard compares it against self.scrape_interval (seconds):

// sqlpattern_matcher.rs:178-180
scraped_intervals = scrape_duration / self.scrape_interval;

if scraped_intervals < self.scrape_interval {  // BUG: ratio < seconds
    return Err((QueryError::SpatialDurationSmall, ...));
}

The intent is to check "does the query span at least 1 scrape interval?", so the RHS should be 1.0.

With scrape_interval=15s:

  • A 15s query (1 interval): scraped_intervals = 1.0, 1.0 < 15.0false positive SpatialDurationSmall
  • A 30s query (2 intervals): scraped_intervals = 2.0, 2.0 < 15.0false positive SpatialDurationSmall
  • Only queries spanning ≥ 15 intervals (≥ 225s) pass — the threshold is effectively scrape_interval² seconds

All existing tests use scrape_interval=1.0 (hardcoded in check_query), so < 1.0 is coincidentally correct and the bug is invisible.

Fix

if scraped_intervals < 1.0 {

Breaking tests

Add to sqlparser_test.rs:

fn check_query_with_interval(
    sql: &str,
    scrape_interval: f64,
    expected_types: Vec<QueryType>,
    expected_error: Option<QueryError>,
) {
    let schema = create_test_schema();
    let matcher = SQLPatternMatcher::new(schema.clone(), scrape_interval);
    let query_data =
        parse_sql_query(sql).unwrap_or_else(|| panic!("Failed to parse query: {}", sql));
    let result = matcher.query_info_to_pattern(&query_data);
    assert_eq!(result.query_type, expected_types);
    assert_eq!(result.error, expected_error);
}

/// scraped_intervals = 15/15 = 1.0; bug fires 1.0 < 15.0 → false positive error
#[test]
fn test_bug_validation_rejects_valid_single_interval_spatial_query() {
    check_query_with_interval(
        "SELECT AVG(value) FROM cpu_usage \
         WHERE time BETWEEN DATEADD(s, -15, '2025-10-01 00:00:00') AND '2025-10-01 00:00:00' \
         GROUP BY L1, L2, L3, L4",
        15.0,
        vec![QueryType::Spatial],
        None,
    );
}

/// scraped_intervals = 30/15 = 2.0; bug fires 2.0 < 15.0 → false positive error
#[test]
fn test_bug_validation_rejects_valid_two_interval_temporal_query() {
    check_query_with_interval(
        "SELECT SUM(value) FROM cpu_usage \
         WHERE time BETWEEN DATEADD(s, -30, '2025-10-01 00:00:00') AND '2025-10-01 00:00:00' \
         GROUP BY L1, L2, L3, L4",
        15.0,
        vec![QueryType::TemporalGeneric],
        None,
    );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions