You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by xu...@apache.org on 2013/11/07 06:57:39 UTC
svn commit: r1539530 [4/4] - in /hive/trunk:
common/src/java/org/apache/hadoop/hive/common/type/
common/src/java/org/apache/hive/common/util/
common/src/test/org/apache/hadoop/hive/common/type/
ql/src/java/org/apache/hadoop/hive/ql/exec/ ql/src/java/or...
Modified: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorFactory.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorFactory.java?rev=1539530&r1=1539529&r2=1539530&view=diff
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorFactory.java (original)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorFactory.java Thu Nov 7 05:57:37 2013
@@ -26,6 +26,7 @@ import org.apache.hadoop.hive.serde.serd
import org.apache.hadoop.hive.serde2.io.ByteWritable;
import org.apache.hadoop.hive.serde2.io.DateWritable;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;
+import org.apache.hadoop.hive.serde2.io.HiveCharWritable;
import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
import org.apache.hadoop.hive.serde2.io.HiveVarcharWritable;
import org.apache.hadoop.hive.serde2.io.ShortWritable;
@@ -34,6 +35,7 @@ import org.apache.hadoop.hive.serde2.obj
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveTypeEntry;
+import org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
@@ -73,6 +75,8 @@ public final class PrimitiveObjectInspec
new WritableDoubleObjectInspector();
public static final WritableStringObjectInspector writableStringObjectInspector =
new WritableStringObjectInspector();
+ public static final WritableHiveCharObjectInspector writableHiveCharObjectInspector =
+ new WritableHiveCharObjectInspector((CharTypeInfo) TypeInfoFactory.charTypeInfo);
public static final WritableHiveVarcharObjectInspector writableHiveVarcharObjectInspector =
new WritableHiveVarcharObjectInspector((VarcharTypeInfo) TypeInfoFactory.varcharTypeInfo);
public static final WritableVoidObjectInspector writableVoidObjectInspector =
@@ -106,6 +110,7 @@ public final class PrimitiveObjectInspec
writableDoubleObjectInspector);
cachedPrimitiveWritableInspectorCache.put(TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.STRING_TYPE_NAME),
writableStringObjectInspector);
+ cachedPrimitiveWritableInspectorCache.put(TypeInfoFactory.charTypeInfo, writableHiveCharObjectInspector);
cachedPrimitiveWritableInspectorCache.put(TypeInfoFactory.varcharTypeInfo, writableHiveVarcharObjectInspector);
cachedPrimitiveWritableInspectorCache.put(TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.VOID_TYPE_NAME),
writableVoidObjectInspector);
@@ -129,6 +134,7 @@ public final class PrimitiveObjectInspec
primitiveCategoryToWritableOI.put(PrimitiveCategory.FLOAT, writableFloatObjectInspector);
primitiveCategoryToWritableOI.put(PrimitiveCategory.DOUBLE, writableDoubleObjectInspector);
primitiveCategoryToWritableOI.put(PrimitiveCategory.STRING, writableStringObjectInspector);
+ primitiveCategoryToWritableOI.put(PrimitiveCategory.CHAR, writableHiveCharObjectInspector);
primitiveCategoryToWritableOI.put(PrimitiveCategory.VARCHAR, writableHiveVarcharObjectInspector);
primitiveCategoryToWritableOI.put(PrimitiveCategory.VOID, writableVoidObjectInspector);
primitiveCategoryToWritableOI.put(PrimitiveCategory.DATE, writableDateObjectInspector);
@@ -153,6 +159,8 @@ public final class PrimitiveObjectInspec
new JavaDoubleObjectInspector();
public static final JavaStringObjectInspector javaStringObjectInspector =
new JavaStringObjectInspector();
+ public static final JavaHiveCharObjectInspector javaHiveCharObjectInspector =
+ new JavaHiveCharObjectInspector((CharTypeInfo) TypeInfoFactory.charTypeInfo);
public static final JavaHiveVarcharObjectInspector javaHiveVarcharObjectInspector =
new JavaHiveVarcharObjectInspector((VarcharTypeInfo) TypeInfoFactory.varcharTypeInfo);
public static final JavaVoidObjectInspector javaVoidObjectInspector =
@@ -186,6 +194,7 @@ public final class PrimitiveObjectInspec
javaDoubleObjectInspector);
cachedPrimitiveJavaInspectorCache.put(TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.STRING_TYPE_NAME),
javaStringObjectInspector);
+ cachedPrimitiveJavaInspectorCache.put(TypeInfoFactory.charTypeInfo, javaHiveCharObjectInspector);
cachedPrimitiveJavaInspectorCache.put(TypeInfoFactory.varcharTypeInfo, javaHiveVarcharObjectInspector);
cachedPrimitiveJavaInspectorCache.put(TypeInfoFactory.getPrimitiveTypeInfo(serdeConstants.VOID_TYPE_NAME),
javaVoidObjectInspector);
@@ -209,6 +218,7 @@ public final class PrimitiveObjectInspec
primitiveCategoryToJavaOI.put(PrimitiveCategory.FLOAT, javaFloatObjectInspector);
primitiveCategoryToJavaOI.put(PrimitiveCategory.DOUBLE, javaDoubleObjectInspector);
primitiveCategoryToJavaOI.put(PrimitiveCategory.STRING, javaStringObjectInspector);
+ primitiveCategoryToJavaOI.put(PrimitiveCategory.CHAR, javaHiveCharObjectInspector);
primitiveCategoryToJavaOI.put(PrimitiveCategory.VARCHAR, javaHiveVarcharObjectInspector);
primitiveCategoryToJavaOI.put(PrimitiveCategory.VOID, javaVoidObjectInspector);
primitiveCategoryToJavaOI.put(PrimitiveCategory.DATE, javaDateObjectInspector);
@@ -247,6 +257,9 @@ public final class PrimitiveObjectInspec
}
switch (typeInfo.getPrimitiveCategory()) {
+ case CHAR:
+ result = new WritableHiveCharObjectInspector((CharTypeInfo) typeInfo);
+ break;
case VARCHAR:
result = new WritableHiveVarcharObjectInspector((VarcharTypeInfo)typeInfo);
break;
@@ -287,6 +300,9 @@ public final class PrimitiveObjectInspec
return new WritableConstantDoubleObjectInspector((DoubleWritable)value);
case STRING:
return new WritableConstantStringObjectInspector((Text)value);
+ case CHAR:
+ return new WritableConstantHiveCharObjectInspector((CharTypeInfo) typeInfo,
+ (HiveCharWritable) value);
case VARCHAR:
return new WritableConstantHiveVarcharObjectInspector((VarcharTypeInfo)typeInfo,
(HiveVarcharWritable)value);
@@ -336,6 +352,9 @@ public final class PrimitiveObjectInspec
}
switch (typeInfo.getPrimitiveCategory()) {
+ case CHAR:
+ result = new JavaHiveCharObjectInspector((CharTypeInfo) typeInfo);
+ break;
case VARCHAR:
result = new JavaHiveVarcharObjectInspector((VarcharTypeInfo)typeInfo);
break;
Modified: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java?rev=1539530&r1=1539529&r2=1539530&view=diff
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java (original)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/PrimitiveObjectInspectorUtils.java Thu Nov 7 05:57:37 2013
@@ -29,12 +29,14 @@ import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hive.common.type.HiveChar;
import org.apache.hadoop.hive.common.type.HiveDecimal;
import org.apache.hadoop.hive.common.type.HiveVarchar;
import org.apache.hadoop.hive.serde.serdeConstants;
import org.apache.hadoop.hive.serde2.io.ByteWritable;
import org.apache.hadoop.hive.serde2.io.DateWritable;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;
+import org.apache.hadoop.hive.serde2.io.HiveCharWritable;
import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
import org.apache.hadoop.hive.serde2.io.HiveVarcharWritable;
import org.apache.hadoop.hive.serde2.io.ShortWritable;
@@ -225,6 +227,9 @@ public final class PrimitiveObjectInspec
public static final PrimitiveTypeEntry varcharTypeEntry = new PrimitiveTypeEntry(
PrimitiveCategory.VARCHAR, serdeConstants.VARCHAR_TYPE_NAME, null, HiveVarchar.class,
HiveVarcharWritable.class);
+ public static final PrimitiveTypeEntry charTypeEntry = new PrimitiveTypeEntry(
+ PrimitiveCategory.CHAR, serdeConstants.CHAR_TYPE_NAME, null, HiveChar.class,
+ HiveCharWritable.class);
// The following is a complex type for special handling
public static final PrimitiveTypeEntry unknownTypeEntry = new PrimitiveTypeEntry(
@@ -233,6 +238,7 @@ public final class PrimitiveObjectInspec
static {
registerType(binaryTypeEntry);
registerType(stringTypeEntry);
+ registerType(charTypeEntry);
registerType(varcharTypeEntry);
registerType(booleanTypeEntry);
registerType(intTypeEntry);
@@ -404,6 +410,10 @@ public final class PrimitiveObjectInspec
.getPrimitiveWritableObject(o2);
return t1.equals(t2);
}
+ case CHAR: {
+ return ((HiveCharObjectInspector)oi1).getPrimitiveWritableObject(o1)
+ .equals(((HiveCharObjectInspector)oi2).getPrimitiveWritableObject(o2));
+ }
case VARCHAR: {
return ((HiveVarcharObjectInspector)oi1).getPrimitiveWritableObject(o1)
.equals(((HiveVarcharObjectInspector)oi2).getPrimitiveWritableObject(o2));
@@ -611,6 +621,7 @@ public final class PrimitiveObjectInspec
}
break;
}
+ case CHAR:
case VARCHAR: {
result = Integer.parseInt(getString(o, oi));
break;
@@ -674,6 +685,7 @@ public final class PrimitiveObjectInspec
result = Long.parseLong(s);
}
break;
+ case CHAR:
case VARCHAR: {
result = Long.parseLong(getString(o, oi));
break;
@@ -731,6 +743,7 @@ public final class PrimitiveObjectInspec
String s = soi.getPrimitiveJavaObject(o);
result = Double.parseDouble(s);
break;
+ case CHAR:
case VARCHAR:
result = Double.parseDouble(getString(o, oi));
break;
@@ -809,6 +822,10 @@ public final class PrimitiveObjectInspec
StringObjectInspector soi = (StringObjectInspector) oi;
result = soi.getPrimitiveJavaObject(o);
break;
+ case CHAR:
+ // when converting from char to string/varchar, strip any trailing spaces
+ result = ((HiveCharObjectInspector) oi).getPrimitiveJavaObject(o).getStrippedValue();
+ break;
case VARCHAR:
HiveVarcharObjectInspector hcoi = (HiveVarcharObjectInspector) oi;
result = hcoi.getPrimitiveJavaObject(o).toString();
@@ -830,6 +847,25 @@ public final class PrimitiveObjectInspec
return result;
}
+ public static HiveChar getHiveChar(Object o, PrimitiveObjectInspector oi) {
+ if (o == null) {
+ return null;
+ }
+
+ HiveChar result = null;
+ switch (oi.getPrimitiveCategory()) {
+ case CHAR:
+ result = ((HiveCharObjectInspector) oi).getPrimitiveJavaObject(o);
+ break;
+ default:
+ // No char length available, copy whole string value here.
+ result = new HiveChar();
+ result.setValue(getString(o, oi));
+ break;
+ }
+ return result;
+ }
+
public static HiveVarchar getHiveVarchar(Object o, PrimitiveObjectInspector oi) {
if (o == null) {
@@ -852,6 +888,12 @@ public final class PrimitiveObjectInspec
return result;
}
+ public static BytesWritable getBinaryFromText(Text text) {
+ BytesWritable bw = new BytesWritable();
+ bw.set(text.getBytes(), 0, text.getLength());
+ return bw;
+ }
+
public static BytesWritable getBinary(Object o, PrimitiveObjectInspector oi) {
if (null == o) {
@@ -865,9 +907,14 @@ public final class PrimitiveObjectInspec
case STRING:
Text text = ((StringObjectInspector) oi).getPrimitiveWritableObject(o);
- BytesWritable bw = new BytesWritable();
- bw.set(text.getBytes(), 0, text.getLength());
- return bw;
+ return getBinaryFromText(text);
+ case CHAR:
+ // char to binary conversion: include trailing spaces?
+ return getBinaryFromText(
+ ((HiveCharObjectInspector) oi).getPrimitiveWritableObject(o).getPaddedValue());
+ case VARCHAR:
+ return getBinaryFromText(
+ ((HiveVarcharObjectInspector) oi).getPrimitiveWritableObject(o).getTextValue());
case BINARY:
return ((BinaryObjectInspector) oi).getPrimitiveWritableObject(o);
@@ -915,6 +962,7 @@ public final class PrimitiveObjectInspec
case STRING:
result = HiveDecimal.create(((StringObjectInspector) oi).getPrimitiveJavaObject(o));
break;
+ case CHAR:
case VARCHAR:
result = HiveDecimal.create(getString(o, oi));
break;
@@ -953,6 +1001,7 @@ public final class PrimitiveObjectInspec
result = null;
}
break;
+ case CHAR:
case VARCHAR: {
try {
String val = getString(o, oi).trim();
@@ -1017,6 +1066,7 @@ public final class PrimitiveObjectInspec
String s = soi.getPrimitiveJavaObject(o);
result = getTimestampFromString(s);
break;
+ case CHAR:
case VARCHAR:
result = getTimestampFromString(getString(o, oi));
break;
@@ -1089,6 +1139,7 @@ public final class PrimitiveObjectInspec
case DECIMAL:
return PrimitiveGrouping.NUMERIC_GROUP;
case STRING:
+ case CHAR:
case VARCHAR:
return PrimitiveGrouping.STRING_GROUP;
case BOOLEAN:
Added: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/SettableHiveCharObjectInspector.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/SettableHiveCharObjectInspector.java?rev=1539530&view=auto
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/SettableHiveCharObjectInspector.java (added)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/SettableHiveCharObjectInspector.java Thu Nov 7 05:57:37 2013
@@ -0,0 +1,29 @@
+/**
+ * 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.hadoop.hive.serde2.objectinspector.primitive;
+
+import org.apache.hadoop.hive.common.type.HiveChar;
+
+public interface SettableHiveCharObjectInspector extends HiveCharObjectInspector {
+ Object set(Object o, HiveChar value);
+
+ Object set(Object o, String value);
+
+ Object create(HiveChar value);
+
+}
Added: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableConstantHiveCharObjectInspector.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableConstantHiveCharObjectInspector.java?rev=1539530&view=auto
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableConstantHiveCharObjectInspector.java (added)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableConstantHiveCharObjectInspector.java Thu Nov 7 05:57:37 2013
@@ -0,0 +1,48 @@
+/**
+ * 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.hadoop.hive.serde2.objectinspector.primitive;
+
+import org.apache.hadoop.hive.serde2.io.HiveCharWritable;
+import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector;
+import org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo;
+
+/**
+ * A WritableConstantHiveCharObjectInspector is a WritableHiveCharObjectInspector
+ * that implements ConstantObjectInspector.
+ */
+public class WritableConstantHiveCharObjectInspector extends
+ WritableHiveCharObjectInspector implements
+ ConstantObjectInspector {
+
+ protected HiveCharWritable value;
+
+ // no-arg ctor required for Kyro serialization
+ WritableConstantHiveCharObjectInspector() {
+ }
+
+ WritableConstantHiveCharObjectInspector(CharTypeInfo typeInfo,
+ HiveCharWritable value) {
+ super(typeInfo);
+ this.value = value;
+ }
+
+ @Override
+ public HiveCharWritable getWritableConstantValue() {
+ return value;
+ }
+}
Added: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableHiveCharObjectInspector.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableHiveCharObjectInspector.java?rev=1539530&view=auto
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableHiveCharObjectInspector.java (added)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableHiveCharObjectInspector.java Thu Nov 7 05:57:37 2013
@@ -0,0 +1,117 @@
+/**
+ * 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.hadoop.hive.serde2.objectinspector.primitive;
+
+import org.apache.hadoop.hive.common.type.HiveChar;
+import org.apache.hadoop.hive.serde2.io.HiveCharWritable;
+import org.apache.hadoop.hive.serde2.typeinfo.BaseCharUtils;
+import org.apache.hadoop.hive.serde2.typeinfo.CharTypeInfo;
+
+public class WritableHiveCharObjectInspector extends AbstractPrimitiveWritableObjectInspector
+ implements SettableHiveCharObjectInspector {
+ // no-arg ctor required for Kyro serialization
+ public WritableHiveCharObjectInspector() {
+ }
+
+ public WritableHiveCharObjectInspector(CharTypeInfo typeInfo) {
+ super(typeInfo);
+ }
+
+ @Override
+ public HiveChar getPrimitiveJavaObject(Object o) {
+ // check input object's length, if it doesn't match
+ // then output a new primitive with the correct params.
+ if (o == null) {
+ return null;
+ }
+ HiveCharWritable writable = ((HiveCharWritable) o);
+ if (doesWritableMatchTypeParams(writable)) {
+ return writable.getHiveChar();
+ }
+ return getPrimitiveWithParams(writable);
+ }
+
+ @Override
+ public HiveCharWritable getPrimitiveWritableObject(Object o) {
+ // check input object's length, if it doesn't match
+ // then output new writable with correct params.
+ if (o == null) {
+ return null;
+ }
+ HiveCharWritable writable = ((HiveCharWritable) o);
+ if (doesWritableMatchTypeParams((HiveCharWritable) o)) {
+ return writable;
+ }
+
+ return getWritableWithParams(writable);
+ }
+
+ private HiveChar getPrimitiveWithParams(HiveCharWritable val) {
+ HiveChar hv = new HiveChar();
+ hv.setValue(val.getHiveChar(), getMaxLength());
+ return hv;
+ }
+
+ private HiveCharWritable getWritableWithParams(HiveCharWritable val) {
+ HiveCharWritable newValue = new HiveCharWritable();
+ newValue.set(val, getMaxLength());
+ return newValue;
+ }
+
+ private boolean doesWritableMatchTypeParams(HiveCharWritable writable) {
+ return BaseCharUtils.doesWritableMatchTypeParams(
+ writable, (CharTypeInfo)typeInfo);
+ }
+
+ public Object copyObject(Object o) {
+ if (o == null) {
+ return null;
+ }
+ HiveCharWritable writable = (HiveCharWritable) o;
+ if (doesWritableMatchTypeParams((HiveCharWritable) o)) {
+ return new HiveCharWritable(writable);
+ }
+ return getWritableWithParams(writable);
+ }
+
+ @Override
+ public Object set(Object o, HiveChar value) {
+ HiveCharWritable writable = (HiveCharWritable) o;
+ writable.set(value, getMaxLength());
+ return o;
+ }
+
+ @Override
+ public Object set(Object o, String value) {
+ HiveCharWritable writable = (HiveCharWritable) o;
+ writable.set(value, getMaxLength());
+ return o;
+ }
+
+ @Override
+ public Object create(HiveChar value) {
+ HiveCharWritable ret;
+ ret = new HiveCharWritable();
+ ret.set(value, getMaxLength());
+ return ret;
+ }
+
+ public int getMaxLength() {
+ return ((CharTypeInfo)typeInfo).getLength();
+ }
+}
Modified: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableHiveVarcharObjectInspector.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableHiveVarcharObjectInspector.java?rev=1539530&r1=1539529&r2=1539530&view=diff
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableHiveVarcharObjectInspector.java (original)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/objectinspector/primitive/WritableHiveVarcharObjectInspector.java Thu Nov 7 05:57:37 2013
@@ -22,7 +22,7 @@ import org.apache.commons.logging.LogFac
import org.apache.hadoop.hive.common.type.HiveVarchar;
import org.apache.hadoop.hive.serde2.io.HiveVarcharWritable;
import org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo;
-import org.apache.hadoop.hive.serde2.typeinfo.VarcharUtils;
+import org.apache.hadoop.hive.serde2.typeinfo.BaseCharUtils;
public class WritableHiveVarcharObjectInspector extends AbstractPrimitiveWritableObjectInspector
implements SettableHiveVarcharObjectInspector {
@@ -78,7 +78,7 @@ implements SettableHiveVarcharObjectInsp
}
private boolean doesWritableMatchTypeParams(HiveVarcharWritable writable) {
- return VarcharUtils.doesWritableMatchTypeParams(
+ return BaseCharUtils.doesWritableMatchTypeParams(
writable, (VarcharTypeInfo)typeInfo);
}
Added: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/BaseCharUtils.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/BaseCharUtils.java?rev=1539530&view=auto
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/BaseCharUtils.java (added)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/BaseCharUtils.java Thu Nov 7 05:57:37 2013
@@ -0,0 +1,53 @@
+/**
+ * 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.hadoop.hive.serde2.typeinfo;
+
+import org.apache.hadoop.hive.common.type.HiveBaseChar;
+import org.apache.hadoop.hive.common.type.HiveChar;
+import org.apache.hadoop.hive.common.type.HiveVarchar;
+import org.apache.hadoop.hive.serde2.io.HiveBaseCharWritable;
+import org.apache.hadoop.hive.serde2.io.HiveCharWritable;
+import org.apache.hadoop.hive.serde2.io.HiveVarcharWritable;
+
+public class BaseCharUtils {
+
+ public static void validateVarcharParameter(int length) {
+ if (length > HiveVarchar.MAX_VARCHAR_LENGTH || length < 1) {
+ throw new RuntimeException("Varchar length " + length + " out of allowed range [1, " +
+ HiveVarchar.MAX_VARCHAR_LENGTH + "]");
+ }
+ }
+
+ public static void validateCharParameter(int length) {
+ if (length > HiveChar.MAX_CHAR_LENGTH || length < 1) {
+ throw new RuntimeException("Char length " + length + " out of allowed range [1, " +
+ HiveChar.MAX_CHAR_LENGTH + "]");
+ }
+ }
+
+ public static boolean doesWritableMatchTypeParams(HiveBaseCharWritable writable,
+ BaseCharTypeInfo typeInfo) {
+ return typeInfo.getLength() >= writable.getCharacterLength();
+ }
+
+ public static boolean doesPrimitiveMatchTypeParams(HiveBaseChar value,
+ BaseCharTypeInfo typeInfo) {
+ return typeInfo.getLength() == value.getCharacterLength();
+ }
+}
Added: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/CharTypeInfo.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/CharTypeInfo.java?rev=1539530&view=auto
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/CharTypeInfo.java (added)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/CharTypeInfo.java Thu Nov 7 05:57:37 2013
@@ -0,0 +1,46 @@
+package org.apache.hadoop.hive.serde2.typeinfo;
+
+import org.apache.hadoop.hive.serde.serdeConstants;
+
+public class CharTypeInfo extends BaseCharTypeInfo {
+ private static final long serialVersionUID = 1L;
+
+ // no-arg constructor to make kyro happy.
+ public CharTypeInfo() {
+ }
+
+ public CharTypeInfo(int length) {
+ super(serdeConstants.CHAR_TYPE_NAME, length);
+ BaseCharUtils.validateCharParameter(length);
+ }
+
+ @Override
+ public String getTypeName() {
+ return getQualifiedName();
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (other == null || !(other instanceof CharTypeInfo)) {
+ return false;
+ }
+
+ CharTypeInfo pti = (CharTypeInfo) other;
+
+ return this.typeName.equals(pti.typeName) && this.getLength() == pti.getLength();
+ }
+
+ /**
+ * Generate the hashCode for this TypeInfo.
+ */
+ @Override
+ public int hashCode() {
+ return getQualifiedName().hashCode();
+ }
+
+ @Override
+ public String toString() {
+ return getQualifiedName();
+ }
+
+}
Modified: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoFactory.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoFactory.java?rev=1539530&r1=1539529&r2=1539530&view=diff
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoFactory.java (original)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoFactory.java Thu Nov 7 05:57:37 2013
@@ -24,9 +24,11 @@ import java.util.concurrent.ConcurrentHa
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.hive.common.type.HiveChar;
import org.apache.hadoop.hive.common.type.HiveVarchar;
import org.apache.hadoop.hive.common.type.HiveDecimal;
import org.apache.hadoop.hive.serde.serdeConstants;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveTypeEntry;
@@ -49,6 +51,7 @@ public final class TypeInfoFactory {
public static final PrimitiveTypeInfo intTypeInfo = new PrimitiveTypeInfo(serdeConstants.INT_TYPE_NAME);
public static final PrimitiveTypeInfo longTypeInfo = new PrimitiveTypeInfo(serdeConstants.BIGINT_TYPE_NAME);
public static final PrimitiveTypeInfo stringTypeInfo = new PrimitiveTypeInfo(serdeConstants.STRING_TYPE_NAME);
+ public static final PrimitiveTypeInfo charTypeInfo = new CharTypeInfo(HiveChar.MAX_CHAR_LENGTH);
public static final PrimitiveTypeInfo varcharTypeInfo = new VarcharTypeInfo(HiveVarchar.MAX_VARCHAR_LENGTH);
public static final PrimitiveTypeInfo floatTypeInfo = new PrimitiveTypeInfo(serdeConstants.FLOAT_TYPE_NAME);
public static final PrimitiveTypeInfo doubleTypeInfo = new PrimitiveTypeInfo(serdeConstants.DOUBLE_TYPE_NAME);
@@ -76,6 +79,7 @@ public final class TypeInfoFactory {
cachedPrimitiveTypeInfo.put(serdeConstants.INT_TYPE_NAME, intTypeInfo);
cachedPrimitiveTypeInfo.put(serdeConstants.BIGINT_TYPE_NAME, longTypeInfo);
cachedPrimitiveTypeInfo.put(serdeConstants.STRING_TYPE_NAME, stringTypeInfo);
+ cachedPrimitiveTypeInfo.put(charTypeInfo.getQualifiedName(), charTypeInfo);
cachedPrimitiveTypeInfo.put(varcharTypeInfo.getQualifiedName(), varcharTypeInfo);
cachedPrimitiveTypeInfo.put(serdeConstants.FLOAT_TYPE_NAME, floatTypeInfo);
cachedPrimitiveTypeInfo.put(serdeConstants.DOUBLE_TYPE_NAME, doubleTypeInfo);
@@ -132,6 +136,11 @@ public final class TypeInfoFactory {
}
switch (typeEntry.primitiveCategory) {
+ case CHAR:
+ if (parts.typeParams.length != 1) {
+ return null;
+ }
+ return new CharTypeInfo(Integer.valueOf(parts.typeParams[0]));
case VARCHAR:
if (parts.typeParams.length != 1) {
return null;
@@ -148,6 +157,11 @@ public final class TypeInfoFactory {
}
}
+ public static CharTypeInfo getCharTypeInfo(int length) {
+ String fullName = BaseCharTypeInfo.getQualifiedName(serdeConstants.CHAR_TYPE_NAME, length);
+ return (CharTypeInfo) getPrimitiveTypeInfo(fullName);
+ }
+
public static VarcharTypeInfo getVarcharTypeInfo(int length) {
String fullName = BaseCharTypeInfo.getQualifiedName(serdeConstants.VARCHAR_TYPE_NAME, length);
return (VarcharTypeInfo) getPrimitiveTypeInfo(fullName);
Modified: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoUtils.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoUtils.java?rev=1539530&r1=1539529&r2=1539530&view=diff
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoUtils.java (original)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/TypeInfoUtils.java Thu Nov 7 05:57:37 2013
@@ -29,6 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import org.apache.hadoop.hive.common.type.HiveChar;
import org.apache.hadoop.hive.common.type.HiveVarchar;
import org.apache.hadoop.hive.serde.serdeConstants;
import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
@@ -397,17 +398,24 @@ public final class TypeInfoUtils {
String qualifiedTypeName = typeEntry.typeName;
String[] params = parseParams();
switch (typeEntry.primitiveCategory) {
+ case CHAR:
case VARCHAR:
if (params == null || params.length == 0) {
- throw new IllegalArgumentException( "Varchar type is specified without length: " + typeInfoString);
+ throw new IllegalArgumentException(typeEntry.typeName
+ + " type is specified without length: " + typeInfoString);
}
if (params.length == 1) {
int length = Integer.valueOf(params[0]);
- VarcharUtils.validateParameter(length);
+ if (typeEntry.primitiveCategory == PrimitiveCategory.VARCHAR) {
+ BaseCharUtils.validateVarcharParameter(length);
+ } else {
+ BaseCharUtils.validateCharParameter(length);
+ }
qualifiedTypeName = BaseCharTypeInfo.getQualifiedName(typeEntry.typeName, length);
} else if (params.length > 1) {
- throw new IllegalArgumentException("Type varchar only takes one parameter, but " +
+ throw new IllegalArgumentException(
+ "Type " + typeEntry.typeName+ " only takes one parameter, but " +
params.length + " is seen");
}
@@ -778,9 +786,10 @@ public final class TypeInfoUtils {
switch (typeInfo.getPrimitiveCategory()) {
case STRING:
return HiveVarchar.MAX_VARCHAR_LENGTH;
+ case CHAR:
case VARCHAR:
- VarcharTypeInfo varcharTypeInfo = (VarcharTypeInfo) typeInfo;
- return varcharTypeInfo.getLength();
+ BaseCharTypeInfo baseCharTypeInfo = (BaseCharTypeInfo) typeInfo;
+ return baseCharTypeInfo.getLength();
default:
return 0;
}
Modified: hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/VarcharTypeInfo.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/VarcharTypeInfo.java?rev=1539530&r1=1539529&r2=1539530&view=diff
==============================================================================
--- hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/VarcharTypeInfo.java (original)
+++ hive/trunk/serde/src/java/org/apache/hadoop/hive/serde2/typeinfo/VarcharTypeInfo.java Thu Nov 7 05:57:37 2013
@@ -29,7 +29,7 @@ public class VarcharTypeInfo extends Bas
public VarcharTypeInfo(int length) {
super(serdeConstants.VARCHAR_TYPE_NAME, length);
- VarcharUtils.validateParameter(length);
+ BaseCharUtils.validateVarcharParameter(length);
}
@Override
Added: hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/io/TestHiveCharWritable.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/io/TestHiveCharWritable.java?rev=1539530&view=auto
==============================================================================
--- hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/io/TestHiveCharWritable.java (added)
+++ hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/io/TestHiveCharWritable.java Thu Nov 7 05:57:37 2013
@@ -0,0 +1,114 @@
+package org.apache.hadoop.hive.serde2.io;
+
+import junit.framework.TestCase;
+import org.apache.hadoop.hive.common.type.HiveChar;
+
+public class TestHiveCharWritable extends TestCase {
+ public void testConstructor() throws Exception {
+ HiveCharWritable hcw1 = new HiveCharWritable(new HiveChar("abc", 5));
+ assertEquals("abc ", hcw1.toString());
+
+ HiveCharWritable hcw2 = new HiveCharWritable(hcw1);
+ assertEquals("abc ", hcw2.toString());
+ }
+
+ public void testSet() throws Exception {
+ HiveCharWritable hcw1 = new HiveCharWritable();
+
+ HiveChar hc1 = new HiveChar("abcd", 8);
+ hcw1.set(hc1);
+ assertEquals("abcd ", hcw1.toString());
+
+ hcw1.set(hc1, 10);
+ assertEquals("abcd ", hcw1.toString());
+
+ hcw1.set(hc1, 2);
+ assertEquals("ab", hcw1.toString());
+
+ // copy whole value for strings
+ hcw1.set("abcd");
+ assertEquals("abcd", hcw1.toString());
+
+ // whole value is copied, including spaces
+ hcw1.set("abcd ");
+ assertEquals("abcd ", hcw1.toString());
+
+ hcw1.set("abcd", 10);
+ assertEquals("abcd ", hcw1.toString());
+
+ hcw1.set("abcd", 2);
+ assertEquals("ab", hcw1.toString());
+
+ HiveCharWritable hcw2 = new HiveCharWritable(hc1);
+ hcw1.set(hcw2);
+ assertEquals("abcd ", hcw1.toString());
+
+ hcw1.set(hcw2, 10);
+ assertEquals("abcd ", hcw1.toString());
+ assertEquals("abcd ", hcw1.getTextValue().toString());
+
+ hcw1.set(hcw2, 2);
+ assertEquals("ab", hcw1.toString());
+ assertEquals("ab", hcw1.getTextValue().toString());
+ }
+
+ public void testGetHiveChar() throws Exception {
+ HiveCharWritable hcw = new HiveCharWritable();
+ hcw.set("abcd", 10);
+ assertEquals("abcd ", hcw.getHiveChar().toString());
+ }
+
+ public void testGetCharacterLength() throws Exception {
+ HiveCharWritable hcw = new HiveCharWritable();
+ hcw.set("abcd", 10);
+ assertEquals(4, hcw.getCharacterLength());
+ }
+
+ public void testEnforceMaxLength() {
+ HiveCharWritable hcw1 = new HiveCharWritable();
+ hcw1.set("abcdefghij", 10);
+ assertEquals("abcdefghij", hcw1.toString());
+ hcw1.enforceMaxLength(12);
+ assertEquals("abcdefghij ", hcw1.toString());
+ hcw1.enforceMaxLength(5);
+ assertEquals("abcde", hcw1.toString());
+ }
+
+ public void testComparison() throws Exception {
+ HiveCharWritable hcw1 = new HiveCharWritable();
+ HiveCharWritable hcw2 = new HiveCharWritable();
+
+ // same string
+ hcw1.set("abcd", 4);
+ hcw2.set("abcd", 4);
+ assertEquals(hcw1, hcw2);
+ assertEquals(hcw2, hcw1);
+ assertEquals(0, hcw1.compareTo(hcw2));
+ assertEquals(0, hcw2.compareTo(hcw1));
+
+ // unequal strings
+ hcw1.set("abcd", 4);
+ hcw2.set("abc", 4);
+ assertFalse(hcw1.equals(hcw2));
+ assertFalse(hcw2.equals(hcw1));
+ assertFalse(0 == hcw1.compareTo(hcw2));
+ assertFalse(0 == hcw2.compareTo(hcw1));
+
+ // trailing spaces are not significant
+ hcw1.set("abcd ", 10);
+ hcw2.set("abcd", 4);
+ assertEquals("abcd ", hcw1.toString());
+ assertEquals(hcw1, hcw2);
+ assertEquals(hcw2, hcw1);
+ assertEquals(0, hcw1.compareTo(hcw2));
+ assertEquals(0, hcw2.compareTo(hcw1));
+
+ // leading spaces are significant
+ hcw1.set(" abcd", 5);
+ hcw2.set("abcd", 5);
+ assertFalse(hcw1.equals(hcw2));
+ assertFalse(hcw2.equals(hcw1));
+ assertFalse(0 == hcw1.compareTo(hcw2));
+ assertFalse(0 == hcw2.compareTo(hcw1));
+ }
+}
Modified: hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/objectinspector/primitive/TestPrimitiveObjectInspectorFactory.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/objectinspector/primitive/TestPrimitiveObjectInspectorFactory.java?rev=1539530&r1=1539529&r2=1539530&view=diff
==============================================================================
--- hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/objectinspector/primitive/TestPrimitiveObjectInspectorFactory.java (original)
+++ hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/objectinspector/primitive/TestPrimitiveObjectInspectorFactory.java Thu Nov 7 05:57:37 2013
@@ -30,6 +30,11 @@ public class TestPrimitiveObjectInspecto
PrimitiveObjectInspector poi = PrimitiveObjectInspectorFactory
.getPrimitiveWritableObjectInspector(PrimitiveCategory.VARCHAR);
assertEquals(poi, PrimitiveObjectInspectorFactory.writableHiveVarcharObjectInspector);
+
+ // Same for char
+ poi = PrimitiveObjectInspectorFactory
+ .getPrimitiveWritableObjectInspector(PrimitiveCategory.CHAR);
+ assertEquals(poi, PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector);
}
public void testGetPrimitiveJavaObjectInspector() {
@@ -37,5 +42,10 @@ public class TestPrimitiveObjectInspecto
PrimitiveObjectInspector poi = PrimitiveObjectInspectorFactory
.getPrimitiveJavaObjectInspector(PrimitiveCategory.VARCHAR);
assertEquals(poi, PrimitiveObjectInspectorFactory.javaHiveVarcharObjectInspector);
+
+ // same for char
+ poi = PrimitiveObjectInspectorFactory
+ .getPrimitiveJavaObjectInspector(PrimitiveCategory.CHAR);
+ assertEquals(poi, PrimitiveObjectInspectorFactory.javaHiveCharObjectInspector);
}
}
Modified: hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/typeinfo/TestTypeInfoUtils.java
URL: http://svn.apache.org/viewvc/hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/typeinfo/TestTypeInfoUtils.java?rev=1539530&r1=1539529&r2=1539530&view=diff
==============================================================================
--- hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/typeinfo/TestTypeInfoUtils.java (original)
+++ hive/trunk/serde/src/test/org/apache/hadoop/hive/serde2/typeinfo/TestTypeInfoUtils.java Thu Nov 7 05:57:37 2013
@@ -40,6 +40,7 @@ public class TestTypeInfoUtils extends T
"int",
"string",
"varchar(10)",
+ "char(15)",
"array<int>"
};
@@ -48,7 +49,11 @@ public class TestTypeInfoUtils extends T
"varchar(123",
"varchar(123,",
"varchar()",
- "varchar("
+ "varchar(",
+ "char(123",
+ "char(123,)",
+ "char()",
+ "char("
};
for (String typeString : validTypeStrings) {
@@ -59,7 +64,7 @@ public class TestTypeInfoUtils extends T
}
}
- public void testVarcharNoParams() {
+ public void testQualifiedTypeNoParams() {
boolean caughtException = false;
try {
TypeInfoUtils.getTypeInfoFromTypeString("varchar");
@@ -67,5 +72,12 @@ public class TestTypeInfoUtils extends T
caughtException = true;
}
assertEquals("varchar TypeInfo with no params should fail", true, caughtException);
+
+ try {
+ TypeInfoUtils.getTypeInfoFromTypeString("char");
+ } catch (Exception err) {
+ caughtException = true;
+ }
+ assertEquals("char TypeInfo with no params should fail", true, caughtException);
}
}