Skip to content

Commit e2fb3a3

Browse files
committed
Merge branches 'main' and 'main' of ssh://github.com/Chasen-Zhang/databend
2 parents 0dc2bf9 + 9119792 commit e2fb3a3

File tree

16 files changed

+820
-414
lines changed

16 files changed

+820
-414
lines changed

Cargo.lock

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/doc/14-sql-commands/20-query-syntax/01-dml-select.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ SELECT number%2 as c1, number%3 as c2, MAX(number) FROM numbers(10000) GROUP BY
142142

143143
```
144144

145+
146+
`GROUP BY` can be extended with [GROUPING SETS](./21-grouping-sets.md) to do more complex grouping operations.
147+
145148
## HAVING Clause
146149

147150
```sql
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
title: GROUPING SETS
3+
---
4+
5+
More complex grouping operations than those described above are possible using the concept of grouping sets. The data is grouped separately by each specified grouping set, aggregates computed for each group just as for simple GROUP BY clauses. The final result is the union of each separate result.
6+
7+
Each sublist of `GROUPING SETS` may specify zero or more columns or expressions and is interpreted the same way as though it were directly in the `GROUP BY` clause. An empty grouping set means that all rows are aggregated down to a single group (which is output even if no input rows were present), as described above for the case of aggregate functions with no GROUP BY clause.
8+
9+
References to the grouping columns or expressions are replaced by null values in result rows for grouping sets in which those columns do not appear. To distinguish which grouping a particular output row resulted from, the [GROUPING function](../../15-sql-functions/120-other-functions/grouping.md) can be used.
10+
11+
## Syntax
12+
13+
```sql
14+
SELECT ...
15+
GROUP BY
16+
GROUPING SETS ( grouping_sets, [ grouping_sets, ...] )
17+
```
18+
19+
```
20+
grouping_sets := ( expr [, expr, ...] )
21+
```
22+
23+
## `CUBE` and `ROLLUP`
24+
25+
Two syntactic sugar forms of `GROUPING SETS` are available: `CUBE` and `ROLLUP`.
26+
27+
```sql
28+
SELECT ...
29+
GROUP BY CUBE ( expr [, expr, ...] );
30+
31+
SELECT ...
32+
GROUP BY ROLLUP ( expr [, expr, ...] );
33+
```
34+
35+
`CUBE` represents the given list and all of its possible subsets (i.e., the power set). For example, `CUBE (a, b, c)` will be desugared to:
36+
37+
```sql
38+
GROUPING SETS (
39+
( a, b, c ),
40+
( a, b ),
41+
( a, c ),
42+
( a ),
43+
( b, c ),
44+
( b ),
45+
( c ),
46+
( )
47+
)
48+
```
49+
50+
`ROLLUP ` represents the given list of expressions and all prefixes of the list including the empty list. For example `ROLLUP (a, b, c)` will be desugared to:
51+
52+
```sql
53+
GROUPING SETS (
54+
( a, b, c ),
55+
( a, b ),
56+
( a ),
57+
( )
58+
)
59+
```
60+
61+
## Examples
62+
63+
```sql
64+
SELECT number%2 as c1, number%3 as c2, MAX(number) FROM numbers(10) GROUP BY GROUPING SETS ((c1, c2), (c1), (c2), ());
65+
+------+------+-------------+
66+
| c1 | c2 | max(number) |
67+
+------+------+-------------+
68+
| 0 | 0 | 6 |
69+
| 0 | NULL | 8 |
70+
| 1 | 0 | 9 |
71+
| NULL | NULL | 9 |
72+
| 1 | 2 | 5 |
73+
| 1 | NULL | 9 |
74+
| NULL | 0 | 9 |
75+
| NULL | 1 | 7 |
76+
| NULL | 2 | 8 |
77+
| 1 | 1 | 7 |
78+
| 0 | 2 | 8 |
79+
| 0 | 1 | 4 |
80+
+------+------+-------------+
81+
82+
SELECT number%2 as c1, number%3 as c2, MAX(number) FROM numbers(10) GROUP BY CUBE (c1, c2);
83+
+------+------+-------------+
84+
| c1 | c2 | max(number) |
85+
+------+------+-------------+
86+
| 0 | 0 | 6 |
87+
| 0 | NULL | 8 |
88+
| 1 | 0 | 9 |
89+
| NULL | NULL | 9 |
90+
| 1 | 2 | 5 |
91+
| 1 | NULL | 9 |
92+
| NULL | 0 | 9 |
93+
| NULL | 1 | 7 |
94+
| NULL | 2 | 8 |
95+
| 1 | 1 | 7 |
96+
| 0 | 2 | 8 |
97+
| 0 | 1 | 4 |
98+
+------+------+-------------+
99+
100+
SELECT number%2 as c1, number%3 as c2, MAX(number) FROM numbers(10) GROUP BY ROLLUP (c1, c2);
101+
+------+------+-------------+
102+
| c1 | c2 | max(number) |
103+
+------+------+-------------+
104+
| 0 | 0 | 6 |
105+
| 0 | NULL | 8 |
106+
| 1 | 0 | 9 |
107+
| NULL | NULL | 9 |
108+
| 1 | 2 | 5 |
109+
| 1 | NULL | 9 |
110+
| 1 | 1 | 7 |
111+
| 0 | 2 | 8 |
112+
| 0 | 1 | 4 |
113+
+------+------+-------------+
114+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
title: GROUPING
3+
---
4+
5+
Returns a bit mask indicating which `GROUP BY` expressions are not included in the current grouping set. Bits are assigned with the rightmost argument corresponding to the least-significant bit; each bit is 0 if the corresponding expression is included in the grouping criteria of the grouping set generating the current result row, and 1 if it is not included.
6+
7+
## Syntax
8+
9+
```sql
10+
GROUPING ( expr [, expr, ...] )
11+
```
12+
13+
:::note
14+
`GROUPING` can only be used with `GROUPING SETS`, `ROLLUP`, or `CUBE`, and its arguments must be in the grouping sets list.
15+
:::
16+
17+
## Arguments
18+
19+
Grouping sets items.
20+
21+
## Return Type
22+
23+
UInt32.
24+
25+
## Examples
26+
27+
```sql
28+
select a, b, grouping(a), grouping(b), grouping(a,b), grouping(b,a) from t group by grouping sets ((a,b),(a),(b), ()) ;
29+
+------+------+-------------+-------------+----------------+----------------+
30+
| a | b | grouping(a) | grouping(b) | grouping(a, b) | grouping(b, a) |
31+
+------+------+-------------+-------------+----------------+----------------+
32+
| NULL | A | 1 | 0 | 2 | 1 |
33+
| a | NULL | 0 | 1 | 1 | 2 |
34+
| b | A | 0 | 0 | 0 | 0 |
35+
| NULL | NULL | 1 | 1 | 3 | 3 |
36+
| a | A | 0 | 0 | 0 | 0 |
37+
| b | B | 0 | 0 | 0 | 0 |
38+
| b | NULL | 0 | 1 | 1 | 2 |
39+
| a | B | 0 | 0 | 0 | 0 |
40+
| NULL | B | 1 | 0 | 2 | 1 |
41+
+------+------+-------------+-------------+----------------+----------------+
42+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"label": "AI Functions"
3+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: 'AI Functions'
3+
---
4+
5+
Databend can utilize the OpenAI `Code-Davinci-002` engine to translate natural language into SQL queries.
6+
7+
By integrating OLAP and AI, Databend simplifies the process of writing SQL queries based on your table schema.
8+
9+
The `ai_to_sql` function allows us to effortlessly generate SQL queries using natural language.
10+
11+
## Syntax
12+
13+
```sql
14+
USE <your-database>;
15+
SELECT * FROM ai_to_sql('<prompt>', '<openai-api-key>');
16+
```
17+
18+
## Example
19+
20+
:::note
21+
Please note that the generated SQL query may need to be adapted to match Databend's syntax and functionality, as it might be based on PostgreSQL-standard SQL.
22+
:::
23+
24+
25+
```sql
26+
CREATE DATABASE openai;
27+
USE openai;
28+
29+
CREATE TABLE users(
30+
id INT,
31+
name VARCHAR,
32+
age INT
33+
);
34+
35+
SELECT * FROM ai_to_sql('List all users older than 30 years', '<openai-api-key>');
36+
```
37+
38+
Output:
39+
```sql
40+
*************************** 1. row ***************************
41+
database: simple_example
42+
generated_sql: SELECT * FROM users WHERE age > 30;
43+
```

