Skip to content

Commit 61fade9

Browse files
authored
Merge pull request #23 from trocco-io/add_column_type_numeric
[Ruby版][ADD]NUMERIC対応
2 parents be2ad7c + 3f863c9 commit 61fade9

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

.github/workflows/gem-push.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Ruby Gem
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
tags:
7+
- 'v*'
8+
9+
jobs:
10+
build:
11+
name: Build + Publish
12+
runs-on: ubuntu-latest
13+
permissions:
14+
packages: write
15+
contents: read
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Set up Ruby 2.7
19+
uses: ruby/setup-ruby@v1
20+
with:
21+
ruby-version: 2.7
22+
- name: push gem
23+
uses: trocco-io/push-gem-to-gpr-action@v1
24+
with:
25+
github-token: "${{ secrets.GITHUB_TOKEN }}"

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source 'https://rubygems.org/'
22

33
gemspec
4-
gem 'embulk'
4+
gem 'embulk', '< 0.10'
55
gem 'liquid', '= 4.0.0' # the version included in embulk.jar
66
gem 'embulk-parser-none'
77
gem 'embulk-parser-jsonl'

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,18 +307,20 @@ Column options are used to aid guessing BigQuery schema, or to define conversion
307307

308308
- **column_options**: advanced: an array of options for columns
309309
- **name**: column name
310-
- **type**: BigQuery type such as `BOOLEAN`, `INTEGER`, `FLOAT`, `STRING`, `TIMESTAMP`, `DATETIME`, `DATE`, and `RECORD`. See belows for supported conversion type.
310+
- **type**: BigQuery type such as `BOOLEAN`, `INTEGER`, `FLOAT`, `STRING`, `TIMESTAMP`, `DATETIME`, `DATE`, `RECORD`, and `NUMERIC`. See belows for supported conversion type.
311311
- boolean: `BOOLEAN`, `STRING` (default: `BOOLEAN`)
312312
- long: `BOOLEAN`, `INTEGER`, `FLOAT`, `STRING`, `TIMESTAMP` (default: `INTEGER`)
313313
- double: `INTEGER`, `FLOAT`, `STRING`, `TIMESTAMP` (default: `FLOAT`)
314314
- string: `BOOLEAN`, `INTEGER`, `FLOAT`, `STRING`, `TIMESTAMP`, `DATETIME`, `DATE`, `RECORD` (default: `STRING`)
315315
- timestamp: `INTEGER`, `FLOAT`, `STRING`, `TIMESTAMP`, `DATETIME`, `DATE` (default: `TIMESTAMP`)
316316
- json: `STRING`, `RECORD` (default: `STRING`)
317+
- numeric: `STRING`
317318
- **mode**: BigQuery mode such as `NULLABLE`, `REQUIRED`, and `REPEATED` (string, default: `NULLABLE`)
318319
- **fields**: Describes the nested schema fields if the type property is set to RECORD. Please note that this is **required** for `RECORD` column.
319320
- **timestamp_format**: timestamp format to convert into/from `timestamp` (string, default is `default_timestamp_format`)
320321
- **timezone**: timezone to convert into/from `timestamp`, `date` (string, default is `default_timezone`).
321322
- **description**: description for the column.
323+
- **scale**: optional, [scale](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types?hl=ja#decimal_types) for numeric column (long, default is 9).
322324
- **default_timestamp_format**: default timestamp format for column_options (string, default is "%Y-%m-%d %H:%M:%S.%6N")
323325
- **default_timezone**: default timezone for column_options (string, default is "UTC")
324326

embulk-output-bigquery.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |spec|
22
spec.name = "embulk-output-bigquery"
3-
spec.version = "0.6.13"
3+
spec.version = "0.7.0"
44
spec.authors = ["Satoshi Akama", "Naotoshi Seo"]
55
spec.summary = "Google BigQuery output plugin for Embulk"
66
spec.description = "Embulk plugin that insert records to Google BigQuery."

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)