mayankshriv commented on a change in pull request #4535: Implement DISTINCT 
clause
URL: https://github.com/apache/incubator-pinot/pull/4535#discussion_r315313490
 
 

 ##########
 File path: 
pinot-common/src/main/java/org/apache/pinot/pql/parsers/pql2/ast/OutputColumnListAstNode.java
 ##########
 @@ -41,8 +42,54 @@ public void addChild(AstNode childNode) {
     }
   }
 
+  private void validate() {
+    boolean identifierPresent = false;
+    boolean distinctPresent = false;
+    boolean aggregateFunctionPresent = false;
+    if (hasChildren()) {
+      for (AstNode child : getChildren()) {
+        if (child instanceof OutputColumnAstNode) {
+          if (child.hasChildren()) {
+            for (AstNode selectChild : child.getChildren()) {
+              if (selectChild instanceof IdentifierAstNode) {
+                if (distinctPresent) {
+                  throw new Pql2CompilationException("Syntax error: SELECT 
list columns should be part of DISTINCT clause");
+                } else if (aggregateFunctionPresent) {
+                  throw new Pql2CompilationException("Syntax error: 
Aggregation functions cannot be used with select list columns");
+                } else {
+                  identifierPresent = true;
+                }
+              } else if (selectChild instanceof FunctionCallAstNode) {
+                if (((FunctionCallAstNode) 
selectChild).getName().equalsIgnoreCase("distinct")) {
+                  if (identifierPresent) {
+                    throw new Pql2CompilationException("Syntax error: SELECT 
list columns should be part of DISTINCT clause");
+                  } else if (aggregateFunctionPresent) {
+                    throw new Pql2CompilationException("Syntax error: 
Aggregation functions cannot be used with DISTINCT clause");
+                  } else if (distinctPresent) {
+                    throw new Pql2CompilationException("Syntax error: DISTINCT 
clause can be used only once in a query");
+                  } else {
+                    distinctPresent = true;
+                  }
+                } else {
+                  if (distinctPresent) {
+                    throw new Pql2CompilationException("Syntax error: 
Aggregation functions cannot be used with DISTINCT clause");
+                  } else if (identifierPresent) {
+                    throw new Pql2CompilationException("Syntax error: 
Aggregation functions cannot be used with select list columns");
+                  } else {
+                    aggregateFunctionPresent = true;
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+
   @Override
   public void updateBrokerRequest(BrokerRequest brokerRequest) {
+    validate();
 
 Review comment:
   If this is just a validation, we should only do that during testing, and not 
in production, right?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to