You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by dm...@apache.org on 2020/05/05 13:12:49 UTC
[hive] branch master updated: Cleanup and add tests for
HiveBaseResultSet.java (David Mollitor, reviewed by Peter Vary)
This is an automated email from the ASF dual-hosted git repository.
dmollitor pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git
The following commit(s) were added to refs/heads/master by this push:
new 2b0e424 Cleanup and add tests for HiveBaseResultSet.java (David Mollitor, reviewed by Peter Vary)
2b0e424 is described below
commit 2b0e424daa67bfa62e695701533fb80b8f10f383
Author: David Mollitor <dm...@apache.org>
AuthorDate: Tue May 5 09:11:38 2020 -0400
Cleanup and add tests for HiveBaseResultSet.java (David Mollitor, reviewed by Peter Vary)
---
.../org/apache/hive/jdbc/HiveBaseResultSet.java | 598 ++++++++++++++-------
.../apache/hive/jdbc/TestHiveBaseResultSet.java | 240 +++++++++
2 files changed, 649 insertions(+), 189 deletions(-)
diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java b/jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java
index 350648f..a69ea95 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/HiveBaseResultSet.java
@@ -52,8 +52,8 @@ import org.apache.hadoop.hive.serde2.thrift.Type;
import org.apache.hive.service.cli.TableSchema;
/**
- * Data independent base class which implements the common part of
- * all Hive result sets.
+ * Data independent base class which implements the common part of all Hive
+ * result sets.
*/
public abstract class HiveBaseResultSet implements ResultSet {
@@ -68,358 +68,457 @@ public abstract class HiveBaseResultSet implements ResultSet {
private TableSchema schema;
+ @Override
public boolean absolute(int row) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void afterLast() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void beforeFirst() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void cancelRowUpdates() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void deleteRow() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public int findColumn(String columnName) throws SQLException {
+ @Override
+ public int findColumn(final String columnName) throws SQLException {
int columnIndex = 0;
- boolean findColumn = false;
- for (String normalizedColumnName : normalizedColumnNames) {
- ++columnIndex;
- String[] names = normalizedColumnName.split("\\.");
- String name = names[names.length -1];
- if (name.equalsIgnoreCase(columnName) || normalizedColumnName.equalsIgnoreCase(columnName)) {
- findColumn = true;
- break;
+ if (columnName != null) {
+ final String lcColumnName = columnName.toLowerCase();
+ for (final String normalizedColumnName : normalizedColumnNames) {
+ ++columnIndex;
+ final int idx = normalizedColumnName.lastIndexOf('.');
+ final String name = (idx == -1) ? normalizedColumnName : normalizedColumnName.substring(1 + idx);
+ if (name.equals(lcColumnName) || normalizedColumnName.equals(lcColumnName)) {
+ return columnIndex;
+ }
}
}
- if (!findColumn) {
- throw new SQLException("Could not find " + columnName + " in " + normalizedColumnNames);
- } else {
- return columnIndex;
- }
+ throw new SQLException("Could not find " + columnName + " in " + normalizedColumnNames);
}
+ @Override
public boolean first() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Array getArray(int i) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Array getArray(String colName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public InputStream getAsciiStream(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public InputStream getAsciiStream(String columnName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
- Object val = getObject(columnIndex);
-
- if (val == null || val instanceof BigDecimal) {
- return (BigDecimal)val;
+ final Object val = getObject(columnIndex);
+ if (val == null) {
+ return null;
}
-
- throw new SQLException("Illegal conversion");
+ if (val instanceof BigDecimal) {
+ return (BigDecimal) val;
+ }
+ throw new SQLException("Illegal conversion to BigDecimal from column " + columnIndex + " [" + val + "]");
}
+ @Override
public BigDecimal getBigDecimal(String columnName) throws SQLException {
return getBigDecimal(findColumn(columnName));
}
+ @Override
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
MathContext mc = new MathContext(scale);
return getBigDecimal(columnIndex).round(mc);
}
+ @Override
public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException {
return getBigDecimal(findColumn(columnName), scale);
}
+ @Override
public InputStream getBinaryStream(int columnIndex) throws SQLException {
- Object obj = getObject(columnIndex);
+ final Object obj = getObject(columnIndex);
if (obj == null) {
return null;
- } else if (obj instanceof InputStream) {
- return (InputStream)obj;
- } else if (obj instanceof byte[]) {
- byte[] byteArray = (byte[])obj;
- InputStream is = new ByteArrayInputStream(byteArray);
- return is;
- } else if (obj instanceof String) {
- String str = (String)obj;
+ }
+ if (obj instanceof InputStream) {
+ return (InputStream) obj;
+ }
+ if (obj instanceof byte[]) {
+ byte[] byteArray = (byte[]) obj;
+ return new ByteArrayInputStream(byteArray);
+ }
+ if (obj instanceof String) {
+ final String str = (String) obj;
return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
}
- throw new SQLException("Illegal conversion to binary stream from column " + columnIndex);
+ throw new SQLException("Illegal conversion to binary stream from column " + columnIndex + " [" + obj + "]");
}
+ @Override
public InputStream getBinaryStream(String columnName) throws SQLException {
return getBinaryStream(findColumn(columnName));
}
+ @Override
public Blob getBlob(int i) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Blob getBlob(String colName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ /**
+ * Retrieves the value of the designated column in the current row of this
+ * ResultSet object as a boolean in the Java programming language. If the
+ * designated column has a datatype of CHAR or VARCHAR and contains a "0" or
+ * has a datatype of BIT, TINYINT, SMALLINT, INTEGER or BIGINT and contains a
+ * 0, a value of false is returned.
+ *
+ * For a true value, this implementation relaxes the JDBC specs. If the
+ * designated column has a datatype of CHAR or VARCHAR and contains a values
+ * other than "0" or has a datatype of BIT, TINYINT, SMALLINT, INTEGER or
+ * BIGINT and contains a value other than 0, a value of true is returned.
+ *
+ * @param columnIndex the first column is 1, the second is 2, ...
+ * @return the column value; if the value is SQL NULL, the value returned is
+ * false
+ * @throws if the columnIndex is not valid; if a database access error occurs
+ * or this method is called on a closed result set
+ * @see ResultSet#getBoolean(int)
+ */
+ @Override
public boolean getBoolean(int columnIndex) throws SQLException {
- Object obj = getObject(columnIndex);
- if (Boolean.class.isInstance(obj)) {
- return (Boolean) obj;
- } else if (obj == null) {
+ final Object obj = getObject(columnIndex);
+ if (obj == null) {
return false;
- } else if (Number.class.isInstance(obj)) {
+ }
+ if (obj instanceof Boolean) {
+ return (Boolean) obj;
+ }
+ if (obj instanceof Number) {
return ((Number) obj).intValue() != 0;
- } else if (String.class.isInstance(obj)) {
+ }
+ if (obj instanceof String) {
return !((String) obj).equals("0");
}
- throw new SQLException("Cannot convert column " + columnIndex + " to boolean");
+ throw new SQLException("Illegal conversion to boolean from column " + columnIndex + " [" + obj + "]");
}
- public boolean getBoolean(String columnName) throws SQLException {
- return getBoolean(findColumn(columnName));
+ /**
+ * Retrieves the value of the designated column in the current row of this
+ * ResultSet object as a boolean in the Java programming language. If the
+ * designated column has a datatype of CHAR or VARCHAR and contains a "0" or
+ * has a datatype of BIT, TINYINT, SMALLINT, INTEGER or BIGINT and contains a
+ * 0, a value of false is returned.
+ *
+ * For a true value, this implementation relaxes the JDBC specs. If the
+ * designated column has a datatype of CHAR or VARCHAR and contains a values
+ * other than "0" or has a datatype of BIT, TINYINT, SMALLINT, INTEGER or
+ * BIGINT and contains a value other than 0, a value of true is returned.
+ *
+ * @param columnLabel the label for the column specified with the SQL AS
+ * clause. If the SQL AS clause was not specified, then the label is
+ * the name of the column
+ * @return the column value; if the value is SQL NULL, the value returned is
+ * false
+ * @throws if the columnIndex is not valid; if a database access error occurs
+ * or this method is called on a closed result set
+ * @see ResultSet#getBoolean(String)
+ */
+ @Override
+ public boolean getBoolean(String columnLabel) throws SQLException {
+ return getBoolean(findColumn(columnLabel));
}
+ @Override
public byte getByte(int columnIndex) throws SQLException {
- Object obj = getObject(columnIndex);
- if (Number.class.isInstance(obj)) {
- return ((Number) obj).byteValue();
- } else if (obj == null) {
+ final Object obj = getObject(columnIndex);
+ if (obj == null) {
return 0;
}
- throw new SQLException("Cannot convert column " + columnIndex + " to byte");
+ if (obj instanceof Number) {
+ return ((Number) obj).byteValue();
+ }
+ throw new SQLException("Illegal conversion to byte from column " + columnIndex + " [" + obj + "]");
}
+ @Override
public byte getByte(String columnName) throws SQLException {
return getByte(findColumn(columnName));
}
+ @Override
public byte[] getBytes(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public byte[] getBytes(String columnName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Reader getCharacterStream(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Reader getCharacterStream(String columnName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Clob getClob(int i) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Clob getClob(String colName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public int getConcurrency() throws SQLException {
return ResultSet.CONCUR_READ_ONLY;
}
+ @Override
public String getCursorName() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Date getDate(int columnIndex) throws SQLException {
- Object obj = getObject(columnIndex);
+ final Object obj = getObject(columnIndex);
if (obj == null) {
return null;
}
if (obj instanceof Date) {
return (Date) obj;
}
- try {
- if (obj instanceof String) {
- return Date.valueOf((String)obj);
+ if (obj instanceof String) {
+ try {
+ return Date.valueOf((String) obj);
+ } catch (Exception e) {
+ throw new SQLException("Illegal conversion to Date from column " + columnIndex + " [" + obj + "]", e);
}
- } catch (Exception e) {
- throw new SQLException("Cannot convert column " + columnIndex
- + " to date: " + e.toString(), e);
}
- // If we fell through to here this is not a valid type conversion
- throw new SQLException("Cannot convert column " + columnIndex
- + " to date: Illegal conversion");
+ throw new SQLException("Illegal conversion to Date from column " + columnIndex + " [" + obj + "]");
}
+ @Override
public Date getDate(String columnName) throws SQLException {
return getDate(findColumn(columnName));
}
+ @Override
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Date getDate(String columnName, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public double getDouble(int columnIndex) throws SQLException {
- try {
- Object obj = getObject(columnIndex);
- if (Number.class.isInstance(obj)) {
- return ((Number) obj).doubleValue();
- } else if (obj == null) {
- return 0;
- } else if (String.class.isInstance(obj)) {
- return Double.parseDouble((String)obj);
+ final Object obj = getObject(columnIndex);
+ if (obj == null) {
+ return 0.0;
+ }
+ if (obj instanceof Number) {
+ return ((Number) obj).doubleValue();
+ }
+ if (obj instanceof String) {
+ try {
+ return Double.parseDouble((String) obj);
+ } catch (Exception e) {
+ throw new SQLException("Illegal conversion to double from column " + columnIndex + " [" + obj + "]", e);
}
- throw new Exception("Illegal conversion");
- } catch (Exception e) {
- throw new SQLException("Cannot convert column " + columnIndex
- + " to double: " + e.toString(), e);
}
+ throw new SQLException("Illegal conversion to double from column " + columnIndex + " [" + obj + "]");
}
+ @Override
public double getDouble(String columnName) throws SQLException {
return getDouble(findColumn(columnName));
}
+ @Override
public int getFetchDirection() throws SQLException {
return ResultSet.FETCH_FORWARD;
}
+ @Override
public int getFetchSize() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public float getFloat(int columnIndex) throws SQLException {
- try {
- Object obj = getObject(columnIndex);
- if (Number.class.isInstance(obj)) {
- return ((Number) obj).floatValue();
- } else if (obj == null) {
- return 0;
- } else if (String.class.isInstance(obj)) {
- return Float.parseFloat((String)obj);
+ final Object obj = getObject(columnIndex);
+ if (obj == null) {
+ return 0.0f;
+ }
+ if (obj instanceof Number) {
+ return ((Number) obj).floatValue();
+ }
+ if (obj instanceof String) {
+ try {
+ return Float.parseFloat((String) obj);
+ } catch (Exception e) {
+ throw new SQLException("Illegal conversion to float from column " + columnIndex + " [" + obj + "]", e);
}
- throw new Exception("Illegal conversion");
- } catch (Exception e) {
- throw new SQLException("Cannot convert column " + columnIndex
- + " to float: " + e.toString(), e);
}
+ throw new SQLException("Illegal conversion to float from column " + columnIndex + " [" + obj + "]");
}
+ @Override
public float getFloat(String columnName) throws SQLException {
return getFloat(findColumn(columnName));
}
+ @Override
public int getHoldability() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public int getInt(int columnIndex) throws SQLException {
- try {
- Object obj = getObject(columnIndex);
- if (Number.class.isInstance(obj)) {
- return ((Number) obj).intValue();
- } else if (obj == null) {
- return 0;
- } else if (String.class.isInstance(obj)) {
- return Integer.parseInt((String)obj);
+ final Object obj = getObject(columnIndex);
+ if (obj == null) {
+ return 0;
+ }
+ if (obj instanceof Number) {
+ return ((Number) obj).intValue();
+ }
+ if (obj instanceof String) {
+ try {
+ return Integer.parseInt((String) obj);
+ } catch (Exception e) {
+ throw new SQLException("Illegal conversion to int from column " + columnIndex + " [" + obj + "]", e);
}
- throw new Exception("Illegal conversion");
- } catch (Exception e) {
- throw new SQLException(
- "Cannot convert column " + columnIndex + " to integer" + e.toString(),
- e);
}
+ throw new SQLException("Illegal conversion to int from column " + columnIndex + " [" + obj + "]");
}
+ @Override
public int getInt(String columnName) throws SQLException {
return getInt(findColumn(columnName));
}
+ @Override
public long getLong(int columnIndex) throws SQLException {
- try {
- Object obj = getObject(columnIndex);
- if (Number.class.isInstance(obj)) {
- return ((Number) obj).longValue();
- } else if (obj == null) {
- return 0;
- } else if (String.class.isInstance(obj)) {
- return Long.parseLong((String)obj);
+ final Object obj = getObject(columnIndex);
+ if (obj == null) {
+ return 0L;
+ }
+ if (obj instanceof Number) {
+ return ((Number) obj).longValue();
+ }
+ if (obj instanceof String) {
+ try {
+ return Long.parseLong((String) obj);
+ } catch (Exception e) {
+ throw new SQLException("Illegal conversion to long from column " + columnIndex + " [" + obj + "]", e);
}
- throw new Exception("Illegal conversion");
- } catch (Exception e) {
- throw new SQLException(
- "Cannot convert column " + columnIndex + " to long: " + e.toString(),
- e);
}
+ throw new SQLException("Illegal conversion to long from column " + columnIndex + " [" + obj + "]");
}
+ @Override
public long getLong(String columnName) throws SQLException {
return getLong(findColumn(columnName));
}
+ @Override
public ResultSetMetaData getMetaData() throws SQLException {
return new HiveResultSetMetaData(columnNames, columnTypes, columnAttributes);
}
+ @Override
public Reader getNCharacterStream(int arg0) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Reader getNCharacterStream(String arg0) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public NClob getNClob(int arg0) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public NClob getNClob(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public String getNString(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public String getNString(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
private Object getColumnValue(int columnIndex) throws SQLException {
if (row == null) {
- throw new SQLException("No row found.");
+ throw new SQLException("No row found");
}
if (row.length == 0) {
- throw new SQLException("RowSet does not contain any columns!");
+ throw new SQLException("RowSet does not contain any columns");
}
- if (columnIndex > row.length) {
+ // the first column is 1, the second is 2, ...
+ if (columnIndex <= 0 || columnIndex > row.length) {
throw new SQLException("Invalid columnIndex: " + columnIndex);
}
- Type columnType = getSchema().getColumnDescriptorAt(columnIndex - 1).getType();
+
+ final Type columnType = getSchema().getColumnDescriptorAt(columnIndex - 1).getType();
+ final Object value = row[columnIndex - 1];
try {
- Object evaluated = evaluate(columnType, row[columnIndex - 1]);
+ final Object evaluated = evaluate(columnType, value);
wasNull = evaluated == null;
return evaluated;
} catch (Exception e) {
- throw new SQLException("Unrecognized column type: " + columnType, e);
+ throw new SQLException("Failed to evaluate " + columnType + " [" + value + "]", e);
}
}
@@ -428,139 +527,157 @@ public abstract class HiveBaseResultSet implements ResultSet {
return null;
}
switch (type) {
- case BINARY_TYPE:
- if (value instanceof String) {
- return ((String) value).getBytes();
- }
- return value;
- case TIMESTAMP_TYPE:
- return Timestamp.valueOf((String) value);
- case TIMESTAMPLOCALTZ_TYPE:
- return TimestampTZUtil.parse((String) value);
- case DECIMAL_TYPE:
- return new BigDecimal((String)value);
- case DATE_TYPE:
- return Date.valueOf((String) value);
- case INTERVAL_YEAR_MONTH_TYPE:
- return HiveIntervalYearMonth.valueOf((String) value);
- case INTERVAL_DAY_TIME_TYPE:
- return HiveIntervalDayTime.valueOf((String) value);
- case ARRAY_TYPE:
- case MAP_TYPE:
- case STRUCT_TYPE:
- // todo: returns json string. should recreate object from it?
- return value;
- default:
- return value;
+ case BINARY_TYPE:
+ if (value instanceof String) {
+ return ((String) value).getBytes(StandardCharsets.UTF_8);
+ }
+ return value;
+ case TIMESTAMP_TYPE:
+ return Timestamp.valueOf((String) value);
+ case TIMESTAMPLOCALTZ_TYPE:
+ return TimestampTZUtil.parse((String) value);
+ case DECIMAL_TYPE:
+ return new BigDecimal((String) value);
+ case DATE_TYPE:
+ return Date.valueOf((String) value);
+ case INTERVAL_YEAR_MONTH_TYPE:
+ return HiveIntervalYearMonth.valueOf((String) value);
+ case INTERVAL_DAY_TIME_TYPE:
+ return HiveIntervalDayTime.valueOf((String) value);
+ case ARRAY_TYPE:
+ case MAP_TYPE:
+ case STRUCT_TYPE:
+ // todo: returns json string. should recreate object from it?
+ return value;
+ default:
+ return value;
}
}
+ @Override
public Object getObject(int columnIndex) throws SQLException {
return getColumnValue(columnIndex);
}
+ @Override
public Object getObject(String columnName) throws SQLException {
return getObject(findColumn(columnName));
}
+ @Override
public Object getObject(int i, Map<String, Class<?>> map) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Object getObject(String colName, Map<String, Class<?>> map) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Ref getRef(int i) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Ref getRef(String colName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public int getRow() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public RowId getRowId(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public RowId getRowId(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public SQLXML getSQLXML(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public SQLXML getSQLXML(String columnLabel) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public short getShort(int columnIndex) throws SQLException {
- try {
- Object obj = getObject(columnIndex);
- if (Number.class.isInstance(obj)) {
- return ((Number) obj).shortValue();
- } else if (obj == null) {
- return 0;
- } else if (String.class.isInstance(obj)) {
- return Short.parseShort((String)obj);
+ final Object obj = getObject(columnIndex);
+
+ if (Number.class.isInstance(obj)) {
+ return ((Number) obj).shortValue();
+ } else if (obj == null) {
+ return 0;
+ } else if (String.class.isInstance(obj)) {
+ try {
+ return Short.parseShort((String) obj);
+ } catch (Exception e) {
+ throw new SQLException("Illegal conversion to int from column " + columnIndex + " [" + obj + "]", e);
}
- throw new Exception("Illegal conversion");
- } catch (Exception e) {
- throw new SQLException("Cannot convert column " + columnIndex
- + " to short: " + e.toString(), e);
+ } else {
+ throw new SQLException("Illegal conversion to int from column " + columnIndex + " [" + obj + "]");
}
}
+ @Override
public short getShort(String columnName) throws SQLException {
return getShort(findColumn(columnName));
}
+ @Override
public Statement getStatement() throws SQLException {
return this.statement;
}
- /**
- * @param columnIndex - the first column is 1, the second is 2, ...
- * @see java.sql.ResultSet#getString(int)
- */
+ @Override
public String getString(int columnIndex) throws SQLException {
- Object value = getColumnValue(columnIndex);
- if (wasNull) {
+ final Object value = getColumnValue(columnIndex);
+ if (value == null) {
return null;
}
- if (value instanceof byte[]){
- return new String((byte[])value);
+ if (value instanceof byte[]) {
+ return new String((byte[]) value, StandardCharsets.UTF_8);
}
return value.toString();
}
+ @Override
public String getString(String columnName) throws SQLException {
return getString(findColumn(columnName));
}
+ @Override
public Time getTime(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Time getTime(String columnName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Time getTime(String columnName, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Timestamp getTimestamp(int columnIndex) throws SQLException {
- Object obj = getObject(columnIndex);
+ final Object obj = getObject(columnIndex);
if (obj == null) {
return null;
}
@@ -568,478 +685,581 @@ public abstract class HiveBaseResultSet implements ResultSet {
return (Timestamp) obj;
}
if (obj instanceof String) {
- return Timestamp.valueOf((String)obj);
+ try {
+ return Timestamp.valueOf((String) obj);
+ } catch (Exception e) {
+ throw new SQLException("Illegal conversion to int from column " + columnIndex + " [" + obj + "]", e);
+ }
}
- throw new SQLException("Illegal conversion");
+ throw new SQLException("Illegal conversion to int from column " + columnIndex + " [" + obj + "]");
}
+ @Override
public Timestamp getTimestamp(String columnName) throws SQLException {
return getTimestamp(findColumn(columnName));
}
+ @Override
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public Timestamp getTimestamp(String columnName, Calendar cal) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public int getType() throws SQLException {
return ResultSet.TYPE_FORWARD_ONLY;
}
+ @Override
public URL getURL(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public URL getURL(String columnName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public InputStream getUnicodeStream(String columnName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void insertRow() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean isAfterLast() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean isBeforeFirst() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean isClosed() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean isFirst() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean isLast() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean last() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void moveToCurrentRow() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void moveToInsertRow() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean previous() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void refreshRow() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean relative(int rows) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean rowDeleted() throws SQLException {
return false;
}
+ @Override
public boolean rowInserted() throws SQLException {
return false;
}
+ @Override
public boolean rowUpdated() throws SQLException {
return false;
}
+ @Override
public void setFetchDirection(int direction) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void setFetchSize(int rows) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateArray(int columnIndex, Array x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateArray(String columnName, Array x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateAsciiStream(int columnIndex, InputStream x, int length)
- throws SQLException {
+ @Override
+ public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateAsciiStream(String columnName, InputStream x, int length)
- throws SQLException {
+ @Override
+ public void updateAsciiStream(String columnName, InputStream x, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateAsciiStream(int columnIndex, InputStream x, long length)
- throws SQLException {
+ @Override
+ public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateAsciiStream(String columnLabel, InputStream x, long length)
- throws SQLException {
+ @Override
+ public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBigDecimal(String columnName, BigDecimal x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateBinaryStream(int columnIndex, InputStream x, int length)
- throws SQLException {
+ @Override
+ public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateBinaryStream(String columnName, InputStream x, int length)
- throws SQLException {
+ @Override
+ public void updateBinaryStream(String columnName, InputStream x, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateBinaryStream(int columnIndex, InputStream x, long length)
- throws SQLException {
+ @Override
+ public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateBinaryStream(String columnLabel, InputStream x, long length)
- throws SQLException {
+ @Override
+ public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBlob(int columnIndex, Blob x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBlob(String columnName, Blob x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateBlob(int columnIndex, InputStream inputStream, long length)
- throws SQLException {
+ @Override
+ public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateBlob(String columnLabel, InputStream inputStream,
- long length) throws SQLException {
+ @Override
+ public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBoolean(int columnIndex, boolean x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBoolean(String columnName, boolean x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateByte(int columnIndex, byte x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateByte(String columnName, byte x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBytes(int columnIndex, byte[] x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateBytes(String columnName, byte[] x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateCharacterStream(int columnIndex, Reader x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateCharacterStream(int columnIndex, Reader x, int length)
- throws SQLException {
+ @Override
+ public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateCharacterStream(String columnName, Reader reader, int length)
- throws SQLException {
+ @Override
+ public void updateCharacterStream(String columnName, Reader reader, int length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateCharacterStream(int columnIndex, Reader x, long length)
- throws SQLException {
+ @Override
+ public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateCharacterStream(String columnLabel, Reader reader,
- long length) throws SQLException {
+ @Override
+ public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateClob(int columnIndex, Clob x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateClob(String columnName, Clob x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateClob(int columnIndex, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateClob(String columnLabel, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateClob(int columnIndex, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateClob(String columnLabel, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateDate(int columnIndex, Date x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateDate(String columnName, Date x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateDouble(int columnIndex, double x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateDouble(String columnName, double x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateFloat(int columnIndex, float x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateFloat(String columnName, float x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateInt(int columnIndex, int x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateInt(String columnName, int x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateLong(int columnIndex, long x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateLong(String columnName, long x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
- public void updateNCharacterStream(String columnLabel, Reader reader,
- long length) throws SQLException {
+ @Override
+ public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNClob(int columnIndex, NClob clob) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNClob(String columnLabel, NClob clob) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNClob(int columnIndex, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNClob(String columnLabel, Reader reader) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNString(int columnIndex, String string) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNString(String columnLabel, String string) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNull(int columnIndex) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateNull(String columnName) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateObject(int columnIndex, Object x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateObject(String columnName, Object x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateObject(int columnIndex, Object x, int scale) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateObject(String columnName, Object x, int scale) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateRef(int columnIndex, Ref x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateRef(String columnName, Ref x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateRow() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateRowId(int columnIndex, RowId x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateRowId(String columnLabel, RowId x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateShort(int columnIndex, short x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateShort(String columnName, short x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateString(int columnIndex, String x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateString(String columnName, String x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateTime(int columnIndex, Time x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateTime(String columnName, Time x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public void updateTimestamp(String columnName, Timestamp x) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public SQLWarning getWarnings() throws SQLException {
return warningChain;
}
+ @Override
public void clearWarnings() throws SQLException {
warningChain = null;
}
+ @Override
public void close() throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public boolean wasNull() throws SQLException {
return wasNull;
}
+ @Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
+ @Override
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLFeatureNotSupportedException("Method not supported");
}
diff --git a/jdbc/src/test/org/apache/hive/jdbc/TestHiveBaseResultSet.java b/jdbc/src/test/org/apache/hive/jdbc/TestHiveBaseResultSet.java
new file mode 100644
index 0000000..9d42317
--- /dev/null
+++ b/jdbc/src/test/org/apache/hive/jdbc/TestHiveBaseResultSet.java
@@ -0,0 +1,240 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hive.jdbc;
+
+import static org.mockito.Mockito.when;
+
+import java.nio.charset.StandardCharsets;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hive.service.cli.TableSchema;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+/**
+ * Test suite for {@link HiveBaseResultSet} class.
+ */
+public class TestHiveBaseResultSet {
+
+ @Test
+ public void testGetStringString() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("varchar(64)");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {"ABC"};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getString(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertEquals("ABC", resultSet.getString(1));
+ Assert.assertFalse(resultSet.wasNull());
+ }
+
+ @Test
+ public void testGetStringByteArray() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("varchar(64)");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {"ABC".getBytes(StandardCharsets.UTF_8)};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getString(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertEquals("ABC", resultSet.getString(1));
+ Assert.assertFalse(resultSet.wasNull());
+ }
+
+ @Test
+ public void testGetStringNull() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("varchar(64)");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {null};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getString(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertNull(resultSet.getString(1));
+ Assert.assertTrue(resultSet.wasNull());
+ }
+
+ @Test
+ public void testGetBooleanBoolean() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("boolean");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {Boolean.TRUE};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getBoolean(1)).thenCallRealMethod();
+ when(resultSet.getObject(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertEquals(true, resultSet.getBoolean(1));
+ Assert.assertFalse(resultSet.wasNull());
+ }
+
+ @Test
+ public void testGetBooleanNull() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("boolean");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {null};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getBoolean(1)).thenCallRealMethod();
+ when(resultSet.getObject(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertEquals(false, resultSet.getBoolean(1));
+ Assert.assertTrue(resultSet.wasNull());
+ }
+
+ /**
+ * If the designated column has a datatype of BIT, TINYINT, SMALLINT, INTEGER
+ * or BIGINT and contains a 0, a value of false is returned.
+ *
+ * @see ResultSet#getBoolean(int)
+ */
+ @Test
+ public void testGetBooleanNumberFalse() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("int");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {new Integer(0)};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getBoolean(1)).thenCallRealMethod();
+ when(resultSet.getObject(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertEquals(false, resultSet.getBoolean(1));
+ Assert.assertFalse(resultSet.wasNull());
+ }
+
+ /**
+ * If the designated column has a datatype of BIT, TINYINT, SMALLINT, INTEGER
+ * or BIGINT and contains a 0, a value of false is returned or true otherwise.
+ *
+ * @see ResultSet#getBoolean(int)
+ */
+ @Test
+ public void testGetBooleanNumberTrue() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("int");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {new Integer(3)};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getBoolean(1)).thenCallRealMethod();
+ when(resultSet.getObject(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertEquals(true, resultSet.getBoolean(1));
+ Assert.assertFalse(resultSet.wasNull());
+ }
+
+ /**
+ * If the designated column has a datatype of CHAR or VARCHAR and contains a
+ * "0" a value of false is returned.
+ *
+ * @see ResultSet#getBoolean(int)
+ */
+ @Test
+ public void testGetBooleanStringFalse() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("varchar(16)");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {"0"};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getBoolean(1)).thenCallRealMethod();
+ when(resultSet.getObject(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertEquals(false, resultSet.getBoolean(1));
+ Assert.assertFalse(resultSet.wasNull());
+ }
+
+ /**
+ * If the designated column has a datatype of CHAR or VARCHAR and contains a
+ * "1" a value of true is returned.
+ *
+ * @see ResultSet#getBoolean(int)
+ */
+ @Test
+ public void testGetBooleanStringTrue() throws SQLException {
+ FieldSchema fieldSchema = new FieldSchema();
+ fieldSchema.setType("varchar(16)");
+
+ List<FieldSchema> fieldSchemas = Arrays.asList(fieldSchema);
+ TableSchema schema = new TableSchema(fieldSchemas);
+
+ HiveBaseResultSet resultSet = Mockito.mock(HiveBaseResultSet.class);
+ resultSet.row = new Object[] {"1"};
+
+ when(resultSet.getSchema()).thenReturn(schema);
+ when(resultSet.getBoolean(1)).thenCallRealMethod();
+ when(resultSet.getObject(1)).thenCallRealMethod();
+ when(resultSet.wasNull()).thenCallRealMethod();
+
+ Assert.assertEquals(true, resultSet.getBoolean(1));
+ Assert.assertFalse(resultSet.wasNull());
+ }
+
+}