From b916a89159b58f8a4ee6df3b2ae0bb33109d582d Mon Sep 17 00:00:00 2001 From: "Christopher M. Wolff" Date: Mon, 17 Jul 2023 12:39:30 -0700 Subject: [PATCH] fix: recurse through SubqueryAlias when finding gap fill time range (#8249) --- .../tests/query_tests/cases/in/gapfill.sql | 16 +++++++++++ .../query_tests/cases/in/gapfill.sql.expected | 27 ++++++++++++++++++- .../handle_gapfill/range_predicate.rs | 6 +++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/influxdb_iox/tests/query_tests/cases/in/gapfill.sql b/influxdb_iox/tests/query_tests/cases/in/gapfill.sql index 8239051102..90d073b654 100644 --- a/influxdb_iox/tests/query_tests/cases/in/gapfill.sql +++ b/influxdb_iox/tests/query_tests/cases/in/gapfill.sql @@ -135,3 +135,19 @@ SELECT from cpu where time between timestamp '2000-05-05T12:00:00Z' and timestamp '2000-05-05T12:59:00Z' group by region, minute; + +-- With a VALUES clause, which affects how the range is found +-- Fix for https://github.com/influxdata/idpe/issues/17880 +SELECT + date_bin_gapfill(INTERVAL '1 minute', time) as _time, + pod, + locf(selector_last(image, time)) +FROM + (VALUES ('2023-06-10T12:00:00Z'::timestamp, 'pod1', 'imageA'), + ('2023-06-10T12:00:00Z'::timestamp, 'pod2', 'imageA'), + ('2023-06-10T12:00:01Z'::timestamp, 'pod1', 'imageB'), + ('2023-06-10T12:00:02Z'::timestamp, 'pod1', 'imageB'), + ('2023-06-10T12:00:02Z'::timestamp, 'pod2', 'imageB') + ) AS data(time, pod, image) +WHERE time >= timestamp '2023-06-10T11:55:00Z' AND time < timestamp '2023-06-10T12:05:00Z' +GROUP BY _time, pod; diff --git a/influxdb_iox/tests/query_tests/cases/in/gapfill.sql.expected b/influxdb_iox/tests/query_tests/cases/in/gapfill.sql.expected index 84e628f08b..649a5ef063 100644 --- a/influxdb_iox/tests/query_tests/cases/in/gapfill.sql.expected +++ b/influxdb_iox/tests/query_tests/cases/in/gapfill.sql.expected @@ -222,4 +222,29 @@ Error during planning: gap-filling query is missing lower time bound | b | 2000-05-05T12:30:00Z | 27.049999999999997 | | b | 2000-05-05T12:40:00Z | 27.049999999999997 | | b | 2000-05-05T12:50:00Z | 27.049999999999997 | -+--------+----------------------+--------------------+ \ No newline at end of file ++--------+----------------------+--------------------+ +-- SQL: SELECT date_bin_gapfill(INTERVAL '1 minute', time) as _time, pod, locf(selector_last(image, time)) FROM (VALUES ('2023-06-10T12:00:00Z'::timestamp, 'pod1', 'imageA'), ('2023-06-10T12:00:00Z'::timestamp, 'pod2', 'imageA'), ('2023-06-10T12:00:01Z'::timestamp, 'pod1', 'imageB'), ('2023-06-10T12:00:02Z'::timestamp, 'pod1', 'imageB'), ('2023-06-10T12:00:02Z'::timestamp, 'pod2', 'imageB') ) AS data(time, pod, image) WHERE time >= timestamp '2023-06-10T11:55:00Z' AND time < timestamp '2023-06-10T12:05:00Z' GROUP BY _time, pod; ++----------------------+------+--------------------------------------------+ +| _time | pod | locf(selector_last(image,time)) | ++----------------------+------+--------------------------------------------+ +| 2023-06-10T11:55:00Z | pod1 | | +| 2023-06-10T11:56:00Z | pod1 | | +| 2023-06-10T11:57:00Z | pod1 | | +| 2023-06-10T11:58:00Z | pod1 | | +| 2023-06-10T11:59:00Z | pod1 | | +| 2023-06-10T12:00:00Z | pod1 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T12:01:00Z | pod1 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T12:02:00Z | pod1 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T12:03:00Z | pod1 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T12:04:00Z | pod1 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T11:55:00Z | pod2 | | +| 2023-06-10T11:56:00Z | pod2 | | +| 2023-06-10T11:57:00Z | pod2 | | +| 2023-06-10T11:58:00Z | pod2 | | +| 2023-06-10T11:59:00Z | pod2 | | +| 2023-06-10T12:00:00Z | pod2 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T12:01:00Z | pod2 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T12:02:00Z | pod2 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T12:03:00Z | pod2 | {value: imageB, time: 2023-06-10T12:00:02} | +| 2023-06-10T12:04:00Z | pod2 | {value: imageB, time: 2023-06-10T12:00:02} | ++----------------------+------+--------------------------------------------+ \ No newline at end of file diff --git a/iox_query/src/logical_optimizer/handle_gapfill/range_predicate.rs b/iox_query/src/logical_optimizer/handle_gapfill/range_predicate.rs index 9965b86f0d..9c435b56e9 100644 --- a/iox_query/src/logical_optimizer/handle_gapfill/range_predicate.rs +++ b/iox_query/src/logical_optimizer/handle_gapfill/range_predicate.rs @@ -65,6 +65,12 @@ impl TreeNodeVisitor for TimeRangeVisitor { self.range = range; Ok(VisitRecursion::Continue) } + LogicalPlan::SubqueryAlias(_) => { + // The nodes below this one refer to the column with a different table name, + // just unset the relation so we match on the column name. + self.col.relation = None; + Ok(VisitRecursion::Continue) + } // These nodes do not alter their schema, so we can recurse through them LogicalPlan::Sort(_) | LogicalPlan::Repartition(_)