diff --git jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java index 4cda742..b693e93 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java @@ -59,6 +59,7 @@ * all Hive result sets. */ public abstract class HiveBaseResultSet implements ResultSet { + protected Statement statement = null; protected SQLWarning warningChain = null; protected boolean wasNull = false; protected TRow row; @@ -615,7 +616,7 @@ public short getShort(String columnName) throws SQLException { } public Statement getStatement() throws SQLException { - throw new SQLException("Method not supported"); + return this.statement; } /** diff --git jdbc/src/java/org/apache/hive/jdbc/HiveCallableStatement.java jdbc/src/java/org/apache/hive/jdbc/HiveCallableStatement.java index 2bc4317..d04f786 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveCallableStatement.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveCallableStatement.java @@ -46,12 +46,13 @@ * */ public class HiveCallableStatement implements java.sql.CallableStatement { + private final Connection connection; /** * */ - public HiveCallableStatement() { - // TODO Auto-generated constructor stub + public HiveCallableStatement(Connection connection) { + this.connection = connection; } /* @@ -1387,7 +1388,7 @@ public boolean execute() throws SQLException { */ public ResultSet executeQuery() throws SQLException { - return new HiveQueryResultSet.Builder().build(); + return new HiveQueryResultSet.Builder(this).build(); } /* @@ -2169,8 +2170,7 @@ public int executeUpdate(String sql, String[] columnNames) */ public Connection getConnection() throws SQLException { - // TODO Auto-generated method stub - throw new SQLException("Method not supported"); + return this.connection; } /* diff --git jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java index f155686..5a6ce44 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java @@ -415,7 +415,7 @@ public Statement createStatement() throws SQLException { if (isClosed) { throw new SQLException("Can't create Statement, connection is closed"); } - return new HiveStatement(client, sessHandle); + return new HiveStatement(this, client, sessHandle); } /* @@ -514,7 +514,7 @@ public int getHoldability() throws SQLException { */ public DatabaseMetaData getMetaData() throws SQLException { - return new HiveDatabaseMetaData(client, sessHandle); + return new HiveDatabaseMetaData(this, client, sessHandle); } public int getNetworkTimeout() throws SQLException { @@ -643,7 +643,7 @@ public CallableStatement prepareCall(String sql, int resultSetType, */ public PreparedStatement prepareStatement(String sql) throws SQLException { - return new HivePreparedStatement(client, sessHandle, sql); + return new HivePreparedStatement(this, client, sessHandle, sql); } /* @@ -654,7 +654,7 @@ public PreparedStatement prepareStatement(String sql) throws SQLException { public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { - return new HivePreparedStatement(client, sessHandle, sql); + return new HivePreparedStatement(this, client, sessHandle, sql); } /* @@ -690,7 +690,7 @@ public PreparedStatement prepareStatement(String sql, String[] columnNames) public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { - return new HivePreparedStatement(client, sessHandle, sql); + return new HivePreparedStatement(this, client, sessHandle, sql); } /* diff --git jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java index 7a3170a..c447d44 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveDatabaseMetaData.java @@ -55,6 +55,7 @@ */ public class HiveDatabaseMetaData implements DatabaseMetaData { + private final HiveConnection connection; private final TCLIService.Iface client; private final TSessionHandle sessHandle; private static final String CATALOG_SEPARATOR = "."; @@ -67,7 +68,9 @@ /** * */ - public HiveDatabaseMetaData(TCLIService.Iface client, TSessionHandle sessHandle) { + public HiveDatabaseMetaData(HiveConnection connection, TCLIService.Iface client, + TSessionHandle sessHandle) { + this.connection = connection; this.client = client; this.sessHandle = sessHandle; } @@ -128,7 +131,7 @@ public ResultSet getCatalogs() throws SQLException { } Utils.verifySuccess(catalogResp.getStatus()); - return new HiveQueryResultSet.Builder() + return new HiveQueryResultSet.Builder(null) .setClient(client) .setSessionHandle(sessHandle) .setStmtHandle(catalogResp.getOperationHandle()) @@ -212,7 +215,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, } Utils.verifySuccess(colResp.getStatus()); // build the resultset from response - return new HiveQueryResultSet.Builder() + return new HiveQueryResultSet.Builder(null) .setClient(client) .setSessionHandle(sessHandle) .setStmtHandle(colResp.getOperationHandle()) @@ -241,7 +244,7 @@ public int compare(JdbcColumn o1, JdbcColumn o2) { } public Connection getConnection() throws SQLException { - throw new SQLException("Method not supported"); + return this.connection; } public ResultSet getCrossReference(String primaryCatalog, @@ -327,7 +330,7 @@ public ResultSet getFunctions(String catalogName, String schemaPattern, String f } Utils.verifySuccess(funcResp.getStatus()); - return new HiveQueryResultSet.Builder() + return new HiveQueryResultSet.Builder(null) .setClient(client) .setSessionHandle(sessHandle) .setStmtHandle(funcResp.getOperationHandle()) @@ -340,7 +343,7 @@ public String getIdentifierQuoteString() throws SQLException { public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException { - return new HiveQueryResultSet.Builder() + return new HiveQueryResultSet.Builder(null) .setClient(client) .setEmptyResultSet(true) .setSchema( @@ -482,7 +485,7 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException { // Hive doesn't support primary keys // using local schema with empty resultset - return new HiveQueryResultSet.Builder().setClient(client).setEmptyResultSet(true). + return new HiveQueryResultSet.Builder(null).setClient(client).setEmptyResultSet(true). setSchema(Arrays.asList("TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME", "COLUMN_NAME", "KEY_SEQ", "PK_NAME" ), Arrays.asList("STRING", "STRING", "STRING", "STRING", "INT", "STRING")) .build(); @@ -493,7 +496,7 @@ public ResultSet getProcedureColumns(String catalog, String schemaPattern, throws SQLException { // Hive doesn't support primary keys // using local schema with empty resultset - return new HiveQueryResultSet.Builder().setClient(client).setEmptyResultSet(true). + return new HiveQueryResultSet.Builder(null).setClient(client).setEmptyResultSet(true). setSchema( Arrays.asList("PROCEDURE_CAT", "PROCEDURE_SCHEM", "PROCEDURE_NAME", "COLUMN_NAME", "COLUMN_TYPE", "DATA_TYPE", "TYPE_NAME", "PRECISION", "LENGTH", "SCALE", "RADIX", "NULLABLE", "REMARKS", @@ -514,7 +517,7 @@ public ResultSet getProcedures(String catalog, String schemaPattern, String procedureNamePattern) throws SQLException { // Hive doesn't support primary keys // using local schema with empty resultset - return new HiveQueryResultSet.Builder().setClient(client).setEmptyResultSet(true). + return new HiveQueryResultSet.Builder(null).setClient(client).setEmptyResultSet(true). setSchema( Arrays.asList("PROCEDURE_CAT", "PROCEDURE_SCHEM", "PROCEDURE_NAME", "RESERVERD", "RESERVERD", "RESERVERD", "REMARKS", "PROCEDURE_TYPE", "SPECIFIC_NAME"), @@ -568,7 +571,7 @@ public ResultSet getSchemas(String catalog, String schemaPattern) } Utils.verifySuccess(schemaResp.getStatus()); - return new HiveQueryResultSet.Builder() + return new HiveQueryResultSet.Builder(null) .setClient(client) .setSessionHandle(sessHandle) .setStmtHandle(schemaResp.getOperationHandle()) @@ -612,7 +615,7 @@ public ResultSet getTableTypes() throws SQLException { } Utils.verifySuccess(tableTypeResp.getStatus()); - return new HiveQueryResultSet.Builder() + return new HiveQueryResultSet.Builder(null) .setClient(client) .setSessionHandle(sessHandle) .setStmtHandle(tableTypeResp.getOperationHandle()) @@ -645,7 +648,7 @@ public ResultSet getTables(String catalog, String schemaPattern, } Utils.verifySuccess(getTableResp.getStatus()); - return new HiveQueryResultSet.Builder() + return new HiveQueryResultSet.Builder(null) .setClient(client) .setSessionHandle(sessHandle) .setStmtHandle(getTableResp.getOperationHandle()) @@ -701,7 +704,7 @@ public ResultSet getTypeInfo() throws SQLException { throw new SQLException(e.getMessage(), "08S01", e); } Utils.verifySuccess(getTypeInfoResp.getStatus()); - return new HiveQueryResultSet.Builder() + return new HiveQueryResultSet.Builder(null) .setClient(client) .setSessionHandle(sessHandle) .setStmtHandle(getTypeInfoResp.getOperationHandle()) @@ -1123,7 +1126,7 @@ public boolean isWrapperFor(Class iface) throws SQLException { } public static void main(String[] args) throws SQLException { - HiveDatabaseMetaData meta = new HiveDatabaseMetaData(null, null); + HiveDatabaseMetaData meta = new HiveDatabaseMetaData(null, null, null); System.out.println("DriverName: " + meta.getDriverName()); System.out.println("DriverVersion: " + meta.getDriverVersion()); } diff --git jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java index 8fb23cc..3116100 100644 --- jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java +++ jdbc/src/java/org/apache/hive/jdbc/HivePreparedStatement.java @@ -55,9 +55,9 @@ */ private final HashMap parameters=new HashMap(); - public HivePreparedStatement(TCLIService.Iface client, TSessionHandle sessHandle, - String sql) { - super(client, sessHandle); + public HivePreparedStatement(HiveConnection connection, TCLIService.Iface client, + TSessionHandle sessHandle, String sql) { + super(connection, client, sessHandle); this.sql = sql; } diff --git jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java index 131a522..114cd6d 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveQueryResultSet.java @@ -21,6 +21,7 @@ import static org.apache.hive.service.cli.thrift.TCLIServiceConstants.TYPE_NAMES; import java.sql.ResultSetMetaData; +import java.sql.Statement; import java.sql.SQLException; import java.util.ArrayList; import java.util.Iterator; @@ -55,7 +56,6 @@ private TCLIService.Iface client; private TOperationHandle stmtHandle; - private HiveStatement hiveStatement; private TSessionHandle sessHandle; private int maxRows; private int fetchSize; @@ -68,10 +68,10 @@ public static class Builder { + private final Statement statement; private TCLIService.Iface client = null; private TOperationHandle stmtHandle = null; private TSessionHandle sessHandle = null; - private HiveStatement hiveStatement = null; /** * Sets the limit for the maximum number of rows that any ResultSet object produced by this @@ -86,6 +86,10 @@ private int fetchSize = 50; private boolean emptyResultSet = false; + public Builder(Statement statement) { + this.statement = statement; + } + public Builder setClient(TCLIService.Iface client) { this.client = client; return this; @@ -101,11 +105,6 @@ public Builder setSessionHandle(TSessionHandle sessHandle) { return this; } - public Builder setHiveStatement(HiveStatement hiveStatement) { - this.hiveStatement = hiveStatement; - return this; - } - public Builder setMaxRows(int maxRows) { this.maxRows = maxRows; return this; @@ -149,11 +148,11 @@ public HiveQueryResultSet build() throws SQLException { } protected HiveQueryResultSet(Builder builder) throws SQLException { + this.statement = builder.statement; this.client = builder.client; this.stmtHandle = builder.stmtHandle; this.sessHandle = builder.sessHandle; this.fetchSize = builder.fetchSize; - this.hiveStatement = builder.hiveStatement; columnNames = new ArrayList(); columnTypes = new ArrayList(); columnAttributes = new ArrayList(); @@ -252,13 +251,13 @@ private void setSchema(List colNames, List colTypes, @Override public void close() throws SQLException { - if (hiveStatement != null) { - hiveStatement.closeClientOperation(); + if (this.statement != null && (this.statement instanceof HiveStatement)) { + HiveStatement s = (HiveStatement) this.statement; + s.closeClientOperation(); } // Need reset during re-open when needed client = null; stmtHandle = null; - hiveStatement = null; sessHandle = null; isClosed = true; } diff --git jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java index 2912ece..fce19bf 100644 --- jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java +++ jdbc/src/java/org/apache/hive/jdbc/HiveStatement.java @@ -42,6 +42,7 @@ * */ public class HiveStatement implements java.sql.Statement { + private final HiveConnection connection; private TCLIService.Iface client; private TOperationHandle stmtHandle = null; private final TSessionHandle sessHandle; @@ -76,7 +77,9 @@ /** * */ - public HiveStatement(TCLIService.Iface client, TSessionHandle sessHandle) { + public HiveStatement(HiveConnection connection, TCLIService.Iface client, + TSessionHandle sessHandle) { + this.connection = connection; this.client = client; this.sessHandle = sessHandle; } @@ -244,8 +247,8 @@ public boolean execute(String sql) throws SQLException { } return false; } - resultSet = new HiveQueryResultSet.Builder().setClient(client).setSessionHandle(sessHandle) - .setStmtHandle(stmtHandle).setHiveStatement(this).setMaxRows(maxRows).setFetchSize(fetchSize) + resultSet = new HiveQueryResultSet.Builder(this).setClient(client).setSessionHandle(sessHandle) + .setStmtHandle(stmtHandle).setMaxRows(maxRows).setFetchSize(fetchSize) .build(); return true; } @@ -351,7 +354,7 @@ public int executeUpdate(String sql, String[] columnNames) throws SQLException { */ public Connection getConnection() throws SQLException { - throw new SQLException("Method not supported"); + return this.connection; } /* diff --git jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java index e667aa6..b05d9af 100644 --- jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java +++ jdbc/src/test/org/apache/hive/jdbc/TestJdbcDriver2.java @@ -262,6 +262,78 @@ private void checkBadUrl(String url) throws SQLException { } @Test + public void testParentReferences() throws Exception { + /* Test parent references from Statement */ + Statement s = this.con.createStatement(); + ResultSet rs = s.executeQuery("SELECT * FROM " + dataTypeTableName); + + rs.close(); + s.close(); + + assertTrue(s.getConnection() == this.con); + assertTrue(rs.getStatement() == s); + + /* Test parent references from PreparedStatement */ + PreparedStatement ps = this.con.prepareStatement("SELECT * FROM " + dataTypeTableName); + rs = ps.executeQuery(); + + rs.close(); + ps.close(); + + assertTrue(ps.getConnection() == this.con); + assertTrue(rs.getStatement() == ps); + + /* Test DatabaseMetaData queries which do not have a parent Statement */ + DatabaseMetaData md = this.con.getMetaData(); + + assertTrue(md.getConnection() == this.con); + + rs = md.getCatalogs(); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getColumns(null, null, null, null); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getFunctions(null, null, null); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getImportedKeys(null, null, null); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getPrimaryKeys(null, null, null); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getProcedureColumns(null, null, null, null); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getProcedures(null, null, null); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getSchemas(); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getTableTypes(); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getTables(null, null, null, null); + assertNull(rs.getStatement()); + rs.close(); + + rs = md.getTypeInfo(); + assertNull(rs.getStatement()); + rs.close(); + } + + @Test public void testDataTypes2() throws Exception { Statement stmt = con.createStatement();