Skip to content

Commit

Permalink
HIVE-14382: Improve the Functionality of Reverse FOR Statement (Akash…
Browse files Browse the repository at this point in the history
… Sethi, reviewed by Dmitry Tolpeko)
  • Loading branch information
dmtolpeko committed Aug 4, 2016
1 parent 3182378 commit 5193064
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
39 changes: 27 additions & 12 deletions hplsql/src/main/java/org/apache/hive/hplsql/Stmt.java
Original file line number Diff line number Diff line change
Expand Up @@ -976,24 +976,39 @@ public Integer forRange(HplsqlParser.For_range_stmtContext ctx) {
int end = evalPop(ctx.expr(1)).intValue();
int step = evalPop(ctx.expr(2), 1L).intValue();
exec.enterScope(Scope.Type.LOOP);
Var index = new Var(ctx.L_ID().getText(), new Long(start));
Var index = setIndex(start, end, ctx);
exec.addVariable(index);
if (ctx.T_REVERSE() == null) {
for (int i = start; i <= end; i += step) {
visit(ctx.block());
index.increment(new Long(step));
}
} else {
for (int i = start; i >= end; i -= step) {
visit(ctx.block());
index.decrement(new Long(step));
}
}
updateIndex(step, index, ctx);
}
exec.leaveScope();
trace(ctx, "FOR RANGE - LEFT");
return 0;
}

}

/**
* To set the Value index for FOR Statement
*/
private Var setIndex(int start, int end, HplsqlParser.For_range_stmtContext ctx) {

if (ctx.T_REVERSE() == null)
return new Var(ctx.L_ID().getText(), new Long(start));
else
return new Var(ctx.L_ID().getText(), new Long(end));
}

/**
* To update the value of index for FOR Statement
*/
private void updateIndex(int step, Var index, HplsqlParser.For_range_stmtContext ctx) {

if (ctx.T_REVERSE() == null)
index.increment(new Long(step));
else
index.decrement(new Long(step));
}

/**
* EXEC, EXECUTE and EXECUTE IMMEDIATE statement to execute dynamic SQL or stored procedure
*/
Expand Down
2 changes: 1 addition & 1 deletion hplsql/src/test/queries/local/for_range.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ END LOOP;

PRINT i;

FOR i IN REVERSE 10..1 LOOP
FOR i IN REVERSE 1..10 LOOP
PRINT i;
END LOOP;

Expand Down

0 comments on commit 5193064

Please sign in to comment.