active_median 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +8 -7
- data/lib/active_median.rb +1 -0
- data/lib/active_median/model.rb +9 -0
- data/lib/active_median/sqlite_handler.rb +32 -0
- data/lib/active_median/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7589b66aa9d74af6afe94a766603a36feadf9b0f36d7be01212e97a4ae1de423
|
4
|
+
data.tar.gz: de6fa949edc2220610f97ff42a03ce7f50cc1cf9e66979846c570c3a13e3e6b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2739f3b083658090a61b283d5d416f61e4b998576fe8a704cbcb22a7c2fb621f8b1c89c422bae8cf67cf3784624d5f056837980c2fa80737df67edbc337490c
|
7
|
+
data.tar.gz: 9bac410d399bf81668e999d28da3f804ad4ab7e8891db726bbb07980a201902ca107d866b0f27866170df41c97c63d72cf4be4768666f67197f593d559da88b7
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,11 +4,12 @@ Median and percentile for Active Record, Mongoid, arrays, and hashes
|
|
4
4
|
|
5
5
|
Supports:
|
6
6
|
|
7
|
-
- PostgreSQL
|
8
|
-
-
|
9
|
-
-
|
10
|
-
-
|
11
|
-
-
|
7
|
+
- PostgreSQL
|
8
|
+
- SQLite
|
9
|
+
- MariaDB
|
10
|
+
- MySQL (with an extension)
|
11
|
+
- SQL Server
|
12
|
+
- MongoDB
|
12
13
|
|
13
14
|
:fire: Uses native functions for blazing performance
|
14
15
|
|
@@ -22,7 +23,7 @@ Add this line to your application’s Gemfile:
|
|
22
23
|
gem 'active_median'
|
23
24
|
```
|
24
25
|
|
25
|
-
For MySQL
|
26
|
+
For MySQL, also follow [these instructions](#additional-instructions).
|
26
27
|
|
27
28
|
## Models
|
28
29
|
|
@@ -81,7 +82,7 @@ mysql <options> < load.sql
|
|
81
82
|
|
82
83
|
### SQLite
|
83
84
|
|
84
|
-
SQLite
|
85
|
+
Improve performance with SQLite with an extension. Download [percentile.c](https://www.sqlite.org/src/file?name=ext/misc/percentile.c&ci=d49c32e6e7cc341b) and follow the [instructions for compiling loadable extensions](https://www.sqlite.org/loadext.html#compiling_a_loadable_extension) for your platform.
|
85
86
|
|
86
87
|
On Linux, use:
|
87
88
|
|
data/lib/active_median.rb
CHANGED
data/lib/active_median/model.rb
CHANGED
@@ -66,6 +66,15 @@ module ActiveMedian
|
|
66
66
|
|
67
67
|
relation.select(*group_values, "PERCENTILE_CONT(#{percentile}) WITHIN GROUP (ORDER BY #{column}) OVER (#{over}) AS #{column_alias}").unscope(:group)
|
68
68
|
when /sqlite/i
|
69
|
+
db = connection.raw_connection
|
70
|
+
unless db.instance_variable_get(:@active_median_handler)
|
71
|
+
if db.get_first_value("SELECT 1 FROM pragma_function_list WHERE name = 'percentile'").nil?
|
72
|
+
require "active_median/sqlite_handler"
|
73
|
+
db.create_aggregate_handler(ActiveMedian::SQLiteHandler)
|
74
|
+
end
|
75
|
+
db.instance_variable_set(:@active_median_handler, true)
|
76
|
+
end
|
77
|
+
|
69
78
|
relation.select(*group_values, "PERCENTILE(#{column}, #{percentile} * 100) AS #{column_alias}")
|
70
79
|
when /postg/i, /redshift/i # postgis too
|
71
80
|
relation.select(*group_values, "PERCENTILE_CONT(#{percentile}) WITHIN GROUP (ORDER BY #{column}) AS #{column_alias}")
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module ActiveMedian
|
2
|
+
class SQLiteHandler
|
3
|
+
def self.arity
|
4
|
+
2
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.name
|
8
|
+
"percentile"
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@values = []
|
13
|
+
@percentile = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
# skip checks for
|
17
|
+
# 1. percentile between 0 and 100
|
18
|
+
# 2. percentile same for all rows
|
19
|
+
# since input is already checked
|
20
|
+
def step(ctx, value, percentile)
|
21
|
+
raise ActiveRecord::StatementInvalid, "1st argument to percentile() is not numeric" unless value.is_a?(Numeric)
|
22
|
+
@percentile ||= percentile
|
23
|
+
@values << value
|
24
|
+
end
|
25
|
+
|
26
|
+
def finalize(ctx)
|
27
|
+
if @values.any?
|
28
|
+
ctx.result = @values.percentile(@percentile / 100.0)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_median
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- lib/active_median/enumerable.rb
|
38
38
|
- lib/active_median/model.rb
|
39
39
|
- lib/active_median/mongoid.rb
|
40
|
+
- lib/active_median/sqlite_handler.rb
|
40
41
|
- lib/active_median/version.rb
|
41
42
|
homepage: https://github.com/ankane/active_median
|
42
43
|
licenses:
|
@@ -57,7 +58,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
57
58
|
- !ruby/object:Gem::Version
|
58
59
|
version: '0'
|
59
60
|
requirements: []
|
60
|
-
rubygems_version: 3.2.
|
61
|
+
rubygems_version: 3.2.22
|
61
62
|
signing_key:
|
62
63
|
specification_version: 4
|
63
64
|
summary: Median and percentile for Active Record, Mongoid, arrays, and hashes
|