Skip to content

Commit

Permalink
Use IS [NOT] NULL instead of DBMS_LOB.COMPARE for nil CLOB/BLOB queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Thoman committed Oct 16, 2024
1 parent 67fef7d commit 29c8d6c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/arel/visitors/oracle_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ module OracleCommon
# Fixes ORA-00932: inconsistent datatypes: expected - got CLOB
def visit_Arel_Nodes_Equality(o, collector)
left = o.left

return super unless %i(text binary).include?(cached_column_for(left)&.type)
return super if o.right.nil?

# https://docs.oracle.com/cd/B19306_01/appdev.102/b14258/d_lob.htm#i1016668
# returns 0 when the comparison succeeds
Expand Down
21 changes: 21 additions & 0 deletions spec/active_record/oracle_enhanced/type/binary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,25 @@ class ::TestEmployee < ActiveRecord::Base
@employee.reload
expect(@employee.binary_data).to eq(@binary_data)
end

it "should find serialized NULL BLOB data when queried with nil" do
TestEmployee.delete_all
TestEmployee.create!(binary_data: nil)
TestEmployee.create!(binary_data: @binary_data)
expect(TestEmployee.where(binary_data: nil)).to have_attributes(count: 1)
end

it "should find serialized NULL BLOB data when queried with nil" do
TestSerializedHashEmployee.delete_all
TestSerializedHashEmployee.create!(binary_data: nil)
TestSerializedHashEmployee.create!(binary_data: { data: 'some data' })
expect(TestSerializedHashEmployee.where(binary_data: nil)).to have_attributes(count: 1)
end

it "should find serialized NULL BLOB data when queried with {}" do
TestSerializedHashEmployee.delete_all
TestSerializedHashEmployee.create!(binary_data: nil)
TestSerializedHashEmployee.create!(binary_data: { data: 'some data' })
expect(TestSerializedHashEmployee.where(binary_data: {})).to have_attributes(count: 1)
end
end
21 changes: 21 additions & 0 deletions spec/active_record/oracle_enhanced/type/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,25 @@ class ::TestSerializeEmployee < ActiveRecord::Base
)
expect(Test2Employee.where(comments: search_data)).to have_attributes(count: 1)
end

it "should find NULL CLOB data when queried with nil" do
TestEmployee.delete_all
TestEmployee.create!(comments: nil)
TestEmployee.create!(comments: @char_data)
expect(TestEmployee.where(comments: nil)).to have_attributes(count: 1)
end

it "should find serialized NULL CLOB data when queried with nil" do
Test2Employee.delete_all
Test2Employee.create!(comments: nil)
Test2Employee.create!(comments: { some: 'text' })
expect(Test2Employee.where(comments: nil)).to have_attributes(count: 1)
end

it "should find serialized NULL CLOB data when queried with {}" do
TestSerializedHashEmployee.delete_all
TestSerializedHashEmployee.create!(comments: nil)
TestSerializedHashEmployee.create!(comments: { some: 'text' })
expect(TestSerializedHashEmployee.where(comments: {})).to have_attributes(count: 1)
end
end

0 comments on commit 29c8d6c

Please sign in to comment.