Repository: incubator-zeppelin Updated Branches: refs/heads/master d74f98ab3 -> c59229afc
[Zeppelin-628 ] Fix parse propertyKey in interpreter name for Hive ### What is this PR for? Fix bug https://issues.apache.org/jira/browse/ZEPPELIN-628 ### Todos ### How should this be tested? run a query that contains (something)...eg: ``` %hive select max(ss_promo_sk), ss_customer_sk from qhive.tpcds_orc_500.store_sales where ss_sold_date_sk >= 2452640 and ss_customer_sk > 3 and ss_customer_sk < 20 group by ss_customer_sk ``` It is ok if the **propertyKey** is default: ``` PropertyKey: default, SQL command: 'select max(ss_promo_sk), ss_customer_sk from qhive.tpcds_orc_500.store_sales where ss_sold_date_sk >= 2452640 and ss_customer_sk > 3 and ss_customer_sk < 20 group by ss_customer_sk' ``` ### Questions: Does the licenses files need update? no Is there breaking changes for older versions? no Does this needs documentation? no Author: vgmartinez <[email protected]> Closes #668 from vgmartinez/bug_628_for_hive and squashes the following commits: 5953458 [vgmartinez] add control of default in interpret 10bd7e5 [vgmartinez] fix parse propertyKey and add test 85161f9 [vgmartinez] add test for parse propertyKey 470c2ac [vgmartinez] fix parse propertykey in name interpreter Project: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/commit/c59229af Tree: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/tree/c59229af Diff: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/diff/c59229af Branch: refs/heads/master Commit: c59229afc6f8ad2bd59d17f4326f53be2991b66a Parents: d74f98a Author: vgmartinez <[email protected]> Authored: Wed Feb 17 09:56:02 2016 +0100 Committer: Felix Cheung <[email protected]> Committed: Thu Feb 18 16:43:43 2016 -0800 ---------------------------------------------------------------------- .../apache/zeppelin/hive/HiveInterpreter.java | 37 ++++++++++------ .../zeppelin/hive/HiveInterpreterTest.java | 46 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/c59229af/hive/src/main/java/org/apache/zeppelin/hive/HiveInterpreter.java ---------------------------------------------------------------------- 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 912b55e..42916b4 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 class HiveInterpreter extends Interpreter { 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()) { @@ -203,6 +206,10 @@ public class HiveInterpreter extends Interpreter { } else { connection = getConnection(propertyKey); } + + if (connection == null) { + return null; + } Statement statement = connection.createStatement(); if (isStatementClosed(statement)) { @@ -232,6 +239,10 @@ public class HiveInterpreter extends Interpreter { 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 @@ public class HiveInterpreter extends Interpreter { 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 @@ public class HiveInterpreter extends Interpreter { } 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 http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/c59229af/hive/src/test/java/org/apache/zeppelin/hive/HiveInterpreterTest.java ---------------------------------------------------------------------- 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 8f1285d..bc4ec31 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 class HiveInterpreterTest { @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();
