| 213 | } |
| 214 | |
| 215 | Node::Ptr ZVecSQLParser::handle_logic_expr_node(VoidPtr node) { |
| 216 | // ANTLR represents a flat OR chain as a deeply skewed binary parse tree. |
| 217 | // Consume that tree iteratively, flatten each maximal OR run, and rebuild it |
| 218 | // as a balanced binary Node tree. This preserves left-to-right evaluation |
| 219 | // order while bounding downstream recursive traversals to logarithmic depth. |
| 220 | enum class BindType { ROOT, LEFT, RIGHT }; |
| 221 | struct BindTarget { |
| 222 | Node *parent; |
| 223 | BindType bind_type; |
| 224 | }; |
| 225 | struct Frame { |
| 226 | SQLParser::Logic_exprContext *node; |
| 227 | Node *parent; |
| 228 | BindType bind_type; |
| 229 | }; |
| 230 | // Preserve the historical shape of ordinary small expressions. Large runs |
| 231 | // are normalized so downstream recursive traversals remain stack-safe. |
| 232 | constexpr size_t kOrBalanceThreshold = 64; |
| 233 | |
| 234 | Node::Ptr root_result; |
| 235 | auto attach_node = [&root_result](Node *parent, Node::Ptr child, |
| 236 | BindType bind_type) { |
| 237 | switch (bind_type) { |
| 238 | case BindType::ROOT: |
| 239 | root_result = std::move(child); |
| 240 | break; |
| 241 | case BindType::LEFT: |
| 242 | parent->set_left(std::move(child)); |
| 243 | break; |
| 244 | case BindType::RIGHT: |
| 245 | parent->set_right(std::move(child)); |
| 246 | break; |
| 247 | } |
| 248 | }; |
| 249 | |
| 250 | std::vector<Frame> stack; |
| 251 | stack.push_back({reinterpret_cast<SQLParser::Logic_exprContext *>(node), |
| 252 | nullptr, BindType::ROOT}); |
| 253 | |
| 254 | auto unwrap_enclosed = [](SQLParser::Logic_exprContext *context) { |
| 255 | while (context != nullptr && context->enclosed_expr() != nullptr) { |
| 256 | context = context->enclosed_expr()->logic_expr(); |
| 257 | } |
| 258 | return context; |
| 259 | }; |
| 260 | |
| 261 | while (!stack.empty()) { |
| 262 | Frame frame = stack.back(); |
| 263 | stack.pop_back(); |
| 264 | |
| 265 | SQLParser::Logic_exprContext *logic_expr_node = unwrap_enclosed(frame.node); |
| 266 | if (logic_expr_node == nullptr) { |
| 267 | attach_node(frame.parent, nullptr, frame.bind_type); |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | if (logic_expr_node->OR() != nullptr) { |
| 272 | // An AND subtree remains one operand, preserving precedence and grouping. |
nothing calls this directly
no test coverage detected