@@ -215,6 +215,9 @@ abstract class DataType {
215
215
case _ => false
216
216
}
217
217
218
+ /** The default size of a value of this data type. */
219
+ def defaultSize : Int
220
+
218
221
def isPrimitive : Boolean = false
219
222
220
223
def typeName : String = this .getClass.getSimpleName.stripSuffix(" $" ).dropRight(4 ).toLowerCase
@@ -235,33 +238,25 @@ abstract class DataType {
235
238
* @group dataType
236
239
*/
237
240
@ DeveloperApi
238
- case object NullType extends DataType
241
+ case object NullType extends DataType {
242
+ override def defaultSize : Int = 1
243
+ }
239
244
240
245
241
- object NativeType {
246
+ protected [sql] object NativeType {
242
247
val all = Seq (
243
248
IntegerType , BooleanType , LongType , DoubleType , FloatType , ShortType , ByteType , StringType )
244
249
245
250
def unapply (dt : DataType ): Boolean = all.contains(dt)
246
-
247
- val defaultSizeOf : Map [NativeType , Int ] = Map (
248
- IntegerType -> 4 ,
249
- BooleanType -> 1 ,
250
- LongType -> 8 ,
251
- DoubleType -> 8 ,
252
- FloatType -> 4 ,
253
- ShortType -> 2 ,
254
- ByteType -> 1 ,
255
- StringType -> 4096 )
256
251
}
257
252
258
253
259
- trait PrimitiveType extends DataType {
254
+ protected [sql] trait PrimitiveType extends DataType {
260
255
override def isPrimitive = true
261
256
}
262
257
263
258
264
- object PrimitiveType {
259
+ protected [sql] object PrimitiveType {
265
260
private val nonDecimals = Seq (NullType , DateType , TimestampType , BinaryType ) ++ NativeType .all
266
261
private val nonDecimalNameToType = nonDecimals.map(t => t.typeName -> t).toMap
267
262
@@ -276,7 +271,7 @@ object PrimitiveType {
276
271
}
277
272
}
278
273
279
- abstract class NativeType extends DataType {
274
+ protected [sql] abstract class NativeType extends DataType {
280
275
private [sql] type JvmType
281
276
@ transient private [sql] val tag : TypeTag [JvmType ]
282
277
private [sql] val ordering : Ordering [JvmType ]
@@ -300,6 +295,11 @@ case object StringType extends NativeType with PrimitiveType {
300
295
private [sql] type JvmType = String
301
296
@ transient private [sql] lazy val tag = ScalaReflectionLock .synchronized { typeTag[JvmType ] }
302
297
private [sql] val ordering = implicitly[Ordering [JvmType ]]
298
+
299
+ /**
300
+ * The default size of a value of the StringType is 4096 bytes.
301
+ */
302
+ override def defaultSize : Int = 4096
303
303
}
304
304
305
305
@@ -324,6 +324,11 @@ case object BinaryType extends NativeType with PrimitiveType {
324
324
x.length - y.length
325
325
}
326
326
}
327
+
328
+ /**
329
+ * The default size of a value of the BinaryType is 4096 bytes.
330
+ */
331
+ override def defaultSize : Int = 4096
327
332
}
328
333
329
334
@@ -339,6 +344,11 @@ case object BooleanType extends NativeType with PrimitiveType {
339
344
private [sql] type JvmType = Boolean
340
345
@ transient private [sql] lazy val tag = ScalaReflectionLock .synchronized { typeTag[JvmType ] }
341
346
private [sql] val ordering = implicitly[Ordering [JvmType ]]
347
+
348
+ /**
349
+ * The default size of a value of the BooleanType is 1 byte.
350
+ */
351
+ override def defaultSize : Int = 1
342
352
}
343
353
344
354
@@ -359,6 +369,11 @@ case object TimestampType extends NativeType {
359
369
private [sql] val ordering = new Ordering [JvmType ] {
360
370
def compare (x : Timestamp , y : Timestamp ) = x.compareTo(y)
361
371
}
372
+
373
+ /**
374
+ * The default size of a value of the TimestampType is 8 bytes.
375
+ */
376
+ override def defaultSize : Int = 8
362
377
}
363
378
364
379
@@ -379,10 +394,15 @@ case object DateType extends NativeType {
379
394
private [sql] val ordering = new Ordering [JvmType ] {
380
395
def compare (x : Date , y : Date ) = x.compareTo(y)
381
396
}
397
+
398
+ /**
399
+ * The default size of a value of the DateType is 8 bytes.
400
+ */
401
+ override def defaultSize : Int = 8
382
402
}
383
403
384
404
385
- abstract class NumericType extends NativeType with PrimitiveType {
405
+ protected [sql] abstract class NumericType extends NativeType with PrimitiveType {
386
406
// Unfortunately we can't get this implicitly as that breaks Spark Serialization. In order for
387
407
// implicitly[Numeric[JvmType]] to be valid, we have to change JvmType from a type variable to a
388
408
// type parameter and and add a numeric annotation (i.e., [JvmType : Numeric]). This gets
@@ -392,21 +412,21 @@ abstract class NumericType extends NativeType with PrimitiveType {
392
412
}
393
413
394
414
395
- object NumericType {
415
+ protected [sql] object NumericType {
396
416
def unapply (e : Expression ): Boolean = e.dataType.isInstanceOf [NumericType ]
397
417
}
398
418
399
419
400
420
/** Matcher for any expressions that evaluate to [[IntegralType ]]s */
401
- object IntegralType {
421
+ protected [sql] object IntegralType {
402
422
def unapply (a : Expression ): Boolean = a match {
403
423
case e : Expression if e.dataType.isInstanceOf [IntegralType ] => true
404
424
case _ => false
405
425
}
406
426
}
407
427
408
428
409
- sealed abstract class IntegralType extends NumericType {
429
+ protected [sql] sealed abstract class IntegralType extends NumericType {
410
430
private [sql] val integral : Integral [JvmType ]
411
431
}
412
432
@@ -425,6 +445,11 @@ case object LongType extends IntegralType {
425
445
private [sql] val numeric = implicitly[Numeric [Long ]]
426
446
private [sql] val integral = implicitly[Integral [Long ]]
427
447
private [sql] val ordering = implicitly[Ordering [JvmType ]]
448
+
449
+ /**
450
+ * The default size of a value of the LongType is 8 bytes.
451
+ */
452
+ override def defaultSize : Int = 8
428
453
}
429
454
430
455
@@ -442,6 +467,11 @@ case object IntegerType extends IntegralType {
442
467
private [sql] val numeric = implicitly[Numeric [Int ]]
443
468
private [sql] val integral = implicitly[Integral [Int ]]
444
469
private [sql] val ordering = implicitly[Ordering [JvmType ]]
470
+
471
+ /**
472
+ * The default size of a value of the IntegerType is 4 bytes.
473
+ */
474
+ override def defaultSize : Int = 4
445
475
}
446
476
447
477
@@ -459,6 +489,11 @@ case object ShortType extends IntegralType {
459
489
private [sql] val numeric = implicitly[Numeric [Short ]]
460
490
private [sql] val integral = implicitly[Integral [Short ]]
461
491
private [sql] val ordering = implicitly[Ordering [JvmType ]]
492
+
493
+ /**
494
+ * The default size of a value of the ShortType is 2 bytes.
495
+ */
496
+ override def defaultSize : Int = 2
462
497
}
463
498
464
499
@@ -476,19 +511,24 @@ case object ByteType extends IntegralType {
476
511
private [sql] val numeric = implicitly[Numeric [Byte ]]
477
512
private [sql] val integral = implicitly[Integral [Byte ]]
478
513
private [sql] val ordering = implicitly[Ordering [JvmType ]]
514
+
515
+ /**
516
+ * The default size of a value of the ByteType is 1 byte.
517
+ */
518
+ override def defaultSize : Int = 1
479
519
}
480
520
481
521
482
522
/** Matcher for any expressions that evaluate to [[FractionalType ]]s */
483
- object FractionalType {
523
+ protected [sql] object FractionalType {
484
524
def unapply (a : Expression ): Boolean = a match {
485
525
case e : Expression if e.dataType.isInstanceOf [FractionalType ] => true
486
526
case _ => false
487
527
}
488
528
}
489
529
490
530
491
- sealed abstract class FractionalType extends NumericType {
531
+ protected [sql] sealed abstract class FractionalType extends NumericType {
492
532
private [sql] val fractional : Fractional [JvmType ]
493
533
private [sql] val asIntegral : Integral [JvmType ]
494
534
}
@@ -530,6 +570,11 @@ case class DecimalType(precisionInfo: Option[PrecisionInfo]) extends FractionalT
530
570
case Some (PrecisionInfo (precision, scale)) => s " DecimalType( $precision, $scale) "
531
571
case None => " DecimalType()"
532
572
}
573
+
574
+ /**
575
+ * The default size of a value of the DecimalType is 4096 bytes.
576
+ */
577
+ override def defaultSize : Int = 4096
533
578
}
534
579
535
580
@@ -580,6 +625,11 @@ case object DoubleType extends FractionalType {
580
625
private [sql] val fractional = implicitly[Fractional [Double ]]
581
626
private [sql] val ordering = implicitly[Ordering [JvmType ]]
582
627
private [sql] val asIntegral = DoubleAsIfIntegral
628
+
629
+ /**
630
+ * The default size of a value of the DoubleType is 8 bytes.
631
+ */
632
+ override def defaultSize : Int = 8
583
633
}
584
634
585
635
@@ -598,6 +648,11 @@ case object FloatType extends FractionalType {
598
648
private [sql] val fractional = implicitly[Fractional [Float ]]
599
649
private [sql] val ordering = implicitly[Ordering [JvmType ]]
600
650
private [sql] val asIntegral = FloatAsIfIntegral
651
+
652
+ /**
653
+ * The default size of a value of the FloatType is 4 bytes.
654
+ */
655
+ override def defaultSize : Int = 4
601
656
}
602
657
603
658
@@ -636,6 +691,12 @@ case class ArrayType(elementType: DataType, containsNull: Boolean) extends DataT
636
691
(" type" -> typeName) ~
637
692
(" elementType" -> elementType.jsonValue) ~
638
693
(" containsNull" -> containsNull)
694
+
695
+ /**
696
+ * The default size of a value of the ArrayType is 100 * the default size of the element type.
697
+ * (We assume that there are 100 elements).
698
+ */
699
+ override def defaultSize : Int = 100 * elementType.defaultSize
639
700
}
640
701
641
702
@@ -805,6 +866,11 @@ case class StructType(fields: Array[StructField]) extends DataType with Seq[Stru
805
866
override def length : Int = fields.length
806
867
807
868
override def iterator : Iterator [StructField ] = fields.iterator
869
+
870
+ /**
871
+ * The default size of a value of the StructType is the total default sizes of all field types.
872
+ */
873
+ override def defaultSize : Int = fields.map(_.dataType.defaultSize).sum
808
874
}
809
875
810
876
@@ -848,6 +914,13 @@ case class MapType(
848
914
(" keyType" -> keyType.jsonValue) ~
849
915
(" valueType" -> valueType.jsonValue) ~
850
916
(" valueContainsNull" -> valueContainsNull)
917
+
918
+ /**
919
+ * The default size of a value of the MapType is
920
+ * 100 * (the default size of the key type + the default size of the value type).
921
+ * (We assume that there are 100 elements).
922
+ */
923
+ override def defaultSize : Int = 100 * (keyType.defaultSize + valueType.defaultSize)
851
924
}
852
925
853
926
@@ -896,4 +969,9 @@ abstract class UserDefinedType[UserType] extends DataType with Serializable {
896
969
* Class object for the UserType
897
970
*/
898
971
def userClass : java.lang.Class [UserType ]
972
+
973
+ /**
974
+ * The default size of a value of the UserDefinedType is 4096 bytes.
975
+ */
976
+ override def defaultSize : Int = 4096
899
977
}
0 commit comments