Skip to content

Commit 2199b2c

Browse files
committed
[add]NUMERIC対応の追加
1 parent b20a8a0 commit 2199b2c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/embulk/output/bigquery/value_converter_factory.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'time'
22
require 'time_with_zone'
33
require 'json'
4+
require 'bigdecimal'
45
require_relative 'helper'
56

67
module Embulk
@@ -14,6 +15,7 @@ class TypeCastError < StandardError; end
1415

1516
DEFAULT_TIMESTAMP_FORMAT = "%Y-%m-%d %H:%M:%S.%6N" # BigQuery timestamp format
1617
DEFAULT_TIMEZONE = "UTC"
18+
DEFAULT_SCALE = 9
1719

1820
# @param [Hash] task
1921
# @option task [String] default_timestamp_format
@@ -29,13 +31,15 @@ def self.create_converters(task, schema)
2931
column_name = column[:name]
3032
embulk_type = column[:type]
3133
column_option = column_options_map[column_name] || {}
34+
scale = column_option['scale'] || DEFAULT_SCALE
3235
self.new(
3336
embulk_type, column_option['type'],
3437
timestamp_format: column_option['timestamp_format'],
3538
timezone: column_option['timezone'],
3639
strict: column_option['strict'],
3740
default_timestamp_format: default_timestamp_format,
3841
default_timezone: default_timezone,
42+
scale: scale
3943
).create_converter
4044
end
4145
end
@@ -46,7 +50,8 @@ def initialize(
4650
embulk_type, type = nil,
4751
timestamp_format: nil, timezone: nil, strict: nil,
4852
default_timestamp_format: DEFAULT_TIMESTAMP_FORMAT,
49-
default_timezone: DEFAULT_TIMEZONE
53+
default_timezone: DEFAULT_TIMEZONE,
54+
scale: DEFAULT_SCALE
5055
)
5156
@embulk_type = embulk_type
5257
@type = (type || Helper.bq_type_from_embulk_type(embulk_type)).upcase
@@ -55,6 +60,7 @@ def initialize(
5560
@timezone = timezone || default_timezone
5661
@zone_offset = TimeWithZone.zone_offset(@timezone)
5762
@strict = strict.nil? ? true : strict
63+
@scale = scale
5864
end
5965

6066
def create_converter
@@ -231,6 +237,13 @@ def string_converter
231237
JSON.parse(val)
232238
end
233239
}
240+
when 'NUMERIC'
241+
Proc.new {|val|
242+
next nil if val.nil?
243+
with_typecast_error(val) do |val|
244+
BigDecimal(val).round(@scale, BigDecimal::ROUND_CEILING)
245+
end
246+
}
234247
else
235248
raise NotSupportedType, "cannot take column type #{type} for string column"
236249
end

0 commit comments

Comments
 (0)