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

Added ParseTreeParser #1090

Merged
merged 5 commits into from
Feb 28, 2022
Merged

Conversation

sbayer55
Copy link
Member

Signed-off-by: Steven Bayer [email protected]

Description

Adds a Parser implementation that uses an ANTLR generated internally

Issues Resolved

n/a

Check List

  • New functionality includes testing.
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed with a real name per the DCO

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.

Signed-off-by: Steven Bayer <[email protected]>
@sbayer55 sbayer55 requested a review from a team as a code owner February 24, 2022 20:35
Signed-off-by: Steven Bayer <[email protected]>
private final Map<String, ParseTree> cache = new HashMap<>();
private final ParserErrorListener errorListener;
private final Lexer lexer;
private final DataPrepperExpressionParser parser;
Copy link
Collaborator

Choose a reason for hiding this comment

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

If there is nothing specifically tied to DataPrepperExpressionParser, shall we keep use generic antlr Parser here to make it consistent with Lexer?

Copy link
Member Author

Choose a reason for hiding this comment

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

DataPrepperExpressionParser is needed to access DataPrepperExpressionParser.expression()

}

@Override
public void printStackTrace(final PrintStream s) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need to override this method?

Copy link
Member Author

Choose a reason for hiding this comment

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

The default implementation is only designed to recursively print exceptions that have a single cause. ParseExceptions may have multiple causes that should be printed.

Copy link
Collaborator

@chenqi0805 chenqi0805 Feb 24, 2022

Choose a reason for hiding this comment

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

But AFAIK this print is just a side effect that will not be invoked when we log the exception, e.g. LOG.error(e). If that is the case, it will not be useful in production.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @chenqi0805, after some testing you're correct. Overriding printStackTrace did not change the message printed. I've refactored the custom exception to work as expected and added some unit tests to verify.

Signed-off-by: Steven Bayer <[email protected]>
Signed-off-by: Steven Bayer <[email protected]>
dapowers87
dapowers87 previously approved these changes Feb 25, 2022
Copy link
Contributor

@dapowers87 dapowers87 left a comment

Choose a reason for hiding this comment

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

My comments were more questions than requests for changes. Looks good


public CompositeException(final List<Throwable> exceptions) {
if (exceptions.isEmpty()) {
throw new IllegalArgumentException("errors is empty");
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this say, "exceptions is empty"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the suggestions, I'll update the message.

/**
* @since 1.3
*
* Clears any error events received. Should be called before a new statement is parsed.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have a comment saying that resetErrors needs to be called on the parsing method?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a message in the javadoc and added unit test to verify method is called.

Copy link
Collaborator

@chenqi0805 chenqi0805 left a comment

Choose a reason for hiding this comment

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

Minor comments.

}

@Override
public synchronized Throwable fillInStackTrace() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any reason we need to override this method? I did not find it called anywhere in the PR but it maybe under the hood.

Copy link
Member Author

Choose a reason for hiding this comment

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

Overriding the default method reduces the stack-trace size on each cause exception to a single line.

Without override:

|-- java.lang.RuntimeException: Error 3
    at org.opensearch.dataprepper.expression.ParseTreeCompositeExceptionTest.testMultipleExceptionsPrinted(ParseTreeCompositeExceptionTest.java:41)
	at org.opensearch.dataprepper.expression.ParseTreeCompositeException.getCause(ParseTreeCompositeException.java:69)
	at org.gradle.internal.serialize.ExceptionPlaceholder.extractCauses(ExceptionPlaceholder.java:302)
	at org.gradle.internal.serialize.ExceptionPlaceholder.<init>(ExceptionPlaceholder.java:96)
	at org.gradle.internal.serialize.TopLevelExceptionPlaceholder.<init>(TopLevelExceptionPlaceholder.java:27)
	at org.gradle.internal.serialize.ExceptionReplacingObjectOutputStream.doReplaceObject(ExceptionReplacingObjectOutputStream.java:67)
	at org.gradle.internal.serialize.ExceptionReplacingObjectOutputStream$1.transform(ExceptionReplacingObjectOutputStream.java:31)
	at org.gradle.internal.serialize.ExceptionReplacingObjectOutputStream.replaceObject(ExceptionReplacingObjectOutputStream.java:62)
	at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1153)
	at java.base/java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:353)
	at org.gradle.internal.serialize.Message.send(Message.java:36)
	at org.gradle.internal.serialize.BaseSerializerFactory$ThrowableSerializer.write(BaseSerializerFactory.java:321)
	at org.gradle.internal.serialize.BaseSerializerFactory$ThrowableSerializer.write(BaseSerializerFactory.java:313)
	at org.gradle.internal.remote.internal.hub.DefaultMethodArgsSerializer$ArraySerializer.write(DefaultMethodArgsSerializer.java:80)
	at org.gradle.internal.remote.internal.hub.DefaultMethodArgsSerializer$ArraySerializer.write(DefaultMethodArgsSerializer.java:61)
	at org.gradle.internal.remote.internal.hub.MethodInvocationSerializer$MethodInvocationWriter.writeArgs(MethodInvocationSerializer.java:78)
	at org.gradle.internal.remote.internal.hub.MethodInvocationSerializer$MethodInvocationWriter.write(MethodInvocationSerializer.java:74)
	at org.gradle.internal.remote.internal.hub.MethodInvocationSerializer$MethodInvocationWriter.write(MethodInvocationSerializer.java:58)
	at org.gradle.internal.serialize.kryo.TypeSafeSerializer$2.write(TypeSafeSerializer.java:47)
	at org.gradle.internal.remote.internal.hub.InterHubMessageSerializer$MessageWriter.write(InterHubMessageSerializer.java:104)
	at org.gradle.internal.remote.internal.hub.InterHubMessageSerializer$MessageWriter.write(InterHubMessageSerializer.java:88)
	at org.gradle.internal.remote.internal.inet.SocketConnection.dispatch(SocketConnection.java:122)
	at org.gradle.internal.remote.internal.hub.MessageHub$ConnectionDispatch.run(MessageHub.java:328)
	... 6 more

With override:

|-- java.lang.RuntimeException: Error 3
    at org.opensearch.dataprepper.expression.ParseTreeCompositeExceptionTest.testMultipleExceptionsPrinted(ParseTreeCompositeExceptionTest.java:41)

* <p>
* Exception thrown by {@link ParseTreeParser} if ANTLR parse emits error events.
*/
public class CompositeException extends RuntimeException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest give it a more specific name, e.g. ParseTreeCompositeException.

@sbayer55 sbayer55 merged commit 70e0f79 into opensearch-project:main Feb 28, 2022
@sbayer55 sbayer55 deleted the parse-tree-parser branch March 17, 2022 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants