Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TIME type conversion to string converter #168

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/embulk/output/bigquery/value_converter_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ def string_converter
val # Users must care of BQ timestamp format
}
end
when 'TIME'
# TimeWithZone doesn't affect any change to the time value
Proc.new {|val|
next nil if val.nil?
with_typecast_error(val) do |val|
TimeWithZone.set_zone_offset(Time.parse(val), zone_offset).strftime("%H:%M:%S.%6N")
end
p-eye marked this conversation as resolved.
Show resolved Hide resolved
}
when 'RECORD'
Proc.new {|val|
next nil if val.nil?
Expand Down Expand Up @@ -271,6 +279,11 @@ def timestamp_converter
next nil if val.nil?
val.localtime(zone_offset).strftime("%Y-%m-%d %H:%M:%S.%6N")
}
when 'TIME'
Proc.new {|val|
next nil if val.nil?
val.localtime(zone_offset).strftime("%H:%M:%S.%6N")
}
else
raise NotSupportedType, "cannot take column type #{type} for timestamp column"
end
Expand Down
35 changes: 35 additions & 0 deletions test/test_value_converter_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ def test_datetime
assert_equal "2016-02-26 00:00:00", converter.call("2016-02-26 00:00:00")
end

def test_time
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'TIME').create_converter
assert_equal nil, converter.call(nil)
assert_equal "00:03:22.000000", converter.call("00:03:22")
assert_equal "15:22:00.000000", converter.call("3:22 PM")
assert_equal "03:22:00.000000", converter.call("3:22 AM")
assert_equal "00:00:00.000000", converter.call("2016-02-26 00:00:00")

# TimeWithZone doesn't affect any change to the time value
converter = ValueConverterFactory.new(
SCHEMA_TYPE, 'TIME', timezone: 'Asia/Tokyo'
).create_converter
assert_equal "15:00:01.000000", converter.call("15:00:01")

assert_raise { converter.call('foo') }
end

def test_record
hiroyuki-sato marked this conversation as resolved.
Show resolved Hide resolved
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'RECORD').create_converter
assert_equal({'foo'=>'foo'}, converter.call(%Q[{"foo":"foo"}]))
Expand Down Expand Up @@ -350,6 +367,24 @@ def test_datetime
assert_raise { converter.call('foo') }
end

def test_time
converter = ValueConverterFactory.new(SCHEMA_TYPE, 'TIME').create_converter
assert_equal nil, converter.call(nil)
timestamp = Time.parse("2016-02-26 00:00:00.500000 +00:00")
expected = "00:00:00.500000"
assert_equal expected, converter.call(timestamp)

converter = ValueConverterFactory.new(
SCHEMA_TYPE, 'TIME', timezone: 'Asia/Tokyo'
).create_converter
assert_equal nil, converter.call(nil)
timestamp = Time.parse("2016-02-25 15:00:00.500000 +00:00")
expected = "00:00:00.500000"
assert_equal expected, converter.call(timestamp)

p-eye marked this conversation as resolved.
Show resolved Hide resolved
assert_raise { converter.call('foo') }
end

def test_record
assert_raise { ValueConverterFactory.new(SCHEMA_TYPE, 'RECORD').create_converter }
end
Expand Down