xiangfu0 commented on a change in pull request #7678:
URL: https://github.com/apache/pinot/pull/7678#discussion_r743357232



##########
File path: 
pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java
##########
@@ -161,7 +162,9 @@ private static void validateDistinctQuery(PinotQuery 
pinotQuery)
         }
         List<Expression> orderByList = pinotQuery.getOrderByList();
         if (orderByList != null) {
-          List<Expression> distinctExpressions = function.getOperands();
+          List<Expression> distinctExpressions = 
function.getOperands().stream().map(

Review comment:
       revised the logic

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/TransformFunctionFactory.java
##########
@@ -82,6 +82,7 @@ private TransformFunctionFactory() {
           put(canonicalize(TransformFunctionType.LN.getName().toLowerCase()), 
LnTransformFunction.class);
           
put(canonicalize(TransformFunctionType.SQRT.getName().toLowerCase()), 
SqrtTransformFunction.class);
 
+          put(canonicalize(TransformFunctionType.AS.getName().toLowerCase()), 
AsTransformFunction.class);

Review comment:
       no, this is used universally can not be changed

##########
File path: 
pinot-common/src/test/java/org/apache/pinot/sql/parsers/rewriter/NonAggregationGroupByToDistinctQueryRewriterTest.java
##########
@@ -0,0 +1,86 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.sql.parsers.rewriter;
+
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.sql.parsers.CalciteSqlParser;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class NonAggregationGroupByToDistinctQueryRewriterTest {
+
+  private static final QueryRewriter QUERY_REWRITER = new 
NonAggregationGroupByToDistinctQueryRewriter();
+
+  @Test
+  public void testQuery1() {
+    final PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQuery("SELECT 
A FROM myTable GROUP BY A");
+    QUERY_REWRITER.rewrite(pinotQuery);
+    
Assert.assertEquals(pinotQuery.getSelectList().get(0).getFunctionCall().getOperator(),
 "DISTINCT");
+    Assert.assertEquals(
+        
pinotQuery.getSelectList().get(0).getFunctionCall().getOperands().get(0).getIdentifier().getName(),
 "A");
+  }
+
+  @Test
+  // SELECT col1+col2*5 FROM foo GROUP BY col1, col2 => SELECT distinct 
col1+col2*5 FROM foo
+  public void testQuery2() {
+    final PinotQuery pinotQuery =
+        CalciteSqlParser.compileToPinotQuery("SELECT col1+col2*5 FROM foo 
GROUP BY col1, col2");
+    QUERY_REWRITER.rewrite(pinotQuery);
+    
Assert.assertEquals(pinotQuery.getSelectList().get(0).getFunctionCall().getOperator(),
 "DISTINCT");
+    Assert.assertEquals(
+        
pinotQuery.getSelectList().get(0).getFunctionCall().getOperands().get(0).getFunctionCall().getOperands().get(0)
+            .getIdentifier().getName(), "col1");
+    Assert.assertEquals(
+        
pinotQuery.getSelectList().get(0).getFunctionCall().getOperands().get(0).getFunctionCall().getOperands().get(1)
+            .getFunctionCall().getOperands().get(0).getIdentifier().getName(), 
"col2");
+    Assert.assertEquals(
+        
pinotQuery.getSelectList().get(0).getFunctionCall().getOperands().get(0).getFunctionCall().getOperands().get(1)
+            
.getFunctionCall().getOperands().get(1).getLiteral().getLongValue(), 5L);
+  }
+
+  @Test
+  // SELECT col1, col2 FROM foo GROUP BY col1, col2 => SELECT distinct col1, 
col2 FROM foo
+  public void testQuery3() {
+    final PinotQuery pinotQuery =
+        CalciteSqlParser.compileToPinotQuery("SELECT col1, col2 FROM foo GROUP 
BY col1, col2 ");
+    QUERY_REWRITER.rewrite(pinotQuery);
+    
Assert.assertEquals(pinotQuery.getSelectList().get(0).getFunctionCall().getOperator(),
 "DISTINCT");
+    Assert.assertEquals(
+        
pinotQuery.getSelectList().get(0).getFunctionCall().getOperands().get(0).getIdentifier().getName(),
 "col1");
+    Assert.assertEquals(
+        
pinotQuery.getSelectList().get(0).getFunctionCall().getOperands().get(1).getIdentifier().getName(),
 "col2");
+  }
+
+  @Test
+  // SELECT col1 as col2 FROM foo GROUP BY col1 => SELECT distinct col1 as 
col2 FROM foo

Review comment:
       added.

##########
File path: 
pinot-core/src/test/java/org/apache/pinot/queries/DistinctQueriesTest.java
##########
@@ -580,18 +578,16 @@ private void testDistinctInnerSegmentHelper(String[] 
queries, boolean isPql) {
    */
   @Test
   public void testDistinctInnerSegment() {
-    testDistinctInnerSegmentHelper(
-        new String[]{
-            "SELECT DISTINCT(intColumn, longColumn, floatColumn, doubleColumn, 
stringColumn, bytesColumn) FROM "
-                + "testTable LIMIT 10000",
-            "SELECT DISTINCT(stringColumn, bytesColumn, floatColumn) FROM 
testTable WHERE intColumn >= 60 LIMIT 10000",
-            "SELECT DISTINCT(intColumn, rawBytesColumn) FROM testTable ORDER 
BY rawBytesColumn LIMIT 5",
-            "SELECT DISTINCT(ADD(intColumn, floatColumn), stringColumn) FROM 
testTable WHERE longColumn < 60 ORDER BY"
-                + " stringColumn DESC, ADD(intColumn, floatColumn) ASC LIMIT 
10",
-            "SELECT DISTINCT(floatColumn, longColumn) FROM testTable WHERE 
stringColumn = 'a' ORDER BY longColumn "
-                + "LIMIT 10"
-        },
-        true);
+    testDistinctInnerSegmentHelper(new String[]{
+        "SELECT DISTINCT(intColumn, longColumn, floatColumn, doubleColumn, 
stringColumn, bytesColumn) FROM "
+            + "testTable LIMIT 10000",
+        "SELECT DISTINCT(stringColumn, bytesColumn, floatColumn) FROM 
testTable WHERE " + "intColumn >= 60 LIMIT 10000",
+        "SELECT DISTINCT(intColumn, rawBytesColumn) FROM testTable ORDER BY " 
+ "rawBytesColumn LIMIT 5",

Review comment:
       done.

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/operator/transform/function/TransformFunctionFactory.java
##########
@@ -198,6 +199,9 @@ public static TransformFunction get(ExpressionContext 
expression, Map<String, Da
         FunctionContext function = expression.getFunction();
         String functionName = canonicalize(function.getFunctionName());
         List<ExpressionContext> arguments = function.getArguments();
+        if (functionName.equalsIgnoreCase("AS")) {
+          arguments = arguments.subList(0, 1);
+        }

Review comment:
       cause the other side is an identifier but not defined in schema, it will 
cause underly data block initialization error with NPE.




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

To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to