0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.sql.types;
0019
0020 import java.util.*;
0021
0022 import org.apache.spark.annotation.Stable;
0023
0024
0025
0026
0027
0028
0029
0030 @Stable
0031 public class DataTypes {
0032
0033
0034
0035 public static final DataType StringType = StringType$.MODULE$;
0036
0037
0038
0039
0040 public static final DataType BinaryType = BinaryType$.MODULE$;
0041
0042
0043
0044
0045 public static final DataType BooleanType = BooleanType$.MODULE$;
0046
0047
0048
0049
0050 public static final DataType DateType = DateType$.MODULE$;
0051
0052
0053
0054
0055 public static final DataType TimestampType = TimestampType$.MODULE$;
0056
0057
0058
0059
0060 public static final DataType CalendarIntervalType = CalendarIntervalType$.MODULE$;
0061
0062
0063
0064
0065 public static final DataType DoubleType = DoubleType$.MODULE$;
0066
0067
0068
0069
0070 public static final DataType FloatType = FloatType$.MODULE$;
0071
0072
0073
0074
0075 public static final DataType ByteType = ByteType$.MODULE$;
0076
0077
0078
0079
0080 public static final DataType IntegerType = IntegerType$.MODULE$;
0081
0082
0083
0084
0085 public static final DataType LongType = LongType$.MODULE$;
0086
0087
0088
0089
0090 public static final DataType ShortType = ShortType$.MODULE$;
0091
0092
0093
0094
0095 public static final DataType NullType = NullType$.MODULE$;
0096
0097
0098
0099
0100
0101 public static ArrayType createArrayType(DataType elementType) {
0102 if (elementType == null) {
0103 throw new IllegalArgumentException("elementType should not be null.");
0104 }
0105 return new ArrayType(elementType, true);
0106 }
0107
0108
0109
0110
0111
0112 public static ArrayType createArrayType(DataType elementType, boolean containsNull) {
0113 if (elementType == null) {
0114 throw new IllegalArgumentException("elementType should not be null.");
0115 }
0116 return new ArrayType(elementType, containsNull);
0117 }
0118
0119
0120
0121
0122 public static DecimalType createDecimalType(int precision, int scale) {
0123 return DecimalType$.MODULE$.apply(precision, scale);
0124 }
0125
0126
0127
0128
0129 public static DecimalType createDecimalType() {
0130 return DecimalType$.MODULE$.USER_DEFAULT();
0131 }
0132
0133
0134
0135
0136
0137 public static MapType createMapType(DataType keyType, DataType valueType) {
0138 if (keyType == null) {
0139 throw new IllegalArgumentException("keyType should not be null.");
0140 }
0141 if (valueType == null) {
0142 throw new IllegalArgumentException("valueType should not be null.");
0143 }
0144 return new MapType(keyType, valueType, true);
0145 }
0146
0147
0148
0149
0150
0151
0152 public static MapType createMapType(
0153 DataType keyType,
0154 DataType valueType,
0155 boolean valueContainsNull) {
0156 if (keyType == null) {
0157 throw new IllegalArgumentException("keyType should not be null.");
0158 }
0159 if (valueType == null) {
0160 throw new IllegalArgumentException("valueType should not be null.");
0161 }
0162 return new MapType(keyType, valueType, valueContainsNull);
0163 }
0164
0165
0166
0167
0168
0169 public static StructField createStructField(
0170 String name,
0171 DataType dataType,
0172 boolean nullable,
0173 Metadata metadata) {
0174 if (name == null) {
0175 throw new IllegalArgumentException("name should not be null.");
0176 }
0177 if (dataType == null) {
0178 throw new IllegalArgumentException("dataType should not be null.");
0179 }
0180 if (metadata == null) {
0181 throw new IllegalArgumentException("metadata should not be null.");
0182 }
0183 return new StructField(name, dataType, nullable, metadata);
0184 }
0185
0186
0187
0188
0189
0190
0191 public static StructField createStructField(String name, DataType dataType, boolean nullable) {
0192 return createStructField(name, dataType, nullable, (new MetadataBuilder()).build());
0193 }
0194
0195
0196
0197
0198 public static StructType createStructType(List<StructField> fields) {
0199 return createStructType(fields.toArray(new StructField[fields.size()]));
0200 }
0201
0202
0203
0204
0205 public static StructType createStructType(StructField[] fields) {
0206 if (fields == null) {
0207 throw new IllegalArgumentException("fields should not be null.");
0208 }
0209 Set<String> distinctNames = new HashSet<>();
0210 for (StructField field : fields) {
0211 if (field == null) {
0212 throw new IllegalArgumentException(
0213 "fields should not contain any null.");
0214 }
0215
0216 distinctNames.add(field.name());
0217 }
0218 if (distinctNames.size() != fields.length) {
0219 throw new IllegalArgumentException("fields should have distinct names.");
0220 }
0221
0222 return StructType$.MODULE$.apply(fields);
0223 }
0224 }