fix odd convert from string to integer and float#175
Merged
kaisecheng merged 7 commits intologstash-plugins:mainfrom Oct 31, 2025
Merged
fix odd convert from string to integer and float#175kaisecheng merged 7 commits intologstash-plugins:mainfrom
kaisecheng merged 7 commits intologstash-plugins:mainfrom
Conversation
5ea5076 to
f60229c
Compare
Contributor
Author
benchmark Integer(Float(value)) VS Integer(BigDecimal(value))require 'benchmark/ips'
require 'bigdecimal'
test_cases = {
'small_integer' => '123',
'large_integer' => '9007199254740993',
'decimal' => '123.456',
'large_decimal' => '1234567.89012345',
'scientific' => '1.23e2',
'scientific_large' => '1.23e10'
}
test_cases.each do |name, value|
clean_value = value.delete(',')
puts "Testing: #{name} (#{value})"
puts "-" * 80
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
x.report("Integer(Float(value))") do
Integer(Float(clean_value))
end
x.report("Integer(BigDecimal(value))") do
Integer(BigDecimal(clean_value))
end
x.compare!
end
puts
end
|
Contributor
Author
|
created an issue upstream for the difference of jruby and cruby jruby/jruby#9044 |
jsvd
reviewed
Oct 31, 2025
lib/logstash/filters/mutate.rb
Outdated
Comment on lines
365
to
366
| # floating point number. BigDecimal() can't parse hex string | ||
| return Integer(BigDecimal(value)) if value.count('.') == 1 |
Member
There was a problem hiding this comment.
At this point we're only left with decimals and exponentials, and I think that to_i can handle anything that is not exponential, and Float handle the exponentials?
Suggested change
| # floating point number. BigDecimal() can't parse hex string | |
| return Integer(BigDecimal(value)) if value.count('.') == 1 | |
| return Float(value).to_i if value.include?("e") | |
| return value.to_i |
This change makes the performance hit less significant:
Testing: small_integer (123)
Comparison:
Test Original: 2117612.5 i/s
Test Change: 1971480.7 i/s - 1.07x slower
Testing: large_integer (9007199254740993)
Comparison:
Test Original: 1780093.5 i/s
Test Change: 1616786.2 i/s - 1.10x slower
Testing: decimal (123.456)
Comparison:
Test Original: 2104084.0 i/s
Test Change: 1956469.1 i/s - 1.08x slower
Testing: large_decimal (1234567.89012345)
Comparison:
Test Original: 2085646.4 i/s
Test Change: 1905504.3 i/s - 1.09x slower
Testing: scientific (1.23e2)
Comparison:
Test Original: 2004532.4 i/s
Test Change: 1687208.7 i/s - 1.19x slower
Testing: scientific_large (1.23e10)
Comparison:
Test Original: 2115260.8 i/s
Test Change: 1680841.1 i/s - 1.26x slower
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix
convertto covert hexadecimal notation and scientific notation string to float and integerAdd support for hexadecimal float notation (0x1.edcdp6) and scientific
notation (1.230000e+02) in convert option for
integerandfloat. Includes manualsign parsing for JRuby compatibility and BigDecimal for precision.
Fix: #174
JRuby cannot parse negative hex string to float
Float("-0x7b")and uppsercase hexFloat("0X1A"), but CRuby can.JRuby 9.4.5.0 Logstash 8.13 also cannot parse floating point hex string
Float("0x1.edcdp6")This commit adds parsing for signed hex strings for Logstash version >= 8.14. For older versions, type conversions fall back to the old implementation.