jrf 0.1.7 → 0.1.9
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 +4 -4
- data/lib/jrf/row_context.rb +23 -17
- data/lib/jrf/stage.rb +2 -1
- data/lib/jrf/version.rb +1 -1
- data/test/jrf_test.rb +32 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e826b340d1d48d7e8cdd3de9b619e5b019f1b568794d0d306b4001f93b5478b8
|
|
4
|
+
data.tar.gz: 061102212643ccfd8654c31e3c8b7e48b6f1bc0131dcac77badb0295a7074d84
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '09883c9cd3ba7f52190df336553cfd45156a90f400a4b87099a86c7e753dab2e07dbe73cfea1c45fb37ca88cb06ebee7f15e286988c0ec42fe526599d897b355'
|
|
7
|
+
data.tar.gz: 5d8d0cc2d749c647a3587d2cf8659876a55e8fe8a41e38a9183ad3551b6cca2f16f99ba7c806aebbe0f82afcd5b2c4e026f81fb781f338a6898d4d807f15c347
|
data/lib/jrf/row_context.rb
CHANGED
|
@@ -136,25 +136,31 @@ module Jrf
|
|
|
136
136
|
end
|
|
137
137
|
|
|
138
138
|
define_reducer(:percentile) do |ctx, value, percentage, block: nil|
|
|
139
|
-
percentages = percentage.is_a?(Array) ? percentage : [percentage]
|
|
140
|
-
percentages.each { |p| ctx.send(:validate_percentile!, p) }
|
|
141
|
-
scalar = !percentage.is_a?(Array)
|
|
142
|
-
|
|
143
|
-
finish =
|
|
144
|
-
if scalar
|
|
145
|
-
->(values) { [ctx.send(:percentile_value, values.sort, percentages.first)] }
|
|
146
|
-
else
|
|
147
|
-
->(values) {
|
|
148
|
-
sorted = values.sort
|
|
149
|
-
[percentages.map { |p| ctx.send(:percentile_value, sorted, p) }]
|
|
150
|
-
}
|
|
151
|
-
end
|
|
152
|
-
|
|
153
139
|
{
|
|
154
140
|
value: value,
|
|
155
|
-
initial: -> {
|
|
156
|
-
|
|
157
|
-
|
|
141
|
+
initial: {config: -> {
|
|
142
|
+
scalar = !percentage.is_a?(Enumerable)
|
|
143
|
+
percentages = scalar ? [percentage] : percentage.to_a
|
|
144
|
+
percentages.each { |p| ctx.send(:validate_percentile!, p) }
|
|
145
|
+
[scalar, percentages]
|
|
146
|
+
}, values: []},
|
|
147
|
+
finish: ->(state) {
|
|
148
|
+
scalar, percentages = state.fetch(:config)
|
|
149
|
+
sorted = state.fetch(:values).sort
|
|
150
|
+
if scalar
|
|
151
|
+
[ctx.send(:percentile_value, sorted, percentages.first)]
|
|
152
|
+
else
|
|
153
|
+
[percentages.map { |p| ctx.send(:percentile_value, sorted, p) }]
|
|
154
|
+
end
|
|
155
|
+
},
|
|
156
|
+
step: ->(state, v) {
|
|
157
|
+
config = state.fetch(:config)
|
|
158
|
+
state[:config] = config.call if config.respond_to?(:call)
|
|
159
|
+
return state if v.nil?
|
|
160
|
+
|
|
161
|
+
state.fetch(:values) << v
|
|
162
|
+
state
|
|
163
|
+
}
|
|
158
164
|
}
|
|
159
165
|
end
|
|
160
166
|
|
data/lib/jrf/stage.rb
CHANGED
|
@@ -51,8 +51,9 @@ module Jrf
|
|
|
51
51
|
(@mode == :reducer) ? Control::DROPPED : result
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
-
def step_reduce(value, initial:, finish: nil, &
|
|
54
|
+
def step_reduce(value, initial:, finish: nil, step_fn: nil, &step_block)
|
|
55
55
|
idx = @cursor
|
|
56
|
+
step_fn ||= step_block
|
|
56
57
|
|
|
57
58
|
if @reducers[idx].nil?
|
|
58
59
|
finish_rows = finish || ->(acc) { [acc] }
|
data/lib/jrf/version.rb
CHANGED
data/test/jrf_test.rb
CHANGED
|
@@ -469,6 +469,14 @@ assert_equal(
|
|
|
469
469
|
"array percentile output"
|
|
470
470
|
)
|
|
471
471
|
|
|
472
|
+
stdout, stderr, status = run_jrf('percentile(_["foo"], 0.25.step(1.0, 0.25))', input_sum)
|
|
473
|
+
assert_success(status, stderr, "enumerable percentile")
|
|
474
|
+
assert_equal(
|
|
475
|
+
['[1,2,3,4]'],
|
|
476
|
+
lines(stdout),
|
|
477
|
+
"enumerable percentile output"
|
|
478
|
+
)
|
|
479
|
+
|
|
472
480
|
input_with_nil = <<~NDJSON
|
|
473
481
|
{"foo":1}
|
|
474
482
|
{"foo":null}
|
|
@@ -942,6 +950,30 @@ assert_equal([[5, 7]], j.call([{"a" => 1, "b" => 2}, {"a" => 2, "b" => 3}]), "li
|
|
|
942
950
|
j = Jrf.new(proc { group_by(_["k"]) { count() } })
|
|
943
951
|
assert_equal([{"x" => 2, "y" => 1}], j.call([{"k" => "x"}, {"k" => "x"}, {"k" => "y"}]), "library group_by")
|
|
944
952
|
|
|
953
|
+
# reducer configuration is fixed by the first row
|
|
954
|
+
j = Jrf.new(proc { percentile(_["a"], _["p"]) })
|
|
955
|
+
assert_equal([2], j.call([{"a" => 1, "p" => 0.5}, {"a" => 2, "p" => [0.5, 1.0]}, {"a" => 3, "p" => [0.5, 1.0]}]), "library percentile configuration fixed by first row")
|
|
956
|
+
|
|
957
|
+
counting_percentiles = Class.new do
|
|
958
|
+
include Enumerable
|
|
959
|
+
|
|
960
|
+
attr_reader :each_calls
|
|
961
|
+
|
|
962
|
+
def initialize(values)
|
|
963
|
+
@values = values
|
|
964
|
+
@each_calls = 0
|
|
965
|
+
end
|
|
966
|
+
|
|
967
|
+
def each(&block)
|
|
968
|
+
@each_calls += 1
|
|
969
|
+
@values.each(&block)
|
|
970
|
+
end
|
|
971
|
+
end.new([0.25, 0.5, 1.0])
|
|
972
|
+
|
|
973
|
+
j = Jrf.new(proc { percentile(_["a"], counting_percentiles) })
|
|
974
|
+
assert_equal([[1, 2, 3]], j.call([{"a" => 1}, {"a" => 2}, {"a" => 3}]), "library percentile enumerable values")
|
|
975
|
+
assert_equal(1, counting_percentiles.each_calls, "library percentile materializes enumerable once")
|
|
976
|
+
|
|
945
977
|
# reducer then passthrough
|
|
946
978
|
j = Jrf.new(
|
|
947
979
|
proc { sum(_["a"]) },
|