.../hadoop/hive/metastore/MetaStoreDirectSql.java | 36 +++++++++++++++------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java index 500fba9..c7f5e1e 100644 --- a/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java +++ b/metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreDirectSql.java @@ -40,6 +40,7 @@ import javax.jdo.datastore.JDOConnection; import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.BooleanUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; @@ -858,18 +859,31 @@ static Long extractSqlLong(Object obj) throws MetaException { return ((Number)obj).longValue(); } + /** + * Convert a boolean value returned from the RDBMS to a Java Boolean object. + * MySQL has booleans, but e.g. Derby uses 'Y'/'N' mapping. + * + * @param value + * column value from the database + * @return The Boolean value of the database column value, null if the column + * value is null + * @throws MetaException + * if the column value cannot be converted into a Boolean object + */ private static Boolean extractSqlBoolean(Object value) throws MetaException { - // MySQL has booleans, but e.g. Derby uses 'Y'/'N' mapping. People using derby probably - // don't care about performance anyway, but let's cover the common case. - if (value == null) return null; - if (value instanceof Boolean) return (Boolean)value; - Character c = null; - if (value instanceof String && ((String)value).length() == 1) { - c = ((String)value).charAt(0); - } - if (c == null) return null; - if (c == 'Y') return true; - if (c == 'N') return false; + if (value == null) { + return null; + } + if (value instanceof Boolean) { + return (Boolean)value; + } + if (value instanceof String) { + try { + return BooleanUtils.toBooleanObject((String) value, "Y", "N", null); + } catch (IllegalArgumentException iae) { + // NOOP + } + } throw new MetaException("Cannot extract boolean from column value " + value); }