Repository: incubator-zeppelin Updated Branches: refs/heads/master 39417c073 -> 9a610a7a2
Zeppelin 639 ### What is this PR for? JDBCInterpreter should check for a null object before simply calling `getString`. This will prevent poorly implemented JDBC drivers from causing a NullPointerException. ### What type of PR is it? Improvement ### Todos ### Is there a relevant Jira issue? https://issues.apache.org/jira/browse/ZEPPELIN-639 ### How should this be tested? Run the `testSelectQueryWithNull` test included with this PR. ### Questions: * Does the licenses files need update? No * Is there breaking changes for older versions? No * Does this needs documentation? No Author: Chris Matta <[email protected]> Author: vgmartinez <[email protected]> Closes #684 from cjmatta/ZEPPELIN-639 and squashes the following commits: 875fdc9 [Chris Matta] Merge branch 'master' of https://github.com/apache/incubator-zeppelin into ZEPPELIN-639 1475cb9 [Chris Matta] replacing modified note.json with master note.json 15d3e69 [Chris Matta] Revert "add test for parse propertyKey" 5d6df65 [Chris Matta] Revert "fix parse property in interprete name" 901a59c [Chris Matta] Merged in upstream changes and fixed test case 0f9737d [Chris Matta] Merge in upstream master ed8c228 [Chris Matta] Zeppelin 639 ### What is this PR for? JDBCInterpreter should check for a null object before simply calling . This will prevent poorly implemented JDBC drivers from causing a NullPointerException. 4247356 [Chris Matta] Added check for null in resultSet. Added tests. 6db8d0a [Chris Matta] Merge branch 'master' into bug_628 f228b84 [vgmartinez] add test for parse propertyKey 0a0ad72 [vgmartinez] fix parse property in interprete name Project: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/commit/9a610a7a Tree: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/tree/9a610a7a Diff: http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/diff/9a610a7a Branch: refs/heads/master Commit: 9a610a7a29a2c2d0a4d6f4cc4c3f7df0b4c912b6 Parents: 39417c0 Author: Chris Matta <[email protected]> Authored: Tue Feb 16 10:06:21 2016 -0500 Committer: Jongyoul Lee <[email protected]> Committed: Sun Feb 21 00:13:56 2016 +0900 ---------------------------------------------------------------------- .../apache/zeppelin/jdbc/JDBCInterpreter.java | 10 +++++- .../zeppelin/jdbc/JDBCInterpreterTest.java | 36 +++++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/9a610a7a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java ---------------------------------------------------------------------- diff --git a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java index 0f74e6e..0c53281 100644 --- a/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java +++ b/jdbc/src/main/java/org/apache/zeppelin/jdbc/JDBCInterpreter.java @@ -305,7 +305,15 @@ public class JDBCInterpreter extends Interpreter { int displayRowCount = 0; while (resultSet.next() && displayRowCount < getMaxResult()) { for (int i = 1; i < md.getColumnCount() + 1; i++) { - msg.append(replaceReservedChars(isTableType, resultSet.getString(i))); + Object resultObject; + String resultValue; + resultObject = resultSet.getObject(i); + if (resultObject == null) { + resultValue = "null"; + } else { + resultValue = resultSet.getString(i); + } + msg.append(replaceReservedChars(isTableType, resultValue)); if (i != md.getColumnCount()) { msg.append(TAB); } http://git-wip-us.apache.org/repos/asf/incubator-zeppelin/blob/9a610a7a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java ---------------------------------------------------------------------- diff --git a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java index 302f490..18e8b33 100644 --- a/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java +++ b/jdbc/src/test/java/org/apache/zeppelin/jdbc/JDBCInterpreterTest.java @@ -26,10 +26,7 @@ import static org.apache.zeppelin.jdbc.JDBCInterpreter.COMMON_MAX_LINE; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; +import java.sql.*; import java.util.Properties; import org.apache.zeppelin.interpreter.InterpreterContext; @@ -64,9 +61,10 @@ public class JDBCInterpreterTest extends BasicJDBCTestCaseAdapter { statement.execute( "DROP TABLE IF EXISTS test_table; " + "CREATE TABLE test_table(id varchar(255), name varchar(255));"); - statement.execute( - "insert into test_table(id, name) values ('a', 'a_name'),('b', 'b_name');" - ); + + PreparedStatement insertStatement = connection.prepareStatement("insert into test_table(id, name) values ('a', 'a_name'),('b', 'b_name'),('c', ?);"); + insertStatement.setString(1, null); + insertStatement.execute(); } @@ -139,7 +137,7 @@ public class JDBCInterpreterTest extends BasicJDBCTestCaseAdapter { JDBCInterpreter t = new JDBCInterpreter(properties); t.open(); - String sqlQuery = "select * from test_table"; + String sqlQuery = "select * from test_table WHERE ID in ('a', 'b')"; InterpreterResult interpreterResult = t.interpret(sqlQuery, new InterpreterContext("", "1", "","", null,null,null,null,null,null)); @@ -149,6 +147,28 @@ public class JDBCInterpreterTest extends BasicJDBCTestCaseAdapter { } @Test + public void testSelectQueryWithNull() 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", ""); + JDBCInterpreter t = new JDBCInterpreter(properties); + t.open(); + + String sqlQuery = "select * from test_table WHERE ID = 'c'"; + + InterpreterResult interpreterResult = t.interpret(sqlQuery, new InterpreterContext("", "1", "","", null,null,null,null,null,null)); + + assertEquals(InterpreterResult.Code.SUCCESS, interpreterResult.code()); + assertEquals(InterpreterResult.Type.TABLE, interpreterResult.type()); + assertEquals("ID\tNAME\nc\tnull\n", interpreterResult.message()); + } + + + @Test public void testSelectQueryMaxResult() throws SQLException, IOException { Properties properties = new Properties();
