Module: DatabricksSql::TypeCoercer

Defined in:
lib/databricks_sql/type_coercer.rb

Class Method Summary collapse

Class Method Details

.coerce_row(row, column_schema) ⇒ Object



15
16
17
18
19
20
# File 'lib/databricks_sql/type_coercer.rb', line 15

def coerce_row(row, column_schema)
  row.each_with_object({}) do |(key, value), coerced|
    target_type = column_schema[key.to_s] || column_schema[key.to_sym]
    coerced[key] = coerce_value(value, target_type)
  end
end

.coerce_rows(rows, column_schema) ⇒ Object



9
10
11
12
13
# File 'lib/databricks_sql/type_coercer.rb', line 9

def coerce_rows(rows, column_schema)
  return rows if column_schema.nil? || column_schema.empty?

  rows.map { |row| coerce_row(row, column_schema) }
end

.coerce_value(value, target_type) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/databricks_sql/type_coercer.rb', line 22

def coerce_value(value, target_type)
  return value if value.nil? || target_type.nil?

  type = target_type.to_s.downcase

  case type
  when "string"
    value.to_s
  when "integer", "int"
    Integer(value)
  when "float", "double", "decimal"
    Float(value)
  when "boolean", "bool"
    coerce_boolean(value)
  when "time", "datetime", "timestamp"
    Time.parse(value.to_s)
  else
    value
  end
rescue ArgumentError, TypeError
  value
end