Skip to content

Commit 8c59023

Browse files
committed
Check in missing files.
1 parent dcd5193 commit 8c59023

File tree

5 files changed

+345
-0
lines changed

5 files changed

+345
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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.types
19+
20+
import org.json4s.JsonDSL._
21+
22+
import org.apache.spark.annotation.DeveloperApi
23+
24+
25+
object ArrayType {
26+
/** Construct a [[ArrayType]] object with the given element type. The `containsNull` is true. */
27+
def apply(elementType: DataType): ArrayType = ArrayType(elementType, containsNull = true)
28+
}
29+
30+
31+
/**
32+
* :: DeveloperApi ::
33+
* The data type for collections of multiple values.
34+
* Internally these are represented as columns that contain a ``scala.collection.Seq``.
35+
*
36+
* Please use [[DataTypes.createArrayType()]] to create a specific instance.
37+
*
38+
* An [[ArrayType]] object comprises two fields, `elementType: [[DataType]]` and
39+
* `containsNull: Boolean`. The field of `elementType` is used to specify the type of
40+
* array elements. The field of `containsNull` is used to specify if the array has `null` values.
41+
*
42+
* @param elementType The data type of values.
43+
* @param containsNull Indicates if values have `null` values
44+
*
45+
* @group dataType
46+
*/
47+
@DeveloperApi
48+
case class ArrayType(elementType: DataType, containsNull: Boolean) extends DataType {
49+
50+
/** No-arg constructor for kryo. */
51+
protected def this() = this(null, false)
52+
53+
private[sql] def buildFormattedString(prefix: String, builder: StringBuilder): Unit = {
54+
builder.append(
55+
s"$prefix-- element: ${elementType.typeName} (containsNull = $containsNull)\n")
56+
DataType.buildFormattedString(elementType, s"$prefix |", builder)
57+
}
58+
59+
override private[sql] def jsonValue =
60+
("type" -> typeName) ~
61+
("elementType" -> elementType.jsonValue) ~
62+
("containsNull" -> containsNull)
63+
64+
/**
65+
* The default size of a value of the ArrayType is 100 * the default size of the element type.
66+
* (We assume that there are 100 elements).
67+
*/
68+
override def defaultSize: Int = 100 * elementType.defaultSize
69+
70+
override def simpleString: String = s"array<${elementType.simpleString}>"
71+
72+
private[spark] override def asNullable: ArrayType =
73+
ArrayType(elementType.asNullable, containsNull = true)
74+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.types
19+
20+
import org.json4s.JsonAST.JValue
21+
import org.json4s.JsonDSL._
22+
23+
24+
/**
25+
* :: DeveloperApi ::
26+
* The data type for Maps. Keys in a map are not allowed to have `null` values.
27+
*
28+
* Please use [[DataTypes.createMapType()]] to create a specific instance.
29+
*
30+
* @param keyType The data type of map keys.
31+
* @param valueType The data type of map values.
32+
* @param valueContainsNull Indicates if map values have `null` values.
33+
*
34+
* @group dataType
35+
*/
36+
case class MapType(
37+
keyType: DataType,
38+
valueType: DataType,
39+
valueContainsNull: Boolean) extends DataType {
40+
41+
/** No-arg constructor for kryo. */
42+
def this() = this(null, null, false)
43+
44+
private[sql] def buildFormattedString(prefix: String, builder: StringBuilder): Unit = {
45+
builder.append(s"$prefix-- key: ${keyType.typeName}\n")
46+
builder.append(s"$prefix-- value: ${valueType.typeName} " +
47+
s"(valueContainsNull = $valueContainsNull)\n")
48+
DataType.buildFormattedString(keyType, s"$prefix |", builder)
49+
DataType.buildFormattedString(valueType, s"$prefix |", builder)
50+
}
51+
52+
override private[sql] def jsonValue: JValue =
53+
("type" -> typeName) ~
54+
("keyType" -> keyType.jsonValue) ~
55+
("valueType" -> valueType.jsonValue) ~
56+
("valueContainsNull" -> valueContainsNull)
57+
58+
/**
59+
* The default size of a value of the MapType is
60+
* 100 * (the default size of the key type + the default size of the value type).
61+
* (We assume that there are 100 elements).
62+
*/
63+
override def defaultSize: Int = 100 * (keyType.defaultSize + valueType.defaultSize)
64+
65+
override def simpleString: String = s"map<${keyType.simpleString},${valueType.simpleString}>"
66+
67+
private[spark] override def asNullable: MapType =
68+
MapType(keyType.asNullable, valueType.asNullable, valueContainsNull = true)
69+
}
70+
71+
72+
object MapType {
73+
/**
74+
* Construct a [[MapType]] object with the given key type and value type.
75+
* The `valueContainsNull` is true.
76+
*/
77+
def apply(keyType: DataType, valueType: DataType): MapType =
78+
MapType(keyType: DataType, valueType: DataType, valueContainsNull = true)
79+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.types
19+
20+
import org.json4s.JsonAST.JValue
21+
import org.json4s.JsonDSL._
22+
23+
/**
24+
* A field inside a StructType.
25+
* @param name The name of this field.
26+
* @param dataType The data type of this field.
27+
* @param nullable Indicates if values of this field can be `null` values.
28+
* @param metadata The metadata of this field. The metadata should be preserved during
29+
* transformation if the content of the column is not modified, e.g, in selection.
30+
*/
31+
case class StructField(
32+
name: String,
33+
dataType: DataType,
34+
nullable: Boolean = true,
35+
metadata: Metadata = Metadata.empty) {
36+
37+
/** No-arg constructor for kryo. */
38+
protected def this() = this(null, null)
39+
40+
private[sql] def buildFormattedString(prefix: String, builder: StringBuilder): Unit = {
41+
builder.append(s"$prefix-- $name: ${dataType.typeName} (nullable = $nullable)\n")
42+
DataType.buildFormattedString(dataType, s"$prefix |", builder)
43+
}
44+
45+
// override the default toString to be compatible with legacy parquet files.
46+
override def toString: String = s"StructField($name,$dataType,$nullable)"
47+
48+
private[sql] def jsonValue: JValue = {
49+
("name" -> name) ~
50+
("type" -> dataType.jsonValue) ~
51+
("nullable" -> nullable) ~
52+
("metadata" -> metadata.jsonValue)
53+
}
54+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.types
19+
20+
import java.sql.Timestamp
21+
22+
import scala.math.Ordering
23+
import scala.reflect.runtime.universe.typeTag
24+
25+
import org.apache.spark.annotation.DeveloperApi
26+
import org.apache.spark.sql.catalyst.ScalaReflectionLock
27+
28+
29+
/**
30+
* :: DeveloperApi ::
31+
* The data type representing `java.sql.Timestamp` values.
32+
* Please use the singleton [[DataTypes.TimestampType]].
33+
*
34+
* @group dataType
35+
*/
36+
@DeveloperApi
37+
class TimestampType private() extends AtomicType {
38+
// The companion object and this class is separated so the companion object also subclasses
39+
// this type. Otherwise, the companion object would be of type "TimestampType$" in byte code.
40+
// Defined with a private constructor so the companion object is the only possible instantiation.
41+
private[sql] type InternalType = Timestamp
42+
43+
@transient private[sql] lazy val tag = ScalaReflectionLock.synchronized { typeTag[InternalType] }
44+
45+
private[sql] val ordering = new Ordering[InternalType] {
46+
def compare(x: Timestamp, y: Timestamp): Int = x.compareTo(y)
47+
}
48+
49+
/**
50+
* The default size of a value of the TimestampType is 12 bytes.
51+
*/
52+
override def defaultSize: Int = 12
53+
54+
private[spark] override def asNullable: TimestampType = this
55+
}
56+
57+
case object TimestampType extends TimestampType
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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.types
19+
20+
import org.json4s.JsonAST.JValue
21+
import org.json4s.JsonDSL._
22+
23+
import org.apache.spark.annotation.DeveloperApi
24+
25+
/**
26+
* ::DeveloperApi::
27+
* The data type for User Defined Types (UDTs).
28+
*
29+
* This interface allows a user to make their own classes more interoperable with SparkSQL;
30+
* e.g., by creating a [[UserDefinedType]] for a class X, it becomes possible to create
31+
* a `DataFrame` which has class X in the schema.
32+
*
33+
* For SparkSQL to recognize UDTs, the UDT must be annotated with
34+
* [[SQLUserDefinedType]].
35+
*
36+
* The conversion via `serialize` occurs when instantiating a `DataFrame` from another RDD.
37+
* The conversion via `deserialize` occurs when reading from a `DataFrame`.
38+
*/
39+
@DeveloperApi
40+
abstract class UserDefinedType[UserType] extends DataType with Serializable {
41+
42+
/** Underlying storage type for this UDT */
43+
def sqlType: DataType
44+
45+
/** Paired Python UDT class, if exists. */
46+
def pyUDT: String = null
47+
48+
/**
49+
* Convert the user type to a SQL datum
50+
*
51+
* TODO: Can we make this take obj: UserType? The issue is in
52+
* CatalystTypeConverters.convertToCatalyst, where we need to convert Any to UserType.
53+
*/
54+
def serialize(obj: Any): Any
55+
56+
/** Convert a SQL datum to the user type */
57+
def deserialize(datum: Any): UserType
58+
59+
override private[sql] def jsonValue: JValue = {
60+
("type" -> "udt") ~
61+
("class" -> this.getClass.getName) ~
62+
("pyClass" -> pyUDT) ~
63+
("sqlType" -> sqlType.jsonValue)
64+
}
65+
66+
/**
67+
* Class object for the UserType
68+
*/
69+
def userClass: java.lang.Class[UserType]
70+
71+
/**
72+
* The default size of a value of the UserDefinedType is 4096 bytes.
73+
*/
74+
override def defaultSize: Int = 4096
75+
76+
/**
77+
* For UDT, asNullable will not change the nullability of its internal sqlType and just returns
78+
* itself.
79+
*/
80+
private[spark] override def asNullable: UserDefinedType[UserType] = this
81+
}

0 commit comments

Comments
 (0)