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

group.sum returns string hash values if the column being summed is a string #13230

Closed
bughit opened this Issue Dec 8, 2013 · 2 comments

Comments

Projects
None yet
3 participants
@bughit
Copy link
Contributor

commented Dec 8, 2013

group(:column_a).sum('CAST(column_b AS integer)')

@pftg

This comment has been minimized.

Copy link
Contributor

commented Dec 8, 2013

Confirmed for rails master and 4.0.2 and PG adapter. Without grouping aggregated function returns Fixnum:

GEMFILE =  <<-GEMFILE
    source 'http://rubygems.org'
    #gem 'rails', path: '../rails'
    #gem 'arel', github: 'rails/arel'
    gem 'rails', '4.0.2'
    gem 'pg'
    #gem 'sqlite3'
GEMFILE

gemfile_path = "#{File.basename(__FILE__, '.rb')}.gemfile"
ENV['BUNDLE_GEMFILE'] = File.expand_path(gemfile_path)

if File.exists?(gemfile_path)
  is_gemfile_changed = File.read(gemfile_path) != GEMFILE
  is_gemfile_old = File.stat(gemfile_path).ctime < Time.now - 3600

  if is_gemfile_changed || is_gemfile_old
    puts 'Removing old Gemfile'
    File.delete(gemfile_path)
  end
end

unless File.exists?(gemfile_path)
  puts 'Generating new Gemfile'
  File.write gemfile_path, GEMFILE

  system "bundle --gemfile=#{gemfile_path} --clean"
end

require 'bundler'
Bundler.setup(:default)

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

# This connection will do for database-independent bug reports.
#ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
database_configuration = { adapter: 'postgresql', database: 'rails_issues' }
#
adapter_tasker = ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(database_configuration.stringify_keys)
adapter_tasker.drop
adapter_tasker.create

ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :articles, force: true do |t|
    t.integer :readers_count
    t.string :name
  end
end


class Article < ActiveRecord::Base
end


class SubQueryTest < MiniTest::Unit::TestCase
  def test_bind_values
    Article.create!(name: :qwe, readers_count: 10)
    readers_count_by_name = Article.group(:name).sum(:readers_count)
    assert_equal Fixnum, readers_count_by_name.values.first.class

    readers_count_by_name = Article.sum('CAST(readers_count AS integer)')
    assert_equal Fixnum, readers_count_by_name.class

    readers_count_by_name = Article.group(:name).sum('CAST(readers_count AS integer)')
    assert_equal Fixnum, readers_count_by_name.values.first.class
  end
end
@senny

This comment has been minimized.

Copy link
Member

commented Dec 9, 2013

@pftg @bughit isn't this a duplicate of #12937 ?

pftg added a commit to jetthoughts/rails that referenced this issue Dec 10, 2013

Fix type cast on group sum with custom expression
For PG adapters with custom expression and grouped result
of aggregate functions have not found correct column type
for it. Extract column type from query result.

Closes: rails#13230

@senny senny closed this in #13236 Dec 10, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.