dentaku 4.2.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3346a9667db13855bf31fd78573e74772517391158da77a4c240533f05eb8ca
4
- data.tar.gz: 1e982004771efb2e0dfc1702ca475007ebaa1c8c7d0d10023694a5a66c694e05
3
+ metadata.gz: 8c72201c74cc7bb734b0b339ed3c7b013c84ca168c9f2227012c8ac17036dd2b
4
+ data.tar.gz: 3449be5cfad83709a7ca74f18f4cf737f2dbbd5d7a64232aa8f5eb8afae1cd0c
5
5
  SHA512:
6
- metadata.gz: 0160a7b2170ff95648be302ce6feb40655f362a90d72ad06255913747ec59b02da69e02e070110ad39677c946faf2098419102d220c2f30c8ebc129c6096140d
7
- data.tar.gz: 60ff9d3aa81fa35c31565ced324ca740af6c7c9e5d0aacc9f56c459bbe758fd9e6eda19594d1660d9d1e46ec7cd36d544eac4c5ec10f12b6ddf3a20b1e28eeb9
6
+ metadata.gz: 2beb81c610525fb4507b4da06669ae9720a35f598d75ccfc3125212e077ef9bb0b1370b5217cb45bce113d6dcb3247c9a8650d49459b50b0994fa922ffbdaa35
7
+ data.tar.gz: 06d9128f3409ce278f5a8e909e30c256c1d741f474da3277bcf069e670c930accb84a606e1d742e1cb4955871d8d8593ce80e87784dede3a0d0bd0520e01feec
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Change Log
2
2
 
3
+ ## [v4.2.1] 2026-11-23
4
+ - `ROUNDUP` and `ROUNDDOWN` no longer hang on a very negative precision.
5
+ Since 4.2.0, `ROUNDUP(7, -1000000000)` built a billion-digit power of ten
6
+ in a single C call that `Timeout` can't interrupt, so one short formula
7
+ could tie up a thread. A precision past the number's digits now returns
8
+ `0` when rounding toward zero, and raises `Dentaku::ArgumentError` when
9
+ rounding away from zero would need more than 10,000 digits
10
+ - **behavior change:** numbers are limited to 10,000 digits
11
+ (`Dentaku::NumericLimits::MAX_DIGITS`). Ruby does bignum arithmetic in
12
+ single C calls that `Timeout` can't interrupt, so `7 ^ 100000000`,
13
+ `1 << 10000000000`, `ROUND(1e100000000)` or `LEFT("abc", 1e100000000)`
14
+ could tie up a thread for hours. `^` and `<<` are checked before they run,
15
+ arithmetic, bitwise and `MUL` results are checked after, and function
16
+ arguments are checked when coerced. Past the limit they raise
17
+ `Dentaku::ArgumentError` (`evaluate` returns `nil`)
18
+
3
19
  ## [v4.2.0] 2026-11-18
