diff --git hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchemaUtils.java hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchemaUtils.java index 16c1604..d945486 100644 --- hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchemaUtils.java +++ hcatalog/core/src/main/java/org/apache/hive/hcatalog/data/schema/HCatSchemaUtils.java @@ -112,24 +112,24 @@ public MapBuilder withKeyType(PrimitiveTypeInfo keyType) { public static HCatFieldSchema getHCatFieldSchema(FieldSchema fs) throws HCatException { String fieldName = fs.getName(); TypeInfo baseTypeInfo = TypeInfoUtils.getTypeInfoFromTypeString(fs.getType()); - return getHCatFieldSchema(fieldName, baseTypeInfo); + return getHCatFieldSchema(fieldName, baseTypeInfo, fs.getComment()); } - private static HCatFieldSchema getHCatFieldSchema(String fieldName, TypeInfo fieldTypeInfo) throws HCatException { + private static HCatFieldSchema getHCatFieldSchema(String fieldName, TypeInfo fieldTypeInfo, String comment) throws HCatException { Category typeCategory = fieldTypeInfo.getCategory(); HCatFieldSchema hCatFieldSchema; if (Category.PRIMITIVE == typeCategory) { - hCatFieldSchema = new HCatFieldSchema(fieldName, (PrimitiveTypeInfo)fieldTypeInfo, null); + hCatFieldSchema = new HCatFieldSchema(fieldName, (PrimitiveTypeInfo)fieldTypeInfo, comment); } else if (Category.STRUCT == typeCategory) { HCatSchema subSchema = constructHCatSchema((StructTypeInfo) fieldTypeInfo); - hCatFieldSchema = new HCatFieldSchema(fieldName, HCatFieldSchema.Type.STRUCT, subSchema, null); + hCatFieldSchema = new HCatFieldSchema(fieldName, HCatFieldSchema.Type.STRUCT, subSchema, comment); } else if (Category.LIST == typeCategory) { HCatSchema subSchema = getHCatSchema(((ListTypeInfo) fieldTypeInfo).getListElementTypeInfo()); - hCatFieldSchema = new HCatFieldSchema(fieldName, HCatFieldSchema.Type.ARRAY, subSchema, null); + hCatFieldSchema = new HCatFieldSchema(fieldName, HCatFieldSchema.Type.ARRAY, subSchema, comment); } else if (Category.MAP == typeCategory) { HCatSchema subSchema = getHCatSchema(((MapTypeInfo) fieldTypeInfo).getMapValueTypeInfo()); hCatFieldSchema = HCatFieldSchema.createMapTypeFieldSchema(fieldName, - (PrimitiveTypeInfo)((MapTypeInfo)fieldTypeInfo).getMapKeyTypeInfo(), subSchema, null); + (PrimitiveTypeInfo)((MapTypeInfo)fieldTypeInfo).getMapKeyTypeInfo(), subSchema, comment); } else { throw new TypeNotPresentException(fieldTypeInfo.getTypeName(), null); } @@ -151,7 +151,7 @@ public static HCatSchema getHCatSchema(List fslist) throw private static HCatSchema constructHCatSchema(StructTypeInfo stypeInfo) throws HCatException { CollectionBuilder builder = getStructSchemaBuilder(); for (String fieldName : stypeInfo.getAllStructFieldNames()) { - builder.addField(getHCatFieldSchema(fieldName, stypeInfo.getStructFieldTypeInfo(fieldName))); + builder.addField(getHCatFieldSchema(fieldName, stypeInfo.getStructFieldTypeInfo(fieldName), null)); } return builder.build(); } @@ -166,7 +166,7 @@ public static HCatSchema getHCatSchema(TypeInfo typeInfo) throws HCatException { hCatSchema = getStructSchemaBuilder().addField(new HCatFieldSchema(null, Type.STRUCT, subSchema, null)).build(); } else if (Category.LIST == typeCategory) { CollectionBuilder builder = getListSchemaBuilder(); - builder.addField(getHCatFieldSchema(null, ((ListTypeInfo) typeInfo).getListElementTypeInfo())); + builder.addField(getHCatFieldSchema(null, ((ListTypeInfo) typeInfo).getListElementTypeInfo(), null)); hCatSchema = new HCatSchema(Arrays.asList(new HCatFieldSchema("", Type.ARRAY, builder.build(), ""))); } else if (Category.MAP == typeCategory) { HCatSchema subSchema = getHCatSchema(((MapTypeInfo) typeInfo).getMapValueTypeInfo()); diff --git hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchemaUtils.java hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchemaUtils.java index e80c3d2..fb859ec 100644 --- hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchemaUtils.java +++ hcatalog/core/src/test/java/org/apache/hive/hcatalog/data/schema/TestHCatSchemaUtils.java @@ -22,6 +22,8 @@ import junit.framework.TestCase; +import org.apache.hadoop.hive.metastore.api.FieldSchema; +import org.apache.hadoop.hive.serde.serdeConstants; import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils; import org.apache.hive.hcatalog.common.HCatException; @@ -50,6 +52,32 @@ public void testSimpleOperation() throws Exception { assertEquals(ti.getTypeName().toLowerCase(), hsch.get(0).getTypeString()); assertEquals(hsch.get(0).getTypeString(), typeString.toLowerCase()); } + + public void testHCatFieldSchemaConversion() throws Exception { + FieldSchema stringFieldSchema = new FieldSchema("name1", serdeConstants.STRING_TYPE_NAME, "comment1"); + HCatFieldSchema stringHCatFieldSchema = HCatSchemaUtils.getHCatFieldSchema(stringFieldSchema); + assertEquals(stringHCatFieldSchema.getName(), "name1"); + assertEquals(stringHCatFieldSchema.getCategory(), Category.PRIMITIVE); + assertEquals(stringHCatFieldSchema.getComment(), "comment1"); + + FieldSchema listFieldSchema = new FieldSchema("name1", "array", "comment1"); + HCatFieldSchema listHCatFieldSchema = HCatSchemaUtils.getHCatFieldSchema(listFieldSchema); + assertEquals(listHCatFieldSchema.getName(), "name1"); + assertEquals(listHCatFieldSchema.getCategory(), Category.ARRAY); + assertEquals(listHCatFieldSchema.getComment(), "comment1"); + + FieldSchema mapFieldSchema = new FieldSchema("name1", "map", "comment1"); + HCatFieldSchema mapHCatFieldSchema = HCatSchemaUtils.getHCatFieldSchema(mapFieldSchema); + assertEquals(mapHCatFieldSchema.getName(), "name1"); + assertEquals(mapHCatFieldSchema.getCategory(), Category.MAP); + assertEquals(mapHCatFieldSchema.getComment(), "comment1"); + + FieldSchema structFieldSchema = new FieldSchema("name1", "struct", "comment1"); + HCatFieldSchema structHCatFieldSchema = HCatSchemaUtils.getHCatFieldSchema(structFieldSchema); + assertEquals(structHCatFieldSchema.getName(), "name1"); + assertEquals(structHCatFieldSchema.getCategory(), Category.STRUCT); + assertEquals(structHCatFieldSchema.getComment(), "comment1"); + } @SuppressWarnings("unused") private void pretty_print(PrintStream pout, HCatSchema hsch) throws HCatException {