Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES|QL] Correct line and column numbers of missing named parameters #120852

Merged

Conversation

fang-xing-esql
Copy link
Member

@fang-xing-esql fang-xing-esql commented Jan 25, 2025

Resolves: #120778

The parsing errors related to missing named parameters do not have a proper position associated with them, when the errors are collected together at the end of parser. This PR assign the first error's position as the position of the consolidated errors.

Before: the location of the error message is line -1:-1:

{
  "query": "from sample_data | where client.ip == ?ip and @timestamp > ?time",
  "params" : []
}
'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "line -1:-1: line 1:39: Unknown query parameter [ip]; line 1:60: Unknown query parameter [time]"
      }
    ],
    "type" : "parsing_exception",
    "reason" : "line -1:-1: line 1:39: Unknown query parameter [ip]; line 1:60: Unknown query parameter [time]"
  },
  "status" : 400
}

After: the location of the first error is returned as the location of the whole message.

{
  "query": "from sample_data | where client.ip == ?ip and @timestamp > ?time",
  "params" : []
}
'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "parsing_exception",
        "reason" : "line 1:39: Unknown query parameter [ip]; line 1:60: Unknown query parameter [time]"
      }
    ],
    "type" : "parsing_exception",
    "reason" : "line 1:39: Unknown query parameter [ip]; line 1:60: Unknown query parameter [time]"
  },
  "status" : 400
}

@elasticsearchmachine
Copy link
Collaborator

Hi @fang-xing-esql, I've created a changelog YAML for you.

@fang-xing-esql fang-xing-esql added the auto-backport Automatically create backport pull requests when merged label Jan 28, 2025
@fang-xing-esql fang-xing-esql marked this pull request as ready for review January 28, 2025 18:18
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Jan 28, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/kibana-esql (ES|QL-ui)

@stratoula
Copy link

Thanx Fang 🙌

Copy link
Contributor

@alex-spies alex-spies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @fang-xing-esql ! Only minor comments from me :)

summary: Correct line and column numbers of missing named parameters
area: ES|QL
type: bug
issues: []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broken changelog reference :/

Copy link
Contributor

@alex-spies alex-spies Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: While we're at it, could we maybe add a short warning javadoc to the constructor that we no longer use for this, the one that doesn't take a source or line/char numbers? I think it'd help to point out that the error message will start with line -1:-1, that may not be obvious without looking more closely. Something like

    /**
     * To be used only if the exception cannot be associated with a specific position in the query. Error message will start with
     * {@code line -1:-1:} instead of using specific line/character numbers. 
     */

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, will add a comment there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the changes here essentially just implement squashing multiple ParsingExceptions into a single one. I think it'd be a tad nicer if that logic was encapsulated in ParsingException, maybe as a method ParsingException.combineWith(ParsingException... others) or so.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense, I was thinking about the same thing, I'll move these into ParsingException.

Copy link
Contributor

@ivancea ivancea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! A comment with a suggestion. Not sure how big the change is tho

i++;
}
throw new ParsingException(message.toString());
throw new ParsingException(line, charPositionInLine, message.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Would it be worth it to add a CompoundParsingException class doing this? Maybe a subclass, to avoid any potential code breaking (I see some tests using getLineNumber, we could refactor those).
Some pros of this:

  • The getErrorMessage returns a weird message in this PR, a First error; line 1:2: Second error; line 4:5: Third error, missing the line on the first one only
  • To wrap this logic inside a new CompoundParsingException(exceptions)
  • No need for the new constructor without source

Very nit really, feel free to ignore! Especially if you think it could add some extra complexity somewhere

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding to this, I see some uses of instanceof ParsingException just to do a .getErrorMessage() instead of getMessage(). I would check if those would still work "as expected" with this change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we indeed return a combined ParsingException that represents a collection of ParsingExceptions related to named parameters.

It is actually not a bad idea to have a CompoundParsingException I think, the only thing that I can think of is that the downstream components may need to deal with them differently, if we throw a ParsingException, they can locate it in the query text directly, however if we throw a CompoundParsingException, they may need to iterate through each ParsingException that belongs to the CompoundParsingException to extract the locations, this could be a better way to present all parsing errors that are related to the named parameters, it requires changes in kibana potentially. For now, I'll combine them into one ParsingException, CompoundParsingException is a potential future enhancement if we need a better way to present these errors.

getErrorMessage() does not include the location of the query text, I double checked, there are a couple of places that call it directly, they don't require the location information.

@fang-xing-esql fang-xing-esql added the test-release Trigger CI checks against release build label Jan 30, 2025
@fang-xing-esql
Copy link
Member Author

@elasticmachine update branch

@fang-xing-esql fang-xing-esql added v9.0.0 v8.18.0 v8.18.1 v9.0.1 and removed test-release Trigger CI checks against release build labels Jan 30, 2025
@fang-xing-esql
Copy link
Member Author

@elasticmachine update branch

@fang-xing-esql
Copy link
Member Author

@elasticmachine update branch

@fang-xing-esql
Copy link
Member Author

@elasticmachine update branch

@fang-xing-esql fang-xing-esql merged commit 06fee76 into elastic:main Jan 31, 2025
16 of 17 checks passed
fang-xing-esql added a commit to fang-xing-esql/Elasticsearch that referenced this pull request Jan 31, 2025
…lastic#120852)

* correct line and column numbers of missing named parameters
fang-xing-esql added a commit to fang-xing-esql/Elasticsearch that referenced this pull request Jan 31, 2025
…lastic#120852)

* correct line and column numbers of missing named parameters
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
9.0
8.18
8.x

elasticsearchmachine pushed a commit that referenced this pull request Jan 31, 2025
…120852) (#121449)

* correct line and column numbers of missing named parameters
elasticsearchmachine pushed a commit that referenced this pull request Jan 31, 2025
…120852) (#121450)

* correct line and column numbers of missing named parameters
elasticsearchmachine pushed a commit that referenced this pull request Jan 31, 2025
…120852) (#121451)

* correct line and column numbers of missing named parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL auto-backport Automatically create backport pull requests when merged >bug ES|QL-ui Impacts ES|QL UI Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.18.0 v8.18.1 v8.19.0 v9.0.0 v9.0.1 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ES|QL] Error on request with named params gives wrong line,column information
7 participants