|
17 | 17 |
|
18 | 18 | package org.apache.spark.sql.hive.client
|
19 | 19 |
|
20 |
| -import java.io.{ByteArrayOutputStream, File, PrintStream} |
| 20 | +import java.io.{ByteArrayOutputStream, File, PrintStream, PrintWriter} |
21 | 21 | import java.net.URI
|
22 | 22 |
|
23 | 23 | import org.apache.hadoop.conf.Configuration
|
@@ -841,6 +841,76 @@ class VersionsSuite extends SparkFunSuite with Logging {
|
841 | 841 | }
|
842 | 842 | }
|
843 | 843 |
|
| 844 | + test(s"$version: SPARK-17920: Insert into/overwrite avro table") { |
| 845 | + withTempDir { dir => |
| 846 | + val path = dir.getAbsolutePath |
| 847 | + val schemaPath = s"""$path${File.separator}avroschemadir""" |
| 848 | + |
| 849 | + new File(schemaPath).mkdir() |
| 850 | + val avroSchema = |
| 851 | + """{ |
| 852 | + | "name": "test_record", |
| 853 | + | "type": "record", |
| 854 | + | "fields": [ { |
| 855 | + | "name": "f0", |
| 856 | + | "type": [ |
| 857 | + | "null", |
| 858 | + | { |
| 859 | + | "precision": 38, |
| 860 | + | "scale": 2, |
| 861 | + | "type": "bytes", |
| 862 | + | "logicalType": "decimal" |
| 863 | + | } |
| 864 | + | ] |
| 865 | + | } ] |
| 866 | + |} |
| 867 | + """.stripMargin |
| 868 | + val schemaUrl = s"""$schemaPath${File.separator}avroDecimal.avsc""" |
| 869 | + val schemaFile = new File(schemaPath, "avroDecimal.avsc") |
| 870 | + val writer = new PrintWriter(schemaFile) |
| 871 | + writer.write(avroSchema) |
| 872 | + writer.close() |
| 873 | + |
| 874 | + val url = Thread.currentThread().getContextClassLoader.getResource("avroDecimal") |
| 875 | + val srcLocation = new File(url.getFile) |
| 876 | + val destTableName = "tab1" |
| 877 | + val srcTableName = "tab2" |
| 878 | + |
| 879 | + withTable(srcTableName, destTableName) { |
| 880 | + versionSpark.sql( |
| 881 | + s""" |
| 882 | + |CREATE EXTERNAL TABLE $srcTableName |
| 883 | + |ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe' |
| 884 | + |WITH SERDEPROPERTIES ('respectSparkSchema' = 'true') |
| 885 | + |STORED AS |
| 886 | + | INPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat' |
| 887 | + | OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat' |
| 888 | + |LOCATION '$srcLocation' |
| 889 | + |TBLPROPERTIES ('avro.schema.url' = '$schemaUrl') |
| 890 | + """.stripMargin |
| 891 | + ) |
| 892 | + |
| 893 | + versionSpark.sql( |
| 894 | + s""" |
| 895 | + |CREATE TABLE $destTableName |
| 896 | + |ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe' |
| 897 | + |WITH SERDEPROPERTIES ('respectSparkSchema' = 'true') |
| 898 | + |STORED AS |
| 899 | + | INPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat' |
| 900 | + | OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat' |
| 901 | + |TBLPROPERTIES ('avro.schema.url' = '$schemaUrl') |
| 902 | + """.stripMargin |
| 903 | + ) |
| 904 | + versionSpark.sql( |
| 905 | + s"""INSERT OVERWRITE TABLE $destTableName SELECT * FROM $srcTableName""") |
| 906 | + val result = versionSpark.table(srcTableName).collect() |
| 907 | + assert(versionSpark.table(destTableName).collect() === result) |
| 908 | + versionSpark.sql( |
| 909 | + s"""INSERT INTO TABLE $destTableName SELECT * FROM $srcTableName""") |
| 910 | + assert(versionSpark.table(destTableName).collect().toSeq === result ++ result) |
| 911 | + } |
| 912 | + } |
| 913 | + } |
844 | 914 | // TODO: add more tests.
|
845 | 915 | }
|
846 | 916 | }
|
0 commit comments