4
20
  - `XOR` works as an operator, like `AND` and `OR`: `a XOR b` (#344). It
5
21
  shares their precedence and treats `null` as false, as the function does
@@ -1,5 +1,6 @@
1
1
  require_relative './operation'
2
2
  require_relative '../date_arithmetic'
3
+ require_relative '../numeric_limits'
3
4
  require 'bigdecimal'
4
5
  require 'bigdecimal/util'
5
6
 
@@ -37,6 +38,7 @@ module Dentaku
37
38
  def calculate(left_value, right_value)
38
39
  l = cast(left_value)
39
40
  r = cast(right_value)
41
+ check_operands!(l, r)
40
42
 
41
43
  result = begin
42
44
  l.public_send(operator, r)
@@ -65,6 +67,7 @@ module Dentaku
65
67
  # as the same rejection newer Rubies raise -- otherwise Infinity leaks
66
68
  # out of Calculator#evaluate, which promises nil on failure (#332).
67
69
  def validate_result(result, l, r)
70
+ Dentaku::NumericLimits.check!(result)
68
71
  return result unless nonfinite?(result)
69
72
  return result if nonfinite?(l) || nonfinite?(r)
70
73
 
@@ -76,9 +79,16 @@ module Dentaku
76
79
  val.is_a?(::Numeric) && val.respond_to?(:finite?) && !val.finite?
77
80
  end
78
81
 
82
+ # Hook for operators that can build a huge number from small operands
83
+ def check_operands!(_l, _r); end
84
+
79
85
  def cast(val)
80
86
  validate_value(val)
81
- Dentaku::NumericParser.ensure_numeric(val) || val
87
+ # strings here already match the numeric format; ensure_numeric! also
88
+ # rejects numbers past NumericLimits::MAX_DIGITS
89
+ return Dentaku::NumericParser.ensure_numeric!(val) if val.is_a?(::Numeric) || val.is_a?(::String)
90
+
91
+ val
82
92
  end
83
93
 
84
94
  def decimal(val)
@@ -285,6 +295,12 @@ module Dentaku
285
295
  def self.precedence
286
296
  30
287
297
  end
298
+
299
+ private
300
+
301
+ def check_operands!(l, r)
302
+ Dentaku::NumericLimits.check_power!(l, r)
303
+ end
288
304
  end
289
305
  end
290
306
  end
@@ -1,4 +1,5 @@
1
1
  require_relative './operation'
2
+ require_relative '../numeric_limits'
2
3
 
3
4
  module Dentaku
4
5
  module AST
@@ -6,8 +7,11 @@ module Dentaku
6
7
  def value(context = {})
7
8
  left_value = left.value(context)
8
9
  right_value = right.value(context)
10
+ Dentaku::NumericLimits.check!(left_value)
11
+ Dentaku::NumericLimits.check!(right_value)
12
+ check_operands!(left_value, right_value)
9
13
 
10
- left_value.public_send(operator, right_value)
14
+ Dentaku::NumericLimits.check!(left_value.public_send(operator, right_value))
11
15
  rescue NoMethodError
12
16
  raise Dentaku::ArgumentError.for(:incompatible_type, actual: left_value, expected: Integer),
13
17
  "#{self.class} requires integer operands, but got #{left_value.class}"
@@ -15,6 +19,11 @@ module Dentaku
15
19
  raise Dentaku::ArgumentError.for(:incompatible_type, actual: right_value, expected: Integer),
16
20
  "#{self.class} requires integer operands, but got #{right_value.class}"
17
21
  end
22
+
23
+ private
24
+
25
+ # Hook for operators that can build a huge number from small operands
26
+ def check_operands!(_left_value, _right_value); end
18
27
  end
19
28
 
20
29
  class BitwiseOr < Bitwise
@@ -33,6 +42,12 @@ module Dentaku
33
42
  def operator
34
43
  :<<
35
44
  end
45
+
46
+ private
47
+
48
+ def check_operands!(left_value, right_value)
49
+ Dentaku::NumericLimits.check_shift!(left_value, right_value)
50
+ end
36
51
  end
37
52
 
38
53
  class BitwiseShiftRight < Bitwise
@@ -0,0 +1,37 @@
1
+ require 'bigdecimal'
2
+ require_relative '../../numeric_limits'
3
+ require_relative '../../numeric_parser'
4
+
5
+ module Dentaku
6
+ # Shared by ROUNDUP (:ceil) and ROUNDDOWN (:floor).
7
+ module DirectedRounding
8
+ def self.call(numeric, precision, direction)
9
+ precision = NumericParser.ensure_numeric!(precision || 0).to_i
10
+ numeric = NumericParser.ensure_numeric!(numeric)
11
+ # Float#ceil(digits) works on the binary value, so 0.07.ceil(2) is 0.08;
12
+ # going through the shortest decimal form rounds the number as written
13
+ decimal = numeric.is_a?(Float) ? BigDecimal(numeric.to_s) : numeric
14
+
15
+ # Integer#ceil(-n) builds 10**n in one uninterruptible C call, even when
16
+ # the answer is 0, so a precision past the number's digits is answered here
17
+ if precision < 0 && -precision > BigDecimal(decimal).exponent
18
+ return beyond_digits(numeric, precision, direction)
19
+ end
20
+
21
+ result = decimal.public_send(direction, precision)
22
+ result = result.to_f if numeric.is_a?(Float)
23
+ precision <= 0 ? result.to_i : result
24
+ end
25
+
26
+ # |numeric| < 10**-precision, so the result is 0 or ±10**-precision
27
+ def self.beyond_digits(numeric, precision, direction)
28
+ away_from_zero = direction == :ceil ? numeric.positive? : numeric.negative?
29
+ return 0 unless away_from_zero
30
+
31
+ # 10**n has n + 1 digits
32
+ NumericLimits.check_digits!(1 - precision)
33
+ (numeric.positive? ? 1 : -1) * 10**-precision
34
+ end
35
+ private_class_method :beyond_digits
36
+ end
37
+ end
@@ -8,5 +8,6 @@ Dentaku::AST::Function.register(:mul, :numeric, ->(*args) {
8
8
  )
9
9
  end
10
10
 
11
- args.flatten.map { |arg| Dentaku::NumericParser.ensure_numeric!(arg) }.reduce(1, :*)
11
+ args.flatten.map { |arg| Dentaku::NumericParser.ensure_numeric!(arg) }
12
+ .reduce(1) { |product, arg| Dentaku::NumericLimits.check!(product * arg) }
12
13
  })
