From 562a6d00dfb42e507ca4275802ff074c0561261e Mon Sep 17 00:00:00 2001 From: "kyle.cao" Date: Thu, 15 Jul 2021 15:51:15 +0800 Subject: [PATCH] fix alias check in with clause --- src/validator/MatchValidator.cpp | 9 +++++++++ tests/tck/features/match/With.feature | 23 ++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/validator/MatchValidator.cpp b/src/validator/MatchValidator.cpp index 7998b49e7..401a94dc3 100644 --- a/src/validator/MatchValidator.cpp +++ b/src/validator/MatchValidator.cpp @@ -437,6 +437,15 @@ Status MatchValidator::validateWith(const WithClause *with, std::vector exprs; exprs.reserve(with->columns()->size()); for (auto *col : with->columns()->columns()) { + auto labelExprs = ExpressionUtils::collectAll(col->expr(), {Expression::Kind::kLabel}); + for (auto *labelExpr : labelExprs) { + DCHECK_EQ(labelExpr->kind(), Expression::Kind::kLabel); + auto label = static_cast(labelExpr)->name(); + if (!withClauseCtx.yield->aliasesUsed || + !withClauseCtx.yield->aliasesUsed->count(label)) { + return Status::SemanticError("Variable `%s` not defined", label.c_str()); + } + } if (col->alias().empty()) { if (col->expr()->kind() == Expression::Kind::kLabel) { col->setAlias(col->toString()); diff --git a/tests/tck/features/match/With.feature b/tests/tck/features/match/With.feature index f2b33aac9..6b86ac804 100644 --- a/tests/tck/features/match/With.feature +++ b/tests/tck/features/match/With.feature @@ -29,13 +29,6 @@ Feature: With clause Then the result should be, in any order, with relax comparison: | a | b | | [1,2,3] | "hello" | - When executing query: - """ - WITH [1, 2, 3] AS a - WITH a, "hello" - RETURN a - """ - Then a SemanticError should be raised at runtime: Expression in WITH must be aliased (use AS) Scenario: with agg return When executing query: @@ -199,3 +192,19 @@ Feature: With clause Then the result should be, in any order, with relax comparison: | exists(m.abc) | exists(NULL.abc) | | NULL | NULL | + + Scenario: error check + When executing query: + """ + WITH [1, 2, 3] AS a + WITH a, "hello" + RETURN a + """ + Then a SemanticError should be raised at runtime: Expression in WITH must be aliased (use AS) + When executing query: + """ + WITH [1, 2, 3] AS a + WITH a, a+b AS c + RETURN a + """ + Then a SemanticError should be raised at runtime: Variable `b` not defined