fix(query): keep TQL views serializable and expose native diagnostics

Signed-off-by: discord9 <discord9@163.com>
This commit is contained in:
discord9
2026-10-24 21:30:49 +08:00
parent 92cd8c463d
commit 2ee24f6aef
4 changed files with 70 additions and 6 deletions
+7 -1
View File
@@ -75,6 +75,7 @@ use partition::expr::{Operand, PartitionExpr, RestrictedOp};
use partition::multi_dim::MultiDimPartitionRule;
use query::parser::QueryStatement;
use query::plan::extract_and_rewrite_full_table_names;
use query::promql::planner::NATIVE_PROMQL_EXTENSION_KEY;
use query::query_engine::DefaultSerializer;
use query::sql::create_table_stmt;
use session::context::QueryContextRef;
@@ -1164,7 +1165,12 @@ impl StatementExecutor {
)
.await?
}
Statement::Tql(query) => self.plan_tql(query.clone(), &ctx).await?,
Statement::Tql(query) => {
// The serialized view plan must not depend on the native execution extension.
let mut view_ctx = ctx.as_ref().clone();
view_ctx.set_extension(NATIVE_PROMQL_EXTENSION_KEY, "false");
self.plan_tql(query.clone(), &Arc::new(view_ctx)).await?
}
_ => {
return InvalidViewStmtSnafu {}.fail();
}
+6 -1
View File
@@ -513,7 +513,12 @@ impl PlanRewriter {
/// `plan` must be the root of the tree that is about to be rewritten, otherwise
/// [`PlanRewriter::should_expand`] may skip a check it has to perform.
fn new(plan: &LogicalPlan, in_process_region_transport: bool) -> Self {
let skip_encodability_gate = in_process_region_transport && is_fused_series_plan(plan);
let native_root = match plan {
LogicalPlan::Analyze(analyze) => analyze.input.as_ref(),
_ => plan,
};
let skip_encodability_gate =
in_process_region_transport && is_fused_series_plan(native_root);
Self {
// Preserve the ordinary whole-plan fast path unless this is the in-process root
// exception: no node in that tree needs an encoding check.
+15 -3
View File
@@ -17,6 +17,7 @@ mod function_plans;
mod island;
mod matching_filters;
pub(crate) mod native;
pub use native::NATIVE_PROMQL_EXTENSION_KEY;
mod set_operator;
@@ -2421,9 +2422,20 @@ impl PromPlanner {
// let promql_parser::parser::ast::Extension { expr } = ext_expr;
let expr = &ext_expr.expr;
let children = expr.children();
let plan = self
.prom_root_expr_to_plan(&children[0], query_engine_state)
.await?;
let native_plan = match expr.name() {
ANALYZE_NODE_NAME
| ANALYZE_VERBOSE_NODE_NAME
| EXPLAIN_NODE_NAME
| EXPLAIN_VERBOSE_NODE_NAME => self.try_plan_native_fused(&children[0]).await?,
_ => None,
};
let plan = match native_plan {
Some(plan) => plan,
None => {
self.prom_root_expr_to_plan(&children[0], query_engine_state)
.await?
}
};
// Wrapper for the explanation/analyze of the existing plan
// https://docs.rs/datafusion-expr/latest/datafusion_expr/logical_plan/builder/struct.LogicalPlanBuilder.html#method.explain
// if `analyze` is true, runs the actual plan and produces
+42 -1
View File
@@ -55,6 +55,7 @@ use crate::statements::create::{
CreateTableLike, CreateView, Partitions, SqlOrTql, TableConstraint, VECTOR_OPT_DIM,
};
use crate::statements::statement::Statement;
use crate::statements::tql::Tql;
use crate::statements::transform::type_alias::get_data_type_by_alias_name;
use crate::statements::{OptionMap, sql_data_type_to_concrete_data_type};
use crate::util::{OptionValue, location_to_index, parse_option_string};
@@ -148,7 +149,18 @@ impl<'a> ParserContext<'a> {
.expect_keyword(Keyword::AS)
.context(SyntaxSnafu)?;
let query = self.parse_query()?;
let query = match self.parser.peek_token().token {
Token::Word(word)
if word.quote_style.is_none()
&& word.value.eq_ignore_ascii_case(tql_parser::TQL) =>
{
match self.parse_tql(false)? {
Statement::Tql(tql @ Tql::Eval(_)) => Statement::Tql(tql),
_ => return self.expected("TQL EVAL", self.parser.peek_token()),
}
}
_ => self.parse_query()?,
};
Ok(Statement::CreateView(CreateView {
name: view_name,
@@ -3315,6 +3327,35 @@ CREATE TABLE spans (
}
}
#[test]
fn test_parse_create_view_tql_eval() {
let sql = "CREATE VIEW test AS TQL EVAL (0, 15, '5s') metric";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
assert!(matches!(
&result[0],
Statement::CreateView(CreateView {
query,
..
}) if matches!(query.as_ref(), Statement::Tql(Tql::Eval(_)))
));
for sql in [
"CREATE VIEW test AS TQL EXPLAIN (0, 15, '5s') metric",
"CREATE VIEW test AS TQL ANALYZE (0, 15, '5s') metric",
] {
assert!(
ParserContext::create_with_dialect(
sql,
&GreptimeDbDialect {},
ParseOptions::default(),
)
.is_err()
);
}
}
#[test]
fn test_parse_create_view_with_columns() {
let sql = "CREATE VIEW test () AS SELECT * FROM NUMBERS";