@@ -234,6 +234,100 @@ class SparkHoloReadWriteSuite extends SparkHoloSuiteBase {
234234 }
235235
236236
237+ test(" SaveMode = overwrite child table with schema" ) {
238+ val parentTable = " test.\" Table-Parent\" "
239+ val partitionValue = " 20240527"
240+ val table = " test.\" Table-Child_20240527\" "
241+
242+ testUtils.dropTable(parentTable)
243+ testUtils.createSchema(" test" )
244+ testUtils.createPartitionTable(defaultCreateHoloParentTableDDL, parentTable, table, partitionValue)
245+
246+ val byteA = Array (4 .toByte, 5 .toByte, 6 .toByte, 'q' .toByte, 'e' .toByte)
247+ val intA = Array (4 , 5 , 6 )
248+ val doubleA = Array (2.333 , 3.444 , 4.555 )
249+ val date = Date .valueOf(" 2024-05-27" )
250+
251+ val data1 = Seq (
252+ Row (0L , - 7L , 20 , " phone1" , 6.7F , Timestamp .valueOf(" 2021-03-29 00:00:00" ), byteA, intA, doubleA, date),
253+ Row (1L , 6L , - 30 , " phone2" , 7.8F , Timestamp .valueOf(" 2021-04-01 12:00:00" ), byteA, intA, doubleA, date)
254+ )
255+
256+ val data2 = Seq (
257+ Row (0L , - 7L , 20 , " phone1" , 6.7F , Timestamp .valueOf(" 2021-03-29 00:00:00" ), byteA, intA, doubleA, date),
258+ Row (1L , - 7L , 20 , " phone1" , 6.7F , Timestamp .valueOf(" 2021-03-29 00:00:00" ), byteA, intA, doubleA, date),
259+ Row (2L , 6L , - 30 , " phone2" , 7.8F , Timestamp .valueOf(" 2021-04-01 12:00:00" ), byteA, intA, doubleA, date),
260+ Row (3L , 6L , - 30 , " phone2" , 7.8F , Timestamp .valueOf(" 2021-04-01 12:00:00" ), byteA, intA, doubleA, date)
261+ )
262+
263+ val newSchema = StructType (Array (
264+ StructField (" pk" , LongType ),
265+ StructField (" id" , LongType ),
266+ StructField (" count" , IntegerType ),
267+ StructField (" name" , StringType ),
268+ StructField (" thick" , FloatType ),
269+ StructField (" time" , TimestampType ),
270+ StructField (" by" , BinaryType ),
271+ StructField (" inta" , ArrayType (IntegerType )),
272+ StructField (" doublea" , ArrayType (DoubleType )),
273+ StructField (" dt" , DateType )
274+ ))
275+
276+ var df = spark.createDataFrame(
277+ spark.sparkContext.parallelize(data1),
278+ newSchema
279+ ).orderBy(" pk" ).cache()
280+
281+ df.write
282+ .format(" hologres" )
283+ .option(SourceProvider .USERNAME , testUtils.username)
284+ .option(SourceProvider .PASSWORD , testUtils.password)
285+ .option(SourceProvider .JDBCURL , testUtils.jdbcUrl)
286+ .option(SourceProvider .TABLE , table)
287+ .option(SourceProvider .WRITE_MODE , " insertOrUpdate" )
288+ .option(SourceProvider .COPY_WRITE_MODE , " true" )
289+ .option(SourceProvider .ENABLE_TARGET_SHARDS , " true" )
290+ .option(SourceProvider .COPY_WRITE_DIRTY_DATA_CHECK , " true" )
291+ .mode(SaveMode .Overwrite )
292+ .save()
293+
294+ df = spark.createDataFrame(
295+ spark.sparkContext.parallelize(data2),
296+ newSchema
297+ ).orderBy(" pk" ).cache()
298+
299+ df.write
300+ .format(" hologres" )
301+ .option(SourceProvider .USERNAME , testUtils.username)
302+ .option(SourceProvider .PASSWORD , testUtils.password)
303+ .option(SourceProvider .JDBCURL , testUtils.jdbcUrl)
304+ .option(SourceProvider .TABLE , table)
305+ .option(SourceProvider .WRITE_MODE , " insertOrUpdate" )
306+ .option(SourceProvider .COPY_WRITE_MODE , " true" )
307+ .option(SourceProvider .BULK_LOAD , " true" )
308+ .option(SourceProvider .ENABLE_TARGET_SHARDS , " true" )
309+ .option(SourceProvider .COPY_WRITE_DIRTY_DATA_CHECK , " true" )
310+ .mode(SaveMode .Overwrite )
311+ .save()
312+
313+ val readDf = spark.read
314+ .format(" hologres" )
315+ .schema(newSchema) // 指定读取哪些字段
316+ .option(SourceProvider .USERNAME , testUtils.username)
317+ .option(SourceProvider .PASSWORD , testUtils.password)
318+ .option(SourceProvider .JDBCURL , testUtils.jdbcUrl)
319+ .option(SourceProvider .TABLE , table)
320+ .load().orderBy(" pk" ).cache()
321+
322+ assert(df.count() == 4 )
323+ // compare read and write
324+ if (df.except(readDf).count() > 0 ) {
325+ df.show()
326+ readDf.show()
327+ throw new Exception (" The data read is inconsistent with the data written!!!" )
328+ }
329+ }
330+
237331 test(" write or read not exists columns." ) {
238332 val table = " table_for_holo_test_1"
239333 val data = Seq (
0 commit comments