Skip to content

Commit

Permalink
introduce value-field
Browse files Browse the repository at this point in the history
converge value_in_sql and field_in_sql
  • Loading branch information
kbrock committed Apr 12, 2024
1 parent 6f0cc92 commit 196abcf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
18 changes: 9 additions & 9 deletions lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ def preprocess_exp(exp)
field = operator_values["field"]
field_field = operator_values["field-field"] = Field.parse(field) if field
value = operator_values["value"]
operator_values["value-field"] = Field.parse(value) if value

# attempt to do conversion only if db type of column is integer and value to compare to is String
if %w[= != <= >= > <].include?(operator) && field_field&.integer? && value.class == String
Expand Down Expand Up @@ -491,17 +492,17 @@ def sql_supports_atom?(exp)
when "includes"
# Support includes operator using "LIKE" only if first operand is in main table
if exp[operator].key?("field") && (!exp[operator]["field"].include?(".") || (exp[operator]["field"].include?(".") && exp[operator]["field"].split(".").length == 2))
field_in_sql?(exp[operator]["field"], exp[operator]["field-field"])
field_in_sql?(exp[operator]["field-field"])
else
# TODO: Support includes operator for sub-sub-tables
false
end
when "includes any", "includes all", "includes only"
# Support this only from the main model (for now)
if exp[operator].keys.include?("field") && exp[operator]["field"].split(".").length == 1
model, field = exp[operator]["field"].split("-")
method = "miq_expression_#{operator.downcase.tr(' ', '_')}_#{field}_arel"
model.constantize.respond_to?(method)
if exp[operator]["field-field"] && exp[operator]["field"]&.split(".")&.length == 1
field_field = exp[operator]["field-field"]
method = "miq_expression_#{operator.downcase.tr(' ', '_')}_#{field_field.column}_arel"
field_field.model.respond_to?(method)
else
false
end
Expand All @@ -517,17 +518,16 @@ def sql_supports_atom?(exp)
# => TODO: support count of child relationship
return false if exp[operator].key?("count")

field_in_sql?(exp[operator]["field"]) && value_in_sql?(exp[operator]["value"])
field_in_sql?(exp[operator]["field-field"]) && value_in_sql?(exp[operator]["value-field"])
end
end

def value_in_sql?(value)
value_field = Field.parse(value)
def value_in_sql?(value_field)
!value_field&.valid? || value_field&.attribute_supported_by_sql?
end

# NOTE: dropped cached :exclude_col. needed?
def field_in_sql?(field, field_field)
def field_in_sql?(field_field)
field_field.attribute_supported_by_sql? &&
!field_field.exclude_col_by_preprocess_options?(preprocess_options)
end
Expand Down
22 changes: 11 additions & 11 deletions spec/lib/miq_expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@

describe "#reduce_exp" do
let(:sql_field) { {"=" => {"field" => "Vm-name", "value" => "foo"}.freeze}.freeze }
let(:sql_field_out) { {"=" => {"field" => "Vm-name", "field-field" => MiqExpression::Field.parse("Vm-name"), "value" => "foo"}.freeze}.freeze }
let(:sql_field_out) { {"=" => {"field" => "Vm-name", "field-field" => MiqExpression::Field.parse("Vm-name"), "value" => "foo", "value-field" => nil}.freeze}.freeze }
let(:ruby_field) { {"=" => {"field" => "Vm-platform", "value" => "bar"}.freeze}.freeze }
let(:ruby_field_out) { {"=" => {"field" => "Vm-platform", "field-field" => MiqExpression::Field.parse("Vm-platform"), "value" => "bar"}.freeze}.freeze }
let(:ruby_field_out) { {"=" => {"field" => "Vm-platform", "field-field" => MiqExpression::Field.parse("Vm-platform"), "value" => "bar", "value-field" => nil}.freeze}.freeze }

context "mode: :sql" do
it "(sql AND ruby) => (sql)" do
Expand Down Expand Up @@ -3017,7 +3017,7 @@
end
end

describe "#sql_supports_atom?" do
describe "#sql_supports_atom? (private)" do
context "expression key is 'CONTAINS'" do
context "operations with 'tag'" do
it "returns true for tag of the main model" do
Expand Down Expand Up @@ -3166,51 +3166,51 @@
end
end

describe "#field_in_sql?" do
describe "#field_in_sql? (private)" do
it "returns true for model.virtualfield (with sql)" do
field = "ManageIQ::Providers::InfraManager::Vm-archived"
expression = {"=" => {"field" => field, "value" => "true"}}
expect(described_class.new(expression).field_in_sql?(field, MiqExpression::Field.parse(field))).to eq(true)
expect(described_class.new(expression).field_in_sql?(MiqExpression::Field.parse(field))).to eq(true)
end

it "returns false for model.virtualfield (with no sql)" do
field = "ManageIQ::Providers::InfraManager::Vm-uncommitted_storage"
expression = {"=" => {"field" => field, "value" => "true"}}
expect(described_class.new(expression).field_in_sql?(field, MiqExpression::Field.parse(field))).to eq(false)
expect(described_class.new(expression).field_in_sql?(MiqExpression::Field.parse(field))).to eq(false)
end

it "returns false for model.association-virtualfield" do
field = "ManageIQ::Providers::InfraManager::Vm.storage-v_used_space_percent_of_total"
expression = {">=" => {"field" => field, "value" => "50"}}
expect(described_class.new(expression).field_in_sql?(field, MiqExpression::Field.parse(field))).to eq(false)
expect(described_class.new(expression).field_in_sql?(MiqExpression::Field.parse(field))).to eq(false)
end

it "returns true for model-field" do
field = "ManageIQ::Providers::InfraManager::Vm-vendor"
expression = {"=" => {"field" => field, "value" => "redhat"}}
expect(described_class.new(expression).field_in_sql?(field, MiqExpression::Field.parse(field))).to eq(true)
expect(described_class.new(expression).field_in_sql?(MiqExpression::Field.parse(field))).to eq(true)
end

it "returns true for model.association-field" do
field = "ManageIQ::Providers::InfraManager::Vm.guest_applications-vendor"
expression = {"CONTAINS" => {"field" => field, "value" => "redhat"}}
expect(described_class.new(expression).field_in_sql?(field, MiqExpression::Field.parse(field))).to eq(true)
expect(described_class.new(expression).field_in_sql?(MiqExpression::Field.parse(field))).to eq(true)
end

it "returns false if column excluded from processing for adhoc performance metrics" do
field = "EmsClusterPerformance-cpu_usagemhz_rate_average"
expression = {">=" => {"field" => field, "value" => "0"}}
obj = described_class.new(expression)
obj.preprocess_options = {:vim_performance_daily_adhoc => true}
expect(obj.field_in_sql?(field, MiqExpression::Field.parse(field))).to eq(false)
expect(obj.field_in_sql?(MiqExpression::Field.parse(field))).to eq(false)
end

it "returns true if column not excluded from processing for adhoc performance metrics" do
field = "EmsClusterPerformance-derived_cpu_available"
expression = {">=" => {"field" => field, "value" => "0"}}
obj = described_class.new(expression)
obj.preprocess_options = {:vim_performance_daily_adhoc => true}
expect(obj.field_in_sql?(field, MiqExpression::Field.parse(field))).to eq(true)
expect(obj.field_in_sql?(MiqExpression::Field.parse(field))).to eq(true)
end
end

Expand Down

0 comments on commit 196abcf

Please sign in to comment.