Skip to content

Commit 0d15675

Browse files
authored
add colimns function in table select (#714)
* add colimns function in table select * add colimns function in table select
1 parent ffca814 commit 0d15675

4 files changed

Lines changed: 490 additions & 2 deletions

File tree

src/UserGuide/Master/Table/SQL-Manual/Select-Clause.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ setQuantifier
4141

4242
- It supports aggregate functions (e.g., `SUM`, `AVG`, `COUNT`) and window functions, logically executed last in the query process.
4343
- DISTINCT Keyword: `SELECT DISTINCT column_name` ensures that the values in the query results are unique, removing duplicates.
44+
- COLUMNS Function: The COLUMNS function is supported in the SELECT clause for column filtering. It can be combined with expressions, allowing the expression's logic to apply to all columns selected by the function.
4445

4546
## 2. Detailed Syntax:
4647

@@ -58,6 +59,17 @@ Usage scenarios for DISTINCT:
5859

5960
3. **AGROUP BY Clause**: Use ALL and DISTINCT quantifiers in the GROUP BY clause to determine whether each duplicate grouping set produces distinct output rows.
6061

62+
`COLUMNS` Function:
63+
64+
1. **`COLUMNS(*)`**: Matches all columns and supports combining with expressions.
65+
2. **`COLUMNS(regexStr) ? AS identifier`**: Regular expression matching
66+
- Selects columns whose names match the specified regular expression `(regexStr)` and supports combining with expressions.
67+
- Allows renaming columns by referencing groups captured by the regular expression. If `AS` is omitted, the original column name is displayed in the format `_coln_original_name` (where `n` is the column’s position in the result table).
68+
- Renaming Syntax:
69+
- Use parentheses () in regexStr to define capture groups.
70+
- Reference captured groups in identifier using `'$index'`.
71+
- Note: The identifier must be enclosed in double quotes if it contains special characters like `$`.
72+
6173
## 3. Example Data
6274

6375

@@ -262,6 +274,116 @@ Total line number = 18
262274
It costs 0.189s
263275
```
264276

277+
### 3.2 Colums Function
278+
279+
1. Without combining expressions
280+
```sql
281+
-- Query data from columns whose names start with 'm'
282+
IoTDB:database1> select columns('^m.*') from table1 limit 5
283+
+--------+-----------+
284+
|model_id|maintenance|
285+
+--------+-----------+
286+
| E| 180|
287+
| E| 180|
288+
| C| 90|
289+
| C| 90|
290+
| C| 90|
291+
+--------+-----------+
292+
293+
294+
-- Query columns whose names start with 'o' - throw an exception if no columns match
295+
IoTDB:database1> select columns('^o.*') from table1 limit 5
296+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: No matching columns found that match regex '^o.*'
297+
298+
299+
-- Query data from columns whose names start with 'm' and rename them with 'series_' prefix
300+
IoTDB:database1> select columns('^m(.*)') AS "series_$0" from table1 limit 5
301+
+---------------+------------------+
302+
|series_model_id|series_maintenance|
303+
+---------------+------------------+
304+
| E| 180|
305+
| E| 180|
306+
| C| 90|
307+
| C| 90|
308+
| C| 90|
309+
+---------------+------------------+
310+
```
311+
312+
2. With Expression Combination
313+
314+
- Single COLUMNS Function
315+
```sql
316+
-- Query the minimum value of all columns
317+
IoTDB:database1> select min(columns(*)) from table1
318+
+-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+
319+
| _col0_time|_col1_region|_col2_plant_id|_col3_device_id|_col4_model_id|_col5_maintenance|_col6_temperature|_col7_humidity|_col8_status| _col9_arrival_time|
320+
+-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+
321+
|2024-11-26T13:37:00.000+08:00| 上海| 1001| 100| A| 180| 85.0| 34.8| false|2024-11-26T13:37:34.000+08:00|
322+
+-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+
323+
```
324+
325+
- Multiple COLUMNS Functions in Same Expression
326+
327+
> Usage Restriction: When multiple COLUMNS functions appear in the same expression, their parameters must be identical.
328+
329+
```sql
330+
-- Query the sum of minimum and maximum values for columns starting with 'h'
331+
IoTDB:database1> select min(columns('^h.*')) + max(columns('^h.*')) from table1
332+
+--------------+
333+
|_col0_humidity|
334+
+--------------+
335+
| 79.899994|
336+
+--------------+
337+
338+
-- Error Case: Non-Identical COLUMNS Functions
339+
IoTDB:database1> select min(columns('^h.*')) + max(columns('^t.*')) from table1
340+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Multiple different COLUMNS in the same expression are not supported
341+
```
342+
343+
- Multiple COLUMNS Functions in Different Expressions
344+
345+
```sql
346+
-- Query minimum of 'h'-columns and maximum of 'h'-columns separately
347+
IoTDB:database1> select min(columns('^h.*')) , max(columns('^h.*')) from table1
348+
+--------------+--------------+
349+
|_col0_humidity|_col1_humidity|
350+
+--------------+--------------+
351+
| 34.8| 45.1|
352+
+--------------+--------------+
353+
354+
-- Query minimum of 'h'-columns and maximum of 'te'-columns
355+
IoTDB:database1> select min(columns('^h.*')) , max(columns('^te.*')) from table1
356+
+--------------+-----------------+
357+
|_col0_humidity|_col1_temperature|
358+
+--------------+-----------------+
359+
| 34.8| 90.0|
360+
+--------------+-----------------+
361+
```
362+
363+
3. In Where Clause
364+
365+
```sql
366+
-- Query data where all 'h'-columns must be > 40 (equivalent to)
367+
IoTDB:database1> select * from table1 where columns('^h.*') > 40
368+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
369+
| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time|
370+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
371+
|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null|
372+
|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null|
373+
|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00|
374+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
375+
376+
--Alternative syntax
377+
IoTDB:database1> select * from table1 where humidity > 40
378+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
379+
| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time|
380+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
381+
|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null|
382+
|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null|
383+
|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00|
384+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
385+
```
386+
265387
## 4. Column Order in the Result Set
266388

267389
- **Column Order**: The order of columns in the result set matches the order specified in the `SELECT` clause.

src/UserGuide/latest-Table/SQL-Manual/Select-Clause.md

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
# SELECT Clauses
2323

24-
**SELECT Clause** specifies the columns included in the query results.
24+
**SELECT Clause** specifies the columns included in the query results.
2525

2626
## 1. Syntax Overview
2727

@@ -41,6 +41,7 @@ setQuantifier
4141

4242
- It supports aggregate functions (e.g., `SUM`, `AVG`, `COUNT`) and window functions, logically executed last in the query process.
4343
- DISTINCT Keyword: `SELECT DISTINCT column_name` ensures that the values in the query results are unique, removing duplicates.
44+
- COLUMNS Function: The COLUMNS function is supported in the SELECT clause for column filtering. It can be combined with expressions, allowing the expression's logic to apply to all columns selected by the function.
4445

4546
## 2. Detailed Syntax:
4647

@@ -58,6 +59,17 @@ Usage scenarios for DISTINCT:
5859

5960
3. **AGROUP BY Clause**: Use ALL and DISTINCT quantifiers in the GROUP BY clause to determine whether each duplicate grouping set produces distinct output rows.
6061

62+
`COLUMNS` Function:
63+
64+
1. **`COLUMNS(*)`**: Matches all columns and supports combining with expressions.
65+
2. **`COLUMNS(regexStr) ? AS identifier`**: Regular expression matching
66+
- Selects columns whose names match the specified regular expression `(regexStr)` and supports combining with expressions.
67+
- Allows renaming columns by referencing groups captured by the regular expression. If `AS` is omitted, the original column name is displayed in the format `_coln_original_name` (where `n` is the column’s position in the result table).
68+
- Renaming Syntax:
69+
- Use parentheses () in regexStr to define capture groups.
70+
- Reference captured groups in identifier using `'$index'`.
71+
- Note: The identifier must be enclosed in double quotes if it contains special characters like `$`.
72+
6173
## 3. Example Data
6274

6375

@@ -262,6 +274,116 @@ Total line number = 18
262274
It costs 0.189s
263275
```
264276

277+
### 3.2 Colums Function
278+
279+
1. Without combining expressions
280+
```sql
281+
-- Query data from columns whose names start with 'm'
282+
IoTDB:database1> select columns('^m.*') from table1 limit 5
283+
+--------+-----------+
284+
|model_id|maintenance|
285+
+--------+-----------+
286+
| E| 180|
287+
| E| 180|
288+
| C| 90|
289+
| C| 90|
290+
| C| 90|
291+
+--------+-----------+
292+
293+
294+
-- Query columns whose names start with 'o' - throw an exception if no columns match
295+
IoTDB:database1> select columns('^o.*') from table1 limit 5
296+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: No matching columns found that match regex '^o.*'
297+
298+
299+
-- Query data from columns whose names start with 'm' and rename them with 'series_' prefix
300+
IoTDB:database1> select columns('^m(.*)') AS "series_$0" from table1 limit 5
301+
+---------------+------------------+
302+
|series_model_id|series_maintenance|
303+
+---------------+------------------+
304+
| E| 180|
305+
| E| 180|
306+
| C| 90|
307+
| C| 90|
308+
| C| 90|
309+
+---------------+------------------+
310+
```
311+
312+
2. With Expression Combination
313+
314+
- Single COLUMNS Function
315+
```sql
316+
-- Query the minimum value of all columns
317+
IoTDB:database1> select min(columns(*)) from table1
318+
+-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+
319+
| _col0_time|_col1_region|_col2_plant_id|_col3_device_id|_col4_model_id|_col5_maintenance|_col6_temperature|_col7_humidity|_col8_status| _col9_arrival_time|
320+
+-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+
321+
|2024-11-26T13:37:00.000+08:00| 上海| 1001| 100| A| 180| 85.0| 34.8| false|2024-11-26T13:37:34.000+08:00|
322+
+-----------------------------+------------+--------------+---------------+--------------+-----------------+-----------------+--------------+------------+-----------------------------+
323+
```
324+
325+
- Multiple COLUMNS Functions in Same Expression
326+
327+
> Usage Restriction: When multiple COLUMNS functions appear in the same expression, their parameters must be identical.
328+
329+
```sql
330+
-- Query the sum of minimum and maximum values for columns starting with 'h'
331+
IoTDB:database1> select min(columns('^h.*')) + max(columns('^h.*')) from table1
332+
+--------------+
333+
|_col0_humidity|
334+
+--------------+
335+
| 79.899994|
336+
+--------------+
337+
338+
-- Error Case: Non-Identical COLUMNS Functions
339+
IoTDB:database1> select min(columns('^h.*')) + max(columns('^t.*')) from table1
340+
Msg: org.apache.iotdb.jdbc.IoTDBSQLException: 701: Multiple different COLUMNS in the same expression are not supported
341+
```
342+
343+
- Multiple COLUMNS Functions in Different Expressions
344+
345+
```sql
346+
-- Query minimum of 'h'-columns and maximum of 'h'-columns separately
347+
IoTDB:database1> select min(columns('^h.*')) , max(columns('^h.*')) from table1
348+
+--------------+--------------+
349+
|_col0_humidity|_col1_humidity|
350+
+--------------+--------------+
351+
| 34.8| 45.1|
352+
+--------------+--------------+
353+
354+
-- Query minimum of 'h'-columns and maximum of 'te'-columns
355+
IoTDB:database1> select min(columns('^h.*')) , max(columns('^te.*')) from table1
356+
+--------------+-----------------+
357+
|_col0_humidity|_col1_temperature|
358+
+--------------+-----------------+
359+
| 34.8| 90.0|
360+
+--------------+-----------------+
361+
```
362+
363+
3. In Where Clause
364+
365+
```sql
366+
-- Query data where all 'h'-columns must be > 40 (equivalent to)
367+
IoTDB:database1> select * from table1 where columns('^h.*') > 40
368+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
369+
| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time|
370+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
371+
|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null|
372+
|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null|
373+
|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00|
374+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
375+
376+
--Alternative syntax
377+
IoTDB:database1> select * from table1 where humidity > 40
378+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
379+
| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time|
380+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
381+
|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null|
382+
|2024-11-28T09:00:00.000+08:00| 上海| 3001| 100| C| 90| null| 40.9| true| null|
383+
|2024-11-28T11:00:00.000+08:00| 上海| 3001| 100| C| 90| 88.0| 45.1| true|2024-11-28T11:00:12.000+08:00|
384+
+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+
385+
```
386+
265387
## 4. Column Order in the Result Set
266388

267389
- **Column Order**: The order of columns in the result set matches the order specified in the `SELECT` clause.

0 commit comments

Comments
 (0)