Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLParser: Introduce LIMIT clause #544

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions src/parser/sql/SQLGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ select:
whereClause?
groupByClause?
orderByClause?
limitClause?
;

subquery:
Expand Down Expand Up @@ -78,6 +79,9 @@ orderByClause:
SQL_ORDER SQL_BY selectIdent orderInformation
(',' selectIdent orderInformation)*;

limitClause:
SQL_LIMIT limit=INT_LITERAL;

orderInformation:
(asc=SQL_ASC|desc=SQL_DESC)?;

Expand Down Expand Up @@ -139,6 +143,7 @@ SQL_ASC: A S C;
SQL_DESC: D E S C;
SQL_AND: A N D;
SQL_OR: O R;
SQL_LIMIT: L I M I T;

fragment A: [aA];
fragment B: [bB];
Expand Down
25 changes: 25 additions & 0 deletions src/parser/sql/SQLVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <ir/daphneir/Daphne.h>
#include <parser/sql/SQLVisitor.h>
#include "antlr4-runtime.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/OpDefinition.h"

#include <stdexcept>
Expand Down Expand Up @@ -504,6 +505,10 @@ antlrcpp::Any SQLVisitor::visitSelect(
throw std::runtime_error(err_msg.str());
}
}
if(ctx->limitClause()) {
currentFrame = res;
res = utils.valueOrError(visit(ctx->limitClause()));
}
return res;
}

Expand Down Expand Up @@ -826,6 +831,26 @@ antlrcpp::Any SQLVisitor::visitOrderByClause(
);
}

antlrcpp::Any SQLVisitor::visitLimitClause(
SQLGrammarParser::LimitClauseContext *ctx
)
{
mlir::Location loc = utils.getLoc(ctx->start);

mlir::Value start = builder.create<mlir::daphne::ConstantOp>(
loc, utils.sizeType, builder.getIndexAttr(0)
);

mlir::Value end = builder.create<mlir::daphne::ConstantOp>(
loc, utils.sizeType, builder.getIndexAttr(stoi(ctx->limit->getText()))
);

mlir::daphne::FrameType resType = currentFrame.getType().dyn_cast<mlir::daphne::FrameType>().withSameColumnTypes();
return static_cast<mlir::Value>(
builder.create<mlir::daphne::SliceRowOp>(loc, resType, currentFrame, start, end)
);
}

antlrcpp::Any SQLVisitor::visitOrderInformation(
SQLGrammarParser::OrderInformationContext * ctx
)
Expand Down
3 changes: 3 additions & 0 deletions src/parser/sql/SQLVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ class SQLVisitor : public SQLGrammarVisitor {
//orderByClause
antlrcpp::Any visitOrderByClause(SQLGrammarParser::OrderByClauseContext * ctx) override;

//limitClause
antlrcpp::Any visitLimitClause(SQLGrammarParser::LimitClauseContext * ctx) override;

//orderInformation
antlrcpp::Any visitOrderInformation(SQLGrammarParser::OrderInformationContext * ctx) override;

Expand Down
1 change: 1 addition & 0 deletions test/api/cli/sql/SQLTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ MAKE_TEST_CASE("agg_sum", 1)

MAKE_TEST_CASE("reuseString", 2)

MAKE_TEST_CASE("limit", 1)

// TODO Use the scripts testing failure cases.
13 changes: 13 additions & 0 deletions test/api/cli/sql/limit_1.daphne
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Selecting columns a and b from frame and only output the first 5 rows.

f = createFrame(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[5, 4, 3, 2, 1, 9, 8, 7, 6, 5],
"a", "b"
);

registerView("f", f);

res = sql("SELECT f.a, f.b FROM f LIMIT 5;");

print(res);
6 changes: 6 additions & 0 deletions test/api/cli/sql/limit_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Frame(5x2, [f.a:int64_t, f.b:int64_t])
0 5
1 4
2 3
3 2
4 1