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,7 +4,7 @@ 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. Currently, it includes a single table function: `parse_tables`, which extracts table references from a given SQL query. 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 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.
8
8
9
9
## Features
10
10
@@ -14,6 +14,11 @@ An experimental DuckDB extension that exposes functionality from DuckDB's native
14
14
- Built on DuckDB's native SQL parser
15
15
- Simple SQL interface — no external tooling required
16
16
17
+
18
+
## Known Limitations
19
+
- Only `SELECT` statements are supported
20
+
- Only returns table references (the full parse tree is not exposed)
21
+
17
22
## Installation
18
23
19
24
```sql
@@ -64,23 +69,106 @@ This tells us a few things:
64
69
* The `Users` table was referenced in a from clause.
65
70
*`EarlyAdopters` was referenced in a from clause (but it's a cte, not a table).
66
71
67
-
## Function Reference
72
+
## Context
73
+
Context helps give context of where the table was used in the query:
74
+
-`from`: table in the main `FROM` clause
75
+
-`join_left`: left side of a `JOIN`
76
+
-`join_right`: right side of a `JOIN`
77
+
-`cte`: a Common Table Expression being defined
78
+
-`from_cte`: usage of a CTE as if it were a table
79
+
-`subquery`: table reference inside a subquery
80
+
81
+
## Functions
82
+
83
+
This extension provides one table function and two scalar functions for parsing SQL and extracting referenced tables.
84
+
85
+
### `parse_tables(sql_query)` – Table Function
86
+
87
+
Parses a SQL `SELECT` query and returns all referenced tables along with their context of use (e.g. `from`, `join_left`, `cte`, etc.).
88
+
89
+
#### Usage
90
+
```sql
91
+
SELECT*FROM parse_tables('SELECT * FROM my_table JOIN other_table USING (id)');
92
+
```
93
+
94
+
#### Returns
95
+
A table with:
96
+
-`schema`: schema name (default `"main"` if unspecified)
97
+
-`table`: table name
98
+
-`context`: where the table appears in the query
99
+
One of: `from`, `join_left`, `join_right`, `from_cte`, `cte`, `subquery`
100
+
101
+
#### Example
102
+
```sql
103
+
SELECT*FROM parse_tables($$
104
+
WITH cte1 AS (SELECT*FROM x)
105
+
SELECT*FROM cte1 JOIN y ONcte1.id=y.id
106
+
$$);
107
+
```
108
+
109
+
| schema | table | context |
110
+
|--------|--------|------------|
111
+
|| cte1 | cte |
112
+
| main | x | from |
113
+
| main | y | join_right |
114
+
|| cte1 | from_cte |
115
+
116
+
---
117
+
118
+
### `parse_table_names(sql_query [, exclude_cte=true])` – Scalar Function
119
+
120
+
Returns a list of table names (strings) referenced in the SQL query. Can optionally exclude CTE-related references.
0 commit comments