|
| 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.catalyst |
| 19 | + |
| 20 | +import java.sql.Timestamp |
| 21 | + |
| 22 | +import org.scalatest.FunSuite |
| 23 | + |
| 24 | +import org.apache.spark.sql.catalyst.expressions._ |
| 25 | +import org.apache.spark.sql.catalyst.types._ |
| 26 | + |
| 27 | +case class PrimitiveData( |
| 28 | + intField: Int, |
| 29 | + longField: Long, |
| 30 | + doubleField: Double, |
| 31 | + floatField: Float, |
| 32 | + shortField: Short, |
| 33 | + byteField: Byte, |
| 34 | + booleanField: Boolean) |
| 35 | + |
| 36 | +case class NullableData( |
| 37 | + intField: java.lang.Integer, |
| 38 | + longField: java.lang.Long, |
| 39 | + doubleField: java.lang.Double, |
| 40 | + floatField: java.lang.Float, |
| 41 | + shortField: java.lang.Short, |
| 42 | + byteField: java.lang.Byte, |
| 43 | + booleanField: java.lang.Boolean, |
| 44 | + stringField: String, |
| 45 | + decimalField: BigDecimal, |
| 46 | + timestampField: Timestamp, |
| 47 | + binaryField: Array[Byte]) |
| 48 | + |
| 49 | +case class OptionalData( |
| 50 | + intField: Option[Int], |
| 51 | + longField: Option[Long], |
| 52 | + doubleField: Option[Double], |
| 53 | + floatField: Option[Float], |
| 54 | + shortField: Option[Short], |
| 55 | + byteField: Option[Byte], |
| 56 | + booleanField: Option[Boolean]) |
| 57 | + |
| 58 | +case class ComplexData( |
| 59 | + arrayField: Seq[Int], |
| 60 | + mapField: Map[Int, String], |
| 61 | + structField: PrimitiveData) |
| 62 | + |
| 63 | +class ScalaReflectionSuite extends FunSuite { |
| 64 | + import ScalaReflection._ |
| 65 | + |
| 66 | + test("primitive data") { |
| 67 | + val schema = schemaFor[PrimitiveData] |
| 68 | + assert(schema === Schema( |
| 69 | + StructType(Seq( |
| 70 | + StructField("intField", IntegerType, nullable = false), |
| 71 | + StructField("longField", LongType, nullable = false), |
| 72 | + StructField("doubleField", DoubleType, nullable = false), |
| 73 | + StructField("floatField", FloatType, nullable = false), |
| 74 | + StructField("shortField", ShortType, nullable = false), |
| 75 | + StructField("byteField", ByteType, nullable = false), |
| 76 | + StructField("booleanField", BooleanType, nullable = false))), |
| 77 | + nullable = true)) |
| 78 | + } |
| 79 | + |
| 80 | + test("nullable data") { |
| 81 | + val schema = schemaFor[NullableData] |
| 82 | + assert(schema === Schema( |
| 83 | + StructType(Seq( |
| 84 | + StructField("intField", IntegerType, nullable = true), |
| 85 | + StructField("longField", LongType, nullable = true), |
| 86 | + StructField("doubleField", DoubleType, nullable = true), |
| 87 | + StructField("floatField", FloatType, nullable = true), |
| 88 | + StructField("shortField", ShortType, nullable = true), |
| 89 | + StructField("byteField", ByteType, nullable = true), |
| 90 | + StructField("booleanField", BooleanType, nullable = true), |
| 91 | + StructField("stringField", StringType, nullable = true), |
| 92 | + StructField("decimalField", DecimalType, nullable = true), |
| 93 | + StructField("timestampField", TimestampType, nullable = true), |
| 94 | + StructField("binaryField", BinaryType, nullable = true))), |
| 95 | + nullable = true)) |
| 96 | + } |
| 97 | + |
| 98 | + test("optinal data") { |
| 99 | + val schema = schemaFor[OptionalData] |
| 100 | + assert(schema === Schema( |
| 101 | + StructType(Seq( |
| 102 | + StructField("intField", IntegerType, nullable = true), |
| 103 | + StructField("longField", LongType, nullable = true), |
| 104 | + StructField("doubleField", DoubleType, nullable = true), |
| 105 | + StructField("floatField", FloatType, nullable = true), |
| 106 | + StructField("shortField", ShortType, nullable = true), |
| 107 | + StructField("byteField", ByteType, nullable = true), |
| 108 | + StructField("booleanField", BooleanType, nullable = true))), |
| 109 | + nullable = true)) |
| 110 | + } |
| 111 | + |
| 112 | + test("complex data") { |
| 113 | + val schema = schemaFor[ComplexData] |
| 114 | + assert(schema === Schema( |
| 115 | + StructType(Seq( |
| 116 | + StructField("arrayField", ArrayType(IntegerType), nullable = true), |
| 117 | + StructField("mapField", MapType(IntegerType, StringType), nullable = true), |
| 118 | + StructField( |
| 119 | + "structField", |
| 120 | + StructType(Seq( |
| 121 | + StructField("intField", IntegerType, nullable = false), |
| 122 | + StructField("longField", LongType, nullable = false), |
| 123 | + StructField("doubleField", DoubleType, nullable = false), |
| 124 | + StructField("floatField", FloatType, nullable = false), |
| 125 | + StructField("shortField", ShortType, nullable = false), |
| 126 | + StructField("byteField", ByteType, nullable = false), |
| 127 | + StructField("booleanField", BooleanType, nullable = false))), |
| 128 | + nullable = true))), |
| 129 | + nullable = true)) |
| 130 | + } |
| 131 | +} |
0 commit comments