Skip to content

Commit 1b17ecd

Browse files
BuonOmorafiss
authored andcommitted
feat: Add AOST queries
Add _As Of System Time_ support for models. See https://www.cockroachlabs.com/docs/stable/as-of-system-time Fixes #281
1 parent bbb320a commit 1b17ecd

File tree

8 files changed

+167
-1
lines changed

8 files changed

+167
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Ongoing
44

5+
- Add support for [AOST](cockroachlabs.com/docs/stable/as-of-system-time) queries ([#284](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/284))
6+
57
## 7.0.3 - 2023-08-23
68

79
- Fix Multiple Database connections ([#283](https://github.com/cockroachdb/activerecord-cockroachdb-adapter/pull/)).

lib/active_record/connection_adapters/cockroachdb/arel_tosql.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ module Arel # :nodoc:
2222
module Visitors # :nodoc:
2323
class CockroachDB < PostgreSQL # :nodoc:
2424
include RGeo::ActiveRecord::SpatialToSql
25+
26+
def visit_Arel_Nodes_JoinSource(o, collector)
27+
super
28+
if o.aost
29+
collector << " AS OF SYSTEM TIME '#{o.aost.iso8601}'"
30+
end
31+
collector
32+
end
2533
end
2634
end
2735
end

lib/active_record/connection_adapters/cockroachdb_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require "rgeo/active_record"
22

3+
require_relative "../../arel/nodes/join_source_ext"
34
require "active_record/connection_adapters/postgresql_adapter"
45
require "active_record/connection_adapters/cockroachdb/attribute_methods"
56
require "active_record/connection_adapters/cockroachdb/column_methods"

lib/active_record/relation/query_methods_ext.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
module ActiveRecord
44
class Relation
55
module QueryMethodsExt
6+
def aost!(time) # :nodoc:
7+
unless time.nil? || time.is_a?(Time)
8+
raise ArgumentError, "Unsupported argument type: #{time} (#{time.class})"
9+
end
10+
11+
@aost = time
12+
self
13+
end
14+
15+
# Set system time for the current query. Using
16+
# `.aost(nil)` resets.
17+
#
18+
# See cockroachlabs.com/docs/stable/as-of-system-time
19+
def aost(time)
20+
spawn.aost!(time)
21+
end
22+
623
def from!(...) # :nodoc:
724
@force_index = nil
825
@index_hint = nil
@@ -59,8 +76,20 @@ def index_hint!(hint)
5976
self
6077
end
6178

79+
# TODO: reset or no reset?
80+
81+
def show_create
82+
connection.execute("show create table #{connection.quote_table_name self.table_name}").first["create_statement"]
83+
end
84+
6285
private
6386

87+
def build_arel(...)
88+
arel = super
89+
arel.aost(@aost) if @aost.present?
90+
arel
91+
end
92+
6493
def from_clause_is_a_table_name?
6594
# if empty, we are just dealing with the current table.
6695
return true if from_clause.empty?
@@ -94,5 +123,5 @@ def build_from_clause_with_hints
94123
# as ancestor. That is how active_record is doing is as well.
95124
#
96125
# @see https://github.com/rails/rails/blob/914130a9f/activerecord/lib/active_record/querying.rb#L23
97-
Querying.delegate(:force_index, :index_hint, to: :all)
126+
Querying.delegate(:force_index, :index_hint, :aost, to: :all)
98127
end

lib/arel/nodes/join_source_ext.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Arel
2+
module Nodes
3+
module JoinSourceExt
4+
def initialize(...)
5+
super
6+
@aost = nil
7+
end
8+
9+
def hash
10+
[*super, aost].hash
11+
end
12+
13+
def eql?(other)
14+
super && aost == other.aost
15+
end
16+
alias_method :==, :eql?
17+
end
18+
JoinSource.attr_accessor :aost
19+
JoinSource.prepend JoinSourceExt
20+
end
21+
module SelectManagerExt
22+
def aost(time)
23+
@ctx.source.aost = time
24+
nil
25+
end
26+
end
27+
SelectManager.prepend SelectManagerExt
28+
end

test/cases/arel/nodes_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
# This file may be remove after next
3+
# rails release.
4+
# Whenever https://github.com/rails/rails/commit/8bd463c
5+
# is part of a rails version.
6+
7+
require "cases/arel/helper"
8+
9+
module Arel
10+
module Nodes
11+
class TestNodes < Arel::Test
12+
def test_every_arel_nodes_have_hash_eql_eqeq_from_same_class
13+
# #descendants code from activesupport
14+
node_descendants = []
15+
ObjectSpace.each_object(Arel::Nodes::Node.singleton_class) do |k|
16+
next if k.respond_to?(:singleton_class?) && k.singleton_class?
17+
node_descendants.unshift k unless k == self
18+
end
19+
node_descendants.delete(Arel::Nodes::Node)
20+
node_descendants.delete(Arel::Nodes::NodeExpression)
21+
22+
default_hash_owner = Object.instance_method(:hash).owner
23+
24+
bad_node_descendants = node_descendants.reject do |subnode|
25+
eqeq_owner = subnode.instance_method(:==).owner
26+
eql_owner = subnode.instance_method(:eql?).owner
27+
hash_owner = subnode.instance_method(:hash).owner
28+
29+
hash_owner != default_hash_owner &&
30+
eqeq_owner == eql_owner &&
31+
eqeq_owner == hash_owner
32+
end
33+
34+
problem_msg = "Some subclasses of Arel::Nodes::Node do not have a" \
35+
" #== or #eql? or #hash defined from the same class as the others"
36+
assert_empty bad_node_descendants, problem_msg
37+
end
38+
end
39+
end
40+
end

test/cases/relation/aost_test.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper_cockroachdb"
4+
require "models/post"
5+
require "models/comment"
6+
7+
module CockroachDB
8+
class AostTest < ActiveRecord::TestCase
9+
def test_simple_aost
10+
time = 2.days.ago
11+
re_time = Regexp.quote(time.iso8601)
12+
assert_match(/from "posts" as of system time '#{re_time}'/i, Post.aost(time).to_sql)
13+
assert_match(/from "posts" as of system time '#{re_time}'/i, Post.where(name: "foo").aost(time).to_sql)
14+
end
15+
16+
def test_reset_aost
17+
time = 1.second.from_now
18+
assert_match(/from "posts"\z/i, Post.aost(time).aost(nil).to_sql)
19+
end
20+
21+
def test_aost_with_join
22+
time = Time.now
23+
assert_match(
24+
/FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" AS OF SYSTEM TIME '#{Regexp.quote time.iso8601}'/,
25+
Post.joins(:comments).aost(time).to_sql
26+
)
27+
end
28+
29+
def test_aost_with_subquery
30+
time = 4.seconds.ago
31+
assert_match(/from \(.*?\) subquery as of system time '#{Regexp.quote time.iso8601}'/i, Post.from(Post.where(name: "foo")).aost(time).to_sql)
32+
end
33+
34+
def test_only_time_input
35+
time = 1.second.ago
36+
expected = "SELECT \"posts\".* FROM \"posts\" AS OF SYSTEM TIME '#{time.iso8601}'"
37+
assert_equal expected, Post.aost(time).to_sql
38+
assert_raises(ArgumentError) { Post.aost("no time") }
39+
assert_raises(ArgumentError) { Post.aost(true) }
40+
end
41+
end
42+
43+
class AostNoTransactionTest < ActiveRecord::TestCase
44+
# AOST per query is not compatible with transactions.
45+
self.use_transactional_tests = false
46+
47+
def test_aost_with_multiple_queries
48+
time = 1.second.ago
49+
queries = capture_sql {
50+
Post.aost(time).limit(2).find_each(batch_size: 1).to_a
51+
}
52+
queries.each do
53+
assert_match /FROM \"posts\" AS OF SYSTEM TIME '#{Regexp.quote time.iso8601}'/, _1
54+
end
55+
end
56+
end
57+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exclude :test_every_arel_nodes_have_hash_eql_eqeq_from_same_class, "Overwitten, see https://github.com/rails/rails/issues/49274"

0 commit comments

Comments
 (0)