unionAll
语法
unionAll(tableA, tableB, [byColName=false])
或
unionAll(tables, [partition=true], [byColName=false])
参数
对于第一种用法,tableA 和 tableB 是列数相同的表。
对于第二种用法,tables 是一个元组,其中每个元素都是一个表,并且它们具有相同的列数。partition 是布尔值,默认值是true。
byColName 是一个布尔值。若设为true,表示表合并时,依照列名进行,各表中相同列名的列进行合并,缺失的列用空值填充。若设为false,表示表合并时仅基于列顺序,不管列名是否一致。
详情
第一种用法只能将两个表合并成一个表,返回的结果是未分区的内存表。
第二种用法可以多个表合并成一个表。如果 partition 为false,返回的结果是未分区的内存表;如果 partition 为true,返回的结果是一个顺序分区的内存表。默认值为true。
当 byColName =true时,各表可有不同数量的列。若某列在某些表中不存在,结果中会以空值填充。
当 byColName =false时,各表必须有相同数量的列。
例子
例1. 合并两个表
$ t1=table(1 2 3 as id, 11 12 13 as x)
$ t2=table(4 5 6 as id, 14 15 16 as x)
$ re=unionAll(t1,t2)
$ re;
id |
x |
---|---|
1 |
11 |
2 |
12 |
3 |
13 |
4 |
14 |
5 |
15 |
6 |
16 |
$ typestr(re);
IN-MEMORY TABLE
例2. 合并多个表
$ t1=table(1 2 3 as id, 11 12 13 as x)
$ t2=table(4 5 6 as id, 14 15 16 as x)
$ t3=table(7 8 as id, 17 18 as x)
$ re=unionAll([t1,t2,t3])
$ select * from re;
id |
x |
---|---|
1 |
11 |
2 |
12 |
3 |
13 |
4 |
14 |
5 |
15 |
6 |
16 |
7 |
17 |
8 |
18 |
$ typestr(re);
SEGMENTED IN-MEMORY TABLE
例3. 有关 byColName
$ t1=table(1 2 3 as id, 11 12 13 as x)
$ t2=table(14 15 16 as x, 4 5 6 as id)
$ unionAll(t1,t2,true);
id |
x |
---|---|
1 |
11 |
2 |
12 |
3 |
13 |
4 |
14 |
5 |
15 |
6 |
16 |
$ t1=table(1 2 3 as id, 11 12 13 as x)
$ t2=table(14 15 16 as x, 4 5 6 as id)
$ unionAll(t1,t2);
id |
x |
---|---|
1 |
11 |
2 |
12 |
3 |
13 |
14 |
4 |
15 |
5 |
16 |
6 |
上例中可见,若不指定 byColName (即 byColName =false),请务必确认各表中列名顺序一致,否则会产生错误结果。
$ t1=table(1 2 3 as id, 11 12 13 as x, 21 22 23 as y)
$ t2=table(4 5 6 as id, 14 15 16 as x)
$ unionAll(t1,t2,true);
id |
x |
y |
---|---|---|
1 |
11 |
21 |
2 |
12 |
22 |
3 |
13 |
23 |
4 |
14 |
|
5 |
15 |
|
6 |
16 |
$ t1=table(1 2 3 as id, 11 12 13 as x, 21 22 23 as y)
$ t2=table(4 5 6 as id, 14 15 16 as x)
$ unionAll(t1, t2) => The number of columns of the table to insert must be the same as that of the original table.
上例中可见,若各表中列数不一致,必须将 byColName 设为true。