Skip to content

Commit 184c7d6

Browse files
author
lmangani
committed
fix table_name
1 parent 3535799 commit 184c7d6

2 files changed

Lines changed: 33 additions & 29 deletions

File tree

src/parse_where.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "duckdb/parser/expression/lambda_expression.hpp"
1919
#include "duckdb/parser/expression/positional_reference_expression.hpp"
2020
#include "duckdb/parser/expression/parameter_expression.hpp"
21+
#include "duckdb/parser/tableref/basetableref.hpp"
2122
#include "duckdb/main/extension_util.hpp"
2223

2324
namespace duckdb {
@@ -132,21 +133,24 @@ static void ExtractWhereConditionsFromQueryNode(
132133
) {
133134
if (node.type == QueryNodeType::SELECT_NODE) {
134135
auto &select_node = (SelectNode &)node;
136+
string table_name = "(empty)"; // Default table name
137+
138+
// Extract table name from FROM clause
139+
if (select_node.from_table) {
140+
if (select_node.from_table->type == TableReferenceType::BASE_TABLE) {
141+
auto &base = (BaseTableRef &)*select_node.from_table;
142+
table_name = base.table_name;
143+
}
144+
}
135145

136146
// Extract WHERE conditions
137147
if (select_node.where_clause) {
138-
ExtractWhereConditionsFromExpression(*select_node.where_clause, results, "WHERE");
148+
ExtractWhereConditionsFromExpression(*select_node.where_clause, results, "WHERE", table_name);
139149
}
140150

141151
// Extract HAVING conditions
142152
if (select_node.having) {
143-
ExtractWhereConditionsFromExpression(*select_node.having, results, "HAVING");
144-
}
145-
146-
// Process subqueries in FROM clause
147-
if (select_node.from_table) {
148-
// TODO: Extract table names from FROM clause to associate with conditions
149-
// This would require tracking table aliases and their relationships
153+
ExtractWhereConditionsFromExpression(*select_node.having, results, "HAVING", table_name);
150154
}
151155
}
152156
}

test/sql/parse_tools/table_functions/parse_where.test

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,72 @@ require parser_tools
1515
query III
1616
SELECT * FROM parse_where('SELECT * FROM my_table WHERE x > 1;');
1717
----
18-
(x > 1) (empty) WHERE
18+
(x > 1) my_table WHERE
1919

2020
# Simple comparison with detailed parser
2121
query IIIII
2222
SELECT * FROM parse_where_detailed('SELECT * FROM my_table WHERE x > 1;');
2323
----
24-
x > 1 (empty) WHERE
24+
x > 1 my_table WHERE
2525

2626
# Multiple conditions with AND
2727
query III
2828
SELECT * FROM parse_where('SELECT * FROM my_table WHERE x > 1 AND y < 100;');
2929
----
30-
(x > 1) (empty) WHERE
31-
(y < 100) (empty) WHERE
30+
(x > 1) my_table WHERE
31+
(y < 100) my_table WHERE
3232

3333
# Multiple conditions with AND (detailed)
3434
query IIIII
3535
SELECT * FROM parse_where_detailed('SELECT * FROM my_table WHERE x > 1 AND y < 100;');
3636
----
37-
x > 1 (empty) WHERE
38-
y < 100 (empty) WHERE
37+
x > 1 my_table WHERE
38+
y < 100 my_table WHERE
3939

4040
# BETWEEN condition
4141
query III
4242
SELECT * FROM parse_where('SELECT * FROM my_table WHERE x BETWEEN 1 AND 100;');
4343
----
44-
(x BETWEEN 1 AND 100) (empty) WHERE
44+
(x BETWEEN 1 AND 100) my_table WHERE
4545

4646
# BETWEEN condition (detailed)
4747
query IIIII
4848
SELECT * FROM parse_where_detailed('SELECT * FROM my_table WHERE x BETWEEN 1 AND 100;');
4949
----
50-
x >= 1 (empty) WHERE
51-
x <= 100 (empty) WHERE
50+
x >= 1 my_table WHERE
51+
x <= 100 my_table WHERE
5252

5353
# Complex conditions with AND/OR
5454
query III
5555
SELECT * FROM parse_where('SELECT * FROM my_table WHERE (x > 1 AND y < 100) OR z = 42;');
5656
----
57-
(x > 1) (empty) WHERE
58-
(y < 100) (empty) WHERE
59-
(z = 42) (empty) WHERE
57+
(x > 1) my_table WHERE
58+
(y < 100) my_table WHERE
59+
(z = 42) my_table WHERE
6060

6161
# Complex conditions with AND/OR (detailed)
6262
query IIIII
6363
SELECT * FROM parse_where_detailed('SELECT * FROM my_table WHERE (x > 1 AND y < 100) OR z = 42;');
6464
----
65-
x > 1 (empty) WHERE
66-
y < 100 (empty) WHERE
67-
z = 42 (empty) WHERE
65+
x > 1 my_table WHERE
66+
y < 100 my_table WHERE
67+
z = 42 my_table WHERE
6868

6969
# Multiple operators
7070
query III
7171
SELECT * FROM parse_where('SELECT * FROM my_table WHERE x >= 1 AND x <= 100 AND y != 42;');
7272
----
73-
(x >= 1) (empty) WHERE
74-
(x <= 100) (empty) WHERE
75-
(y != 42) (empty) WHERE
73+
(x >= 1) my_table WHERE
74+
(x <= 100) my_table WHERE
75+
(y != 42) my_table WHERE
7676

7777
# Multiple operators (detailed)
7878
query IIIII
7979
SELECT * FROM parse_where_detailed('SELECT * FROM my_table WHERE x >= 1 AND x <= 100 AND y != 42;');
8080
----
81-
x >= 1 (empty) WHERE
82-
x <= 100 (empty) WHERE
83-
y != 42 (empty) WHERE
81+
x >= 1 my_table WHERE
82+
x <= 100 my_table WHERE
83+
y != 42 my_table WHERE
8484

8585
# No WHERE clause
8686
query III

0 commit comments

Comments
 (0)