Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab7445b

Browse files
authored
Fix the wrong usage of limit sentence (vesoft-inc#2570)
1 parent 1525aa3 commit ab7445b

File tree

11 files changed

+105
-39
lines changed

11 files changed

+105
-39
lines changed

src/graph/validator/OrderByValidator.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,53 @@
66

77
#include "graph/validator/OrderByValidator.h"
88

9-
#include "common/expression/LabelExpression.h"
109
#include "graph/planner/plan/Query.h"
1110
#include "parser/TraverseSentences.h"
1211

1312
namespace nebula {
1413
namespace graph {
1514
Status OrderByValidator::validateImpl() {
1615
auto sentence = static_cast<OrderBySentence *>(sentence_);
17-
outputs_ = inputCols();
1816
auto &factors = sentence->factors();
19-
auto *pool = qctx_->objPool();
2017
for (auto &factor : factors) {
21-
if (factor->expr()->kind() == Expression::Kind::kLabel) {
22-
auto *label = static_cast<const LabelExpression *>(factor->expr());
23-
auto *expr = InputPropertyExpression::make(pool, label->name());
24-
factor->setExpr(expr);
25-
}
26-
if (factor->expr()->kind() != Expression::Kind::kInputProperty) {
18+
if (factor->expr()->kind() == Expression::Kind::kInputProperty) {
19+
auto expr = static_cast<InputPropertyExpression *>(factor->expr());
20+
NG_RETURN_IF_ERROR(deduceExprType(expr));
21+
NG_RETURN_IF_ERROR(deduceProps(expr, exprProps_));
22+
const auto &cols = inputCols();
23+
auto &name = expr->prop();
24+
auto eq = [&](const ColDef &col) { return col.name == name; };
25+
auto iter = std::find_if(cols.cbegin(), cols.cend(), eq);
26+
size_t colIdx = std::distance(cols.cbegin(), iter);
27+
colOrderTypes_.emplace_back(std::make_pair(colIdx, factor->orderType()));
28+
} else if (factor->expr()->kind() == Expression::Kind::kVarProperty) {
29+
auto expr = static_cast<VariablePropertyExpression *>(factor->expr());
30+
NG_RETURN_IF_ERROR(deduceExprType(expr));
31+
NG_RETURN_IF_ERROR(deduceProps(expr, exprProps_));
32+
const auto &cols = vctx_->getVar(expr->sym());
33+
auto &name = expr->prop();
34+
auto eq = [&](const ColDef &col) { return col.name == name; };
35+
auto iter = std::find_if(cols.cbegin(), cols.cend(), eq);
36+
size_t colIdx = std::distance(cols.cbegin(), iter);
37+
colOrderTypes_.emplace_back(std::make_pair(colIdx, factor->orderType()));
38+
} else {
2739
return Status::SemanticError("Order by with invalid expression `%s'",
2840
factor->expr()->toString().c_str());
2941
}
30-
auto expr = static_cast<InputPropertyExpression *>(factor->expr());
31-
NG_RETURN_IF_ERROR(deduceExprType(expr));
32-
auto &name = expr->prop();
33-
auto eq = [&](const ColDef &col) { return col.name == name; };
34-
auto iter = std::find_if(outputs_.cbegin(), outputs_.cend(), eq);
35-
size_t colIdx = std::distance(outputs_.cbegin(), iter);
36-
colOrderTypes_.emplace_back(std::make_pair(colIdx, factor->orderType()));
42+
}
43+
44+
if (!exprProps_.inputProps().empty() && !exprProps_.varProps().empty()) {
45+
return Status::SemanticError("Not support both input and variable.");
46+
} else if (!exprProps_.inputProps().empty()) {
47+
outputs_ = inputCols();
48+
} else if (!exprProps_.varProps().empty()) {
49+
if (!userDefinedVarNameList_.empty()) {
50+
if (userDefinedVarNameList_.size() != 1) {
51+
return Status::SemanticError("Multiple user defined vars are not supported yet.");
52+
}
53+
userDefinedVarName_ = *userDefinedVarNameList_.begin();
54+
outputs_ = vctx_->getVar(userDefinedVarName_);
55+
}
3756
}
3857

3958
return Status::OK();
@@ -47,6 +66,10 @@ Status OrderByValidator::toPlan() {
4766
colNames.emplace_back(col.name);
4867
}
4968
sortNode->setColNames(std::move(colNames));
69+
if (!userDefinedVarName_.empty()) {
70+
sortNode->setInputVar(userDefinedVarName_);
71+
}
72+
5073
root_ = sortNode;
5174
tail_ = root_;
5275
return Status::OK();

src/graph/validator/OrderByValidator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class OrderByValidator final : public Validator {
2424

2525
private:
2626
std::vector<std::pair<size_t, OrderFactor::OrderType>> colOrderTypes_;
27+
std::string userDefinedVarName_;
2728
};
2829
} // namespace graph
2930
} // namespace nebula

src/graph/validator/SequentialValidator.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ Status SequentialValidator::validateImpl() {
3232
}
3333

3434
DCHECK(!sentences.empty());
35-
auto firstSentence = getFirstSentence(sentences.front());
36-
switch (firstSentence->kind()) {
37-
case Sentence::Kind::kLimit:
38-
case Sentence::Kind::kOrderBy:
39-
case Sentence::Kind::kGroupBy:
40-
return Status::SyntaxError("Could not start with the statement: %s",
41-
firstSentence->toString().c_str());
42-
default:
43-
break;
44-
}
4535

4636
seqAstCtx_->startNode = StartNode::make(seqAstCtx_->qctx);
4737
for (auto* sentence : sentences) {

src/parser/parser.yy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2544,7 +2544,6 @@ traverse_sentence
25442544
| order_by_sentence { $$ = $1; }
25452545
| fetch_sentence { $$ = $1; }
25462546
| find_path_sentence { $$ = $1; }
2547-
| limit_sentence { $$ = $1; }
25482547
| yield_sentence { $$ = $1; }
25492548
| get_subgraph_sentence { $$ = $1; }
25502549
| delete_vertex_sentence { $$ = $1; }
@@ -2557,6 +2556,7 @@ traverse_sentence
25572556
piped_sentence
25582557
: traverse_sentence { $$ = $1; }
25592558
| piped_sentence PIPE traverse_sentence { $$ = new PipedSentence($1, $3); }
2559+
| piped_sentence PIPE limit_sentence { $$ = new PipedSentence($1, $3); }
25602560
;
25612561

25622562
set_sentence

tests/tck/features/fetch/FetchVertices.intVid.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Feature: Fetch Int Vid Vertices
3939
"""
4040
$var = GO FROM hash('Boris Diaw') over like YIELD like._dst as id;
4141
FETCH PROP ON player $var.id YIELD player.name as name, player.age |
42-
ORDER BY name
42+
ORDER BY $-.name
4343
"""
4444
Then the result should be, in order, and the columns 0 should be hashed:
4545
| VertexID | name | player.age |

tests/tck/features/fetch/FetchVertices.strVid.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Feature: Fetch String Vertices
4747
"""
4848
$var = GO FROM 'Boris Diaw' over like YIELD like._dst as id;
4949
FETCH PROP ON player $var.id YIELD player.name as name, player.age |
50-
ORDER BY name
50+
ORDER BY $-.name
5151
"""
5252
Then the result should be, in order:
5353
| VertexID | name | player.age |

tests/tck/features/go/GroupbyLimit.feature

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ Feature: Groupby & limit Sentence
212212
| "Grizzlies" | 34 |
213213
| "Raptors" | 34 |
214214
| "Lakers" | 40 |
215+
When executing query:
216+
"""
217+
GROUP BY 1 YIELD 1
218+
"""
219+
Then a SemanticError should be raised at runtime:
215220

216221
Scenario: Groupby with all agg functions
217222
When executing query:
@@ -365,3 +370,10 @@ Feature: Groupby & limit Sentence
365370
| GROUP BY $-.name YIELD $-.name AS name
366371
"""
367372
Then a SemanticError should be raised at runtime:
373+
374+
Scenario: Dangle limit
375+
When executing query:
376+
"""
377+
YIELD 1; LIMIT 1;
378+
"""
379+
Then a SyntaxError should be raised at runtime:

tests/tck/features/go/Orderby.feature

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Feature: Orderby Sentence
77
Background: Prepare space
88
Given a graph with space named "nba"
99

10-
Scenario: Syntax Error
10+
Scenario: Syntax Error or Semantic Error
1111
When executing query:
1212
"""
1313
ORDER BY
@@ -22,7 +22,12 @@ Feature: Orderby Sentence
2222
"""
2323
ORDER BY $-.xx
2424
"""
25-
Then a SyntaxError should be raised at runtime:
25+
Then a SemanticError should be raised at runtime: `$-.xx', not exist prop `xx'
26+
When executing query:
27+
"""
28+
ORDER BY 1
29+
"""
30+
Then a SemanticError should be raised at runtime: Order by with invalid expression `1'
2631
2732
Scenario: Empty Input
2833
When executing query:
@@ -165,6 +170,41 @@ Feature: Orderby Sentence
165170
| "Spurs" |
166171
| "Hornets" |
167172
173+
Scenario: Order by with Variable
174+
When executing query:
175+
"""
176+
$var = GO FROM "Tony Parker" OVER like YIELD like._dst AS dst; ORDER BY $var.dst DESC
177+
"""
178+
Then the result should be, in order, with relax comparison:
179+
| dst |
180+
| "Tim Duncan" |
181+
| "Manu Ginobili" |
182+
| "LaMarcus Aldridge" |
183+
When executing query:
184+
"""
185+
$var = GO FROM "Tony Parker" OVER like YIELD like._dst AS dst, like.likeness AS likeness; ORDER BY $var.dst DESC, $var.likeness
186+
"""
187+
Then the result should be, in order, with relax comparison:
188+
| dst | likeness |
189+
| "Tim Duncan" | 95 |
190+
| "Manu Ginobili" | 95 |
191+
| "LaMarcus Aldridge" | 90 |
192+
When executing query:
193+
"""
194+
$var = GO FROM "Tony Parker" OVER like YIELD like._dst AS dst; ORDER BY $var.dst DESC | FETCH PROP ON * $-.dst
195+
"""
196+
Then the result should be, in order, with relax comparison:
197+
| vertices_ |
198+
| ("Manu Ginobili" :player{age: 41, name: "Manu Ginobili"}) |
199+
| ("Tim Duncan" :bachelor{name: "Tim Duncan", speciality: "psychology"} :player{age: 42, name: "Tim Duncan"}) |
200+
| ("LaMarcus Aldridge" :player{age: 33, name: "LaMarcus Aldridge"}) |
201+
When executing query:
202+
"""
203+
$var = GO FROM "Tony Parker" OVER like YIELD like._dst AS dst;
204+
GO FROM $var.dst OVER like YIELD like._dst AS id | ORDER BY $var.dst, $-.id;
205+
"""
206+
Then a SemanticError should be raised at runtime: Not support both input and variable.
207+
168208
Scenario: Duplicate columns
169209
When executing query:
170210
"""

tests/tck/features/lookup/ByIndex.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ Feature: Lookup by index itself
518518
When executing query:
519519
"""
520520
LOOKUP ON player WHERE player.age < 40
521-
YIELD player.age AS Age, player.name AS Name | order by Age DESC, Name| limit 10
521+
YIELD player.age AS Age, player.name AS Name | order by $-.Age DESC, $-.Name| limit 10
522522
"""
523523
Then the result should be, in order, with relax comparison:
524524
| VertexID | Age | Name |
@@ -535,7 +535,7 @@ Feature: Lookup by index itself
535535
When executing query:
536536
"""
537537
LOOKUP ON player WHERE player.age <= 40
538-
YIELD player.age AS Age, player.name AS Name | order by Age DESC, Name| limit 10
538+
YIELD player.age AS Age, player.name AS Name | order by $-.Age DESC, $-.Name| limit 10
539539
"""
540540
Then the result should be, in order, with relax comparison:
541541
| VertexID | Age | Name |

tests/tck/features/lookup/ByIndex.intVid.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ Feature: Lookup by index itself in integer vid
518518
When executing query:
519519
"""
520520
LOOKUP ON player WHERE player.age < 40
521-
YIELD player.age AS Age, player.name AS Name | order by Age DESC, Name| limit 10
521+
YIELD player.age AS Age, player.name AS Name | order by $-.Age DESC, $-.Name| limit 10
522522
"""
523523
Then the result should be, in order, with relax comparison, and the columns 0 should be hashed:
524524
| VertexID | Age | Name |
@@ -535,7 +535,7 @@ Feature: Lookup by index itself in integer vid
535535
When executing query:
536536
"""
537537
LOOKUP ON player WHERE player.age <= 40
538-
YIELD player.age AS Age, player.name AS Name | order by Age DESC, Name| limit 10
538+
YIELD player.age AS Age, player.name AS Name | order by $-.Age DESC, $-.Name| limit 10
539539
"""
540540
Then the result should be, in order, with relax comparison, and the columns 0 should be hashed:
541541
| VertexID | Age | Name |

0 commit comments

Comments
 (0)