Class: Embulk::Input::Presto::TypeConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/input/presto/type_converter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTypeConverter

Returns a new instance of TypeConverter.



6
7
# File 'lib/embulk/input/presto/type_converter.rb', line 6

def initialize
end

Class Method Details

.get_type(type) ⇒ Object



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
# File 'lib/embulk/input/presto/type_converter.rb', line 42

def self.get_type(type)
  if type.start_with?("boolean")
    :boolean
  elsif type.start_with?("tinyint")
    :boolean
  elsif type.start_with?("bigint")
    :long
  elsif type.start_with?("integer")
    :long
  elsif type.start_with?("double")
    :double
  elsif type.start_with?("decimal")
    :double
  elsif type.start_with?("varchar")
    :string
  elsif type.start_with?("varbinary")
    :string
  elsif type.start_with?("json")
    :json
  elsif type.start_with?("date")
    :timestamp
  elsif type.start_with?("time")
    :timestamp
  elsif type.start_with?("time with time zone")
    :timestamp
  elsif type.start_with?("timestamp")
    :timestamp
  elsif type.start_with?("timestamp with time zone")
    :timestamp
  elsif type.start_with?("interval year to month")
    :timestamp
  elsif type.start_with?("interval day to second")
    :timestamp
  elsif type.start_with?("array")
    :json
  elsif type.start_with?("map")
    :json
  elsif type.start_with?("row")
    :json
  end
end

Instance Method Details

#convert_value(value, field) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/embulk/input/presto/type_converter.rb', line 9

def convert_value(value, field)
  return nil if value.nil?
  case field["type"]
  when :string
    value
  when :long
    value.to_i
  when :double
    value.to_f
  when :boolean
    if value.is_a?(TrueClass) || value.is_a?(FalseClass)
      value
    elsif value.class == Fixnum
      value == 0 ? false : true
    else
      downcased_val = value.downcase
      case downcased_val
      when 'true' then true
      when 'false' then false
      when '1' then true
      when '0' then false
      else nil
      end
    end
  when :timestamp
    Time.parse(value)
  when :json
    value
  else
    raise "Unsupported type #{field['type']}"
  end
end