diff --git a/kylin/src/main/java/org/apache/zeppelin/kylin/KylinInterpreter.java b/kylin/src/main/java/org/apache/zeppelin/kylin/KylinInterpreter.java index 5da5ebdd7cc..c831c247721 100755 --- a/kylin/src/main/java/org/apache/zeppelin/kylin/KylinInterpreter.java +++ b/kylin/src/main/java/org/apache/zeppelin/kylin/KylinInterpreter.java @@ -100,10 +100,11 @@ public List completion(String buf, int cursor) { } public HttpResponse prepareRequest(String sql) throws IOException { - String kylinProject = getProject(KYLIN_QUERY_PROJECT); + String kylinProject = getProject(sql); + String kylinSql = getSQL(sql); logger.info("project:" + kylinProject); - logger.info("sql:" + sql); + logger.info("sql:" + kylinSql); logger.info("acceptPartial:" + getProperty(KYLIN_QUERY_ACCEPT_PARTIAL)); logger.info("limit:" + getProperty(KYLIN_QUERY_LIMIT)); logger.info("offset:" + getProperty(KYLIN_QUERY_OFFSET)); @@ -111,7 +112,7 @@ public HttpResponse prepareRequest(String sql) throws IOException { + ":" + getProperty(KYLIN_PASSWORD)).getBytes("UTF-8")); String postContent = new String("{\"project\":" + "\"" + kylinProject + "\"" - + "," + "\"sql\":" + "\"" + sql + "\"" + + "," + "\"sql\":" + "\"" + kylinSql + "\"" + "," + "\"acceptPartial\":" + "\"" + getProperty(KYLIN_QUERY_ACCEPT_PARTIAL) + "\"" + "," + "\"offset\":" + "\"" + getProperty(KYLIN_QUERY_OFFSET) + "\"" + "," + "\"limit\":" + "\"" + getProperty(KYLIN_QUERY_LIMIT) + "\"" + "}"); @@ -132,13 +133,13 @@ public HttpResponse prepareRequest(String sql) throws IOException { } public String getProject(String cmd) { - boolean firstLineIndex = cmd.startsWith("("); + boolean isFirstLineProject = cmd.startsWith("("); - if (firstLineIndex) { - int configStartIndex = cmd.indexOf("("); - int configLastIndex = cmd.indexOf(")"); - if (configStartIndex != -1 && configLastIndex != -1) { - return cmd.substring(configStartIndex + 1, configLastIndex); + if (isFirstLineProject) { + int projectStartIndex = cmd.indexOf("("); + int projectEndIndex = cmd.indexOf(")"); + if (projectStartIndex != -1 && projectEndIndex != -1) { + return cmd.substring(projectStartIndex + 1, projectEndIndex); } else { return getProperty(KYLIN_QUERY_PROJECT); } @@ -147,6 +148,22 @@ public String getProject(String cmd) { } } + public String getSQL(String cmd) { + boolean isFirstLineProject = cmd.startsWith("("); + + if (isFirstLineProject) { + int projectStartIndex = cmd.indexOf("("); + int projectEndIndex = cmd.indexOf(")"); + if (projectStartIndex != -1 && projectEndIndex != -1) { + return cmd.substring(projectEndIndex + 1); + } else { + return cmd; + } + } else { + return cmd; + } + } + private InterpreterResult executeQuery(String sql) throws IOException { HttpResponse response = prepareRequest(sql); diff --git a/kylin/src/test/java/KylinInterpreterTest.java b/kylin/src/test/java/KylinInterpreterTest.java index e11076ac64e..6cee13b9396 100755 --- a/kylin/src/test/java/KylinInterpreterTest.java +++ b/kylin/src/test/java/KylinInterpreterTest.java @@ -64,6 +64,13 @@ public void testWithProject(){ "from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date")); assertEquals("", t.getProject("()\n select a.date,sum(b.measure) as measure " + "from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date")); + assertEquals("\n select a.date,sum(b.measure) as measure from kylin_fact_table a inner join " + + "kylin_lookup_table b on a.date=b.date group by a.date", t.getSQL("(project2)\n select a.date," + + "sum(b.measure) as measure from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date " + + "group by a.date")); + assertEquals("\n select a.date,sum(b.measure) as measure from kylin_fact_table a inner join kylin_lookup_table b " + + "on a.date=b.date group by a.date", t.getSQL("()\n select a.date,sum(b.measure) as measure " + + "from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date")); } private Properties getDefaultProperties(){