Skip to content

Commit 828eb07

Browse files
authored
Merge pull request #2920 from ClickHouse/add-kb-articles-lio
add kb articles on filtering using array-columns and ingest all rows
2 parents 7fe8f42 + 681adca commit 828eb07

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: How to filter a ClickHouse table by an array-column?
3+
description: "Knowledgebase article on how to filter a ClickHouse table by an array-column."
4+
date: 2024-12-17
5+
---
6+
7+
## Introduction
8+
9+
Filtering a ClickHouse table by an array-column is a common task and the product offers a lot of [functions](/docs/en/sql-reference/functions/array-functions) to work with array-columns.
10+
11+
In this article, we're going to focus on filtering a table by an array-column, but the video below covers a lot of other array-related functions:
12+
13+
<iframe width="560" height="315" src="https://www.youtube.com/embed/JKHAdCFtYDg?si=OqS3ry1LFrOlF8Iy" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
14+
15+
## Example
16+
17+
We'll use an example of a table with two columns `tags_string` and `tags_int` that contain an array of strings and integers respectively.
18+
19+
- Create a sample database and table.
20+
21+
```sql
22+
CREATE DATABASE db1;
23+
```
24+
25+
- Create a sample table
26+
27+
```sql
28+
CREATE TABLE db1.tags_table
29+
(
30+
`id` UInt64,
31+
`tags_string` Array(String),
32+
`tags_int` Array(UInt64),
33+
)
34+
ENGINE = MergeTree
35+
ORDER BY id;
36+
```
37+
38+
- Insert some sample data into the table.
39+
40+
```sql
41+
INSERT INTO db1.tags_table VALUES (1, ['tag1', 'tag2', 'tag3'], [1, 2, 3]), (2, ['tag2', 'tag3', 'tag4'], [2, 3, 4]), (3, ['tag1', 'tag3', 'tag5'], [1, 3, 5]);
42+
```
43+
44+
Filter the table using the `has(arr, elem)` function to return the rows where the `arr` array contains the `elem` element.
45+
46+
Filter the table to return the rows where the `tags_string` array contains the `tag1` element.
47+
48+
```sql
49+
SELECT * FROM db1.tags_table WHERE has(tags_string, 'tag1');
50+
```
51+
52+
```text
53+
┌─id─┬─tags_string────────────┬─tags_int─┐
54+
│ 1 │ ['tag1','tag2','tag3'] │ [1,2,3] │
55+
│ 3 │ ['tag1','tag3','tag5'] │ [1,3,5] │
56+
└────┴────────────────────────┴──────────┘
57+
```
58+
59+
Use the `hasAll(arr, elems)` function to return the rows where all the elements in the `elems` array are present in the `arr` array.
60+
61+
Filter the table to return the rows where all the elements in the `tags_string` array are present in the `['tag1', 'tag2']` array.
62+
63+
```sql
64+
SELECT * FROM db1.tags_table WHERE hasAll(tags_string, ['tag1', 'tag2']);
65+
```
66+
67+
```text
68+
┌─id─┬─tags_string────────────┬─tags_int─┐
69+
│ 1 │ ['tag1','tag2','tag3'] │ [1,2,3] │
70+
└────┴────────────────────────┴──────────┘
71+
```
72+
73+
Use the `hasAny(arr, elems)` function to return the rows where at least one element in the `elems` array is present in the `arr` array.
74+
75+
Filter the table to return the rows where at least one element in the `tags_string` array is present in the `['tag1', 'tag2']` array.
76+
77+
```sql
78+
SELECT * FROM db1.tags_table WHERE hasAny(tags_string, ['tag1', 'tag2']);
79+
```
80+
81+
```text
82+
┌─id─┬─tags_string────────────┬─tags_int─┐
83+
│ 1 │ ['tag1','tag2','tag3'] │ [1,2,3] │
84+
│ 2 │ ['tag2','tag3','tag4'] │ [2,3,4] │
85+
│ 3 │ ['tag1','tag3','tag5'] │ [1,3,5] │
86+
└────┴────────────────────────┴──────────┘
87+
```
88+
89+
We can use a lambda function to filter the table using the `arrayExists(lambda, arr)` function.
90+
91+
Filter the table to return the rows where at least one element in the `tags_int` array is greater than 3.
92+
93+
```sql
94+
SELECT * FROM db1.tags_table WHERE arrayExists(x -> x > 3, tags_int);
95+
```
96+
97+
```text
98+
┌─id─┬─tags_string────────────┬─tags_int─┐
99+
│ 2 │ ['tag2','tag3','tag4'] │ [2,3,4] │
100+
│ 3 │ ['tag1','tag3','tag5'] │ [1,3,5] │
101+
└────┴────────────────────────┴──────────┘
102+
```
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
title: How to insert all rows from one table to another?
3+
description: "Knowledgebase article on how to insert all rows from one table to another."
4+
date: 2024-12-17
5+
---
6+
7+
## Introduction
8+
9+
Sometimes you need to reingest all the data from one table to another.
10+
11+
For example, you might want to reingest data from a staging table to a production table. This article shows how to do this using the `INSERT INTO` statement.
12+
13+
## Example
14+
15+
Below is a simple example on how it works and how to test:
16+
17+
- Create a sample database
18+
19+
```sql
20+
CREATE DATABASE db1;
21+
```
22+
23+
- Create a sample table
24+
25+
```sql
26+
CREATE TABLE db1.source_table
27+
(
28+
city VARCHAR,
29+
country VARCHAR,
30+
continent VARCHAR
31+
)
32+
engine = MergeTree()
33+
ORDER BY continent;
34+
```
35+
36+
- Insert some data into the source table
37+
38+
```sql
39+
INSERT INTO db1.source_table (city, country, continent)
40+
VALUES
41+
('New York', 'USA', 'North America'),
42+
('Tokyo', 'Japan', 'Asia'),
43+
('Berlin', 'Germany', 'Europe'),
44+
('Paris', 'France', 'Europe'),
45+
('Cairo', 'Egypt', 'Africa'),
46+
('Sydney', 'Australia', 'Australia');
47+
```
48+
49+
- Check the number of rows in the source table
50+
51+
```sql
52+
SELECT COUNT(*) FROM db1.source_table;
53+
```
54+
55+
```text
56+
┌─count()─┐
57+
│ 6 │
58+
└─────────┘
59+
```
60+
61+
- Create a new table with the same structure as the source table.
62+
63+
```sql
64+
CREATE TABLE db1.target_table AS db1.source_table;
65+
```
66+
67+
- Insert all rows from the source table to the target table.
68+
69+
```sql
70+
INSERT INTO db1.target_table SELECT * FROM db1.source_table;
71+
```
72+
73+
- Check the number of rows in the target table
74+
75+
```sql
76+
SELECT COUNT(*) FROM db1.target_table;
77+
```
78+
79+
```text
80+
┌─count()─┐
81+
│ 6 │
82+
└─────────┘
83+
```
84+
85+
If you want to modify the structure of the new table, you can first display the structure of the source table.
86+
87+
```sql
88+
SHOW CREATE TABLE db1.source_table;
89+
```
90+
91+
Then create the new table with the modified structure. In our case we want to add a new column `population` to the target table.
92+
93+
```sql
94+
CREATE TABLE db1.target_table_population
95+
(
96+
`city` String,
97+
`country` String,
98+
`continent` String,
99+
`population` UInt16,
100+
)
101+
ENGINE = MergeTree
102+
ORDER BY continent;
103+
```
104+
105+
- Insert all rows from the source table to the target table, including the new column. The population field is set to 0 for all rows.
106+
107+
```sql
108+
INSERT INTO db1.target_table_population (city, country, continent, population)
109+
SELECT city, country, continent, 0 FROM db1.source_table;
110+
```
111+
112+
- Check the data in the target table
113+
114+
```sql
115+
SELECT * FROM db1.target_table_population LIMIT 3;
116+
```
117+
118+
```text
119+
┌─city──────┬─country───┬─continent──────┬─population─┐
120+
│ New York │ USA │ North America │ 0 │
121+
│ Tokyo │ Japan │ Asia │ 0 │
122+
│ Berlin │ Germany │ Europe │ 0 │
123+
└───────────┴───────────┴────────────────┴────────────┘
124+
```
125+
126+
127+

0 commit comments

Comments
 (0)