Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading causes breaking change: "PG::FeatureNotSupported: ERROR: input of anonymous composite types is not implemented" #1245

Closed
danielpclark opened this issue Aug 21, 2021 · 2 comments

Comments

@danielpclark
Copy link
Contributor

danielpclark commented Aug 21, 2021

In Rails 6.0.3 And Ransack 2.3.2 this passes. But when you upgrade to Ransack 2.4.0 (and later), with or without Rails 6.1.4, the IN (('major 1', '')) changes to IN ('major 1', '') causing the error:

ActiveRecord::StatementInvalid:
    PG::FeatureNotSupported: ERROR:  input of anonymous composite types is not implemented
    LINE 1: ...OALESCE(TRIM("degrees"."concentration"), '')) IN ('major 1',...

Here is the bug reproduction (just changing versions):

# frozen_string_literal: true

unless File.exist?('Gemfile')
  File.write('Gemfile', %q(
    source 'https://rubygems.org'

    git_source(:github) { |repo| "https://github.com/#{repo}.git" }

    gem 'rails', github: 'rails/rails', tag: 'v6.0.3'
    gem 'ransack', '~> 2.4.2'
    gem 'sqlite3'
  ))

  system 'bundle install'
end

require 'bundler'
Bundler.setup(:default)

require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'ransack'

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new($stdout)

# Display versions.
message = "Running test case with Ruby #{RUBY_VERSION}, Active Record #{
  ::ActiveRecord::VERSION::STRING}, Arel #{Arel::VERSION} and #{
  ::ActiveRecord::Base.connection.adapter_name}"
line = '=' * message.length
puts line, message, line

ActiveRecord::Schema.define do
  create_table :degrees, force: true do |t|
    t.string :concentration
    t.string :major
  end
end

# Model
class Degree < ActiveRecord::Base
  ransacker :major_concentration, formatter: lambda { |tuples|
    major, concentration = JSON.parse(tuples)
    Arel::Nodes::Grouping.new(
      [
        Arel::Nodes.build_quoted(major),
        Arel::Nodes.build_quoted(concentration)
      ]
    )
  } do |_parent|
    degrees = Degree.arel_table
    Arel::Nodes::Grouping.new(
      %i[major concentration].map do |field|
        Arel::Nodes::NamedFunction.new(
          'COALESCE',
          [
            Arel::Nodes::NamedFunction.new('TRIM', [degrees[field]]),
            Arel::Nodes.build_quoted('')
          ]
        )
      end
    )
  end
end

# Bug Test
class BugTest < Minitest::Test
  def test_ransack
    ransack_hash = ActiveSupport::HashWithIndifferentAccess.new(
      {
        m: 'and',
        g: [
          ActiveSupport::HashWithIndifferentAccess.new({ major_concentration_in: ['["major 1", ""]'] })
        ]
      }
    )
    sql = Degree.ransack(ransack_hash).result.to_sql
    puts sql
    assert_equal "SELECT \"degrees\".* FROM \"degrees\" WHERE (COALESCE(TRIM(\"degrees\".\"major\"), ''), COALESCE(TRIM(\"degrees\".\"concentration\"), '')) IN (('major 1', ''))", sql
  end
end

The breaking change was introduced in commit 93615c3 and the method causing this issue is method casted_array?.

The Arel generated changes from

@right=[#<Arel::Nodes::Grouping:0x00005653904e6f58 @expr=[#<Arel::Nodes::Quoted:0x00005653904e6fd0 @expr="major 1">,#<Arel::Nodes::Quoted:0x00005653904e6fa8 @expr="">]>]>,

to

@right=[[#<Arel::Nodes::Quoted:0x0000562801598da8 @expr="major 1">,#<Arel::Nodes::Quoted:0x0000562801598d80 @expr="">]]>,

Any help/fix would be appreciated as this is a blocker in an app Rails upgrade at work.

danielpclark added a commit to danielpclark/ransack that referenced this issue Aug 24, 2021
@danielpclark
Copy link
Contributor Author

I've submitted a PR that resolves this by fixing the casted array checking and handling logic.

danielpclark added a commit to danielpclark/ransack that referenced this issue Aug 25, 2021
@scarroll32
Copy link
Member

Thank you @danielpclark

scarroll32 added a commit that referenced this issue Sep 8, 2021
Fixing non-casted array predicates. Resolves issue #1245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants