Skip to content

Commit

Permalink
[#270] Allow setting of user variables in SQL page
Browse files Browse the repository at this point in the history
  • Loading branch information
pjeli committed Aug 15, 2019
1 parent 9268948 commit 99d60b9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ public void init(
String sum = sqlParser.getSum();
String find = sqlParser.getFind();
Integer limit = sqlParser.getLimit();
Integer parentDirDepth = sqlParser.getParentDirDepth();
int parentDirDepth = sqlParser.getParentDirDepth();
String timeRange = sqlParser.getTimeRange();
Boolean sortAscending = sqlParser.getSortAscending();
Boolean sortDescending = sqlParser.getSortDescending();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.SetStatement;
import net.sf.jsqlparser.statement.StatementVisitorAdapter;
import net.sf.jsqlparser.statement.select.FromItemVisitorAdapter;
import net.sf.jsqlparser.statement.select.GroupByElement;
Expand All @@ -56,11 +57,34 @@ public class INodeSqlStatementVisitor extends StatementVisitorAdapter {
protected Integer limit;
protected Boolean sortAscending;
protected Boolean sortDescending;
protected Integer parentDirDepth;
protected String timeRange;

INodeSqlStatementVisitor() {
filters = new LinkedList<>();
}

@Override
public void visit(SetStatement setStatement) {
String name = setStatement.getName();
switch (name) {
case "parentDirDepth":
parentDirDepth =
Integer.parseInt(setStatement.getExpression().getASTNode().jjtGetValue().toString());
return;
case "timeRange":
timeRange = setStatement.getExpression().getASTNode().jjtGetValue().toString();
return;
default:
throw new IllegalArgumentException(
"Tried to set unknown variable: "
+ name
+ ".\n"
+ "Available options are parentDirDepth = <int> and "
+ "timeRange = <daily|weekly|monthly|yearly>.");
}
}

@Override
public void visit(Select select) {
select
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statements;
import org.apache.hadoop.hdfs.server.namenode.Constants;
import org.apache.hadoop.hdfs.server.namenode.Constants.INodeSet;

Expand All @@ -43,12 +42,8 @@ public class SqlParser {
private Integer limit;
private Boolean sortAscending;
private Boolean sortDescending;

private CCJSqlParserManager parser;

public SqlParser() {
parser = new CCJSqlParserManager();
}
private Integer parentDirDepth;
private String timeRange;

public String showTables() {
Gson gson = new Gson();
Expand Down Expand Up @@ -118,9 +113,9 @@ public String describeInJson(String describe) {
* @throws JSQLParserException - if sql is unreadable
*/
public void parse(String statement) throws JSQLParserException {
Statement parse = parser.parse(new StringReader(statement));
Statements statements = CCJSqlParserUtil.parseStatements(statement);
INodeSqlStatementVisitor inodeVisitor = new INodeSqlStatementVisitor();
parse.accept(inodeVisitor);
statements.accept(inodeVisitor);
set = inodeVisitor.set.toLowerCase();
filters = String.join(",", inodeVisitor.filters);
sum = inodeVisitor.sum;
Expand All @@ -129,6 +124,8 @@ public void parse(String statement) throws JSQLParserException {
limit = inodeVisitor.limit;
sortAscending = inodeVisitor.sortAscending;
sortDescending = inodeVisitor.sortDescending;
parentDirDepth = inodeVisitor.parentDirDepth;
timeRange = inodeVisitor.timeRange;
}

public String getINodeSet() {
Expand All @@ -155,16 +152,12 @@ public Integer getLimit() {
return limit;
}

public boolean getUseRawTimestamps() {
return false;
}

public Integer getParentDirDepth() {
return 3;
public int getParentDirDepth() {
return (parentDirDepth == null) ? 3 : parentDirDepth;
}

public String getTimeRange() {
return "monthly";
return timeRange;
}

public Boolean getSortAscending() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2340,7 +2340,7 @@ public Response sql(MultivaluedMap<String, String> formData) {
String sum = sqlParser.getSum();
String find = sqlParser.getFind();
Integer limit = sqlParser.getLimit();
Integer parentDirDepth = sqlParser.getParentDirDepth();
int parentDirDepth = sqlParser.getParentDirDepth();
String timeRange = sqlParser.getTimeRange();
Boolean sortAscending = sqlParser.getSortAscending();
Boolean sortDescending = sqlParser.getSortDescending();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/webapps/nna/sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ <h2 class="text-center">NameNode Analytics SQL Queries</h2>
$('#dataTable').DataTable().destroy();
let table = $('#dataTable').DataTable( {
data: flattenObject(response),
"order": [],
"iDisplayLength": 100,
"aLengthMenu": [[10, 50, 100, -1], [10, 50, 100, "All"]],
"dom": 'Bfrtipl',
Expand Down

0 comments on commit 99d60b9

Please sign in to comment.