fix: wrap tql cte in a subquery alias (#6910)

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2025-09-04 23:07:20 -07:00
committed by Weny Xu
parent f1b7581dc3
commit aa7e7942f8
3 changed files with 32 additions and 0 deletions

View File

@@ -282,6 +282,16 @@ impl DfLogicalPlanner {
.build()
.context(PlanSqlSnafu)?;
}
// Wrap in SubqueryAlias to ensure proper table qualification for CTE
logical_plan = LogicalPlan::SubqueryAlias(
datafusion_expr::SubqueryAlias::try_new(
Arc::new(logical_plan),
cte.name.value.clone(),
)
.context(PlanSqlSnafu)?,
);
planner_context.insert_cte(&cte.name.value, logical_plan);
}
CteContent::Sql(_) => {

View File

@@ -109,6 +109,22 @@ SELECT count(*) as host1_points FROM host_metrics;
| 5 |
+--------------+
-- TQL CTE with column reference
WITH host_metrics AS (
TQL EVAL (0, 40, '10s') labels{host="host1"}
)
SELECT host_metrics.ts, host_metrics.host FROM host_metrics;
+---------------------+-------+
| ts | host |
+---------------------+-------+
| 1970-01-01T00:00:00 | host1 |
| 1970-01-01T00:00:10 | host1 |
| 1970-01-01T00:00:20 | host1 |
| 1970-01-01T00:00:30 | host1 |
| 1970-01-01T00:00:40 | host1 |
+---------------------+-------+
-- Multiple TQL CTEs referencing different tables
WITH
metric_data(ts, val) AS (TQL EVAL (0, 40, '10s') metric),

View File

@@ -56,6 +56,12 @@ WITH host_metrics AS (
)
SELECT count(*) as host1_points FROM host_metrics;
-- TQL CTE with column reference
WITH host_metrics AS (
TQL EVAL (0, 40, '10s') labels{host="host1"}
)
SELECT host_metrics.ts, host_metrics.host FROM host_metrics;
-- Multiple TQL CTEs referencing different tables
WITH
metric_data(ts, val) AS (TQL EVAL (0, 40, '10s') metric),