diff --git a/hive/src/main/java/org/apache/zeppelin/hive/HiveInterpreter.java b/hive/src/main/java/org/apache/zeppelin/hive/HiveInterpreter.java index 912b55e2651..42916b47452 100644 --- a/hive/src/main/java/org/apache/zeppelin/hive/HiveInterpreter.java +++ b/hive/src/main/java/org/apache/zeppelin/hive/HiveInterpreter.java @@ -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 connectionList = propertyKeyUnusedConnectionListMap.get(propertyKey); if (0 != connectionList.size()) { @@ -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)) { @@ -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; @@ -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(); @@ -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 diff --git a/hive/src/test/java/org/apache/zeppelin/hive/HiveInterpreterTest.java b/hive/src/test/java/org/apache/zeppelin/hive/HiveInterpreterTest.java index 8f1285df98f..bc4ec31ab46 100644 --- a/hive/src/test/java/org/apache/zeppelin/hive/HiveInterpreterTest.java +++ b/hive/src/test/java/org/apache/zeppelin/hive/HiveInterpreterTest.java @@ -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();