-
Notifications
You must be signed in to change notification settings - Fork 144
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
Clean up syntax error reporting #3278
base: main
Are you sure you want to change the base?
Clean up syntax error reporting #3278
Conversation
Signed-off-by: Simeon Widdis <[email protected]>
Signed-off-by: Simeon Widdis <[email protected]>
Signed-off-by: Simeon Widdis <[email protected]>
Signed-off-by: Simeon Widdis <[email protected]>
31649f1
to
a47be17
Compare
{ | ||
"query" : "SELECT * FROM sample:data" | ||
"query" : "SOURCE = test_index | where a > 0)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This SQL error no longer outputs the same error message (new parsing engine?). I couldn't hit the ANTLR exception with a new SQL query, so I updated it to a PPL one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the improvement of this PR is truncate the long error message? If so, is it possible to simplify the changes especially line 60-80?
if (contextStartIndex < 3) { // The ellipses won't save us anything below the first 4 characters | ||
return query.substring(0, offendingToken.getStopIndex() + 1); | ||
} | ||
return "..." + query.substring(contextStartIndex, offendingToken.getStopIndex() + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Optional] Perhaps we should have an unit || IT test for this ...
code path, to make sure indeed lengthy log being truncated as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, though I'm not sure what the value is -- the method has no dependencies and seems unlikely to be changed later on, and if it's being changed they'd almost certainly need to change the associated tests as well so it would only get in the way. If there's a part of the method that's in need of clarification to make it easier for others to parse/modify, let me know
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I share your point, let's it as it is then.
common/src/main/java/org/opensearch/sql/common/antlr/SyntaxAnalysisErrorListener.java
Outdated
Show resolved
Hide resolved
Signed-off-by: Simeon Widdis <[email protected]>
Should be better now, refactored it to only do the mapping for the first few tokens so we can just use |
IntervalSet followSet = e.getExpectedTokens(); | ||
Vocabulary vocab = recognizer.getVocabulary(); | ||
List<String> tokenNames = new ArrayList<>(SUGGESTION_TRUNCATION_THRESHOLD); | ||
for (int tokenType : | ||
followSet | ||
.toList() | ||
.subList(0, Math.min(followSet.size(), SUGGESTION_TRUNCATION_THRESHOLD))) { | ||
tokenNames.add(vocab.getDisplayName(tokenType)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we extract for readability?
import org.antlr.v4.runtime.RecognitionException; | ||
import org.antlr.v4.runtime.Recognizer; | ||
import org.antlr.v4.runtime.Token; | ||
import org.antlr.v4.runtime.*; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can u replace the *
import with explicit classes
@@ -82,25 +82,25 @@ public void queryShouldBeCaseInsensitiveInKeywords() { | |||
@Test | |||
public void queryNotStartingWithSearchCommandShouldFailSyntaxCheck() { | |||
String query = "fields firstname"; | |||
queryShouldThrowSyntaxException(query, "Failed to parse query due to offending symbol"); | |||
queryShouldThrowSyntaxException(query, "is not a valid term at this part of the query"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract this string into a const
Description
While working on fixing PPL AST bugs (#3273), it stood out to me that our error reporting for syntax errors really isn't that good. This PR cleans up the handling of our errors.
Example query that's currently returning syntax errors:
Before:
After:
Even though this particular query should be valid, I think it's much clearer from this what the parser is mad about, and easier to put in a bug report too.
Related Issues
N/A
Check List
--signoff
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.