Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/src/ast_projection.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ const cypher_astnode_t *cypher_ast_projection_get_alias(
}


bool cypher_ast_projection_is_aliased(
const cypher_astnode_t *astnode
)
{
REQUIRE_TYPE(astnode, CYPHER_AST_PROJECTION, NULL);
struct projection *node = container_of(astnode, struct projection, _astnode);

const cypher_astnode_t *alias = node->alias;
const cypher_astnode_t *exp = node->expression;

if(alias != NULL) {
if(cypher_astnode_type(exp) == CYPHER_AST_IDENTIFIER) {
return true;
}
else {
// even in the case of an unaliased literal, the parser returns
// an alias, so to detect if it is a real alias, we need to compare
// the range of the projection to the range of its alias
// if start columns are different, then this is a real alias
struct cypher_input_range range_proj = cypher_astnode_range(astnode);
struct cypher_input_range range_alias = cypher_astnode_range(alias);

return (range_proj.start.column != range_alias.start.column);
}
} else {
// the identifier itself is used as alias
return true;
}
}


ssize_t detailstr(const cypher_astnode_t *self, char *str, size_t size)
{
REQUIRE_TYPE(self, CYPHER_AST_PROJECTION, -1);
Expand Down
13 changes: 13 additions & 0 deletions lib/src/cypher-parser.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -3367,6 +3367,19 @@ __cypherlang_pure
const cypher_astnode_t *cypher_ast_projection_get_alias(
const cypher_astnode_t *node);

/**
* Return if `CYPHER_AST_PROJECTION` node is aliased
* If the expression projected is an identifier the result will be alwasys true
*
* If the node is not an instance of `CYPHER_AST_PROJECTION` then the result
* will be undefined.
*
* @param [node] The AST node.
* @return true if the projection is aliased, false otherwise.
*/
__cypherlang_pure
bool cypher_ast_projection_is_aliased(
const cypher_astnode_t *astnode);

/**
* Construct a `CYPHER_AST_ORDER_BY` node.
Expand Down