-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds a `.readyset_explain` method to our relation extension. This method invokes `EXPLAIN CREATE CACHE` upstream on ReadySet and returns information about the query from ReadySet.
- Loading branch information
Showing
7 changed files
with
217 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module Readyset | ||
class Explain | ||
attr_reader :id, :text, :supported | ||
|
||
# Gets information about the given query from ReadySet, including whether it's supported to be | ||
# cached, its current status, the rewritten query text, and the query ID. | ||
# | ||
# The information about the given query is retrieved by invoking `EXPLAIN CREATE CACHE FROM` on | ||
# ReadySet. | ||
# | ||
# @param [String] a query about which information should be retrieved | ||
# @return [Explain] | ||
def self.call(query) | ||
raw_results = Readyset.raw_query('EXPLAIN CREATE CACHE FROM %s', query) | ||
from_readyset_results(**raw_results.first.to_h.symbolize_keys) | ||
end | ||
|
||
# Creates a new `Explain` with the given attributes. | ||
# | ||
# @param [String] id the ID of the query | ||
# @param [String] text the query text | ||
# @param [Symbol] supported the supported status of the query | ||
# @return [Explain] | ||
def initialize(id:, text:, supported:) # :nodoc: | ||
@id = id | ||
@text = text | ||
@supported = supported | ||
end | ||
|
||
# Compares `self` with another `Explain` by comparing them attribute-wise. | ||
# | ||
# @param [Explain] other the `Explain` to which `self` should be compared | ||
# @return [Boolean] | ||
def ==(other) | ||
id == other.id && | ||
text == other.text && | ||
supported == other.supported | ||
end | ||
|
||
# Returns true if the explain information returned by ReadySet indicates that the query is | ||
# unsupported. | ||
# | ||
# @return [Boolean] | ||
def unsupported? | ||
supported == :unsupported | ||
end | ||
|
||
private | ||
|
||
def self.from_readyset_results(**attributes) | ||
new( | ||
id: attributes[:'query id'], | ||
text: attributes[:query], | ||
supported: attributes[:'readyset supported'].to_sym, | ||
) | ||
end | ||
private_class_method :from_readyset_results | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe Readyset::Explain do | ||
describe '.call' do | ||
it 'retrieves the explain information from ReadySet' do | ||
explain = build(:explain) | ||
raw_result = { | ||
:'query id' => explain.id, | ||
:'readyset supported' => explain.supported, | ||
query: explain.text, | ||
} | ||
allow(Readyset).to receive(:raw_query).with('EXPLAIN CREATE CACHE FROM %s', explain.text). | ||
and_return([raw_result]) | ||
|
||
result = Readyset::Explain.call(explain.text) | ||
|
||
expect(result).to eq(explain) | ||
end | ||
end | ||
|
||
describe '.new' do | ||
it 'creates a new `Explain` with the given attributes' do | ||
attributes = attributes_for(:explain) | ||
|
||
explain = Readyset::Explain.new(**attributes) | ||
|
||
expect(explain).to eq(build(:explain)) | ||
end | ||
end | ||
|
||
describe '#==' do | ||
context "when the other `Explain` has an attribute that doesn't match self's" do | ||
it 'returns false' do | ||
explain = build(:explain) | ||
other = build(:explain, supported: :pending) | ||
|
||
result = explain == other | ||
|
||
expect(result).to eq(false) | ||
end | ||
end | ||
|
||
context 'when the attributes of the other `Explain` match those of `self`' do | ||
it 'returns true' do | ||
explain = build(:explain) | ||
other = build(:explain) | ||
|
||
result = explain == other | ||
|
||
expect(result).to eq(true) | ||
end | ||
end | ||
end | ||
|
||
describe '#unsupported?' do | ||
context 'when the `Explain` indicates that the query is supported' do | ||
it 'returns false' do | ||
explain = build(:explain) | ||
|
||
result = explain.unsupported? | ||
|
||
expect(result).to eq(false) | ||
end | ||
end | ||
|
||
context 'when the `Explain` indicates that the query is unsupported' do | ||
it 'returns false' do | ||
explain = build(:explain, supported: :unsupported) | ||
|
||
result = explain.unsupported? | ||
|
||
expect(result).to eq(true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FactoryBot.define do | ||
factory :explain, class: 'Readyset::Explain' do | ||
id { 'q_eafb620c78f5b9ac' } | ||
text { 'SELECT * FROM "t" WHERE ("x" = $1)' } | ||
supported { :yes } | ||
|
||
initialize_with { new(**attributes) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters