active_median 0.3.1 → 0.3.2

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: 629f5530448797da4aca90cd7d89f1b37550caeeacb64a7ce5f56c761dbad09a
4
- data.tar.gz: 5d9b666c75a1c3ac976f968df91422e9cbc5d2c89f3f41a6cd58a7a409ef8c47
3
+ metadata.gz: 7589b66aa9d74af6afe94a766603a36feadf9b0f36d7be01212e97a4ae1de423
4
+ data.tar.gz: de6fa949edc2220610f97ff42a03ce7f50cc1cf9e66979846c570c3a13e3e6b8
5
5
  SHA512:
6
- metadata.gz: f45451ab6d041058e087a88e56c0faaccfb066a96f3c9f48fae7abc4edbca67dcbf3841bdd142133b5b272847e54e783591046ef8f77589305fb412bdc213c94
7
- data.tar.gz: ed76ff4622fbc943d3190c060193a55fb2d74f16541ccf8965b32313019e22a4bbd45eeb120d8aa8eb3f6866a1601e975123bc1276367c9a8128c2d796e04860
6
+ metadata.gz: d2739f3b083658090a61b283d5d416f61e4b998576fe8a704cbcb22a7c2fb621f8b1c89c422bae8cf67cf3784624d5f056837980c2fa80737df67edbc337490c
7
+ data.tar.gz: 9bac410d399bf81668e999d28da3f804ad4ab7e8891db726bbb07980a201902ca107d866b0f27866170df41c97c63d72cf4be4768666f67197f593d559da88b7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.3.2 (2021-08-16)
2
+
3
+ - Added support for SQLite without an extension
4
+
1
5
  ## 0.3.1 (2021-02-21)
2
6
 
3
7
  - Fixed error with relations with `select` clauses
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 9.4+
8
- - MariaDB 10.3.3+
9
- - MySQL and SQL (with extensions)
10
- - SQL Server 2012+
11
- - MongoDB 2.1+
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 and SQLite, also follow [these instructions](#additional-instructions).
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 requires 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
+ 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
@@ -4,6 +4,7 @@ require "active_median/enumerable"
4
4
  require "active_median/version"
5
5
 
6
6
  module ActiveMedian
7
+ # TODO remove in 0.4.0
7
8
  def self.drop_function
8
9
  ActiveRecord::Base.connection.execute <<-SQL
9
10
  DROP AGGREGATE IF EXISTS median(anyelement);
@@ -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
@@ -1,3 +1,3 @@
1
1
  module ActiveMedian
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  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.1
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-02-22 00:00:00.000000000 Z
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.3
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