@@ -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,29 +238,16 @@ 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
246
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
- def defaultSizeOf (dataType : NativeType ) = dataType match {
248
- case IntegerType => 4
249
- case BooleanType => 1
250
- case LongType => 8
251
- case DoubleType => 8
252
- case FloatType => 4
253
- case ShortType => 2
254
- case ByteType => 1
255
- case StringType => 4096
256
- case decimal : DecimalType => 4096
257
- case TimestampType => 8
258
- case DateType => 8
259
- case BinaryType => 8
260
- }
261
251
}
262
252
263
253
@@ -305,6 +295,11 @@ case object StringType extends NativeType with PrimitiveType {
305
295
private [sql] type JvmType = String
306
296
@ transient private [sql] lazy val tag = ScalaReflectionLock .synchronized { typeTag[JvmType ] }
307
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
308
303
}
309
304
310
305
@@ -329,6 +324,11 @@ case object BinaryType extends NativeType with PrimitiveType {
329
324
x.length - y.length
330
325
}
331
326
}
327
+
328
+ /**
329
+ * The default size of a value of the BinaryType is 4096 bytes.
330
+ */
331
+ override def defaultSize : Int = 4096
332
332
}
333
333
334
334
@@ -344,6 +344,11 @@ case object BooleanType extends NativeType with PrimitiveType {
344
344
private [sql] type JvmType = Boolean
345
345
@ transient private [sql] lazy val tag = ScalaReflectionLock .synchronized { typeTag[JvmType ] }
346
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
347
352
}
348
353
349
354
@@ -364,6 +369,11 @@ case object TimestampType extends NativeType {
364
369
private [sql] val ordering = new Ordering [JvmType ] {
365
370
def compare (x : Timestamp , y : Timestamp ) = x.compareTo(y)
366
371
}
372
+
373
+ /**
374
+ * The default size of a value of the TimestampType is 8 bytes.
375
+ */
376
+ override def defaultSize : Int = 8
367
377
}
368
378
369
379
@@ -384,6 +394,11 @@ case object DateType extends NativeType {
384
394
private [sql] val ordering = new Ordering [JvmType ] {
385
395
def compare (x : Date , y : Date ) = x.compareTo(y)
386
396
}
397
+
398
+ /**
399
+ * The default size of a value of the DateType is 8 bytes.
400
+ */
401
+ override def defaultSize : Int = 8
387
402
}
388
403
389
404
@@ -430,6 +445,11 @@ case object LongType extends IntegralType {
430
445
private [sql] val numeric = implicitly[Numeric [Long ]]
431
446
private [sql] val integral = implicitly[Integral [Long ]]
432
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
433
453
}
434
454
435
455
@@ -447,6 +467,11 @@ case object IntegerType extends IntegralType {
447
467
private [sql] val numeric = implicitly[Numeric [Int ]]
448
468
private [sql] val integral = implicitly[Integral [Int ]]
449
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
450
475
}
451
476
452
477
@@ -464,6 +489,11 @@ case object ShortType extends IntegralType {
464
489
private [sql] val numeric = implicitly[Numeric [Short ]]
465
490
private [sql] val integral = implicitly[Integral [Short ]]
466
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
467
497
}
468
498
469
499
@@ -481,6 +511,11 @@ case object ByteType extends IntegralType {
481
511
private [sql] val numeric = implicitly[Numeric [Byte ]]
482
512
private [sql] val integral = implicitly[Integral [Byte ]]
483
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
484
519
}
485
520
486
521
@@ -535,6 +570,11 @@ case class DecimalType(precisionInfo: Option[PrecisionInfo]) extends FractionalT
535
570
case Some (PrecisionInfo (precision, scale)) => s " DecimalType( $precision, $scale) "
536
571
case None => " DecimalType()"
537
572
}
573
+
574
+ /**
575
+ * The default size of a value of the DecimalType is 4096 bytes.
576
+ */
577
+ override def defaultSize : Int = 4096
538
578
}
539
579
540
580
@@ -585,6 +625,11 @@ case object DoubleType extends FractionalType {
585
625
private [sql] val fractional = implicitly[Fractional [Double ]]
586
626
private [sql] val ordering = implicitly[Ordering [JvmType ]]
587
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
588
633
}
589
634
590
635
@@ -603,6 +648,11 @@ case object FloatType extends FractionalType {
603
648
private [sql] val fractional = implicitly[Fractional [Float ]]
604
649
private [sql] val ordering = implicitly[Ordering [JvmType ]]
605
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
606
656
}
607
657
608
658
@@ -641,6 +691,12 @@ case class ArrayType(elementType: DataType, containsNull: Boolean) extends DataT
641
691
(" type" -> typeName) ~
642
692
(" elementType" -> elementType.jsonValue) ~
643
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
644
700
}
645
701
646
702
@@ -810,6 +866,11 @@ case class StructType(fields: Array[StructField]) extends DataType with Seq[Stru
810
866
override def length : Int = fields.length
811
867
812
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
813
874
}
814
875
815
876
@@ -853,6 +914,13 @@ case class MapType(
853
914
(" keyType" -> keyType.jsonValue) ~
854
915
(" valueType" -> valueType.jsonValue) ~
855
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)
856
924
}
857
925
858
926
@@ -901,4 +969,9 @@ abstract class UserDefinedType[UserType] extends DataType with Serializable {
901
969
* Class object for the UserType
902
970
*/
903
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
904
977
}
0 commit comments