Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
0ce8d452
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
0ce8d452
编写于
5月 01, 2020
作者:
R
Ryuta Kamizono
提交者:
GitHub
5月 01, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #39063 from kamipo/remove_useless_typecast_on_aggregation
Remove useless type cast on aggregated value
上级
ca3579ad
e10765cf
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
16 addition
and
44 deletion
+16
-44
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
...lib/active_record/connection_adapters/abstract_adapter.rb
+0
-4
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
...b/active_record/connection_adapters/postgresql_adapter.rb
+1
-10
activerecord/lib/active_record/relation/calculations.rb
activerecord/lib/active_record/relation/calculations.rb
+15
-30
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
浏览文件 @
0ce8d452
...
...
@@ -565,10 +565,6 @@ def close
pool
.
checkin
self
end
def
column_name_for_operation
(
operation
,
node
)
# :nodoc:
visitor
.
compile
(
node
)
end
def
default_index_type?
(
index
)
# :nodoc:
index
.
using
.
nil?
end
...
...
activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
浏览文件 @
0ce8d452
...
...
@@ -418,16 +418,6 @@ def use_insert_returning?
@use_insert_returning
end
def
column_name_for_operation
(
operation
,
node
)
# :nodoc:
OPERATION_ALIASES
.
fetch
(
operation
)
{
operation
.
downcase
}
end
OPERATION_ALIASES
=
{
# :nodoc:
"maximum"
=>
"max"
,
"minimum"
=>
"min"
,
"average"
=>
"avg"
,
}
# Returns the version of the connected PostgreSQL server.
def
get_database_version
# :nodoc:
@connection
.
server_version
...
...
@@ -895,6 +885,7 @@ def add_pg_decoders
"oid"
=>
PG
::
TextDecoder
::
Integer
,
"float4"
=>
PG
::
TextDecoder
::
Float
,
"float8"
=>
PG
::
TextDecoder
::
Float
,
"numeric"
=>
PG
::
TextDecoder
::
Numeric
,
"bool"
=>
PG
::
TextDecoder
::
Boolean
,
"timestamp"
=>
PG
::
TextDecoder
::
TimestampUtc
,
"timestamptz"
=>
PG
::
TextDecoder
::
TimestampWithTimeZone
,
...
...
activerecord/lib/active_record/relation/calculations.rb
浏览文件 @
0ce8d452
...
...
@@ -281,7 +281,8 @@ def aggregate_column(column_name)
end
end
def
operation_over_aggregate_column
(
column
,
operation
,
distinct
)
def
operation_over_aggregate_column
(
column_name
,
operation
,
distinct
)
column
=
aggregate_column
(
column_name
)
operation
==
"count"
?
column
.
count
(
distinct
)
:
column
.
send
(
operation
)
end
...
...
@@ -291,31 +292,20 @@ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
return
0
if
limit_value
==
0
query_builder
=
build_count_subquery
(
spawn
,
column_name
,
distinct
)
skip_query_cache_if_necessary
{
@klass
.
connection
.
select_value
(
query_builder
)
}
else
# PostgreSQL doesn't like ORDER BY when there are no GROUP BY
relation
=
unscope
(
:order
).
distinct!
(
false
)
column
=
aggregate_column
(
column_name
)
select_value
=
operation_over_aggregate_column
(
column_name
,
operation
,
distinct
)
select_value
.
distinct
=
true
if
operation
==
"sum"
&&
distinct
select_value
=
operation_over_aggregate_column
(
column
,
operation
,
distinct
)
if
operation
==
"sum"
&&
distinct
select_value
.
distinct
=
true
end
column_alias
=
select_value
.
alias
column_alias
||=
@klass
.
connection
.
column_name_for_operation
(
operation
,
select_value
)
relation
.
select_values
=
[
select_value
]
query_builder
=
relation
.
arel
result
=
skip_query_cache_if_necessary
{
@klass
.
connection
.
select_all
(
query_builder
,
nil
)
}
row
=
result
.
rows
.
first
value
=
row
&&
row
.
first
type
=
result
.
column_types
.
fetch
(
column_alias
,
Type
.
default_value
)
type_cast_calculated_value
(
value
,
type
,
operation
)
end
value
=
skip_query_cache_if_necessary
{
@klass
.
connection
.
select_value
(
query_builder
)
}
type_cast_calculated_value
(
value
,
operation
)
end
def
execute_grouped_calculation
(
operation
,
column_name
,
distinct
)
#:nodoc:
...
...
@@ -334,14 +324,11 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
}
group_columns
=
group_aliases
.
zip
(
group_fields
)
aggregate_alias
=
column_alias_for
(
"
#{
operation
}
#{
column_name
.
to_s
.
downcase
}
"
)
column_alias
=
column_alias_for
(
"
#{
operation
}
#{
column_name
.
to_s
.
downcase
}
"
)
select_value
=
operation_over_aggregate_column
(
column_name
,
operation
,
distinct
)
select_value
.
as
(
column_alias
)
select_values
=
[
operation_over_aggregate_column
(
aggregate_column
(
column_name
),
operation
,
distinct
).
as
(
aggregate_alias
)
]
select_values
=
[
select_value
]
select_values
+=
self
.
select_values
unless
having_clause
.
empty?
select_values
.
concat
group_columns
.
map
{
|
aliaz
,
field
|
...
...
@@ -374,8 +361,7 @@ def execute_grouped_calculation(operation, column_name, distinct) #:nodoc:
key
=
key
.
first
if
key
.
size
==
1
key
=
key_records
[
key
]
if
associated
type
=
calculated_data
.
column_types
.
fetch
(
aggregate_alias
,
Type
.
default_value
)
result
[
key
]
=
type_cast_calculated_value
(
row
[
aggregate_alias
],
type
,
operation
)
result
[
key
]
=
type_cast_calculated_value
(
row
[
column_alias
],
operation
)
end
end
...
...
@@ -401,12 +387,11 @@ def type_for(field, &block)
@klass
.
type_for_attribute
(
field_name
,
&
block
)
end
def
type_cast_calculated_value
(
value
,
type
,
operation
)
def
type_cast_calculated_value
(
value
,
operation
)
case
operation
when
"count"
then
value
.
to_i
when
"sum"
then
type
.
deserialize
(
value
||
0
)
when
"sum"
then
value
||
0
when
"average"
then
value
&
.
respond_to?
(
:to_d
)
?
value
.
to_d
:
value
else
type
.
deserialize
(
value
)
else
value
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录