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

Improve error message in painless when parameter must be a constant but isn't #82638

Closed
wants to merge 2 commits into from

Conversation

Kxrr
Copy link
Contributor

@Kxrr Kxrr commented Jan 15, 2022

The pull request is related to #68324 .

I made the following changes:

  • Add symbol (ESymbol) name to error message
  • Use ordinal number in error message

@elasticsearchmachine elasticsearchmachine added v8.1.0 external-contributor Pull request authored by a developer outside the Elasticsearch team labels Jan 15, 2022
@stu-elastic stu-elastic added the :Core/Infra/Scripting Scripting abstractions, Painless, and Mustache label Jan 18, 2022
@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Jan 18, 2022
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@@ -34,5 +34,31 @@ public static char StringTochar(final String value) {
return value.charAt(0);
}

public static String toOrdinal(int number) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just the number is fine, no need to spend lines on adding the ordinal suffix.

"all arguments to [" + javaMethod.getName() + "] must be constant but the [" + (i + 1) + "] argument isn't"
)
);
// offering the symbol name in error message (CastNode was evolved from ESymbol)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this handle cases when there are multiple arguments that should be constants?

Copy link
Contributor

@stu-elastic stu-elastic left a comment

Choose a reason for hiding this comment

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

Good start. This work can be simplified by removal the ordinal code and enhanced by handling multiple missing constant decorations.

@Kxrr
Copy link
Contributor Author

Kxrr commented Jan 19, 2022

@stu-elastic Thanks for your feedback!

I've addressed to your comments in 84e6198 .

@Kxrr Kxrr requested a review from stu-elastic January 19, 2022 16:16
"all arguments to [" + javaMethod.getName() + "] must be constant but the [" + (i + 1) + "] argument isn't"
)
);
// offering the symbol name in error message (CastNode was evolved from ESymbol)
Copy link
Contributor

@stu-elastic stu-elastic Jan 20, 2022

Choose a reason for hiding this comment

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

CastNode was evolved from ESymbol

What does this mean?

Locale.ROOT,
"All arguments of the [%s] method must be constants, but the following arguments are not: %s",
javaMethod.getName(),
argumentsShouldBeConstants
Copy link
Contributor

Choose a reason for hiding this comment

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

The stream and Tuple use is unnecessary, why not build the error directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My willingness is to print out all the errors at once. I will rollback this.

)
);
// offering the symbol name in error message (CastNode was evolved from ESymbol)
String argumentName = argNode instanceof CastNode
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this handle the case of 2 + x as an argument?

Copy link
Contributor Author

@Kxrr Kxrr Jan 27, 2022

Choose a reason for hiding this comment

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

It can't handle the 2 + x case yet, so if we need to handle this more complex case, I'm going to write a special method to build the original expression from Node.

Copy link
Contributor Author

@Kxrr Kxrr Jan 27, 2022

Choose a reason for hiding this comment

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

I see that the argNode has a location field with some information about the original expression, but it's missing the length. It would be much simpler to use this location field (no need to traverse the argNode's data structure)

);
// offering the symbol name in error message (CastNode was evolved from ESymbol)
String argumentName = argNode instanceof CastNode
? ((CastNode) argNode).getChildNode().getDecoration(IRDName.class).getValue()
Copy link
Contributor

@stu-elastic stu-elastic Jan 20, 2022

Choose a reason for hiding this comment

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

If we go with this code (which is not clear at the moment), getDecorationValue should be used to avoid the potential null dereference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I will use getDecorationValue.

Copy link
Contributor

@stu-elastic stu-elastic left a comment

Choose a reason for hiding this comment

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

In addition to the comment I've added below, please add better tests covering cases for expressions and include a mix of constants and non-constants

@kingherc kingherc added v8.7.0 and removed v8.6.0 labels Nov 16, 2022
@rjernst rjernst added v8.8.0 and removed v8.7.0 labels Feb 8, 2023
@stu-elastic
Copy link
Contributor

Hi, I'm going to close this PR as there's been no update in a year. Feel free to reopen it when the conflicts are resolved and the comments have been addressed.

@stu-elastic stu-elastic closed this Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Scripting Scripting abstractions, Painless, and Mustache >enhancement external-contributor Pull request authored by a developer outside the Elasticsearch team Team:Core/Infra Meta label for core/infra team v8.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.