Class: Arrow::Datum

Inherits:
Object
  • Object
show all
Defined in:
lib/arrow/datum.rb

Class Method Summary collapse

Class Method Details

.try_convert(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/arrow/datum.rb', line 22

def try_convert(value)
  case value
  when Table
    TableDatum.new(value)
  when Array
    ArrayDatum.new(value)
  when ChunkedArray
    ChunkedArrayDatum.new(value)
  when Column
    ChunkedArrayDatum.new(value.data)
  when Scalar
    ScalarDatum.new(value)
  when ::Array
    ArrayDatum.new(ArrayBuilder.build(value))
  when Integer
    case value
    when (0..((2 ** 8) - 1))
      try_convert(UInt8Scalar.new(value))
    when ((-(2 ** 7))..((2 ** 7) - 1))
      try_convert(Int8Scalar.new(value))
    when (0..((2 ** 16) - 1))
      try_convert(UInt16Scalar.new(value))
    when ((-(2 ** 15))..((2 ** 15) - 1))
      try_convert(Int16Scalar.new(value))
    when (0..((2 ** 32) - 1))
      try_convert(UInt32Scalar.new(value))
    when ((-(2 ** 31))..((2 ** 31) - 1))
      try_convert(Int32Scalar.new(value))
    when (0..((2 ** 64) - 1))
      try_convert(UInt64Scalar.new(value))
    when ((-(2 ** 63))..((2 ** 63) - 1))
      try_convert(Int64Scalar.new(value))
    else
      nil
    end
  when Float
    try_convert(DoubleScalar.new(value))
  when true, false
    try_convert(BooleanScalar.new(value))
  when String
    if value.ascii_only? or value.encoding == Encoding::UTF_8
      if value.bytesize <= ((2 ** 31) - 1)
        try_convert(StringScalar.new(value))
      else
        try_convert(LargeStringScalar.new(value))
      end
    else
      if value.bytesize <= ((2 ** 31) - 1)
        try_convert(BinaryScalar.new(value))
      else
        try_convert(LargeBinaryScalar.new(value))
      end
    end
  when Date
    date32_value = (value - Date32ArrayBuilder::UNIX_EPOCH).to_i
    try_convert(Date32Scalar.new(date32_value))
  when Time
    case value.unit
    when TimeUnit::SECOND, TimeUnit::MILLI
      data_type = Time32DataType.new(value.unit)
      scalar_class = Time32Scalar
    else
      data_type = Time64DataType.new(value.unit)
      scalar_class = Time64Scalar
    end
    try_convert(scalar_class.new(data_type, value.value))
  when ::Time
    data_type = TimestampDataType.new(:nano)
    timestamp_value = value.to_i * 1_000_000_000 + value.nsec
    try_convert(TimestampScalar.new(data_type, timestamp_value))
  when Decimal128
    data_type = TimestampDataType.new(:nano)
    timestamp_value = value.to_i * 1_000_000_000 + value.nsec
    try_convert(Decimal128Scalar.new(data_type, timestamp_value))
  else
    nil
  end
end