@@ -1,14 +1,6 @@
1
1
  require_relative '../function'
2
+ require_relative './directed_rounding'
2
3
 
3
4
  Dentaku::AST::Function.register(:rounddown, :numeric, lambda { |numeric, precision = 0|
4
- precision = Dentaku::NumericParser.ensure_numeric!(precision || 0).to_i
5
- numeric = Dentaku::NumericParser.ensure_numeric!(numeric)
6
- # Float#floor(digits) works on the binary value, so 0.07.ceil(2) is 0.08;
7
- # going through the shortest decimal form rounds the number as written
8
- result = if numeric.is_a?(Float)
9
- BigDecimal(numeric.to_s).floor(precision).to_f
10
- else
11
- numeric.floor(precision)
12
- end
13
- precision <= 0 ? result.to_i : result
5
+ Dentaku::DirectedRounding.call(numeric, precision, :floor)
14
6
  })
@@ -1,14 +1,6 @@
1
1
  require_relative '../function'
2
+ require_relative './directed_rounding'
2
3
 
3
4
  Dentaku::AST::Function.register(:roundup, :numeric, lambda { |numeric, precision = 0|
4
- precision = Dentaku::NumericParser.ensure_numeric!(precision || 0).to_i
5
- numeric = Dentaku::NumericParser.ensure_numeric!(numeric)
6
- # Float#ceil(digits) works on the binary value, so 0.07.ceil(2) is 0.08;
7
- # going through the shortest decimal form rounds the number as written
8
- result = if numeric.is_a?(Float)
9
- BigDecimal(numeric.to_s).ceil(precision).to_f
10
- else
11
- numeric.ceil(precision)
12
- end
13
- precision <= 0 ? result.to_i : result
5
+ Dentaku::DirectedRounding.call(numeric, precision, :ceil)
14
6
  })
