20
20
package org .apache .spark .sql .hudi
21
21
22
22
import org .apache .hudi .DataSourceWriteOptions
23
+ import org .apache .hudi .DataSourceWriteOptions .{HIVE_STYLE_PARTITIONING , OPERATION , PARTITIONPATH_FIELD , PRECOMBINE_FIELD , RECORDKEY_FIELD , URL_ENCODE_PARTITIONING }
23
24
import org .apache .hudi .common .config .TypedProperties
24
25
import org .apache .hudi .common .table .HoodieTableConfig
26
+ import org .apache .hudi .common .table .HoodieTableConfig .DATABASE_NAME
27
+ import org .apache .hudi .config .HoodieWriteConfig
25
28
import org .apache .hudi .hive .HiveSyncConfig
26
29
import org .apache .hudi .keygen .{ComplexKeyGenerator , CustomKeyGenerator }
27
30
@@ -32,6 +35,9 @@ import org.apache.spark.sql.internal.{SessionState, SQLConf, StaticSQLConf}
32
35
import org .apache .spark .sql .types .StructType
33
36
import org .junit .jupiter .api .Assertions .assertEquals
34
37
import org .junit .jupiter .api .Test
38
+ import org .junit .jupiter .params .ParameterizedTest
39
+ import org .junit .jupiter .params .provider .{Arguments , MethodSource }
40
+ import org .junit .jupiter .params .provider .Arguments .arguments
35
41
import org .mockito .ArgumentMatchers .any
36
42
import org .mockito .Mockito .{mock , spy , when }
37
43
@@ -150,6 +156,60 @@ class TestProvidesHoodieConfig {
150
156
)
151
157
}
152
158
159
+ @ ParameterizedTest
160
+ @ MethodSource (Array (" testBuildCommonOverridingOptsParams" ))
161
+ def testBuildCommonOverridingOpts (primaryKeys : Array [String ],
162
+ preCombineField : String ,
163
+ expectedOpts : Map [String , String ]): Unit = {
164
+ val mockTableConfig = mock(classOf [HoodieTableConfig ])
165
+ when(mockTableConfig.getTableName).thenReturn(" myTable" )
166
+ when(mockTableConfig.getHiveStylePartitioningEnable).thenReturn(" true" )
167
+ when(mockTableConfig.getUrlEncodePartitioning).thenReturn(" false" )
168
+ when(mockTableConfig.getPreCombineField).thenReturn(preCombineField)
169
+
170
+ val mockHoodieTable = mock(classOf [HoodieCatalogTable ])
171
+ when(mockHoodieTable.tableConfig).thenReturn(mockTableConfig)
172
+ when(mockHoodieTable.tableLocation).thenReturn(" /dummy/path" )
173
+ when(mockHoodieTable.primaryKeys).thenReturn(primaryKeys)
174
+
175
+ val partitionPathField = " partField"
176
+ val result = ProvidesHoodieConfig .buildCommonOverridingOpts(mockHoodieTable, partitionPathField)
177
+ assert(result == expectedOpts)
178
+ }
179
+
180
+ @ Test
181
+ def testBuildOverridingOptsForDelete (): Unit = {
182
+ val mockTableConfig = mock(classOf [HoodieTableConfig ])
183
+ when(mockTableConfig.getTableName).thenReturn(" myTable" )
184
+ when(mockTableConfig.getHiveStylePartitioningEnable).thenReturn(" true" )
185
+ when(mockTableConfig.getUrlEncodePartitioning).thenReturn(" false" )
186
+
187
+ val mockHoodieTable = mock(classOf [HoodieCatalogTable ])
188
+ val mockCatalogTable = mock(classOf [CatalogTable ])
189
+ when(mockHoodieTable.table).thenReturn(mockCatalogTable)
190
+ when(mockCatalogTable.database).thenReturn(" myDatabase" )
191
+ when(mockHoodieTable.tableConfig).thenReturn(mockTableConfig)
192
+ when(mockHoodieTable.tableLocation).thenReturn(" /dummy/path" )
193
+ when(mockHoodieTable.primaryKeys).thenReturn(Array (" id1" , " id2" ))
194
+
195
+ val partitionPathField = " partField"
196
+
197
+ val expectedOpts = Map (
198
+ " path" -> " /dummy/path" ,
199
+ HoodieWriteConfig .TBL_NAME .key -> " myTable" ,
200
+ DATABASE_NAME .key -> " myDatabase" ,
201
+ HIVE_STYLE_PARTITIONING .key -> " true" ,
202
+ URL_ENCODE_PARTITIONING .key -> " false" ,
203
+ PARTITIONPATH_FIELD .key -> partitionPathField,
204
+ RECORDKEY_FIELD .key -> " id1,id2" ,
205
+ OPERATION .key -> DataSourceWriteOptions .DELETE_OPERATION_OPT_VAL
206
+ )
207
+
208
+ val result = ProvidesHoodieConfig .buildOverridingOptsForDelete(
209
+ mockHoodieTable, partitionPathField)
210
+ assert(result == expectedOpts)
211
+ }
212
+
153
213
private def mockPartitionWriteConfigInCatalogProps (mockTable : HoodieCatalogTable ,
154
214
value : Option [String ]): Unit = {
155
215
val props = if (value.isDefined) {
@@ -160,3 +220,45 @@ class TestProvidesHoodieConfig {
160
220
when(mockTable.catalogProperties).thenReturn(props)
161
221
}
162
222
}
223
+
224
+ object TestProvidesHoodieConfig {
225
+ def testBuildCommonOverridingOptsParams (): java.util.stream.Stream [Arguments ] = {
226
+ java.util.stream.Stream .of(
227
+ arguments(
228
+ Array .empty[String ],
229
+ " " ,
230
+ Map (
231
+ " path" -> " /dummy/path" ,
232
+ HoodieWriteConfig .TBL_NAME .key -> " myTable" ,
233
+ DATABASE_NAME .key -> " myDatabase" ,
234
+ HIVE_STYLE_PARTITIONING .key -> " true" ,
235
+ URL_ENCODE_PARTITIONING .key -> " false" ,
236
+ PARTITIONPATH_FIELD .key -> " partField"
237
+ )),
238
+ arguments(
239
+ Array (" id1" , " id2" ),
240
+ " " ,
241
+ Map (
242
+ " path" -> " /dummy/path" ,
243
+ HoodieWriteConfig .TBL_NAME .key -> " myTable" ,
244
+ DATABASE_NAME .key -> " myDatabase" ,
245
+ HIVE_STYLE_PARTITIONING .key -> " true" ,
246
+ URL_ENCODE_PARTITIONING .key -> " false" ,
247
+ PARTITIONPATH_FIELD .key -> " partField" ,
248
+ RECORDKEY_FIELD .key -> " id1,id2"
249
+ )),
250
+ arguments(
251
+ Array (" id1" , " id2" ),
252
+ " ts" ,
253
+ Map (
254
+ " path" -> " /dummy/path" ,
255
+ HoodieWriteConfig .TBL_NAME .key -> " myTable" ,
256
+ DATABASE_NAME .key -> " myDatabase" ,
257
+ HIVE_STYLE_PARTITIONING .key -> " true" ,
258
+ URL_ENCODE_PARTITIONING .key -> " false" ,
259
+ PARTITIONPATH_FIELD .key -> " partField" ,
260
+ RECORDKEY_FIELD .key -> " id1,id2" ,
261
+ PRECOMBINE_FIELD .key -> " ts"
262
+ )))
263
+ }
264
+ }
0 commit comments