busload 0.1.2 → 0.1.3
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/CHANGELOG.md +6 -0
- data/README.md +6 -6
- data/lib/busload/adapters/base.rb +2 -3
- data/lib/busload/adapters/postgresql/decoder.rb +31 -0
- data/lib/busload/adapters/postgresql.rb +23 -17
- data/lib/busload/load/cte.rb +12 -0
- data/lib/busload/load.rb +11 -3
- data/lib/busload/preloader.rb +21 -7
- data/lib/busload/version.rb +1 -1
- data/lib/busload.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fc49a1b5baae4b50fba88933b8451559d3fc34983b778082fa10b59323b269f3
|
|
4
|
+
data.tar.gz: 4c24fd3929d6fe0a230838500582c26afe9538f064ed1255de92b4c0ef55c337
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e9b8ad664c9725e2e8911713460479f4c009a58a08f5aa89f741215beb05fe1dbff0fd69a6facd597253cb648c8f6c4e36d2a02f31b5a0ea76767cba6b31c84
|
|
7
|
+
data.tar.gz: d874a14b2ff819cbffbc647dd98603a2ee3b5e92d15437f95f21b8ef3eb518d715e639ba4116faf9cb1780b35e5b468f711d704f61b1d74b247287183917ddf5
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.1.3] - 2026-12-03
|
|
4
|
+
|
|
5
|
+
- Fixed slow loads of rows with long values that need no quoting, such as base64-encoded ciphertext
|
|
6
|
+
- Associations Active Record loads instead, such as polymorphic ones, reuse the records Busload loaded
|
|
7
|
+
- Associations Active Record loads instead load in one query when several relations of a model need them
|
|
8
|
+
|
|
3
9
|
## [0.1.2] - 2026-12-01
|
|
4
10
|
|
|
5
11
|
- Fixed error for associations whose key column types differ
|
data/README.md
CHANGED
|
@@ -47,9 +47,9 @@ WITH
|
|
|
47
47
|
FROM comments
|
|
48
48
|
WHERE post_id IN (SELECT id FROM "busload_0_1")
|
|
49
49
|
)
|
|
50
|
-
SELECT 0, t::text FROM "busload_0" t
|
|
51
|
-
UNION ALL SELECT 1, t::text FROM "busload_0_1" t
|
|
52
|
-
UNION ALL SELECT 2, t::text FROM "busload_0_1_1" t
|
|
50
|
+
SELECT 0, array_to_json(ARRAY[t.id::text, t.name::text])::text FROM "busload_0" t
|
|
51
|
+
UNION ALL SELECT 1, array_to_json(ARRAY[t.id::text, t.user_id::text])::text FROM "busload_0_1" t
|
|
52
|
+
UNION ALL SELECT 2, array_to_json(ARRAY[t.id::text, t.post_id::text])::text FROM "busload_0_1_1" t
|
|
53
53
|
```
|
|
54
54
|
|
|
55
55
|
Works on associations too:
|
|
@@ -71,9 +71,9 @@ WITH
|
|
|
71
71
|
"busload_0" AS (SELECT users.* FROM users WHERE active),
|
|
72
72
|
"busload_1" AS (SELECT posts.* FROM posts ORDER BY id DESC LIMIT 10),
|
|
73
73
|
"busload_2" AS (SELECT tags.* FROM tags WHERE popular)
|
|
74
|
-
SELECT 0, t::text FROM "busload_0" t
|
|
75
|
-
UNION ALL SELECT 1, t::text FROM "busload_1" t
|
|
76
|
-
UNION ALL SELECT 2, t::text FROM "busload_2" t
|
|
74
|
+
SELECT 0, array_to_json(ARRAY[t.id::text, t.name::text])::text FROM "busload_0" t
|
|
75
|
+
UNION ALL SELECT 1, array_to_json(ARRAY[t.id::text, t.title::text])::text FROM "busload_1" t
|
|
76
|
+
UNION ALL SELECT 2, array_to_json(ARRAY[t.id::text, t.name::text])::text FROM "busload_2" t
|
|
77
77
|
```
|
|
78
78
|
|
|
79
79
|
#### preload, includes and eager_load
|
|
@@ -8,9 +8,8 @@ module Busload
|
|
|
8
8
|
@connection = connection
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
# Runs the CTEs
|
|
12
|
-
#
|
|
13
|
-
# CTEs named before it.
|
|
11
|
+
# Runs the CTEs as one statement and returns an ActiveRecord::Result per
|
|
12
|
+
# CTE, in order. A CTE's SQL may select from CTEs named before it.
|
|
14
13
|
def select_all(_ctes)
|
|
15
14
|
raise NoMethodError, "#{self.class} must implement select_all"
|
|
16
15
|
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Busload
|
|
4
|
+
module Adapters # :nodoc: all
|
|
5
|
+
class PostgreSQL < Base
|
|
6
|
+
# One CTE's rows as they come back: its column names and one encoded
|
|
7
|
+
# record per row.
|
|
8
|
+
Rows = Data.define(:columns, :records)
|
|
9
|
+
|
|
10
|
+
# Decodes Rows into an ActiveRecord::Result. Each record is a JSON array
|
|
11
|
+
# of its columns' text, or record text (t::text) when the CTE's columns
|
|
12
|
+
# aren't known.
|
|
13
|
+
module Decoder
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def decode(rows)
|
|
17
|
+
return ActiveRecord::Result.empty if rows.records.empty?
|
|
18
|
+
|
|
19
|
+
ActiveRecord::Result.new(rows.columns, values(rows.records))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def values(records)
|
|
23
|
+
return records.map { |record| JSON.parse(record) } if records.first.start_with?("[")
|
|
24
|
+
|
|
25
|
+
decoder = PG::TextDecoder::Record.new
|
|
26
|
+
records.map { |record| decoder.decode(record) }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -5,12 +5,9 @@ module Busload
|
|
|
5
5
|
# Builds and runs Busload's statement on PostgreSQL.
|
|
6
6
|
class PostgreSQL < Base
|
|
7
7
|
def select_all(ctes)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
@connection.select_rows(statement_for(ctes), "Busload").each do |index, columns_json, record|
|
|
11
|
-
record ? records[index] << record : columns[index] = columns_json
|
|
8
|
+
ctes.zip(*rows_by_cte(ctes)).map do |cte, columns, records|
|
|
9
|
+
Decoder.decode(Rows.new(columns: cte.columns || columns, records:))
|
|
12
10
|
end
|
|
13
|
-
columns.zip(records).map { |columns_json, cte_records| result_from(columns_json, cte_records) }
|
|
14
11
|
end
|
|
15
12
|
|
|
16
13
|
# Statements sent together run in order, as one transaction.
|
|
@@ -20,28 +17,37 @@ module Busload
|
|
|
20
17
|
|
|
21
18
|
private
|
|
22
19
|
|
|
23
|
-
#
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
# Each CTE's column names and records.
|
|
21
|
+
def rows_by_cte(ctes)
|
|
22
|
+
columns = Array.new(ctes.size)
|
|
23
|
+
records = Array.new(ctes.size) { [] }
|
|
24
|
+
@connection.select_rows(statement_for(ctes), "Busload").each do |index, columns_json, record|
|
|
25
|
+
record ? records[index] << record : columns[index] = JSON.parse(columns_json)
|
|
26
|
+
end
|
|
27
|
+
[columns, records]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Each CTE is referenced twice, so Postgres materializes it and keeps its
|
|
31
|
+
# order.
|
|
26
32
|
def statement_for(ctes)
|
|
27
|
-
|
|
28
|
-
definitions = ctes.map { |name,
|
|
29
|
-
selects = ctes.
|
|
33
|
+
names = ctes.map { |cte| @connection.quote_table_name(cte.name) }
|
|
34
|
+
definitions = names.zip(ctes).map { |name, cte| "#{name} AS (#{cte.sql})" }
|
|
35
|
+
selects = names.zip(ctes).each_with_index.flat_map do |(name, cte), index|
|
|
30
36
|
[
|
|
31
|
-
"SELECT #{index}, (SELECT json_agg(k) FROM json_object_keys(" \
|
|
37
|
+
"SELECT #{index}, (SELECT COALESCE(json_agg(k), '[]') FROM json_object_keys(" \
|
|
32
38
|
"(SELECT row_to_json(t) FROM #{name} t LIMIT 1)) k)::text, NULL::text",
|
|
33
|
-
"SELECT #{index}, NULL,
|
|
39
|
+
"SELECT #{index}, NULL, #{record_sql(cte)} FROM #{name} t"
|
|
34
40
|
]
|
|
35
41
|
end
|
|
36
42
|
|
|
37
43
|
"WITH #{definitions.join(", ")} #{selects.join(" UNION ALL ")}"
|
|
38
44
|
end
|
|
39
45
|
|
|
40
|
-
def
|
|
41
|
-
return
|
|
46
|
+
def record_sql(cte)
|
|
47
|
+
return "t::text" unless cte.columns
|
|
42
48
|
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
texts = cte.columns.map { |column| "t.#{@connection.quote_column_name(column)}::text" }
|
|
50
|
+
"array_to_json(ARRAY[#{texts.join(", ")}])::text"
|
|
45
51
|
end
|
|
46
52
|
end
|
|
47
53
|
end
|
data/lib/busload/load/cte.rb
CHANGED
|
@@ -31,6 +31,18 @@ module Busload
|
|
|
31
31
|
relation.to_a
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
# The names of the columns the CTE selects, or nil with a custom select
|
|
35
|
+
# or from. Eager loaded columns are aliased t<table>_r<column>, by their
|
|
36
|
+
# order in the join.
|
|
37
|
+
def columns
|
|
38
|
+
return if relation.select_values.any? || !relation.from_clause.empty?
|
|
39
|
+
return model.column_names unless @join_dependency
|
|
40
|
+
|
|
41
|
+
@join_dependency.enum_for(:each).with_index.flat_map do |part, table|
|
|
42
|
+
part.column_names.each_index.map { |column| "t#{table}_r#{column}" }
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
34
46
|
def preload_scope
|
|
35
47
|
ActiveRecord::Relation::StrictLoadingScope if relation.strict_loading_value
|
|
36
48
|
end
|
data/lib/busload/load.rb
CHANGED
|
@@ -33,13 +33,19 @@ module Busload
|
|
|
33
33
|
|
|
34
34
|
def run
|
|
35
35
|
@ctes.first.model.with_connection do |connection|
|
|
36
|
-
results = Adapters.for(connection).select_all(@ctes
|
|
36
|
+
results = Adapters.for(connection).select_all(@ctes)
|
|
37
37
|
@ctes.zip(results) { |cte, result| cte.load(result) }
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
run_deferred
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
+
# The records this loaded with every column, which Active Record can
|
|
44
|
+
# reuse as they are.
|
|
45
|
+
def complete_records
|
|
46
|
+
@ctes.select(&:columns).flat_map(&:records)
|
|
47
|
+
end
|
|
48
|
+
|
|
43
49
|
private
|
|
44
50
|
|
|
45
51
|
def ensure_same_database(relations)
|
|
@@ -94,12 +100,14 @@ module Busload
|
|
|
94
100
|
preloads.group_by(&:name).map { |name, same| Preload.new(name, merge_preloads(same.flat_map(&:children))) }
|
|
95
101
|
end
|
|
96
102
|
|
|
103
|
+
# One batch, so CTEs deferring the same association load it in one query.
|
|
97
104
|
def run_deferred
|
|
98
|
-
@deferred.
|
|
105
|
+
preloaders = @deferred.map do |cte, preloads|
|
|
99
106
|
ActiveRecord::Associations::Preloader.new(
|
|
100
107
|
records: cte.records, associations: preloads.map(&:argument), scope: cte.preload_scope
|
|
101
|
-
)
|
|
108
|
+
)
|
|
102
109
|
end
|
|
110
|
+
ActiveRecord::Associations::Preloader::Batch.new(preloaders, available_records: complete_records).call
|
|
103
111
|
end
|
|
104
112
|
end
|
|
105
113
|
end
|
data/lib/busload/preloader.rb
CHANGED
|
@@ -23,12 +23,14 @@ module Busload
|
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
def call
|
|
26
|
-
return active_record_preloader(records, associations).call unless busloadable?
|
|
26
|
+
return active_record_preloader(records, associations, @available_records).call unless busloadable?
|
|
27
27
|
return [] if empty?
|
|
28
28
|
|
|
29
29
|
@roots = []
|
|
30
|
+
@deferred = []
|
|
30
31
|
plan(records, associations)
|
|
31
|
-
|
|
32
|
+
available_records = busload
|
|
33
|
+
@deferred.each { |owners, preloads| active_record_preloader(owners, preloads, available_records).call }
|
|
32
34
|
[]
|
|
33
35
|
end
|
|
34
36
|
|
|
@@ -46,16 +48,28 @@ module Busload
|
|
|
46
48
|
|
|
47
49
|
def plan_preload(model, owners, name, nested)
|
|
48
50
|
association = Association.new(model, name)
|
|
49
|
-
return
|
|
51
|
+
return defer(association, owners, name, nested) if association.reason
|
|
50
52
|
|
|
51
53
|
loaded, unloaded = owners.partition { |owner| owner.association(name).loaded? }
|
|
52
54
|
plan(loaded.flat_map { |owner| Array(owner.association(name).target) }, nested)
|
|
53
55
|
@roots << root_for(association, unloaded, nested) if unloaded.any?
|
|
54
56
|
end
|
|
55
57
|
|
|
56
|
-
|
|
58
|
+
# Active Record loads it after the busload, so it can reuse what that
|
|
59
|
+
# loaded.
|
|
60
|
+
def defer(association, owners, name, nested)
|
|
57
61
|
association.log_loads_separately(association.reason)
|
|
58
|
-
|
|
62
|
+
@deferred << [owners, { name => nested }]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Loads the roots in one query, and returns what it loaded with every
|
|
66
|
+
# column.
|
|
67
|
+
def busload
|
|
68
|
+
return [] if @roots.empty?
|
|
69
|
+
|
|
70
|
+
load = Load.new(@roots)
|
|
71
|
+
load.run
|
|
72
|
+
load.complete_records
|
|
59
73
|
end
|
|
60
74
|
|
|
61
75
|
def root_for(association, owners, nested)
|
|
@@ -79,9 +93,9 @@ module Busload
|
|
|
79
93
|
end
|
|
80
94
|
end
|
|
81
95
|
|
|
82
|
-
def active_record_preloader(records, associations)
|
|
96
|
+
def active_record_preloader(records, associations, available_records)
|
|
83
97
|
ActiveRecord::Associations::Preloader.new(
|
|
84
|
-
records:, associations:, scope:, available_records
|
|
98
|
+
records:, associations:, scope:, available_records:, associate_by_default:
|
|
85
99
|
)
|
|
86
100
|
end
|
|
87
101
|
end
|
data/lib/busload/version.rb
CHANGED
data/lib/busload.rb
CHANGED
|
@@ -7,6 +7,7 @@ require_relative "busload/errors"
|
|
|
7
7
|
require_relative "busload/adapters"
|
|
8
8
|
require_relative "busload/adapters/base"
|
|
9
9
|
require_relative "busload/adapters/postgresql"
|
|
10
|
+
require_relative "busload/adapters/postgresql/decoder"
|
|
10
11
|
require_relative "busload/load"
|
|
11
12
|
require_relative "busload/load/cte"
|
|
12
13
|
require_relative "busload/preloader"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: busload
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jason Dougherty
|
|
@@ -47,6 +47,7 @@ files:
|
|
|
47
47
|
- lib/busload/adapters.rb
|
|
48
48
|
- lib/busload/adapters/base.rb
|
|
49
49
|
- lib/busload/adapters/postgresql.rb
|
|
50
|
+
- lib/busload/adapters/postgresql/decoder.rb
|
|
50
51
|
- lib/busload/buffer.rb
|
|
51
52
|
- lib/busload/errors.rb
|
|
52
53
|
- lib/busload/load.rb
|