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

Add COUNT DISTINCT Rewriter #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jnmugerwa
Copy link
Member

Added a rewriter for the following anti-pattern:
-> Uses "COUNT(DISTINCT x) > 1" to check for duplicates

@jnmugerwa jnmugerwa requested a review from kaikalur April 7, 2021 23:22
@jnmugerwa jnmugerwa self-assigned this Apr 7, 2021
private boolean isUsingCountDistinctComparisonToCheckUniqueness(Comparison node)
{
Optional<AstNode> aggregationFunction = Optional.ofNullable(node.GetFirstChildOfKind(JJTAGGREGATIONFUNCTION));
return aggregationFunction.isPresent() && aggregationHasCountDistinct((AggregationFunction) aggregationFunction.get()) && secondArgIsLiteralOne(node);
Copy link

Choose a reason for hiding this comment

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

We need to enhance the AST attributes to remember the operator. It's a TODO(viswanadha) :)

Copy link

Choose a reason for hiding this comment

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

Oops. I forgot I had the correct definition in the doc. So deleting these comments lol

private boolean aggregationHasCountDistinct(AggregationFunction node)
{
Optional<AstNode> setQuantifier = Optional.ofNullable(node.GetFirstChildOfKind(JJTSETQUANTIFIER));
return setQuantifier.isPresent() && node.beginToken.image.equalsIgnoreCase("COUNT") && unparse(setQuantifier.get()).equalsIgnoreCase("DISTINCT");
Copy link

Choose a reason for hiding this comment

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

setQuantifier is a leaf node so you can just get the image and check. We will redo this to make it an in attribute so we can just check the token.

private boolean isUsingCountDistinctComparisonToCheckUniqueness(Comparison node)
{
Optional<AstNode> aggregationFunction = Optional.ofNullable(node.GetFirstChildOfKind(JJTAGGREGATIONFUNCTION));
return aggregationFunction.isPresent() && aggregationHasCountDistinct((AggregationFunction) aggregationFunction.get()) && secondArgIsLiteralOne(node);
Copy link

Choose a reason for hiding this comment

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

We need to enhance the AST attributes to remember the operator. It's a TODO(viswanadha) :)

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.

2 participants