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

 ##########
 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:
   I may have used a not so good function name to indicate the meaning of what 
is being done here.. by validation I still meant to do further parsing and 
syntax analysis to catch bad queries  -- something that was mentioned in the 
design doc as well to catch queries like following at parse time itself. That's 
why I feel this should happen all the time
   
   SELECT DISTINCT(col1), col2 FROM foo
   SELECT col1, SUM(col2) FROM foo
   
   Pql2CompilerTest.java has more tests for such situations...

----------------------------------------------------------------
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