@@ -0,0 +1,67 @@
1
+ require 'bigdecimal'
2
+ require_relative './exceptions'
3
+
4
+ module Dentaku
5
+ # Ruby does bignum arithmetic in single C calls that Timeout can't
6
+ # interrupt, so a short formula like `7 ^ 100000000` or
7
+ # `ROUND(1e100000000)` could tie up a thread for hours. Numbers are kept
8
+ # to MAX_DIGITS decimal digits: values longer than that are rejected where
9
+ # they're used, and the operators that grow numbers fastest (`^`, `<<`)
10
+ # are checked before they run.
11
+ module NumericLimits
12
+ MAX_DIGITS = 10_000
13
+
14
+ LOG10_2 = Math.log10(2)
15
+
16
+ class << self
17
+ # Approximate decimal digits needed to hold value exactly. For a
18
+ # BigDecimal that is the larger of its integer digits (what to_i
19
+ # would build) and its significant digits (what arithmetic carries).
20
+ def digits(value)
21
+ case value
22
+ when ::Integer
23
+ (value.bit_length * LOG10_2).ceil
24
+ when ::BigDecimal
25
+ value.finite? ? [value.exponent, value.n_significant_digits].max : 0
26
+ else
27
+ 0
28
+ end
29
+ end
30
+
31
+ def check!(value)
32
+ check_digits!(digits(value), value.class)
33
+ value
34
+ end
35
+
36
+ # For callers that know a result's size before building it
37
+ def check_digits!(count, type = ::Integer)
38
+ raise too_long(type) if count > MAX_DIGITS
39
+ end
40
+
41
+ # base ** exponent has about |exponent| times as many digits as base
42
+ def check_power!(base, exponent)
43
+ return unless base.is_a?(::Integer) || base.is_a?(::BigDecimal)
44
+ return unless exponent.is_a?(::Numeric) && exponent.real?
45
+ return if base.zero? || base.abs == 1
46
+
47
+ magnitude = base.is_a?(::Integer) ? Math.log10(base.abs) : digits(base)
48
+ check_digits!(magnitude * exponent.abs, base.class)
49
+ end
50
+
51
+ def check_shift!(value, bits)
52
+ return unless value.is_a?(::Integer) && bits.is_a?(::Integer)
53
+ return if value.zero? || bits <= 0
54
+
55
+ check_digits!((value.bit_length + bits) * LOG10_2)
56
+ end
57
+
58
+ private
59
+
60
+ # Carries only the class: printing a huge value is itself slow
61
+ def too_long(type)
62
+ Dentaku::ArgumentError.for(:invalid_value, actual: type)
63
+ .exception("Number has more than #{MAX_DIGITS} digits")
64
+ end
65
+ end
66
+ end
67
+ end
@@ -1,3 +1,5 @@
1
+ require_relative './numeric_limits'
2
+
1
3
  module Dentaku
2
4
  module NumericParser
3
5
  NUMERIC_PATTERN = '((?:\d+(\.\d+)?|\.\d+)(?:(e|E)(\+|-)?\d+)?)\b'.freeze
@@ -46,13 +48,14 @@ module Dentaku
46
48
  end
47
49
 
48
50
  # @return [Numeric] where possible it returns an Integer otherwise a BigDecimal.
49
- # An Exception will be raised if a value is passed that cannot be cast to a Number.
51
+ # An Exception will be raised if a value is passed that cannot be cast to a Number,
52
+ # or that has more than NumericLimits::MAX_DIGITS digits.
50
53
  def ensure_numeric!(value)
51
- return value if value.is_a?(::Numeric)
54
+ return NumericLimits.check!(value) if value.is_a?(::Numeric)
52
55
 
53
56
  if value.is_a?(::String)
54
57
  number = parse_string(value)
55
- return number if number
58
+ return NumericLimits.check!(number) if number
56
59
  end
57
60
 
58
61
  raise Dentaku::ArgumentError.for(:incompatible_type, actual: value, expected: Numeric),
@@ -1,3 +1,3 @@
1
1
  module Dentaku
2
- VERSION = "4.2.0"
2
+ VERSION = "4.2.1"
3
3
  end
@@ -182,7 +182,7 @@ describe Dentaku::AST::Arithmetic do
182
182
  # non-finite result guard turns into the equivalent Dentaku error.
183
183
  expect {
184
184
  Dentaku::Calculator.new.evaluate!(oversized)
185
- }.to raise_error(Dentaku::ArgumentError, /exponent is too large|is not a finite number/)
185
+ }.to raise_error(Dentaku::ArgumentError, /more than \d+ digits|exponent is too large|is not a finite number/)
186
186
  end
