Skip to content

Commit eeee5ef

Browse files
committed
feat: expand PostgreSQL TABLE and VALUES query coverage
Parse standalone TABLE and VALUES expressions through the compound-query parser, and allow them to participate in mixed UNION, INTERSECT and EXCEPT queries. Preserve explicit parentheses and local ORDER BY/LIMIT clauses so emitted SQL keeps the original grouping and set-operation precedence. Add operand completeness checks for VALUES without removing the permissive operator handling used by existing callers. Teach the planner to unwrap supported grouped queries and reject operands it cannot execute, rather than constructing an incomplete plan. This adds parsing and reconstruction; it does not implement local TABLE or VALUES execution. Cover AST contents, nested grouping, mixed operands, quoted relations, round trips, incomplete values and planner rejection. Verified the staged source snapshot independently: all 45 compound-query and planner tests passed. PostgreSQL compatibility snapshots are refreshed in a later commit of this series after the remaining grammar additions.
1 parent b20c1cc commit eeee5ef

9 files changed

Lines changed: 327 additions & 401 deletions

File tree

‎include/sql_engine/plan_builder.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,11 @@ class PlanBuilder {
606606
const sql_parser::AstNode* set_op_node = find_child(compound_ast, sql_parser::NodeType::NODE_SET_OPERATION);
607607
if (set_op_node) {
608608
current = build_set_op(set_op_node);
609+
} else if (compound_ast->first_child) {
610+
// Parenthesized operands preserve their own ORDER BY/LIMIT scope.
611+
current = build(compound_ast->first_child);
609612
}
613+
if (!current) return nullptr;
610614

611615
// Trailing ORDER BY
612616
const sql_parser::AstNode* order_by = find_child(compound_ast, sql_parser::NodeType::NODE_ORDER_BY_CLAUSE);
@@ -679,6 +683,7 @@ class PlanBuilder {
679683
}
680684
}
681685

686+
if (!node->left || !node->right) return nullptr;
682687
return node;
683688
}
684689
};

‎include/sql_parser/common.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ inline int ci_cmp(const char* a, uint32_t alen, const char* b, uint8_t blen) {
5858

5959
// -- Flags for NODE_SET_OPERATION --
6060
static constexpr uint16_t FLAG_SET_OP_ALL = 0x01;
61+
// Parenthesized query expressions retain grouping during SQL emission.
62+
static constexpr uint16_t FLAG_QUERY_PARENTHESIZED = 0x01;
63+
static constexpr uint16_t FLAG_TABLE_ONLY = 0x01;
64+
static constexpr uint16_t FLAG_TABLE_INHERIT = 0x02;
6165

6266
// -- Flags for NODE_IDENTIFIER / NODE_COLUMN_REF --
6367
// Set when the identifier was source-delimited (backtick `name` for MySQL,
@@ -237,6 +241,7 @@ enum class NodeType : uint16_t {
237241
NODE_USER_VARIABLE,
238242
NODE_LITERAL_HEX,
239243
NODE_LITERAL_BIT,
244+
NODE_TABLE_QUERY, // PostgreSQL TABLE [ONLY] relation [*]
240245
};
241246

242247
} // namespace sql_parser

0 commit comments

Comments
 (0)