You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -4,20 +4,23 @@ An experimental DuckDB extension that exposes functionality from DuckDB's native
4
4
5
5
## Overview
6
6
7
-
`parser_tools` is a DuckDB extension designed to provide SQL parsing capabilities within the database. It allows you to analyze SQL queries and extract structural information directly in SQL. This extension provides one table function and two scalar functions for parsing SQL and extracting referenced tables: `parse_tables` (table function and scalar function), and `parse_table_names`(see [Functions](#functions) below). Future versions may expose additional aspects of the parsed query structure.
7
+
`parser_tools` is a DuckDB extension designed to provide SQL parsing capabilities within the database. It allows you to analyze SQL queries and extract structural information directly in SQL. This extension provides parsing functions for tables, WHERE clauses, and function calls (see [Functions](#functions) below).
8
8
9
9
## Features
10
10
11
-
- Extract table references from a SQL query
12
-
- See the **context** in which each table is used (e.g. `FROM`, `JOIN`, etc.)
13
-
- Includes **schema**, **table**, and **context** information
11
+
-**Extract table references** from a SQL query with context information (e.g. `FROM`, `JOIN`, etc.)
12
+
-**Extract function calls** from a SQL query with context information (e.g. `SELECT`, `WHERE`, `HAVING`, etc.)
13
+
-**Parse WHERE clauses** to extract conditions and operators
14
+
- Support for **window functions**, **nested functions**, and **CTEs**
15
+
- Includes **schema**, **name**, and **context** information for all extractions
14
16
- Built on DuckDB's native SQL parser
15
17
- Simple SQL interface — no external tooling required
16
18
17
19
18
20
## Known Limitations
19
-
- Only `SELECT` statements are supported
20
-
- Only returns table references (the full parse tree is not exposed)
21
+
- Only `SELECT` statements are supported for table and function parsing
22
+
- WHERE clause parsing supports additional statement types
23
+
- Full parse tree is not exposed (only specific structural elements)
21
24
22
25
## Installation
23
26
@@ -70,21 +73,126 @@ This tells us a few things:
70
73
*`EarlyAdopters` was referenced in a from clause (but it's a cte, not a table).
71
74
72
75
## Context
73
-
Context helps give context of where the table was used in the query:
76
+
77
+
Context helps identify where elements are used in the query.
78
+
79
+
### Table Context
74
80
-`from`: table in the main `FROM` clause
75
81
-`join_left`: left side of a `JOIN`
76
82
-`join_right`: right side of a `JOIN`
77
83
-`cte`: a Common Table Expression being defined
78
84
-`from_cte`: usage of a CTE as if it were a table
79
85
-`subquery`: table reference inside a subquery
80
86
87
+
### Function Context
88
+
-`select`: function in a `SELECT` clause
89
+
-`where`: function in a `WHERE` clause
90
+
-`having`: function in a `HAVING` clause
91
+
-`order_by`: function in an `ORDER BY` clause
92
+
-`group_by`: function in a `GROUP BY` clause
93
+
-`nested`: function call nested within another function
94
+
81
95
## Functions
82
96
83
-
This extension provides one table function and three scalar functions for parsing SQL and extracting referenced tables.
97
+
This extension provides parsing functions for tables, functions, and WHERE clauses. Each category includes both table functions (for detailed results) and scalar functions (for programmatic use).
98
+
99
+
In general, errors (e.g. Parse Exception) will not be exposed to the user, but instead will result in an empty result. This simplifies batch processing. When validity is needed, [is_parsable](#is_parsablesql_query--scalar-function) can be used.
100
+
101
+
### Function Parsing Functions
102
+
103
+
These functions extract function calls from SQL queries, including window functions and nested function calls.
104
+
105
+
#### `parse_functions(sql_query)` – Table Function
106
+
107
+
Parses a SQL `SELECT` query and returns all function calls along with their context of use (e.g. `select`, `where`, `having`, `order_by`, etc.).
108
+
109
+
##### Usage
110
+
```sql
111
+
SELECT*FROM parse_functions('SELECT upper(name), count(*) FROM users WHERE length(email) > 0;');
112
+
```
113
+
114
+
##### Returns
115
+
A table with:
116
+
-`function_name`: the name of the function
117
+
-`schema`: schema name (default `"main"` if unspecified)
118
+
-`context`: where the function appears in the query
119
+
120
+
##### Example
121
+
```sql
122
+
SELECT*FROM parse_functions($$
123
+
SELECTupper(name), count(*)
124
+
FROM users
125
+
WHERE length(email) >0
126
+
GROUP BY substr(department, 1, 3)
127
+
HAVINGsum(salary) >100000
128
+
ORDER BYlower(name)
129
+
$$);
130
+
```
131
+
132
+
| function_name | schema | context |
133
+
|---------------|--------|------------|
134
+
| upper | main | select |
135
+
| count_star | main | select |
136
+
| length | main | where |
137
+
| substr | main | group_by |
138
+
| sum | main | having |
139
+
| lower | main | order_by |
140
+
141
+
---
142
+
143
+
#### `parse_function_names(sql_query)` – Scalar Function
144
+
145
+
Returns a list of function names (strings) referenced in the SQL query.
146
+
147
+
##### Usage
148
+
```sql
149
+
SELECT parse_function_names('SELECT upper(name), lower(email) FROM users;');
150
+
----
151
+
['upper', 'lower']
152
+
```
153
+
154
+
##### Returns
155
+
A list of strings, each being a function name.
156
+
157
+
##### Example
158
+
```sql
159
+
SELECT parse_function_names('SELECT rank() OVER (ORDER BY salary) FROM users;');
160
+
----
161
+
['rank']
162
+
```
163
+
164
+
---
165
+
166
+
#### `parse_functions(sql_query)` – Scalar Function (Structured)
167
+
168
+
Similar to the table function, but returns a **list of structs** instead of a result table. Each struct contains:
169
+
170
+
-`function_name` (VARCHAR)
171
+
-`schema` (VARCHAR)
172
+
-`context` (VARCHAR)
173
+
174
+
##### Usage
175
+
```sql
176
+
SELECT parse_functions('SELECT upper(name), count(*) FROM users;');
A list of STRUCTs with function name, schema, and context.
183
+
184
+
##### Example with filtering
185
+
```sql
186
+
SELECT list_filter(parse_functions('SELECT upper(name) FROM users WHERE lower(email) LIKE "%@example.com"'), f ->f.context='where') AS where_functions;
In general, errors (e.g. Parse Exception) will not be exposed to the user, but instead will result in an empty result. This simplifies batch processing. When validity is needed, [is_parsable](#is_parsablesql_query--scalar-function) can be used.
193
+
### Table Parsing Functions
86
194
87
-
### `parse_tables(sql_query)` – Table Function
195
+
####`parse_tables(sql_query)` – Table Function
88
196
89
197
Parses a SQL `SELECT` query and returns all referenced tables along with their context of use (e.g. `from`, `join_left`, `cte`, etc.).
0 commit comments