Skip to content
This repository has been archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
fix alias check in with clause (#1258)
Browse files Browse the repository at this point in the history
Co-authored-by: Yee <2520865+yixinglu@users.noreply.github.com>
  • Loading branch information
czpmango and yixinglu committed Jul 15, 2021
1 parent 7c8c878 commit c08b248
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/validator/MatchValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ Status MatchValidator::validateWith(const WithClause *with,
std::vector<const Expression *> 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<const LabelExpression *>(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());
Expand Down
23 changes: 16 additions & 7 deletions tests/tck/features/match/With.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit c08b248

Please sign in to comment.