src/query/service/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ storages-common-table-meta = { path = "../storages/common/table-meta" }
9898
# Crates.io dependencies
9999
aho-corasick = { version = "0.7.20" }
100100
async-channel = "1.7.1"
101+
# Wait for https://github.com/64bit/async-openai/pull/60
102+
async-openai = { version = "0.9.5", git = "https://github.com/Xuanwo/async-openai", rev = "22fd9ca0c9c86a2c57462c5fd32aaff407d6b000" }
101103
async-stream = "0.3.3"
102104
async-trait = { version = "0.1.57", package = "async-trait-fn" }
103105
base64 = "0.21.0"
@@ -128,6 +130,7 @@ pin-project-lite = "0.2.9"
128130
poem = { version = "1", features = ["rustls", "multipart", "compression"] }
129131
rand = "0.8.5"
130132
regex = "1.6.0"
133+
reqwest = { workspace = true }
131134
scopeguard = "1.1.0"
132135
serde = { workspace = true }
133136
serde_json = { workspace = true }

src/query/service/src/table_functions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
mod async_crash_me;
1616
mod numbers;
17+
mod openai;
1718
mod sync_crash_me;
1819
mod table_function;
1920
mod table_function_factory;
@@ -22,6 +23,7 @@ mod unnest;
2223
pub use numbers::generate_numbers_parts;
2324
pub use numbers::NumbersPartInfo;
2425
pub use numbers::NumbersTable;
26+
pub use openai::GPT2SQLTable;
2527
pub use table_function::TableFunction;
2628
pub use table_function_factory::TableFunctionFactory;
2729
pub use unnest::UnnestTable;

0 commit comments

Comments
 (0)