Skip to content

Commit

Permalink
[hive] Fix wrong construct for dlf metastore (#4387)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxdzs0612 authored Oct 29, 2024
1 parent f660445 commit 8ca7619
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -182,25 +183,30 @@ private static IMetaStoreClient constructorDetectedHiveMetastoreProxySupplier(
Class<?> baseClass = Class.forName(clientClassName, false, JavaUtils.getClassLoader());

// Configuration.class or HiveConf.class
Class<?> firstParamType = getProxyMethod.getParameterTypes()[0];

Class<?>[] fullParams =
new Class[] {firstParamType, HiveMetaHookLoader.class, Boolean.TYPE};
Object[] fullParamValues =
new Object[] {hiveConf, (HiveMetaHookLoader) (tbl -> null), Boolean.TRUE};

for (int i = fullParams.length; i >= 1; i--) {
try {
baseClass.getConstructor(Arrays.copyOfRange(fullParams, 0, i));
return (IMetaStoreClient)
getProxyMethod.invoke(
null,
hiveConf,
Arrays.copyOfRange(fullParams, 0, i),
Arrays.copyOfRange(fullParamValues, 0, i),
new ConcurrentHashMap<>(),
clientClassName);
} catch (NoSuchMethodException ignored) {
List<Class<?>> possibleFirstParamTypes =
Arrays.asList(getProxyMethod.getParameterTypes()[0], hiveConf.getClass());

for (Class<?> possibleFirstParamType : possibleFirstParamTypes) {
Class<?>[] fullParams =
new Class[] {
possibleFirstParamType, HiveMetaHookLoader.class, Boolean.TYPE
};
Object[] fullParamValues =
new Object[] {hiveConf, (HiveMetaHookLoader) (tbl -> null), Boolean.TRUE};

for (int i = fullParams.length; i >= 1; i--) {
try {
baseClass.getConstructor(Arrays.copyOfRange(fullParams, 0, i));
return (IMetaStoreClient)
getProxyMethod.invoke(
null,
hiveConf,
Arrays.copyOfRange(fullParams, 0, i),
Arrays.copyOfRange(fullParamValues, 0, i),
new ConcurrentHashMap<>(),
clientClassName);
} catch (NoSuchMethodException ignored) {
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.paimon.hive;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaHookLoader;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
Expand All @@ -28,7 +29,7 @@
public class CustomConstructorMetastoreClient {

/**
* A {@link HiveMetaStoreClient} to test custom Hive metastore client with (HiveConf,
* A {@link HiveMetaStoreClient} to test custom Hive metastore client with (Configuration,
* HiveMetaHookLoader) constructor.
*/
public static class TwoParameterConstructorMetastoreClient extends HiveMetaStoreClient
Expand All @@ -41,7 +42,7 @@ public TwoParameterConstructorMetastoreClient(
}

/**
* A {@link HiveMetaStoreClient} to test custom Hive metastore client with (HiveConf)
* A {@link HiveMetaStoreClient} to test custom Hive metastore client with (Configuration)
* constructor.
*/
public static class OneParameterConstructorMetastoreClient extends HiveMetaStoreClient
Expand All @@ -51,4 +52,16 @@ public OneParameterConstructorMetastoreClient(Configuration conf) throws MetaExc
super(conf);
}
}

/**
* A {@link HiveMetaStoreClient} to test custom Hive metastore client with (HiveConf)
* constructor.
*/
public static class OtherParameterConstructorMetastoreClient extends HiveMetaStoreClient
implements IMetaStoreClient {

public OtherParameterConstructorMetastoreClient(HiveConf conf) throws MetaException {
super(conf);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public void testCustomConstructorMetastoreClient() throws Exception {
EnvironmentSettings settings = EnvironmentSettings.newInstance().inBatchMode().build();
Class<?>[] customConstructorMetastoreClientClass = {
CustomConstructorMetastoreClient.TwoParameterConstructorMetastoreClient.class,
CustomConstructorMetastoreClient.OneParameterConstructorMetastoreClient.class
CustomConstructorMetastoreClient.OneParameterConstructorMetastoreClient.class,
CustomConstructorMetastoreClient.OtherParameterConstructorMetastoreClient.class
};

for (Class<?> clazz : customConstructorMetastoreClientClass) {
Expand Down

0 comments on commit 8ca7619

Please sign in to comment.