187
187
 
188
188
  it 'is rescuable as Dentaku::Error' do
@@ -740,6 +740,24 @@ describe Dentaku::Calculator do
740
740
  expect(calculator.evaluate('roundup(x, 3)', x: 1.2341)).to eql(1.235)
741
741
  expect(calculator.evaluate('roundup(x, -1)', x: 1234.5)).to eql(1240)
742
742
  end
743
+
744
+ it 'rounds to a power of ten past the number of digits' do
745
+ expect(calculator.evaluate('roundup(7, -2)')).to eql(100)
746
+ expect(calculator.evaluate('roundup(x, -3)', x: 7.5)).to eql(1000)
747
+ expect(calculator.evaluate('roundup(-7, -2)')).to eql(0)
748
+ expect(calculator.evaluate('roundup(0, -2)')).to eql(0)
749
+ end
750
+
751
+ it 'returns zero for a very negative precision without computing the power of ten' do
752
+ expect(calculator.evaluate!('roundup(-7, -1000000000)')).to eql(0)
753
+ expect(calculator.evaluate!('roundup(x, -1000000000)', x: -7.5)).to eql(0)
754
+ end
755
+
756
+ it 'rejects a very negative precision when the result would be too long' do
757
+ expect(calculator.evaluate!('roundup(7, -9999)')).to eq(10**9999)
758
+ expect { calculator.evaluate!('roundup(7, -10000)') }.to raise_error(Dentaku::ArgumentError, /more than 10000 digits/)
759
+ expect { calculator.evaluate!('roundup(7.5, -1000000000)') }.to raise_error(Dentaku::ArgumentError)
760
+ end
743
761
  end
744
762
 
745
763
  describe 'rounddown' do
@@ -761,6 +779,23 @@ describe Dentaku::Calculator do
761
779
  expect(calculator.evaluate('rounddown(x, 3)', x: 1.2349)).to eql(1.234)
762
780
  expect(calculator.evaluate('rounddown(x, -1)', x: 1234.5)).to eql(1230)
763
781
  end
782
+
783
+ it 'rounds to a power of ten past the number of digits' do
784
+ expect(calculator.evaluate('rounddown(7, -2)')).to eql(0)
785
+ expect(calculator.evaluate('rounddown(-7, -2)')).to eql(-100)
786
+ expect(calculator.evaluate('rounddown(x, -3)', x: -7.5)).to eql(-1000)
787
+ end
788
+
789
+ it 'returns zero for a very negative precision without computing the power of ten' do
790
+ expect(calculator.evaluate!('rounddown(7, -1000000000)')).to eql(0)
791
+ expect(calculator.evaluate!('rounddown(x, -1000000000)', x: 7.5)).to eql(0)
792
+ end
793
+
794
+ it 'rejects a very negative precision when the result would be too long' do
795
+ expect(calculator.evaluate!('rounddown(-7, -9999)')).to eq(-10**9999)
796
+ expect { calculator.evaluate!('rounddown(-7, -10000)') }.to raise_error(Dentaku::ArgumentError, /more than 10000 digits/)
797
+ expect { calculator.evaluate!('rounddown(-7.5, -1000000000)') }.to raise_error(Dentaku::ArgumentError)
798
+ end
764
799
  end
765
800
  end
