Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HUDI-3603] Support read DateType for both hive2 and hive3 #5012

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.hudi.hadoop.utils;

import org.apache.hadoop.io.Writable;
import org.apache.hudi.exception.HoodieException;

import java.lang.reflect.Constructor;

public class HiveDateTypeUtils {
// this class is unique to Hive3 and cannot be found in Hive2
private static final String HIVE_UNIQUE_CLASS = "org.apache.hadoop.hive.serde2.io.DateWritableV2";
private static final String HIVE3_DATE_WRITE_TYPE = "org.apache.hadoop.hive.serde2.io.DateWritableV2";
private static final String HIVE2_DATE_WRITE_TYPE = "org.apache.hadoop.hive.serde2.io.DateWritable";
private static Constructor<?> dateWriteableTypeConstructor;

static {
setDateWriteableTypeConstructor();
}

public static Writable getDateType(int value) {
try {
return (Writable) dateWriteableTypeConstructor.newInstance(value);
} catch (Exception e) {
throw new HoodieException(e);
}
}

private static void setDateWriteableTypeConstructor() {
try {
Class<?> clazz = Class.forName(hive3Exist() ? HIVE3_DATE_WRITE_TYPE : HIVE2_DATE_WRITE_TYPE);
dateWriteableTypeConstructor = clazz.getDeclaredConstructor(int.class);
} catch (ClassNotFoundException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}

public static boolean hive3Exist() {
try {
Class.forName(HIVE_UNIQUE_CLASS);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;
import org.apache.hadoop.hive.serde2.io.DateWritable;
import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.HiveDecimalUtils;
Expand Down Expand Up @@ -168,7 +167,7 @@ public static Writable avroToArrayWritable(Object value, Schema schema) {
return new BytesWritable(((ByteBuffer)value).array());
case INT:
if (schema.getLogicalType() != null && schema.getLogicalType().getName().equals("date")) {
return new DateWritable((Integer) value);
return HiveDateTypeUtils.getDateType((Integer) value);
}
return new IntWritable((Integer) value);
case LONG:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.hudi.hadoop.utils;

import org.apache.hadoop.io.Writable;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestHiveDateTypeUtils {
@Test
public void testGetDateType() {
boolean isHive3 = HiveDateTypeUtils.hive3Exist();
Writable result = HiveDateTypeUtils.getDateType(19990);
String dateTypeName = isHive3 ? "DateWritableV2" : "DateWritable";
assertTrue(result.getClass().getSimpleName().equals(dateTypeName));
}
}