Skip to content

Commit 1af8493

Browse files
committed
[ZEPPELIN-2078] bug fix in getProject in Kylin interpreter
1 parent 4ef1495 commit 1af8493

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

kylin/src/main/java/org/apache/zeppelin/kylin/KylinInterpreter.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,19 @@ public List<InterpreterCompletion> completion(String buf, int cursor) {
100100
}
101101

102102
public HttpResponse prepareRequest(String sql) throws IOException {
103-
String kylinProject = getProject(KYLIN_QUERY_PROJECT);
103+
String kylinProject = getProject(sql);
104+
String kylinSql = getSQL(sql);
104105

105106
logger.info("project:" + kylinProject);
106-
logger.info("sql:" + sql);
107+
logger.info("sql:" + kylinSql);
107108
logger.info("acceptPartial:" + getProperty(KYLIN_QUERY_ACCEPT_PARTIAL));
108109
logger.info("limit:" + getProperty(KYLIN_QUERY_LIMIT));
109110
logger.info("offset:" + getProperty(KYLIN_QUERY_OFFSET));
110111
byte[] encodeBytes = Base64.encodeBase64(new String(getProperty(KYLIN_USERNAME)
111112
+ ":" + getProperty(KYLIN_PASSWORD)).getBytes("UTF-8"));
112113

113114
String postContent = new String("{\"project\":" + "\"" + kylinProject + "\""
114-
+ "," + "\"sql\":" + "\"" + sql + "\""
115+
+ "," + "\"sql\":" + "\"" + kylinSql + "\""
115116
+ "," + "\"acceptPartial\":" + "\"" + getProperty(KYLIN_QUERY_ACCEPT_PARTIAL) + "\""
116117
+ "," + "\"offset\":" + "\"" + getProperty(KYLIN_QUERY_OFFSET) + "\""
117118
+ "," + "\"limit\":" + "\"" + getProperty(KYLIN_QUERY_LIMIT) + "\"" + "}");
@@ -132,13 +133,13 @@ public HttpResponse prepareRequest(String sql) throws IOException {
132133
}
133134

134135
public String getProject(String cmd) {
135-
boolean firstLineIndex = cmd.startsWith("(");
136+
boolean isFirstLineProject = cmd.startsWith("(");
136137

137-
if (firstLineIndex) {
138-
int configStartIndex = cmd.indexOf("(");
139-
int configLastIndex = cmd.indexOf(")");
140-
if (configStartIndex != -1 && configLastIndex != -1) {
141-
return cmd.substring(configStartIndex + 1, configLastIndex);
138+
if (isFirstLineProject) {
139+
int projectStartIndex = cmd.indexOf("(");
140+
int projectEndIndex = cmd.indexOf(")");
141+
if (projectStartIndex != -1 && projectEndIndex != -1) {
142+
return cmd.substring(projectStartIndex + 1, projectEndIndex);
142143
} else {
143144
return getProperty(KYLIN_QUERY_PROJECT);
144145
}
@@ -147,6 +148,22 @@ public String getProject(String cmd) {
147148
}
148149
}
149150

151+
public String getSQL(String cmd) {
152+
boolean isFirstLineProject = cmd.startsWith("(");
153+
154+
if (isFirstLineProject) {
155+
int projectStartIndex = cmd.indexOf("(");
156+
int projectEndIndex = cmd.indexOf(")");
157+
if (projectStartIndex != -1 && projectEndIndex != -1) {
158+
return cmd.substring(projectEndIndex + 1);
159+
} else {
160+
return cmd;
161+
}
162+
} else {
163+
return cmd;
164+
}
165+
}
166+
150167
private InterpreterResult executeQuery(String sql) throws IOException {
151168

152169
HttpResponse response = prepareRequest(sql);

kylin/src/test/java/KylinInterpreterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ public void testWithProject(){
6464
"from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date"));
6565
assertEquals("", t.getProject("()\n select a.date,sum(b.measure) as measure " +
6666
"from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date"));
67+
assertEquals("\n select a.date,sum(b.measure) as measure from kylin_fact_table a inner join " +
68+
"kylin_lookup_table b on a.date=b.date group by a.date", t.getSQL("(project2)\n select a.date," +
69+
"sum(b.measure) as measure from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date " +
70+
"group by a.date"));
71+
assertEquals("\n select a.date,sum(b.measure) as measure from kylin_fact_table a inner join kylin_lookup_table b " +
72+
"on a.date=b.date group by a.date", t.getSQL("()\n select a.date,sum(b.measure) as measure " +
73+
"from kylin_fact_table a inner join kylin_lookup_table b on a.date=b.date group by a.date"));
6774
}
6875

6976
private Properties getDefaultProperties(){

0 commit comments

Comments
 (0)