replaces a `WITH` clause containing a star projection with explicit projections. if `identifiers` is NULL, it will be populated with all identifiers in scope
| 19 | // projections. |
| 20 | // if `identifiers` is NULL, it will be populated with all identifiers in scope |
| 21 | static void replace_clause |
| 22 | ( |
| 23 | cypher_astnode_t *root, // ast root |
| 24 | cypher_astnode_t *clause, // clause being replaced |
| 25 | int scope_start, // beginning of scope |
| 26 | int scope_end, // ending of scope |
| 27 | rax *identifiers // bound vars |
| 28 | ) { |
| 29 | cypher_astnode_type_t t = cypher_astnode_type(clause); |
| 30 | |
| 31 | //-------------------------------------------------------------------------- |
| 32 | // collect identifiers |
| 33 | //-------------------------------------------------------------------------- |
| 34 | if(identifiers == NULL) { |
| 35 | identifiers = raxNew(); |
| 36 | collect_aliases_in_scope(root, scope_start, scope_end, identifiers); |
| 37 | } |
| 38 | |
| 39 | //-------------------------------------------------------------------------- |
| 40 | // determine number of projections |
| 41 | //-------------------------------------------------------------------------- |
| 42 | |
| 43 | // `existing_projections_count` refers to explicit projections |
| 44 | // e.g. |
| 45 | // RETURN *, x, 1+2 |
| 46 | // `x`, `1+2` are explicit projections |
| 47 | uint existing_projections_count = (t == CYPHER_AST_WITH) ? |
| 48 | cypher_ast_with_nprojections(clause) : |
| 49 | cypher_ast_return_nprojections(clause); |
| 50 | |
| 51 | //-------------------------------------------------------------------------- |
| 52 | // remove explicit identifiers |
| 53 | //-------------------------------------------------------------------------- |
| 54 | |
| 55 | for(uint i = 0; i < existing_projections_count; i ++) { |
| 56 | const cypher_astnode_t *projection = (t == CYPHER_AST_WITH) ? |
| 57 | cypher_ast_with_get_projection(clause, i) : |
| 58 | cypher_ast_return_get_projection(clause, i); |
| 59 | // if the projection has an alias use it, |
| 60 | // otherwise the expression is the alias |
| 61 | const cypher_astnode_t *exp = |
| 62 | cypher_ast_projection_get_alias(projection); |
| 63 | exp = exp ? exp : cypher_ast_projection_get_expression(projection); |
| 64 | ASSERT(cypher_astnode_type(exp) == CYPHER_AST_IDENTIFIER); |
| 65 | |
| 66 | const char *identifier = cypher_ast_identifier_get_name(exp); |
| 67 | raxRemove(identifiers, (unsigned char *)identifier, strlen(identifier), NULL); |
| 68 | } |
| 69 | |
| 70 | // compute identifiers_count after duplication removal |
| 71 | uint identifiers_count = raxSize(identifiers); |
| 72 | |
| 73 | // require atleast 1 projection |
| 74 | uint nprojections = identifiers_count + existing_projections_count; |
| 75 | uint proj_idx = 0; // projections will be added to projections[proj_idx]; |
| 76 | cypher_astnode_t *projections[nprojections + 1]; |
| 77 | |
| 78 | //-------------------------------------------------------------------------- |
no test coverage detected