Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions hive/src/main/java/org/apache/zeppelin/hive/HiveInterpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public void close() {

public Connection getConnection(String propertyKey) throws ClassNotFoundException, SQLException {
Connection connection = null;
if (propertyKey == null || propertiesMap.get(propertyKey) == null) {
return null;
}
if (propertyKeyUnusedConnectionListMap.containsKey(propertyKey)) {
ArrayList<Connection> connectionList = propertyKeyUnusedConnectionListMap.get(propertyKey);
if (0 != connectionList.size()) {
Expand Down Expand Up @@ -203,6 +206,10 @@ public Statement getStatement(String propertyKey, String paragraphId)
} else {
connection = getConnection(propertyKey);
}

if (connection == null) {
return null;
}

Statement statement = connection.createStatement();
if (isStatementClosed(statement)) {
Expand Down Expand Up @@ -232,6 +239,10 @@ public InterpreterResult executeSql(String propertyKey, String sql,

Statement statement = getStatement(propertyKey, paragraphId);

if (statement == null) {
return new InterpreterResult(Code.ERROR, "Prefix not found.");
}

statement.setMaxRows(getMaxResult());

StringBuilder msg;
Expand Down Expand Up @@ -315,10 +326,8 @@ private void moveConnectionToUnused(String propertyKey, String paragraphId) {
public InterpreterResult interpret(String cmd, InterpreterContext contextInterpreter) {
String propertyKey = getPropertyKey(cmd);

if (null != propertyKey) {
if (null != propertyKey && !propertyKey.equals(DEFAULT_KEY)) {
cmd = cmd.substring(propertyKey.length() + 2);
} else {
propertyKey = DEFAULT_KEY;
}

cmd = cmd.trim();
Expand All @@ -334,17 +343,19 @@ private int getMaxResult() {
}

public String getPropertyKey(String cmd) {
int firstLineIndex = cmd.indexOf("\n");
if (-1 == firstLineIndex) {
firstLineIndex = cmd.length();
}
int configStartIndex = cmd.indexOf("(");
int configLastIndex = cmd.indexOf(")");
if (configStartIndex != -1 && configLastIndex != -1
&& configLastIndex < firstLineIndex && configLastIndex < firstLineIndex) {
return cmd.substring(configStartIndex + 1, configLastIndex);
boolean firstLineIndex = cmd.startsWith("(");

if (firstLineIndex) {
int configStartIndex = cmd.indexOf("(");
int configLastIndex = cmd.indexOf(")");
if (configStartIndex != -1 && configLastIndex != -1) {
return cmd.substring(configStartIndex + 1, configLastIndex);
} else {
return null;
}
} else {
return DEFAULT_KEY;
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,53 @@ public static void setUp() throws Exception {
@After
public void tearDown() throws Exception {
}

@Test
public void testForParsePropertyKey() throws IOException {
HiveInterpreter t = new HiveInterpreter(new Properties());

assertEquals(t.getPropertyKey("(fake) select max(cant) from test_table where id >= 2452640"),
"fake");

assertEquals(t.getPropertyKey("() select max(cant) from test_table where id >= 2452640"),
"");

assertEquals(t.getPropertyKey(")fake( select max(cant) from test_table where id >= 2452640"),
"default");

// when you use a %hive(prefix1), prefix1 is the propertyKey as form part of the cmd string
assertEquals(t.getPropertyKey("(prefix1)\n select max(cant) from test_table where id >= 2452640"),
"prefix1");

assertEquals(t.getPropertyKey("(prefix2) select max(cant) from test_table where id >= 2452640"),
"prefix2");

// when you use a %hive, prefix is the default
assertEquals(t.getPropertyKey("select max(cant) from test_table where id >= 2452640"),
"default");
}

@Test
public void testForMapPrefix() throws SQLException, IOException {
Properties properties = new Properties();
properties.setProperty("common.max_count", "1000");
properties.setProperty("common.max_retry", "3");
properties.setProperty("default.driver", "org.h2.Driver");
properties.setProperty("default.url", getJdbcConnection());
properties.setProperty("default.user", "");
properties.setProperty("default.password", "");
HiveInterpreter t = new HiveInterpreter(properties);
t.open();

String sqlQuery = "(fake) select * from test_table";

InterpreterResult interpreterResult = t.interpret(sqlQuery, new InterpreterContext("", "1", "","", null,null,null,null,null,null));

// if prefix not found return ERROR and Prefix not found.
assertEquals(InterpreterResult.Code.ERROR, interpreterResult.code());
assertEquals("Prefix not found.", interpreterResult.message());
}

@Test
public void readTest() throws IOException {
Properties properties = new Properties();
Expand Down