|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql |
| 19 | + |
| 20 | +import org.apache.spark.sql.test.SharedSQLContext |
| 21 | + |
| 22 | +class FileBasedDataSourceSuite extends QueryTest with SharedSQLContext { |
| 23 | + import testImplicits._ |
| 24 | + |
| 25 | + private val allFileBasedDataSources = Seq("orc", "parquet", "csv", "json", "text") |
| 26 | + |
| 27 | + allFileBasedDataSources.foreach { format => |
| 28 | + test(s"Writing empty datasets should not fail - $format") { |
| 29 | + withTempPath { dir => |
| 30 | + Seq("str").toDS().limit(0).write.format(format).save(dir.getCanonicalPath) |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // `TEXT` data source always has a single column whose name is `value`. |
| 36 | + allFileBasedDataSources.filterNot(_ == "text").foreach { format => |
| 37 | + test(s"SPARK-23072 Write and read back unicode column names - $format") { |
| 38 | + withTempPath { path => |
| 39 | + val dir = path.getCanonicalPath |
| 40 | + |
| 41 | + // scalastyle:off nonascii |
| 42 | + val df = Seq("a").toDF("한글") |
| 43 | + // scalastyle:on nonascii |
| 44 | + |
| 45 | + df.write.format(format).option("header", "true").save(dir) |
| 46 | + val answerDf = spark.read.format(format).option("header", "true").load(dir) |
| 47 | + |
| 48 | + assert(df.schema.sameType(answerDf.schema)) |
| 49 | + checkAnswer(df, answerDf) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Only ORC/Parquet support this. `CSV` and `JSON` returns an empty schema. |
| 55 | + // `TEXT` data source always has a single column whose name is `value`. |
| 56 | + Seq("orc", "parquet").foreach { format => |
| 57 | + test(s"SPARK-15474 Write and read back non-emtpy schema with empty dataframe - $format") { |
| 58 | + withTempPath { file => |
| 59 | + val path = file.getCanonicalPath |
| 60 | + val emptyDf = Seq((true, 1, "str")).toDF().limit(0) |
| 61 | + emptyDf.write.format(format).save(path) |
| 62 | + |
| 63 | + val df = spark.read.format(format).load(path) |
| 64 | + assert(df.schema.sameType(emptyDf.schema)) |
| 65 | + checkAnswer(df, emptyDf) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + allFileBasedDataSources.foreach { format => |
| 71 | + test(s"SPARK-22146 read files containing special characters using $format") { |
| 72 | + val nameWithSpecialChars = s"sp&cial%chars" |
| 73 | + withTempDir { dir => |
| 74 | + val tmpFile = s"$dir/$nameWithSpecialChars" |
| 75 | + spark.createDataset(Seq("a", "b")).write.format(format).save(tmpFile) |
| 76 | + val fileContent = spark.read.format(format).load(tmpFile) |
| 77 | + checkAnswer(fileContent, Seq(Row("a"), Row("b"))) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments