多个 groupArray 聚合的排序是否会相互一致?
Will ordering of multiple groupArray aggregations be consistent with each other?
groupArray
函数的 docs 警告说
Values can be added to the array in any (indeterminate) order.... In
some cases, you can still rely on the order of execution. This applies
to cases when SELECT comes from a subquery that uses ORDER BY.
这是否意味着数组不一定按照 ORDER BY
中指定的顺序排列?我能否依赖同一查询中多个 groupArray
的顺序与 彼此一致?
例如给定记录:
{commonField:"common", fieldA: "1a", fieldB:"1b"}
{commonField:"common", fieldA: "2a", fieldB:"2b"}
{commonField:"common", fieldA: "3a", fieldB:"3b"}
能否依赖查询
SELECT commonField, groupArray(fieldA), groupArray(fieldB) FROM myTable GROUP BY commonField
到return
{
commonField:"common",
groupedA:[
"2a", "3a", "1a"
],
groupedB:[
"2b", "3b", "1b"
]
}
multiple groupArrays in the same query being consistent with each other?
是的。它们将是一致的。
无论如何你可以使用元组或单组数组。如果你有空值,元组很有用,因为所有聚合函数都会跳过空值。
create table test (K Int64, A Nullable(String), B Nullable(String)) Engine=Memory;
insert into test values(1, '1A','1B')(2, '2A', Null);
select groupArray(A), groupArray(B) from test;
┌─groupArray(A)─┬─groupArray(B)─┐
│ ['1A','2A'] │ ['1B'] │
└───────────────┴───────────────┘
---- Tuple (A,B) one groupArray ----
select groupArray( (A,B) ) from test;
┌─groupArray(tuple(A, B))───┐
│ [('1A','1B'),('2A',NULL)] │
└───────────────────────────┘
select (groupArray( (A,B) ) as ga).1 _A, ga.2 _B from test;
┌─_A──────────┬─_B──────────┐
│ ['1A','2A'] │ ['1B',NULL] │
└─────────────┴─────────────┘
---- One more Tuple trick - Tuple(Null) is not Null ----
select groupArray(tuple(A)).1 _A , groupArray(tuple(B)).1 _B from test;
┌─_A──────────┬─_B──────────┐
│ ['1A','2A'] │ ['1B',NULL] │
└─────────────┴─────────────┘
---- One more Tuple trick tuple(*)
select groupArray( tuple(*) ) from test;
┌─groupArray(tuple(K, A, B))────┐
│ [(1,'1A','1B'),(2,'2A',NULL)] │
└───────────────────────────────┘
groupArray
函数的 docs 警告说
Values can be added to the array in any (indeterminate) order.... In some cases, you can still rely on the order of execution. This applies to cases when SELECT comes from a subquery that uses ORDER BY.
这是否意味着数组不一定按照 ORDER BY
中指定的顺序排列?我能否依赖同一查询中多个 groupArray
的顺序与 彼此一致?
例如给定记录:
{commonField:"common", fieldA: "1a", fieldB:"1b"}
{commonField:"common", fieldA: "2a", fieldB:"2b"}
{commonField:"common", fieldA: "3a", fieldB:"3b"}
能否依赖查询
SELECT commonField, groupArray(fieldA), groupArray(fieldB) FROM myTable GROUP BY commonField
到return
{
commonField:"common",
groupedA:[
"2a", "3a", "1a"
],
groupedB:[
"2b", "3b", "1b"
]
}
multiple groupArrays in the same query being consistent with each other?
是的。它们将是一致的。
无论如何你可以使用元组或单组数组。如果你有空值,元组很有用,因为所有聚合函数都会跳过空值。
create table test (K Int64, A Nullable(String), B Nullable(String)) Engine=Memory;
insert into test values(1, '1A','1B')(2, '2A', Null);
select groupArray(A), groupArray(B) from test;
┌─groupArray(A)─┬─groupArray(B)─┐
│ ['1A','2A'] │ ['1B'] │
└───────────────┴───────────────┘
---- Tuple (A,B) one groupArray ----
select groupArray( (A,B) ) from test;
┌─groupArray(tuple(A, B))───┐
│ [('1A','1B'),('2A',NULL)] │
└───────────────────────────┘
select (groupArray( (A,B) ) as ga).1 _A, ga.2 _B from test;
┌─_A──────────┬─_B──────────┐
│ ['1A','2A'] │ ['1B',NULL] │
└─────────────┴─────────────┘
---- One more Tuple trick - Tuple(Null) is not Null ----
select groupArray(tuple(A)).1 _A , groupArray(tuple(B)).1 _B from test;
┌─_A──────────┬─_B──────────┐
│ ['1A','2A'] │ ['1B',NULL] │
└─────────────┴─────────────┘
---- One more Tuple trick tuple(*)
select groupArray( tuple(*) ) from test;
┌─groupArray(tuple(K, A, B))────┐
│ [(1,'1A','1B'),(2,'2A',NULL)] │
└───────────────────────────────┘