Skip to content

Commit 447e31f

Browse files
committed
new doc
1 parent 65bb368 commit 447e31f

15 files changed

+992
-0
lines changed

202309/20230928_02.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## DuckDB 语法糖: Reusable column aliases
2+
3+
### 作者
4+
digoal
5+
6+
### 日期
7+
2023-09-28
8+
9+
### 标签
10+
PostgreSQL , PolarDB , duckdb , 语法糖
11+
12+
----
13+
14+
## 背景
15+
duckdb增加了很多SQL语法糖, 目标是要让用户可以随心所欲的写SQL.
16+
17+
https://duckdb.org/2023/08/23/even-friendlier-sql.html
18+
19+
Reusable column aliases
20+
21+
在查询中可以使用前面已经定义过的column alias.
22+
23+
旧的用法:
24+
25+
```
26+
select
27+
'These are the voyages of the starship Enterprise...' AS intro,
28+
instr('These are the voyages of the starship Enterprise...', 'starship') AS starship_loc
29+
substr('These are the voyages of the starship Enterprise...', instr('These are the voyages of the starship Enterprise...', 'starship') + len('starship') + 1) AS trimmed_intro;
30+
```
31+
32+
```
33+
WITH intro_cte AS (
34+
SELECT
35+
'These are the voyages of the starship Enterprise...' AS intro
36+
), starship_loc_cte AS (
37+
SELECT
38+
intro,
39+
instr(intro, 'starship') AS starship_loc
40+
FROM intro_cte
41+
)
42+
SELECT
43+
intro,
44+
starship_loc,
45+
substr(intro, starship_loc + len('starship') + 1) AS trimmed_intro
46+
FROM starship_loc_cte;
47+
```
48+
49+
新用法:
50+
```
51+
SELECT
52+
'These are the voyages of the starship Enterprise...' AS intro,
53+
instr(intro, 'starship') AS starship_loc,
54+
substr(intro, starship_loc + len('starship') + 1) AS trimmed_intro;
55+
```
56+
57+
intro | starship_loc | trimmed_intro
58+
---|---|---
59+
These are the voyages of the starship Enterprise…| 30| Enterprise…
60+
61+
62+
63+
#### [期望 PostgreSQL|开源PolarDB 增加什么功能?](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216")
64+
65+
66+
#### [PolarDB 云原生分布式开源数据库](https://github.com/ApsaraDB "57258f76c37864c6e6d23383d05714ea")
67+
68+
69+
#### [PolarDB 学习图谱: 训练营、培训认证、在线互动实验、解决方案、内核开发公开课、生态合作、写心得拿奖品](https://www.aliyun.com/database/openpolardb/activity "8642f60e04ed0c814bf9cb9677976bd4")
70+
71+
72+
#### [PostgreSQL 解决方案集合](../201706/20170601_02.md "40cff096e9ed7122c512b35d8561d9c8")
73+
74+
75+
#### [德哥 / digoal's github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae")
76+
77+
78+
![digoal's wechat](../pic/digoal_weixin.jpg "f7ad92eeba24523fd47a6e1a0e691b59")
79+

202309/20230928_03.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
## DuckDB 语法糖: Dynamic column selection 支持通配符,exclude,replace,lambda 等动态列选择|值替换|列选择
2+
3+
### 作者
4+
digoal
5+
6+
### 日期
7+
2023-09-28
8+
9+
### 标签
10+
PostgreSQL , PolarDB , duckdb , 语法糖
11+
12+
----
13+
14+
## 背景
15+
duckdb增加了很多SQL语法糖, 目标是要让用户可以随心所欲的写SQL.
16+
17+
https://duckdb.org/2023/08/23/even-friendlier-sql.html
18+
19+
Dynamic column selection 支持通配符,exclude等动态列选择
20+
21+
加载一些测试数据:
22+
23+
```
24+
INSTALL httpfs;
25+
LOAD httpfs;
26+
CREATE TABLE trek_facts AS
27+
SELECT * FROM 'https://raw.githubusercontent.com/Alex-Monahan/example_datasets/main/Star_Trek-Season_1.csv';
28+
29+
DESCRIBE trek_facts;
30+
```
31+
32+
结构如下:
33+
34+
column_name | column_type | null | key | default |extra
35+
---|---|---|---|---|---
36+
season_num |BIGINT |YES |NULL |NULL |NULL
37+
episode_num |BIGINT |YES |NULL |NULL |NULL
38+
aired_date |DATE |YES |NULL |NULL |NULL
39+
cnt_kirk_hookups |BIGINT |YES |NULL |NULL |NULL
40+
cnt_downed_redshirts |BIGINT |YES |NULL |NULL |NULL
41+
bool_aliens_almost_took_over_planet |BIGINT |YES |NULL |NULL |NULL
42+
bool_aliens_almost_took_over_enterprise |BIGINT |YES |NULL |NULL |NULL
43+
cnt_vulcan_nerve_pinch |BIGINT |YES |NULL |NULL |NULL
44+
cnt_warp_speed_orders |BIGINT |YES |NULL |NULL |NULL
45+
highest_warp_speed_issued |BIGINT |YES |NULL |NULL |NULL
46+
bool_hand_phasers_fired |BIGINT |YES |NULL |NULL |NULL
47+
bool_ship_phasers_fired |BIGINT |YES |NULL |NULL |NULL
48+
bool_ship_photon_torpedos_fired |BIGINT |YES |NULL |NULL |NULL
49+
cnt_transporter_pax |BIGINT |YES |NULL |NULL |NULL
50+
cnt_damn_it_jim_quote |BIGINT |YES |NULL |NULL |NULL
51+
cnt_im_givin_her_all_shes_got_quote |BIGINT |YES |NULL |NULL |NULL
52+
cnt_highly_illogical_quote |BIGINT |YES |NULL |NULL |NULL
53+
bool_enterprise_saved_the_day |BIGINT |YES |NULL |NULL |NULL
54+
55+
使用columns(正则)
56+
57+
```
58+
SELECT
59+
episode_num,
60+
COLUMNS('.*warp.*')
61+
FROM trek_facts;
62+
```
63+
64+
65+
episode_num | cnt_warp_speed_orders | highest_warp_speed_issued
66+
---|---|---
67+
0 |1 |1
68+
… |… |…
69+
27 |1 |1
70+
71+
columns可以和其他函数结合使用
72+
73+
```
74+
SELECT
75+
MAX(COLUMNS('.*warp.*'))
76+
FROM trek_facts;
77+
```
78+
79+
max(trek_facts.cnt_warp_speed_orders) |max(trek_facts.highest_warp_speed_issued)
80+
---|---
81+
5 | 8
82+
83+
可以放在where中进行计算, 匹配多列相当于and
84+
85+
```
86+
SELECT
87+
episode_num,
88+
COLUMNS('.*warp.*')
89+
FROM trek_facts
90+
WHERE
91+
COLUMNS('.*warp.*') >= 2;
92+
-- cnt_warp_speed_orders >= 2
93+
-- AND
94+
-- highest_warp_speed_issued >= 2
95+
```
96+
97+
episode_num |cnt_warp_speed_orders |highest_warp_speed_issued
98+
---|---|---
99+
14 |3 |7
100+
17 |2 |7
101+
18 |2 |8
102+
29 |2 |8
103+
104+
COLUMNS() with EXCLUDE and REPLACE
105+
106+
```
107+
-- 排除 season_num 列名.
108+
109+
SELECT
110+
MAX(COLUMNS(* EXCLUDE season_num))
111+
FROM trek_facts;
112+
```
113+
114+
替换列
115+
116+
```
117+
-- 使用aired_date::timestamp 替换 aired_date
118+
SELECT
119+
MAX(COLUMNS(* REPLACE aired_date::timestamp AS aired_date))
120+
FROM trek_facts;
121+
```
122+
123+
COLUMNS() with lambda functions
124+
125+
```
126+
-- 列名符合like ...
127+
128+
SELECT
129+
episode_num,
130+
COLUMNS(col -> col LIKE '%warp%')
131+
FROM trek_facts
132+
WHERE
133+
COLUMNS(col -> col LIKE '%warp%') >= 2;
134+
```
135+
136+
episode_num | cnt_warp_speed_orders | highest_warp_speed_issued
137+
---|---|---
138+
14 |3 |7
139+
17 |2 |7
140+
18 |2 |8
141+
29 |2 |8
142+
143+
144+
145+
146+
147+
#### [期望 PostgreSQL|开源PolarDB 增加什么功能?](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216")
148+
149+
150+
#### [PolarDB 云原生分布式开源数据库](https://github.com/ApsaraDB "57258f76c37864c6e6d23383d05714ea")
151+
152+
153+
#### [PolarDB 学习图谱: 训练营、培训认证、在线互动实验、解决方案、内核开发公开课、生态合作、写心得拿奖品](https://www.aliyun.com/database/openpolardb/activity "8642f60e04ed0c814bf9cb9677976bd4")
154+
155+
156+
#### [PostgreSQL 解决方案集合](../201706/20170601_02.md "40cff096e9ed7122c512b35d8561d9c8")
157+
158+
159+
#### [德哥 / digoal's github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae")
160+
161+
162+
![digoal's wechat](../pic/digoal_weixin.jpg "f7ad92eeba24523fd47a6e1a0e691b59")
163+

202309/20230928_04.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## DuckDB 语法糖: Automatic JSON to nested types conversion
2+
3+
### 作者
4+
digoal
5+
6+
### 日期
7+
2023-09-28
8+
9+
### 标签
10+
PostgreSQL , PolarDB , duckdb , 语法糖
11+
12+
----
13+
14+
## 背景
15+
duckdb增加了很多SQL语法糖, 目标是要让用户可以随心所欲的写SQL.
16+
17+
https://duckdb.org/2023/08/23/even-friendlier-sql.html
18+
19+
Automatic JSON to nested types conversion , 支持自动识别JSON
20+
21+
```
22+
INSTALL httpfs;
23+
LOAD httpfs;
24+
INSTALL json;
25+
LOAD json;
26+
27+
SELECT
28+
starfleet[10].model AS starship
29+
FROM 'https://raw.githubusercontent.com/vlad-saling/star-trek-ipsum/master/src/content/content.json';
30+
```
31+
32+
```
33+
starship
34+
35+
USS Farragut - NCC-1647 - Ship on which James Kirk served as a phaser station operator. Attacked by the Dikironium Cloud Creature, killing half the crew. ad.
36+
```
37+
38+
39+
#### [期望 PostgreSQL|开源PolarDB 增加什么功能?](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216")
40+
41+
42+
#### [PolarDB 云原生分布式开源数据库](https://github.com/ApsaraDB "57258f76c37864c6e6d23383d05714ea")
43+
44+
45+
#### [PolarDB 学习图谱: 训练营、培训认证、在线互动实验、解决方案、内核开发公开课、生态合作、写心得拿奖品](https://www.aliyun.com/database/openpolardb/activity "8642f60e04ed0c814bf9cb9677976bd4")
46+
47+
48+
#### [PostgreSQL 解决方案集合](../201706/20170601_02.md "40cff096e9ed7122c512b35d8561d9c8")
49+
50+
51+
#### [德哥 / digoal's github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae")
52+
53+
54+
![digoal's wechat](../pic/digoal_weixin.jpg "f7ad92eeba24523fd47a6e1a0e691b59")
55+

202309/20230928_05.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## DuckDB 语法糖: FROM first in SELECT statements
2+
3+
### 作者
4+
digoal
5+
6+
### 日期
7+
2023-09-28
8+
9+
### 标签
10+
PostgreSQL , PolarDB , duckdb , 语法糖
11+
12+
----
13+
14+
## 背景
15+
duckdb增加了很多SQL语法糖, 目标是要让用户可以随心所欲的写SQL.
16+
17+
https://duckdb.org/2023/08/23/even-friendlier-sql.html
18+
19+
FROM first in SELECT statements
20+
21+
```
22+
FROM my_table SELECT my_column;
23+
```
24+
25+
```
26+
FROM my_table;
27+
-- 相当于 SELECT * FROM my_table
28+
```
29+
30+
```
31+
COPY (FROM trek_facts) TO 'phaser_filled_facts.parquet';
32+
```
33+
34+
35+
36+
#### [期望 PostgreSQL|开源PolarDB 增加什么功能?](https://github.com/digoal/blog/issues/76 "269ac3d1c492e938c0191101c7238216")
37+
38+
39+
#### [PolarDB 云原生分布式开源数据库](https://github.com/ApsaraDB "57258f76c37864c6e6d23383d05714ea")
40+
41+
42+
#### [PolarDB 学习图谱: 训练营、培训认证、在线互动实验、解决方案、内核开发公开课、生态合作、写心得拿奖品](https://www.aliyun.com/database/openpolardb/activity "8642f60e04ed0c814bf9cb9677976bd4")
43+
44+
45+
#### [PostgreSQL 解决方案集合](../201706/20170601_02.md "40cff096e9ed7122c512b35d8561d9c8")
46+
47+
48+
#### [德哥 / digoal's github - 公益是一辈子的事.](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae")
49+
50+
51+
![digoal's wechat](../pic/digoal_weixin.jpg "f7ad92eeba24523fd47a6e1a0e691b59")
52+

0 commit comments

Comments
 (0)