This is an automated email from the ASF dual-hosted git repository.

xiangfu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ec77b5  Allow override distinctCount to 
segmentPartitionedDistinctCount (#7664)
0ec77b5 is described below

commit 0ec77b561b3d2d73b6005b04cf29524a7af83999
Author: Xiang Fu <[email protected]>
AuthorDate: Mon Nov 1 14:58:15 2021 -0700

    Allow override distinctCount to segmentPartitionedDistinctCount (#7664)
    
    * Allow override distinctCount to segmentPartitionedDistinctCount
    
    * check both rt and offline table field configs
    
    * Address comments
---
 .../requesthandler/BaseBrokerRequestHandler.java   | 117 +++++++++++++++++++++
 .../requesthandler/DistinctCountRewriteTest.java   |  62 +++++++++++
 .../apache/pinot/spi/config/table/FieldConfig.java |   1 +
 3 files changed, 180 insertions(+)

diff --git 
a/pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java
 
b/pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java
index 579cbc2..547c895 100644
--- 
a/pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java
+++ 
b/pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java
@@ -83,6 +83,7 @@ import org.apache.pinot.core.transport.ServerInstance;
 import org.apache.pinot.core.util.QueryOptionsUtils;
 import org.apache.pinot.pql.parsers.pql2.ast.FilterKind;
 import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.spi.config.table.FieldConfig;
 import org.apache.pinot.spi.config.table.TableConfig;
 import org.apache.pinot.spi.config.table.TableType;
 import org.apache.pinot.spi.data.Schema;
@@ -270,6 +271,7 @@ public abstract class BaseBrokerRequestHandler implements 
BrokerRequestHandler {
     if (_enableQueryLimitOverride) {
       handleQueryLimitOverride(pinotQuery, _queryResponseLimit);
     }
+    handleSegmentPartitionedDistinctCountOverride(pinotQuery, 
getSegmentPartitionedColumns(_tableCache, tableName));
     if (_enableDistinctCountBitmapOverride) {
       handleDistinctCountBitmapOverride(pinotQuery);
     }
@@ -624,6 +626,7 @@ public abstract class BaseBrokerRequestHandler implements 
BrokerRequestHandler {
     if (_enableQueryLimitOverride) {
       handleQueryLimitOverride(brokerRequest, _queryResponseLimit);
     }
+    handleSegmentPartitionedDistinctCountOverride(brokerRequest, 
getSegmentPartitionedColumns(_tableCache, tableName));
     if (_enableDistinctCountBitmapOverride) {
       handleDistinctCountBitmapOverride(brokerRequest);
     }
@@ -1027,6 +1030,47 @@ public abstract class BaseBrokerRequestHandler 
implements BrokerRequestHandler {
   }
 
   /**
+   * Retrieve segment partitioned columns for a table.
+   * For a hybrid table, a segment partitioned column has to be the 
intersection of both offline and realtime tables.
+   *
+   * @param tableCache
+   * @param tableName
+   * @return segment partitioned columns belong to both offline and realtime 
tables.
+   */
+  private static Set<String> getSegmentPartitionedColumns(TableCache 
tableCache, String tableName) {
+    final TableConfig offlineTableConfig =
+        
tableCache.getTableConfig(TableNameBuilder.OFFLINE.tableNameWithType(tableName));
+    final TableConfig realtimeTableConfig =
+        
tableCache.getTableConfig(TableNameBuilder.REALTIME.tableNameWithType(tableName));
+    if (offlineTableConfig == null) {
+      return getSegmentPartitionedColumns(realtimeTableConfig);
+    }
+    if (realtimeTableConfig == null) {
+      return getSegmentPartitionedColumns(offlineTableConfig);
+    }
+    Set<String> segmentPartitionedColumns = 
getSegmentPartitionedColumns(offlineTableConfig);
+    
segmentPartitionedColumns.retainAll(getSegmentPartitionedColumns(realtimeTableConfig));
+    return segmentPartitionedColumns;
+  }
+
+  private static Set<String> getSegmentPartitionedColumns(@Nullable 
TableConfig tableConfig) {
+    Set<String> segmentPartitionedColumns = new HashSet<>();
+    if (tableConfig == null) {
+      return segmentPartitionedColumns;
+    }
+    List<FieldConfig> fieldConfigs = tableConfig.getFieldConfigList();
+    if (fieldConfigs != null) {
+      for (FieldConfig fieldConfig : fieldConfigs) {
+        if (fieldConfig.getProperties() != null && Boolean.parseBoolean(
+            
fieldConfig.getProperties().get(FieldConfig.IS_SEGMENT_PARTITIONED_COLUMN_KEY)))
 {
+          segmentPartitionedColumns.add(fieldConfig.getName());
+        }
+      }
+    }
+    return segmentPartitionedColumns;
+  }
+
+  /**
    * Sets the table name in the given broker request (SQL and PQL)
    * NOTE: Set table name in broker request even for SQL query because it is 
used for access control, query routing etc.
    */
@@ -1140,6 +1184,30 @@ public abstract class BaseBrokerRequestHandler 
implements BrokerRequestHandler {
   }
 
   /**
+   * Rewrites 'DistinctCount' to 'SegmentPartitionDistinctCount' for the given 
PQL broker request.
+   */
+  @Deprecated
+  @VisibleForTesting
+  static void handleSegmentPartitionedDistinctCountOverride(BrokerRequest 
brokerRequest,
+      Set<String> segmentPartitionedColumns) {
+    if (segmentPartitionedColumns.isEmpty()) {
+      return;
+    }
+    List<AggregationInfo> aggregationsInfo = 
brokerRequest.getAggregationsInfo();
+    if (aggregationsInfo != null) {
+      for (AggregationInfo aggregationInfo : aggregationsInfo) {
+        if (StringUtils.remove(aggregationInfo.getAggregationType(), '_')
+            .equalsIgnoreCase(AggregationFunctionType.DISTINCTCOUNT.name())) {
+          List<String> expressions = aggregationInfo.getExpressions();
+          if (expressions.size() == 1 && 
segmentPartitionedColumns.contains(expressions.get(0))) {
+            
aggregationInfo.setAggregationType(AggregationFunctionType.SEGMENTPARTITIONEDDISTINCTCOUNT.name());
+          }
+        }
+      }
+    }
+  }
+
+  /**
    * Rewrites 'DistinctCount' to 'DistinctCountBitmap' for the given PQL 
broker request.
    */
   @Deprecated
@@ -1156,6 +1224,55 @@ public abstract class BaseBrokerRequestHandler 
implements BrokerRequestHandler {
   }
 
   /**
+   * Rewrites 'DistinctCount' to 'SegmentPartitionDistinctCount' for the given 
SQL query.
+   */
+  @VisibleForTesting
+  static void handleSegmentPartitionedDistinctCountOverride(PinotQuery 
pinotQuery,
+      Set<String> segmentPartitionedColumns) {
+    if (segmentPartitionedColumns.isEmpty()) {
+      return;
+    }
+    for (Expression expression : pinotQuery.getSelectList()) {
+      handleSegmentPartitionedDistinctCountOverride(expression, 
segmentPartitionedColumns);
+    }
+    List<Expression> orderByExpressions = pinotQuery.getOrderByList();
+    if (orderByExpressions != null) {
+      for (Expression expression : orderByExpressions) {
+        // NOTE: Order-by is always a Function with the ordering of the 
Expression
+        
handleSegmentPartitionedDistinctCountOverride(expression.getFunctionCall().getOperands().get(0),
+            segmentPartitionedColumns);
+      }
+    }
+    Expression havingExpression = pinotQuery.getHavingExpression();
+    if (havingExpression != null) {
+      handleSegmentPartitionedDistinctCountOverride(havingExpression, 
segmentPartitionedColumns);
+    }
+  }
+
+  /**
+   * Rewrites 'DistinctCount' to 'SegmentPartitionDistinctCount' for the given 
SQL expression.
+   */
+  private static void handleSegmentPartitionedDistinctCountOverride(Expression 
expression,
+      Set<String> segmentPartitionedColumns) {
+    Function function = expression.getFunctionCall();
+    if (function == null) {
+      return;
+    }
+    if (StringUtils.remove(function.getOperator(), '_')
+        .equalsIgnoreCase(AggregationFunctionType.DISTINCTCOUNT.name())) {
+      List<Expression> operands = function.getOperands();
+      if (operands.size() == 1 && operands.get(0).isSetIdentifier() && 
segmentPartitionedColumns.contains(
+          operands.get(0).getIdentifier().getName())) {
+        
function.setOperator(AggregationFunctionType.SEGMENTPARTITIONEDDISTINCTCOUNT.name());
+      }
+    } else {
+      for (Expression operand : function.getOperands()) {
+        handleSegmentPartitionedDistinctCountOverride(operand, 
segmentPartitionedColumns);
+      }
+    }
+  }
+
+  /**
    * Rewrites 'DistinctCount' to 'DistinctCountBitmap' for the given SQL query.
    */
   private static void handleDistinctCountBitmapOverride(PinotQuery pinotQuery) 
{
diff --git 
a/pinot-broker/src/test/java/org/apache/pinot/broker/requesthandler/DistinctCountRewriteTest.java
 
b/pinot-broker/src/test/java/org/apache/pinot/broker/requesthandler/DistinctCountRewriteTest.java
new file mode 100644
index 0000000..0a50a8f
--- /dev/null
+++ 
b/pinot-broker/src/test/java/org/apache/pinot/broker/requesthandler/DistinctCountRewriteTest.java
@@ -0,0 +1,62 @@
+/**
+ * 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.broker.requesthandler;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.common.request.PinotQuery;
+import org.apache.pinot.pql.parsers.Pql2Compiler;
+import org.apache.pinot.segment.spi.AggregationFunctionType;
+import org.apache.pinot.sql.parsers.CalciteSqlParser;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+
+public class DistinctCountRewriteTest {
+  private static final Pql2Compiler PQL_COMPILER = new Pql2Compiler();
+
+  @Test
+  @Deprecated
+  public void testPql() {
+    String pql = "SELECT distinctCount(col1) FROM myTable";
+    BrokerRequest brokerRequest = PQL_COMPILER.compileToBrokerRequest(pql);
+    
BaseBrokerRequestHandler.handleSegmentPartitionedDistinctCountOverride(brokerRequest,
+        ImmutableSet.of("col2", "col3"));
+    
Assert.assertEquals(brokerRequest.getAggregationsInfo().get(0).getAggregationType().toUpperCase(),
+        AggregationFunctionType.DISTINCTCOUNT.name());
+    
BaseBrokerRequestHandler.handleSegmentPartitionedDistinctCountOverride(brokerRequest,
+        ImmutableSet.of("col2", "col3", "col1"));
+    
Assert.assertEquals(brokerRequest.getAggregationsInfo().get(0).getAggregationType().toUpperCase(),
+        AggregationFunctionType.SEGMENTPARTITIONEDDISTINCTCOUNT.name());
+  }
+
+  @Test
+  public void testSql() {
+    String sql = "SELECT distinctCount(col1) FROM myTable";
+    PinotQuery pinotQuery = CalciteSqlParser.compileToPinotQuery(sql);
+    
BaseBrokerRequestHandler.handleSegmentPartitionedDistinctCountOverride(pinotQuery,
 ImmutableSet.of("col2", "col3"));
+    
Assert.assertEquals(pinotQuery.getSelectList().get(0).getFunctionCall().getOperator(),
+        AggregationFunctionType.DISTINCTCOUNT.name());
+    
BaseBrokerRequestHandler.handleSegmentPartitionedDistinctCountOverride(pinotQuery,
+        ImmutableSet.of("col1", "col2", "col3"));
+    
Assert.assertEquals(pinotQuery.getSelectList().get(0).getFunctionCall().getOperator(),
+        AggregationFunctionType.SEGMENTPARTITIONEDDISTINCTCOUNT.name());
+  }
+}
diff --git 
a/pinot-spi/src/main/java/org/apache/pinot/spi/config/table/FieldConfig.java 
b/pinot-spi/src/main/java/org/apache/pinot/spi/config/table/FieldConfig.java
index a6f90da..59d7635 100644
--- a/pinot-spi/src/main/java/org/apache/pinot/spi/config/table/FieldConfig.java
+++ b/pinot-spi/src/main/java/org/apache/pinot/spi/config/table/FieldConfig.java
@@ -34,6 +34,7 @@ public class FieldConfig extends BaseJsonConfig {
   public static final String VAR_LENGTH_DICTIONARY_COLUMN_KEY = 
"useVarLengthDictionary";
   public static final String DERIVE_NUM_DOCS_PER_CHUNK_RAW_INDEX_KEY = 
"deriveNumDocsPerChunkForRawIndex";
   public static final String RAW_INDEX_WRITER_VERSION = 
"rawIndexWriterVersion";
+  public static final String IS_SEGMENT_PARTITIONED_COLUMN_KEY = 
"isSegmentPartitioned";
 
   public static final String TEXT_INDEX_REALTIME_READER_REFRESH_KEY = 
"textIndexRealtimeReaderRefreshThreshold";
   // Lucene creates a query result cache if this option is enabled

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

Reply via email to