pri1712 commented on code in PR #19011:
URL: https://github.com/apache/pinot/pull/19011#discussion_r3698166622
##########
pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java:
##########
@@ -480,4 +496,110 @@ protected void onQueryFinish(long requestId) {
protected boolean isQueryCancellationEnabled() {
return _enableQueryCancellation;
}
+
+ /**
+ * Appends a where clause to the query to filter out of retention data if
SKIP_OUT_OF_RETENTION_VALUES is
+ * set to True in query Options.
+ * @param sqlNodeAndOptions
+ */
+ private void applySkipOutOfRetentionValuesIfNeeded(SqlNodeAndOptions
sqlNodeAndOptions) {
+ Map<String, String> options = sqlNodeAndOptions.getOptions();
+ if
(!Boolean.parseBoolean(options.get(QueryOptionKey.SKIP_OUT_OF_RETENTION_VALUES)))
{
+ return;
+ }
+
+ SqlNode sqlNode = sqlNodeAndOptions.getSqlNode();
+ if (sqlNode == null) {
+ return;
+ }
+
+ // Resolve the inner SqlSelect, unwrapping SqlOrderBy if needed.
+ // SqlWith (CTEs) and other statement types are not supported.
+ SqlSelect sqlSelect;
+ if (sqlNode instanceof SqlSelect) {
+ sqlSelect = (SqlSelect) sqlNode;
+ } else if (sqlNode instanceof SqlOrderBy) {
+ SqlNode query = ((SqlOrderBy) sqlNode).query;
+ if (!(query instanceof SqlSelect)) {
+ return;
+ }
+ sqlSelect = (SqlSelect) query;
+ } else {
+ return;
+ }
+
+ TableNameExtractor tableNameExtractor = new TableNameExtractor();
+ tableNameExtractor.extractTableNames(sqlSelect);
+ Set<String> tableNames = tableNameExtractor.getTableNames();
+ if (tableNames.isEmpty()) {
+ return;
+ }
+
+ // Multi-table queries (JOINs) are not supported: it is ambiguous which
table's retention
+ // applies, and injecting a filter with the wrong table's time column
would produce
+ // incorrect results. Callers must issue separate single-table queries.
+ if (tableNames.size() > 1) {
+ LOGGER.debug("skipOutOfRetentionValues is not supported for multi-table
queries; skipping filter injection");
+ return;
+ }
+
+ String rawTableName =
TableNameBuilder.extractRawTableName(tableNames.iterator().next());
+
+ TableConfig tableConfig = _tableCache.getTableConfig(rawTableName);
+ Schema schema = _tableCache.getSchema(rawTableName);
+ if (tableConfig == null || schema == null ||
tableConfig.getValidationConfig() == null) {
+ return;
+ }
+
+ //get timestamp column and retention details
+ String timeColumnName =
tableConfig.getValidationConfig().getTimeColumnName();
+ String retentionTimeUnit =
tableConfig.getValidationConfig().getRetentionTimeUnit();
+ String retentionTimeValue =
tableConfig.getValidationConfig().getRetentionTimeValue();
+ if (StringUtils.isEmpty(timeColumnName) ||
StringUtils.isEmpty(retentionTimeUnit)
+ || StringUtils.isEmpty(retentionTimeValue)) {
+ return;
+ }
+ try {
+ long retentionTimeMs =
+
TimeUnit.valueOf(retentionTimeUnit.toUpperCase()).toMillis(Long.parseLong(retentionTimeValue));
+ long cutoffMs = System.currentTimeMillis() - retentionTimeMs;
+ DateTimeFieldSpec timeFieldSpec =
schema.getSpecForTimeColumn(timeColumnName);
+ if (timeFieldSpec == null) {
+ return;
+ }
+
+ DateTimeFormatSpec formatSpec = new
DateTimeFormatSpec(timeFieldSpec.getFormat());
+ String formattedCutoffTime = formatSpec.fromMillisToFormat(cutoffMs);
+
+ //add where clause to the AST
+ SqlIdentifier timeColNode = new SqlIdentifier(timeColumnName,
SqlParserPos.ZERO);
+ SqlLiteral cutoffNode;
+ if (timeFieldSpec.getDataType().isNumeric()) {
+ cutoffNode = SqlLiteral.createExactNumeric(formattedCutoffTime,
SqlParserPos.ZERO);
+ } else {
+ cutoffNode = SqlLiteral.createCharString(formattedCutoffTime,
SqlParserPos.ZERO);
+ }
+
+ SqlBasicCall rangeFilter = new SqlBasicCall(
+ SqlStdOperatorTable.GREATER_THAN_OR_EQUAL, List.of(timeColNode,
cutoffNode),
+ SqlParserPos.ZERO);
+
+ SqlNode existingWhere = sqlSelect.getWhere();
+ if (existingWhere != null) {
+ //add an AND if the where exists
+ SqlBasicCall andExpr = new SqlBasicCall(
+ SqlStdOperatorTable.AND,
+ List.of(existingWhere, rangeFilter),
+ SqlParserPos.ZERO
+ );
+ sqlSelect.setWhere(andExpr);
+ } else {
+ sqlSelect.setWhere(rangeFilter);
+ }
+ LOGGER.debug("Injected Calcite AST filter for skipOutOfRetentionValues
on table: {}. Cutoff: {}", rawTableName,
+ formattedCutoffTime);
+ } catch (RuntimeException e) {
+ throw new IllegalStateException("Failed to apply
skipOutOfRetentionValues on table: " + rawTableName, e);
Review Comment:
decided to do it this way
##########
pinot-broker/src/test/java/org/apache/pinot/broker/requesthandler/SkipOutOfRetentionValuesTest.java:
##########
@@ -0,0 +1,364 @@
+/**
+ * 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 java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.helix.model.InstanceConfig;
+import org.apache.pinot.broker.broker.AllowAllAccessControlFactory;
+import org.apache.pinot.broker.queryquota.QueryQuotaManager;
+import org.apache.pinot.broker.routing.manager.BrokerRoutingManager;
+import org.apache.pinot.common.config.provider.TableCache;
+import org.apache.pinot.common.metrics.BrokerMetrics;
+import org.apache.pinot.common.request.BrokerRequest;
+import org.apache.pinot.common.request.Expression;
+import org.apache.pinot.common.request.Function;
+import org.apache.pinot.common.response.broker.BrokerResponseNative;
+import org.apache.pinot.core.routing.RoutingTable;
+import org.apache.pinot.core.routing.SegmentsToQuery;
+import org.apache.pinot.core.routing.TableRouteInfo;
+import org.apache.pinot.core.transport.ServerInstance;
+import org.apache.pinot.spi.accounting.ThreadAccountantUtils;
+import org.apache.pinot.spi.config.table.SegmentsValidationAndRetentionConfig;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TenantConfig;
+import org.apache.pinot.spi.data.FieldSpec.DataType;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import
org.apache.pinot.spi.eventlistener.query.BrokerQueryEventListenerFactory;
+import org.apache.pinot.spi.trace.RequestContext;
+import org.apache.pinot.spi.utils.CommonConstants.Query.Range;
+import org.apache.pinot.sql.FilterKind;
+import org.mockito.Mockito;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+
+/**
+ * Tests for the {@code skipOutOfRetentionValues} query option in {@link
BaseBrokerRequestHandler}.
+ *
+ * <p>When the option is set the broker injects a lower-bound time filter
derived from the table's
+ * retention config before the query reaches any server. Tests verify the
injected filter's
+ * structure and value, and the skip conditions (option absent, no retention
config, missing time
+ * column in schema, ORDER BY wrapper, existing WHERE clause).
+ */
+public class SkipOutOfRetentionValuesTest {
Review Comment:
added , pls check
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]