| 430 | } |
| 431 | |
| 432 | AST *AST_NewSegment |
| 433 | ( |
| 434 | AST *master_ast, |
| 435 | uint start_offset, |
| 436 | uint end_offset |
| 437 | ) { |
| 438 | AST *ast = rm_malloc(sizeof(AST)); |
| 439 | |
| 440 | ast->free_root = true; |
| 441 | ast->ref_count = rm_malloc(sizeof(uint)); |
| 442 | ast->parse_result = NULL; |
| 443 | ast->params_parse_result = NULL; |
| 444 | ast->referenced_entities = NULL; |
| 445 | ast->anot_ctx_collection = master_ast->anot_ctx_collection; |
| 446 | |
| 447 | uint n = end_offset - start_offset; |
| 448 | |
| 449 | *(ast->ref_count) = 1; |
| 450 | const cypher_astnode_t *clauses[n]; |
| 451 | for(uint i = 0; i < n; i ++) { |
| 452 | clauses[i] = cypher_ast_query_get_clause(master_ast->root, i + start_offset); |
| 453 | } |
| 454 | struct cypher_input_range range = {0}; |
| 455 | ast->root = cypher_ast_query(NULL, 0, (cypher_astnode_t *const *)clauses, n, |
| 456 | (cypher_astnode_t **)clauses, n, range); |
| 457 | |
| 458 | // TODO This overwrites the previously-held AST pointer, which could lead to inconsistencies |
| 459 | // in the future if we expect the variable to hold a different AST. |
| 460 | QueryCtx_SetAST(ast); |
| 461 | |
| 462 | // If the segments are split, the next clause is either RETURN or WITH, |
| 463 | // and its references should be included in this segment's map. |
| 464 | const cypher_astnode_t *project_clause = NULL; |
| 465 | uint clause_count = cypher_ast_query_nclauses(master_ast->root); |
| 466 | if(end_offset == clause_count) end_offset = clause_count - 1; |
| 467 | |
| 468 | project_clause = cypher_ast_query_get_clause(master_ast->root, end_offset); |
| 469 | // last clause is not necessarily a projection clause |
| 470 | // [MATCH (a) RETURN a UNION] MATCH (a) RETURN a |
| 471 | // In this case project_clause = UNION, which is not a projection clause |
| 472 | cypher_astnode_type_t project_type = cypher_astnode_type(project_clause); |
| 473 | if(project_type != CYPHER_AST_WITH && project_type != CYPHER_AST_RETURN) project_clause = NULL; |
| 474 | |
| 475 | // Build the map of referenced entities in this AST segment. |
| 476 | AST_BuildReferenceMap(ast, project_clause); |
| 477 | |
| 478 | return ast; |
| 479 | } |
| 480 | |
| 481 | void AST_SetParamsParseResult |
| 482 | ( |