|
| 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 | +package org.apache.spark.sql.parquet |
| 18 | + |
| 19 | +import scala.collection.mutable.ArrayBuffer |
| 20 | + |
| 21 | +import org.apache.hadoop.fs.Path |
| 22 | +import org.scalatest.FunSuite |
| 23 | + |
| 24 | +import org.apache.spark.sql.catalyst.expressions.Literal |
| 25 | +import org.apache.spark.sql.parquet.ParquetRelation2._ |
| 26 | +import org.apache.spark.sql.test.TestSQLContext |
| 27 | +import org.apache.spark.sql.types._ |
| 28 | +import org.apache.spark.sql.{Row, SQLContext} |
| 29 | + |
| 30 | +class ParquetPartitionDiscoverySuite extends FunSuite with ParquetTest { |
| 31 | + override val sqlContext: SQLContext = TestSQLContext |
| 32 | + |
| 33 | + val defaultPartitionName = "__NULL__" |
| 34 | + |
| 35 | + test("column type inference") { |
| 36 | + def check(raw: String, literal: Literal): Unit = { |
| 37 | + assert(inferPartitionColumnValue(raw, defaultPartitionName) === literal) |
| 38 | + } |
| 39 | + |
| 40 | + check("10", Literal(10, IntegerType)) |
| 41 | + check("1000000000000000", Literal(1000000000000000L, LongType)) |
| 42 | + check("1.5", Literal(1.5, FloatType)) |
| 43 | + check("hello", Literal("hello", StringType)) |
| 44 | + check(defaultPartitionName, Literal(null, NullType)) |
| 45 | + } |
| 46 | + |
| 47 | + test("parse partition") { |
| 48 | + def check(path: String, expected: PartitionValues): Unit = { |
| 49 | + assert(expected === parsePartition(new Path(path), defaultPartitionName)) |
| 50 | + } |
| 51 | + |
| 52 | + def checkThrows[T <: Throwable: Manifest](path: String, expected: String): Unit = { |
| 53 | + val message = intercept[T] { |
| 54 | + parsePartition(new Path(path), defaultPartitionName) |
| 55 | + }.getMessage |
| 56 | + |
| 57 | + assert(message.contains(expected)) |
| 58 | + } |
| 59 | + |
| 60 | + check( |
| 61 | + "file:///", |
| 62 | + PartitionValues( |
| 63 | + ArrayBuffer.empty[String], |
| 64 | + ArrayBuffer.empty[Literal])) |
| 65 | + |
| 66 | + check( |
| 67 | + "file://path/a=10", |
| 68 | + PartitionValues( |
| 69 | + ArrayBuffer("a"), |
| 70 | + ArrayBuffer(Literal(10, IntegerType)))) |
| 71 | + |
| 72 | + check( |
| 73 | + "file://path/a=10/b=hello/c=1.5", |
| 74 | + PartitionValues( |
| 75 | + ArrayBuffer("a", "b", "c"), |
| 76 | + ArrayBuffer( |
| 77 | + Literal(10, IntegerType), |
| 78 | + Literal("hello", StringType), |
| 79 | + Literal(1.5, FloatType)))) |
| 80 | + |
| 81 | + check( |
| 82 | + "file://path/a=10/b_hello/c=1.5", |
| 83 | + PartitionValues( |
| 84 | + ArrayBuffer("c"), |
| 85 | + ArrayBuffer(Literal(1.5, FloatType)))) |
| 86 | + |
| 87 | + checkThrows[AssertionError]("file://path/=10", "Empty partition column name") |
| 88 | + checkThrows[AssertionError]("file://path/a=", "Empty partition column value") |
| 89 | + } |
| 90 | + |
| 91 | + test("parse partitions") { |
| 92 | + def check(paths: Seq[String], spec: PartitionSpec): Unit = { |
| 93 | + assert(parsePartitions(paths.map(new Path(_)), defaultPartitionName) === spec) |
| 94 | + } |
| 95 | + |
| 96 | + check(Seq( |
| 97 | + "hdfs://host:9000/path/a=10/b=hello"), |
| 98 | + PartitionSpec( |
| 99 | + StructType(Seq( |
| 100 | + StructField("a", IntegerType), |
| 101 | + StructField("b", StringType))), |
| 102 | + Seq(Partition(Row(10, "hello"), "hdfs://host:9000/path/a=10/b=hello")))) |
| 103 | + |
| 104 | + check(Seq( |
| 105 | + "hdfs://host:9000/path/a=10/b=20", |
| 106 | + "hdfs://host:9000/path/a=10.5/b=hello"), |
| 107 | + PartitionSpec( |
| 108 | + StructType(Seq( |
| 109 | + StructField("a", FloatType), |
| 110 | + StructField("b", StringType))), |
| 111 | + Seq( |
| 112 | + Partition(Row(10, "20"), "hdfs://host:9000/path/a=10/b=20"), |
| 113 | + Partition(Row(10.5, "hello"), "hdfs://host:9000/path/a=10.5/b=hello")))) |
| 114 | + |
| 115 | + check(Seq( |
| 116 | + s"hdfs://host:9000/path/a=10/b=$defaultPartitionName", |
| 117 | + s"hdfs://host:9000/path/a=10.5/b=$defaultPartitionName"), |
| 118 | + PartitionSpec( |
| 119 | + StructType(Seq( |
| 120 | + StructField("a", FloatType), |
| 121 | + StructField("b", StringType))), |
| 122 | + Seq( |
| 123 | + Partition(Row(10, null), s"hdfs://host:9000/path/a=10/b=$defaultPartitionName"), |
| 124 | + Partition(Row(10.5, null), s"hdfs://host:9000/path/a=10.5/b=$defaultPartitionName")))) |
| 125 | + } |
| 126 | +} |
0 commit comments