Jackie-Jiang commented on code in PR #8343:
URL: https://github.com/apache/pinot/pull/8343#discussion_r842144361


##########
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/FieldConfig.java:
##########
@@ -50,16 +50,17 @@
   private final List<IndexType> _indexTypes;
   private final CompressionCodec _compressionCodec;
   private final Map<String, String> _properties;
+  private final TimestampConfig _timestampConfig;

Review Comment:
   (minor) Put this in front of other custom `_properties`, same for the 
constructor



##########
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/TimestampIndexGranularity.java:
##########
@@ -0,0 +1,110 @@
+/**
+ * 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.spi.config.table;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.data.DateTimeFieldSpec;
+import org.apache.pinot.spi.data.DimensionFieldSpec;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+
+
+/**
+ * TimestampIndexGranularity is the enum of different time granularities from 
MILLIS to YEAR.
+ */
+public enum TimestampIndexGranularity {
+  MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR;
+
+  private static Set<String> _validValues =
+      Arrays.stream(TimestampIndexGranularity.values()).map(v -> 
v.toString()).collect(Collectors.toSet());
+
+  public static String getColumnNameWithGranularity(String column, 
TimestampIndexGranularity granularity) {
+    return "$" + column + "$" + granularity;
+  }
+
+  public static String extractColumnNameFromColumnWithGranularity(String 
columnWithGranularity) {
+    return StringUtils.split(columnWithGranularity, "$")[0];
+  }
+
+  public static TimestampIndexGranularity 
extractGranularityFromColumnWithGranularity(String columnWithGranularity) {
+    return 
TimestampIndexGranularity.valueOf(StringUtils.split(columnWithGranularity, 
"$")[1]);
+  }
+
+  public static boolean isValidTimeColumnWithGranularityName(String 
columnName) {
+    if (columnName.charAt(0) != '$') {
+      return false;
+    }
+    int secondDollarPos = columnName.indexOf('$', 1);
+    if (secondDollarPos < 0) {
+      return false;
+    }
+    return isValidTimeGranularity(columnName.substring(secondDollarPos + 1));

Review Comment:
   `VALID_VALUES.contains()` to avoid one extra `toUpperCase()`



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -563,6 +569,57 @@ private BrokerResponseNative handleSQLRequest(long 
requestId, String query, Json
     return brokerResponse;
   }
 
+  private void handleTimestampIndexOverride(PinotQuery pinotQuery, TableConfig 
tableConfig) {

Review Comment:
   (minor)
   ```suggestion
     private void handleTimestampIndexOverride(PinotQuery pinotQuery, @Nullable 
TableConfig tableConfig) {
   ```



##########
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/TimestampIndexGranularity.java:
##########
@@ -0,0 +1,110 @@
+/**
+ * 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.spi.config.table;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.data.DateTimeFieldSpec;
+import org.apache.pinot.spi.data.DimensionFieldSpec;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+
+
+/**
+ * TimestampIndexGranularity is the enum of different time granularities from 
MILLIS to YEAR.
+ */
+public enum TimestampIndexGranularity {
+  MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR;
+
+  private static Set<String> _validValues =
+      Arrays.stream(TimestampIndexGranularity.values()).map(v -> 
v.toString()).collect(Collectors.toSet());

Review Comment:
   ```suggestion
     private static final Set<String> VALID_VALUES =
         Arrays.stream(TimestampIndexGranularity.values()).map(v -> 
v.name()).collect(Collectors.toSet());
   ```



##########
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/TimestampIndexGranularity.java:
##########
@@ -0,0 +1,110 @@
+/**
+ * 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.spi.config.table;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.data.DateTimeFieldSpec;
+import org.apache.pinot.spi.data.DimensionFieldSpec;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+
+
+/**
+ * TimestampIndexGranularity is the enum of different time granularities from 
MILLIS to YEAR.
+ */
+public enum TimestampIndexGranularity {
+  MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR;
+
+  private static Set<String> _validValues =
+      Arrays.stream(TimestampIndexGranularity.values()).map(v -> 
v.toString()).collect(Collectors.toSet());
+
+  public static String getColumnNameWithGranularity(String column, 
TimestampIndexGranularity granularity) {
+    return "$" + column + "$" + granularity;
+  }
+
+  public static String extractColumnNameFromColumnWithGranularity(String 
columnWithGranularity) {

Review Comment:
   Consider combining this and `extractGranularityFromColumnWithGranularity` to 
`String[] getColumnNameAndGranularity(String columnWithGranularity)` to safe 
one extra split



##########
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/TimestampIndexGranularity.java:
##########
@@ -0,0 +1,110 @@
+/**
+ * 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.spi.config.table;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.data.DateTimeFieldSpec;
+import org.apache.pinot.spi.data.DimensionFieldSpec;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+
+
+/**
+ * TimestampIndexGranularity is the enum of different time granularities from 
MILLIS to YEAR.
+ */
+public enum TimestampIndexGranularity {
+  MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR;
+
+  private static Set<String> _validValues =
+      Arrays.stream(TimestampIndexGranularity.values()).map(v -> 
v.toString()).collect(Collectors.toSet());
+
+  public static String getColumnNameWithGranularity(String column, 
TimestampIndexGranularity granularity) {
+    return "$" + column + "$" + granularity;
+  }
+
+  public static String extractColumnNameFromColumnWithGranularity(String 
columnWithGranularity) {
+    return StringUtils.split(columnWithGranularity, "$")[0];
+  }
+
+  public static TimestampIndexGranularity 
extractGranularityFromColumnWithGranularity(String columnWithGranularity) {
+    return 
TimestampIndexGranularity.valueOf(StringUtils.split(columnWithGranularity, 
"$")[1]);
+  }
+
+  public static boolean isValidTimeColumnWithGranularityName(String 
columnName) {
+    if (columnName.charAt(0) != '$') {
+      return false;
+    }
+    int secondDollarPos = columnName.indexOf('$', 1);
+    if (secondDollarPos < 0) {
+      return false;
+    }
+    return isValidTimeGranularity(columnName.substring(secondDollarPos + 1));
+  }
+
+  public static boolean isValidTimeGranularity(String granularity) {
+    return _validValues.contains(granularity.toUpperCase());
+  }
+
+  public static Set<String> 
extractTimestampIndexGranularityColumnNames(TableConfig tableConfig) {
+    // Extract Timestamp granularity columns
+    Set<String> timeColumnWithGranularity = new HashSet<>();
+    if (tableConfig == null || tableConfig.getFieldConfigList() == null) {
+      return timeColumnWithGranularity;
+    }
+    for (FieldConfig fieldConfig : tableConfig.getFieldConfigList()) {
+      TimestampConfig timestampConfig = fieldConfig.getTimestampConfig();
+      if (timestampConfig == null || timestampConfig.getGranularities() == 
null) {
+        continue;
+      }
+      String timeColumn = fieldConfig.getName();
+      for (TimestampIndexGranularity granularity : 
timestampConfig.getGranularities()) {
+        timeColumnWithGranularity.add(getColumnNameWithGranularity(timeColumn, 
granularity));
+      }
+    }
+    return timeColumnWithGranularity;
+  }
+
+  /**
+   * Generate the time column with granularity FieldSpec from existing time 
column FieldSpec and granularity.
+   * The new FieldSpec keeps same FieldType, only update the field name.
+   *
+   * @param fieldSpec
+   * @param granularity
+   * @return time column with granularity FieldSpec, null if FieldSpec is not 
Dimension/Metric/DateTime type.
+   */
+  public static FieldSpec 
getFieldSpecForTimestampColumnWithGranularity(FieldSpec fieldSpec,
+      TimestampIndexGranularity granularity) {
+    String columnName = fieldSpec.getName();
+    if (fieldSpec instanceof DateTimeFieldSpec) {
+      DateTimeFieldSpec dateTimeFieldSpec = (DateTimeFieldSpec) fieldSpec;
+      return new 
DateTimeFieldSpec(TimestampIndexGranularity.getColumnNameWithGranularity(columnName,
 granularity),
+          FieldSpec.DataType.TIMESTAMP, dateTimeFieldSpec.getFormat(), 
dateTimeFieldSpec.getGranularity());
+    }
+    if (fieldSpec instanceof DimensionFieldSpec) {

Review Comment:
   (MAJOR) We should only allow `DateTimeFieldSpec` with `EPOCH:MILLISECONDS` 
or fields with `TIMESTAMP` data type, or we don't know if the column is 
`millisSinceEpoch`



##########
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/TimestampIndexGranularity.java:
##########
@@ -0,0 +1,110 @@
+/**
+ * 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.spi.config.table;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.data.DateTimeFieldSpec;
+import org.apache.pinot.spi.data.DimensionFieldSpec;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+
+
+/**
+ * TimestampIndexGranularity is the enum of different time granularities from 
MILLIS to YEAR.
+ */
+public enum TimestampIndexGranularity {
+  MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR;
+
+  private static Set<String> _validValues =
+      Arrays.stream(TimestampIndexGranularity.values()).map(v -> 
v.toString()).collect(Collectors.toSet());
+
+  public static String getColumnNameWithGranularity(String column, 
TimestampIndexGranularity granularity) {
+    return "$" + column + "$" + granularity;
+  }
+
+  public static String extractColumnNameFromColumnWithGranularity(String 
columnWithGranularity) {
+    return StringUtils.split(columnWithGranularity, "$")[0];

Review Comment:
   Faster than splitting with string
   ```suggestion
       return StringUtils.split(columnWithGranularity, '$')[0];
   ```



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -563,6 +569,57 @@ private BrokerResponseNative handleSQLRequest(long 
requestId, String query, Json
     return brokerResponse;
   }
 
+  private void handleTimestampIndexOverride(PinotQuery pinotQuery, TableConfig 
tableConfig) {
+    if (tableConfig == null || tableConfig.getFieldConfigList() == null) {
+      return;
+    }
+    Set<String> timestampIndexColumns =
+        
TimestampIndexGranularity.extractTimestampIndexGranularityColumnNames(tableConfig);
+    for (Expression expression : pinotQuery.getSelectList()) {
+      setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery);
+    }
+    setTimestampIndexExpressionOverrideHints(pinotQuery.getFilterExpression(), 
timestampIndexColumns, pinotQuery);
+    setTimestampIndexExpressionOverrideHints(pinotQuery.getHavingExpression(), 
timestampIndexColumns, pinotQuery);
+    List<Expression> groupByList = pinotQuery.getGroupByList();
+    if (CollectionUtils.isNotEmpty(groupByList)) {
+      groupByList.forEach(
+          expression -> setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery));
+    }
+    List<Expression> orderByList = pinotQuery.getOrderByList();
+    if (CollectionUtils.isNotEmpty(orderByList)) {
+      orderByList.forEach(
+          expression -> setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery));
+    }
+  }
+
+  private void setTimestampIndexExpressionOverrideHints(Expression expression, 
Set<String> timestampIndexColumns,

Review Comment:
   (minor)
   ```suggestion
     private void setTimestampIndexExpressionOverrideHints(@Nullable Expression 
expression, Set<String> timestampIndexColumns,
   ```



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -380,10 +382,12 @@ private BrokerResponseNative handleSQLRequest(long 
requestId, String query, Json
       // Hybrid
       offlineBrokerRequest = getOfflineBrokerRequest(serverBrokerRequest);
       PinotQuery offlinePinotQuery = offlineBrokerRequest.getPinotQuery();
+      handleTimestampIndexOverride(offlinePinotQuery, offlineTableConfig);

Review Comment:
   I think timestamp override should happen after expression override in case 
user wants to directly override expression to the identifier instead of using 
hints



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -563,6 +569,57 @@ private BrokerResponseNative handleSQLRequest(long 
requestId, String query, Json
     return brokerResponse;
   }
 
+  private void handleTimestampIndexOverride(PinotQuery pinotQuery, TableConfig 
tableConfig) {
+    if (tableConfig == null || tableConfig.getFieldConfigList() == null) {
+      return;
+    }
+    Set<String> timestampIndexColumns =
+        
TimestampIndexGranularity.extractTimestampIndexGranularityColumnNames(tableConfig);
+    for (Expression expression : pinotQuery.getSelectList()) {
+      setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery);
+    }
+    setTimestampIndexExpressionOverrideHints(pinotQuery.getFilterExpression(), 
timestampIndexColumns, pinotQuery);
+    setTimestampIndexExpressionOverrideHints(pinotQuery.getHavingExpression(), 
timestampIndexColumns, pinotQuery);
+    List<Expression> groupByList = pinotQuery.getGroupByList();
+    if (CollectionUtils.isNotEmpty(groupByList)) {
+      groupByList.forEach(
+          expression -> setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery));
+    }
+    List<Expression> orderByList = pinotQuery.getOrderByList();
+    if (CollectionUtils.isNotEmpty(orderByList)) {
+      orderByList.forEach(
+          expression -> setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery));
+    }
+  }
+
+  private void setTimestampIndexExpressionOverrideHints(Expression expression, 
Set<String> timestampIndexColumns,
+      PinotQuery pinotQuery) {
+    if (expression == null || expression.getFunctionCall() == null) {
+      return;
+    }
+    Function function = expression.getFunctionCall();
+    switch (function.getOperator().toUpperCase()) {
+      case "DATETRUNC":

Review Comment:
   (minor) function name should be canonical here
   ```suggestion
       switch (function.getOperator()) {
         case "datetrunc":
   ```



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -1784,6 +1841,14 @@ private static String getActualColumnName(String 
rawTableName, String columnName
       }
     }
     if (columnNameMap != null) {
+      if 
(TimestampIndexGranularity.isValidTimeColumnWithGranularityName(columnName)) {

Review Comment:
   Will this ever be hit? Is this for users explicitly query timestamp index 
column? I don't think we support case insensitive for in-built columns (e.g. 
`$docId`)



##########
pinot-spi/src/main/java/org/apache/pinot/spi/config/table/TimestampIndexGranularity.java:
##########
@@ -0,0 +1,110 @@
+/**
+ * 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.spi.config.table;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.pinot.spi.data.DateTimeFieldSpec;
+import org.apache.pinot.spi.data.DimensionFieldSpec;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.MetricFieldSpec;
+
+
+/**
+ * TimestampIndexGranularity is the enum of different time granularities from 
MILLIS to YEAR.
+ */
+public enum TimestampIndexGranularity {
+  MILLISECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR;
+
+  private static Set<String> _validValues =
+      Arrays.stream(TimestampIndexGranularity.values()).map(v -> 
v.toString()).collect(Collectors.toSet());
+
+  public static String getColumnNameWithGranularity(String column, 
TimestampIndexGranularity granularity) {
+    return "$" + column + "$" + granularity;
+  }
+
+  public static String extractColumnNameFromColumnWithGranularity(String 
columnWithGranularity) {
+    return StringUtils.split(columnWithGranularity, "$")[0];
+  }
+
+  public static TimestampIndexGranularity 
extractGranularityFromColumnWithGranularity(String columnWithGranularity) {
+    return 
TimestampIndexGranularity.valueOf(StringUtils.split(columnWithGranularity, 
"$")[1]);
+  }
+
+  public static boolean isValidTimeColumnWithGranularityName(String 
columnName) {
+    if (columnName.charAt(0) != '$') {
+      return false;
+    }
+    int secondDollarPos = columnName.indexOf('$', 1);
+    if (secondDollarPos < 0) {
+      return false;
+    }
+    return isValidTimeGranularity(columnName.substring(secondDollarPos + 1));
+  }
+
+  public static boolean isValidTimeGranularity(String granularity) {
+    return _validValues.contains(granularity.toUpperCase());
+  }
+
+  public static Set<String> 
extractTimestampIndexGranularityColumnNames(TableConfig tableConfig) {

Review Comment:
   Currently this is computed per query. Ideally we should cache the result so 
that it can be reused. If caching is hard, let's add a TODO



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -563,6 +569,57 @@ private BrokerResponseNative handleSQLRequest(long 
requestId, String query, Json
     return brokerResponse;
   }
 
+  private void handleTimestampIndexOverride(PinotQuery pinotQuery, TableConfig 
tableConfig) {
+    if (tableConfig == null || tableConfig.getFieldConfigList() == null) {
+      return;
+    }
+    Set<String> timestampIndexColumns =
+        
TimestampIndexGranularity.extractTimestampIndexGranularityColumnNames(tableConfig);
+    for (Expression expression : pinotQuery.getSelectList()) {
+      setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery);
+    }
+    setTimestampIndexExpressionOverrideHints(pinotQuery.getFilterExpression(), 
timestampIndexColumns, pinotQuery);
+    setTimestampIndexExpressionOverrideHints(pinotQuery.getHavingExpression(), 
timestampIndexColumns, pinotQuery);
+    List<Expression> groupByList = pinotQuery.getGroupByList();
+    if (CollectionUtils.isNotEmpty(groupByList)) {
+      groupByList.forEach(
+          expression -> setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery));
+    }
+    List<Expression> orderByList = pinotQuery.getOrderByList();
+    if (CollectionUtils.isNotEmpty(orderByList)) {
+      orderByList.forEach(
+          expression -> setTimestampIndexExpressionOverrideHints(expression, 
timestampIndexColumns, pinotQuery));
+    }
+  }
+
+  private void setTimestampIndexExpressionOverrideHints(Expression expression, 
Set<String> timestampIndexColumns,
+      PinotQuery pinotQuery) {
+    if (expression == null || expression.getFunctionCall() == null) {
+      return;
+    }
+    Function function = expression.getFunctionCall();
+    switch (function.getOperator().toUpperCase()) {
+      case "DATETRUNC":
+        String granularString = 
function.getOperands().get(0).getLiteral().getStringValue();
+        Expression timeExpression = function.getOperands().get(1);
+        if (function.getOperandsSize() == 2 && 
TimestampIndexGranularity.isValidTimeGranularity(granularString)

Review Comment:
   We can also handle the case when the third argument is `MILLISECONDS`



##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -563,6 +569,57 @@ private BrokerResponseNative handleSQLRequest(long 
requestId, String query, Json
     return brokerResponse;
   }
 
+  private void handleTimestampIndexOverride(PinotQuery pinotQuery, TableConfig 
tableConfig) {
+    if (tableConfig == null || tableConfig.getFieldConfigList() == null) {
+      return;
+    }
+    Set<String> timestampIndexColumns =
+        
TimestampIndexGranularity.extractTimestampIndexGranularityColumnNames(tableConfig);

Review Comment:
   Early terminate when `timestampIndexColumns` is empty



##########
pinot-segment-local/src/main/java/org/apache/pinot/segment/local/indexsegment/immutable/ImmutableSegmentLoader.java:
##########
@@ -210,6 +213,12 @@ public static ImmutableSegment load(SegmentDirectory 
segmentDirectory, IndexLoad
     return segment;
   }
 
+  private static Set<String> extractIndexedColumns(TableConfig tableConfig) {
+    Set<String> indexedColumns =
+        new 
HashSet<>(TimestampIndexGranularity.extractTimestampIndexGranularityColumnNames(tableConfig));

Review Comment:
   No need to copy into another set



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