From 99d60b9c0a5a17328d18c0be17524ab0d9de2b16 Mon Sep 17 00:00:00 2001 From: Plamen Jeliazkov Date: Thu, 15 Aug 2019 11:11:18 -0700 Subject: [PATCH] [#270] Allow setting of user variables in SQL page --- .../namenode/analytics/WebServerMain.java | 2 +- .../sql/INodeSqlStatementVisitor.java | 24 +++++++++++++++ .../namenode/analytics/sql/SqlParser.java | 29 +++++++------------ .../web/NamenodeAnalyticsMethods.java | 2 +- src/main/resources/webapps/nna/sql.html | 1 + 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/WebServerMain.java b/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/WebServerMain.java index 9f59d843..6f9a4242 100644 --- a/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/WebServerMain.java +++ b/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/WebServerMain.java @@ -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(); diff --git a/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/sql/INodeSqlStatementVisitor.java b/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/sql/INodeSqlStatementVisitor.java index a81519c2..f3767713 100644 --- a/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/sql/INodeSqlStatementVisitor.java +++ b/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/sql/INodeSqlStatementVisitor.java @@ -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; @@ -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 = and " + + "timeRange = ."); + } + } + @Override public void visit(Select select) { select diff --git a/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/sql/SqlParser.java b/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/sql/SqlParser.java index fc1711a7..db8de19a 100644 --- a/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/sql/SqlParser.java +++ b/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/sql/SqlParser.java @@ -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; @@ -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(); @@ -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; @@ -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() { @@ -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() { diff --git a/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/web/NamenodeAnalyticsMethods.java b/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/web/NamenodeAnalyticsMethods.java index fc097529..f2e411dd 100644 --- a/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/web/NamenodeAnalyticsMethods.java +++ b/src/main/java/org/apache/hadoop/hdfs/server/namenode/analytics/web/NamenodeAnalyticsMethods.java @@ -2340,7 +2340,7 @@ public Response sql(MultivaluedMap 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(); diff --git a/src/main/resources/webapps/nna/sql.html b/src/main/resources/webapps/nna/sql.html index c4b62b6f..3760ea4a 100644 --- a/src/main/resources/webapps/nna/sql.html +++ b/src/main/resources/webapps/nna/sql.html @@ -122,6 +122,7 @@

NameNode Analytics SQL Queries

$('#dataTable').DataTable().destroy(); let table = $('#dataTable').DataTable( { data: flattenObject(response), + "order": [], "iDisplayLength": 100, "aLengthMenu": [[10, 50, 100, -1], [10, 50, 100, "All"]], "dom": 'Bfrtipl',