766
801
 
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'dentaku'
3
+
4
+ # Ruby does bignum arithmetic in single C calls that Timeout can't interrupt,
5
+ # so one short formula could otherwise tie up a thread for hours.
6
+ describe 'numeric size limits' do
7
+ let(:calculator) { Dentaku::Calculator.new }
8
+ let(:max) { Dentaku::NumericLimits::MAX_DIGITS }
9
+
10
+ def rejects(formula, **context)
11
+ expect { calculator.evaluate!(formula, **context) }
12
+ .to raise_error(Dentaku::ArgumentError, /more than #{max} digits/)
13
+ expect(calculator.evaluate(formula, **context)).to be_nil
14
+ end
15
+
16
+ it 'still computes large numbers within the limit' do
17
+ expect(calculator.evaluate!('2 ^ 33000')).to eq(2**33000)
18
+ expect(calculator.evaluate!('7 ^ -2')).to eq(Rational(1, 49))
19
+ expect(calculator.evaluate!('1 << 33000')).to eq(1 << 33000)
20
+ expect(calculator.evaluate!('ROUND(1e9000)')).to eq(10**9000)
21
+ expect(calculator.evaluate!('1.5 ^ 0.5')).to be_within(1e-12).of(Math.sqrt(1.5))
22
+ expect(calculator.evaluate!('0 ^ 100000000')).to eq(0)
23
+ expect(calculator.evaluate!('1 ^ 100000000')).to eq(1)
24
+ expect(calculator.evaluate!('-1 ^ 100000001')).to eq(-1)
25
+ end
26
+
27
+ it 'rejects a power whose result would be too long' do
28
+ rejects('2 ^ 40000')
29
+ rejects('7 ^ 100000000')
30
+ rejects('7 ^ -100000000')
31
+ rejects('7 ^ 1e8')
32
+ rejects('7.5 ^ 100000000')
33
+ rejects('0xffffffff ^ 10000000')
34
+ rejects('x ^ y', x: 7, y: 100_000_000)
35
+ end
36
+
37
+ it 'rejects a left shift whose result would be too long' do
38
+ rejects('1 << 40000')
39
+ rejects('1 << 10000000000')
40
+ end
41
+
42
+ it 'rejects arithmetic results that grow past the limit' do
43
+ rejects('x * x', x: 10**6000)
44
+ rejects('MUL(7 ^ 9000, 7 ^ 9000)')
45
+ rejects('x | 1', x: 10**20000)
46
+ end
47
+
48
+ it 'rejects oversized numbers where they are used' do
49
+ rejects('ROUND(1e20000)')
50
+ rejects('ROUND(1e100000000, -2)')
51
+ rejects('ROUNDUP(1e100000000)')
52
+ rejects('ROUNDDOWN(1e100000000)')
53
+ rejects('ROUND(1.5, 1e100000000)')
54
+ rejects('LEFT("abc", 1e100000000)')
55
+ rejects('MID("abc", 1e100000000, 1)')
56
+ rejects('1e100000000 + 1')
57
+ rejects('x + 1', x: '1e100000000')
58
+ end
59
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dentaku
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 4.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Solomon White
@@ -92,6 +92,7 @@ files:
92
92
  - lib/dentaku/ast/functions/any.rb
93
93
  - lib/dentaku/ast/functions/avg.rb
94
94
  - lib/dentaku/ast/functions/count.rb
95
+ - lib/dentaku/ast/functions/directed_rounding.rb
95
96
  - lib/dentaku/ast/functions/duration.rb
96
97
  - lib/dentaku/ast/functions/enum.rb
97
98
  - lib/dentaku/ast/functions/filter.rb
@@ -131,6 +132,7 @@ files:
131
132
  - lib/dentaku/exceptions.rb
132
133
  - lib/dentaku/flat_hash.rb
133
134
  - lib/dentaku/humanize_visitor.rb
135
+ - lib/dentaku/numeric_limits.rb
134
136
  - lib/dentaku/numeric_parser.rb
135
137
  - lib/dentaku/parser.rb
136
138
  - lib/dentaku/print_visitor.rb
@@ -187,6 +189,7 @@ files:
187
189
  - spec/gemspec_spec.rb
188
190
  - spec/humanize_visitor_spec.rb
189
191
  - spec/identifiers_spec.rb
192
+ - spec/numeric_limits_spec.rb
190
193
  - spec/numeric_parser_spec.rb
191
194
  - spec/parser_spec.rb
192
195
  - spec/print_visitor_spec.rb