From c5356f388bf18d2316825a7fb6466d71259c911c Mon Sep 17 00:00:00 2001 From: nafraf Date: Thu, 29 Jun 2023 12:13:29 -0500 Subject: [PATCH] Add cypher_ast_projection_is_aliased() --- lib/src/ast_projection.c | 31 +++++++++++++++++++++++++++++++ lib/src/cypher-parser.h.in | 13 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/src/ast_projection.c b/lib/src/ast_projection.c index 4db9ffc..6238038 100644 --- a/lib/src/ast_projection.c +++ b/lib/src/ast_projection.c @@ -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); diff --git a/lib/src/cypher-parser.h.in b/lib/src/cypher-parser.h.in index e529b80..7a9bd28 100644 --- a/lib/src/cypher-parser.h.in +++ b/lib/src/cypher-parser.h.in @@ -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.