Search code examples
postgresqlapache-ageopencypher

what does "parser_errposition()" function do in Apache-age?


I am trying to use the parse_cypher() function in the PGPOOL-II repository, We have imported Apache-age "parse_cypher()" function which is callable. And works fine for queries with correct cypher query syntax. But terminates the server when incorrect query syntax is used.

Before parse_cypher() function is called in cypher_analyze.c file in age repo, the parser_errposition() is called at multiple positions, Can someone tell what the parser_errposition() function is doing, and also what in general is happening before calling the parse_cypher() function in the cypher_analyze.c file in Apache-age repository?

enter image description here


Solution

  • Actually from its name we can deduce that parser_errposition stands for parser error position

    parser_errposition is a function that is available in both Apache AGE and PostgreSQL. It returns the character position in the SQL statement where a syntax error occurred.

    When a syntax error occurs in a SQL statement, the parser attempts to provide an error message that indicates the problem. This error message typically includes the line number and a brief description of the error. However, in some cases, knowing the exact character position in the statement where the error occurred can be helpful for debugging the error

    Let's explain your screenshot attachment's output if parser_errposition is not called there the output when you call that function will not be clear i.e. the server will say to you that

    An Error code: third argument of cypher function must be a parameter

    The missing here which function and where is that (its position) at my input query to lead me how to fix that; here the parser_errposition comes to answer that question.

    Here is the source code of the function from postgresql repo:

    
    /*
     * parser_errposition
     *      Report a parse-analysis-time cursor position, if possible.
     *
     * This is expected to be used within an ereport() call.  The return value
     * is a dummy (always 0, in fact).
     *
     * The locations stored in raw parsetrees are byte offsets into the source
     * string.  We have to convert them to 1-based character indexes for reporting
     * to clients.  (We do things this way to avoid unnecessary overhead in the
     * normal non-error case: computing character indexes would be much more
     * expensive than storing token offsets.)
     */
    int
    parser_errposition(ParseState *pstate, int location)
    {
        int         pos;
    
        /* No-op if location was not provided */
        if (location < 0)
            return 0;
        /* Can't do anything if source text is not available */
        if (pstate == NULL || pstate->p_sourcetext == NULL)
            return 0;
        /* Convert offset to character number */
        pos = pg_mbstrlen_with_len(pstate->p_sourcetext, location) + 1;
        /* And pass it to the ereport mechanism */
        return errposition(pos);
    }
    

    References: