3131import java .nio .file .Paths ;
3232import java .util .Map ;
3333import java .util .Optional ;
34+ import java .util .stream .Collectors ;
3435
3536import static com .facebook .airlift .log .Level .WARN ;
3637import static com .facebook .presto .nativeworker .NativeQueryRunnerUtils .getNativeWorkerHiveProperties ;
@@ -106,6 +107,39 @@ public static PrestoSparkQueryRunner createHiveRunner()
106107 return queryRunner ;
107108 }
108109
110+ /**
111+ * Similar to createHiveRunner(), but also add additional specified catalogs and their
112+ * corresponding properties. This method exists because unlike Java, native execution does not
113+ * allow adding catalogs in the tests after process starts. So any tests that need additional
114+ * catalogs need to add them upon runner creation.
115+ */
116+ public static PrestoSparkQueryRunner createHiveRunner (Map <String , Map <String , String >> additionalCatalogs )
117+ {
118+ // Add connectors on the native side to make them available during execution.
119+ ImmutableMap .Builder <String , Map <String , String >> catalogBuilder = ImmutableMap .builder ();
120+ catalogBuilder .put ("hive" , ImmutableMap .of ("connector.name" , "hive" ))
121+ .putAll (additionalCatalogs );
122+ PrestoSparkQueryRunner queryRunner = createRunner ("hive" , new NativeExecutionModule (
123+ Optional .of (catalogBuilder .build ())));
124+
125+ // Add connectors on the Java side to make them visible during planning.
126+ additionalCatalogs .entrySet ().stream ().forEach (entry -> {
127+ queryRunner .createCatalog (
128+ entry .getKey (),
129+ entry .getValue ().get ("connector.name" ),
130+ entry .getValue ().entrySet ().stream ().filter (propertyEntry -> {
131+ // Add all properties except for "connector.name" as it is not applicable
132+ // for Java config (it's already fed in as the above function parameter).
133+ return !propertyEntry .getKey ().equals ("connector.name" );
134+ }).collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue )));
135+ });
136+
137+ PrestoNativeQueryRunnerUtils .setupJsonFunctionNamespaceManager (queryRunner ,
138+ "external_functions.json" , "json" );
139+
140+ return queryRunner ;
141+ }
142+
109143 private static PrestoSparkQueryRunner createRunner (String defaultCatalog , NativeExecutionModule nativeExecutionModule )
110144 {
111145 // Increases log level to reduce log spamming while running test.
@@ -122,10 +156,10 @@ private static PrestoSparkQueryRunner createRunner(String defaultCatalog, Native
122156 public static PrestoSparkQueryRunner createTpchRunner ()
123157 {
124158 return createRunner (
125- "tpchstandard" ,
126- new NativeExecutionModule (
127- Optional .of (
128- ImmutableMap .of ("hive " , ImmutableMap .of ("connector.name" , "tpch" )))));
159+ "tpchstandard" ,
160+ new NativeExecutionModule (
161+ Optional .of (
162+ ImmutableMap .of ("tpchstandard " , ImmutableMap .of ("connector.name" , "tpch" )))));
129163 }
130164
131165 public static PrestoSparkQueryRunner createRunner (String defaultCatalog , Optional <Path > baseDir , Map <String , String > additionalConfigProperties , Map <String , String > additionalSparkProperties , ImmutableList <Module > nativeModules )
0 commit comments