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


##########
pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotImplicitTableHintRule.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.calcite.rel.rules;
+
+import java.util.ArrayList;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.hint.RelHint;
+import org.apache.calcite.rel.logical.LogicalTableScan;
+import org.apache.pinot.calcite.rel.hint.PinotHintOptions;
+import org.apache.pinot.query.planner.logical.RelToPlanNodeConverter;
+import org.apache.pinot.query.routing.WorkerManager;
+import org.immutables.value.Value;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Value.Enclosing
+public class PinotImplicitTableHintRule extends RelRule<RelRule.Config> {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PinotImplicitTableHintRule.class);
+  private final WorkerManager _workerManager;
+
+  private PinotImplicitTableHintRule(Config config) {
+    super(config);
+    _workerManager = config.getWorkerManager();
+  }
+
+  public static PinotImplicitTableHintRule withWorkerManager(WorkerManager 
workerManager) {
+    return new 
PinotImplicitTableHintRule(ImmutablePinotImplicitTableHintRule.Config.builder()
+        .operandSupplier(b0 -> b0.operand(LogicalTableScan.class).anyInputs())
+        .workerManager(workerManager)
+        .build()
+    );
+  }
+
+  @Override
+  public boolean matches(RelOptRuleCall call) {
+    LogicalTableScan tableScan = call.rel(0);
+
+    RelHint explicitHint = getTableOptionHint(tableScan);
+
+    if (explicitHint == null) {
+      return true;
+    }
+    // we don't want to apply this rule if the explicit hint is complete
+    Map<String, String> kvOptions = explicitHint.kvOptions;
+    return 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_KEY)
+        && 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_FUNCTION)
+        && 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_SIZE);
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    LogicalTableScan tableScan = call.rel(0);
+
+    String tableName = 
RelToPlanNodeConverter.getTableNameFromTableScan(tableScan);
+    @Nullable
+    TableOptions implicitTableOptions = 
_workerManager.inferTableOptions(tableName);
+    if (implicitTableOptions == null) {
+      return;
+    }
+
+    @Nullable
+    RelHint explicitHint = getTableOptionHint(tableScan);
+    TableOptions tableOptions = calculateTableOptions(explicitHint, 
implicitTableOptions, tableScan);
+    RelNode newRel = withNewTableOptions(tableScan, tableOptions);
+    call.transformTo(newRel);
+  }
+
+  /**
+   * Get the table option hint from the table scan, if any.
+   */
+  @Nullable
+  private static RelHint getTableOptionHint(LogicalTableScan tableScan) {
+    return tableScan.getHints().stream()
+        .filter(relHint -> 
relHint.hintName.equals(PinotHintOptions.TABLE_HINT_OPTIONS))
+        .findAny()

Review Comment:
   We want to pick the first hint that matches the name. The reason is that 
user might override the same hint multiple times, and the first one should be 
the closest one.
   Suggest adding this as a util method in `PinotHintStrategyTable`



##########
pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotImplicitTableHintRule.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.calcite.rel.rules;
+
+import java.util.ArrayList;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.hint.RelHint;
+import org.apache.calcite.rel.logical.LogicalTableScan;
+import org.apache.pinot.calcite.rel.hint.PinotHintOptions;
+import org.apache.pinot.query.planner.logical.RelToPlanNodeConverter;
+import org.apache.pinot.query.routing.WorkerManager;
+import org.immutables.value.Value;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Value.Enclosing
+public class PinotImplicitTableHintRule extends RelRule<RelRule.Config> {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PinotImplicitTableHintRule.class);
+  private final WorkerManager _workerManager;
+
+  private PinotImplicitTableHintRule(Config config) {
+    super(config);
+    _workerManager = config.getWorkerManager();
+  }
+
+  public static PinotImplicitTableHintRule withWorkerManager(WorkerManager 
workerManager) {
+    return new 
PinotImplicitTableHintRule(ImmutablePinotImplicitTableHintRule.Config.builder()
+        .operandSupplier(b0 -> b0.operand(LogicalTableScan.class).anyInputs())
+        .workerManager(workerManager)
+        .build()
+    );
+  }
+
+  @Override
+  public boolean matches(RelOptRuleCall call) {
+    LogicalTableScan tableScan = call.rel(0);
+
+    RelHint explicitHint = getTableOptionHint(tableScan);
+
+    if (explicitHint == null) {
+      return true;
+    }
+    // we don't want to apply this rule if the explicit hint is complete
+    Map<String, String> kvOptions = explicitHint.kvOptions;
+    return 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_KEY)
+        && 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_FUNCTION)
+        && 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_SIZE);
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    LogicalTableScan tableScan = call.rel(0);
+
+    String tableName = 
RelToPlanNodeConverter.getTableNameFromTableScan(tableScan);
+    @Nullable
+    TableOptions implicitTableOptions = 
_workerManager.inferTableOptions(tableName);
+    if (implicitTableOptions == null) {
+      return;
+    }
+
+    @Nullable
+    RelHint explicitHint = getTableOptionHint(tableScan);
+    TableOptions tableOptions = calculateTableOptions(explicitHint, 
implicitTableOptions, tableScan);
+    RelNode newRel = withNewTableOptions(tableScan, tableOptions);
+    call.transformTo(newRel);
+  }
+
+  /**
+   * Get the table option hint from the table scan, if any.
+   */
+  @Nullable
+  private static RelHint getTableOptionHint(LogicalTableScan tableScan) {
+    return tableScan.getHints().stream()
+        .filter(relHint -> 
relHint.hintName.equals(PinotHintOptions.TABLE_HINT_OPTIONS))
+        .findAny()
+        .orElse(null);
+  }
+
+  /**
+   * Returns a new node which is a copy of the given table scan with the new 
table options hint.
+   */
+  private static RelNode withNewTableOptions(LogicalTableScan tableScan, 
TableOptions tableOptions) {
+    ArrayList<RelHint> newHints = new ArrayList<>(tableScan.getHints());
+
+    newHints.removeIf(relHint -> 
relHint.hintName.equals(PinotHintOptions.TABLE_HINT_OPTIONS));
+
+    RelHint.Builder builder = 
RelHint.builder(PinotHintOptions.TABLE_HINT_OPTIONS)
+        .hintOption(PinotHintOptions.TableHintOptions.PARTITION_KEY, 
tableOptions.getPartitionKey())
+        .hintOption(PinotHintOptions.TableHintOptions.PARTITION_FUNCTION, 
tableOptions.getPartitionFunction())
+        .hintOption(PinotHintOptions.TableHintOptions.PARTITION_SIZE, 
String.valueOf(tableOptions.getPartitionSize()));
+
+    if (tableOptions.getPartitionParallelism() != null) {
+      
builder.hintOption(PinotHintOptions.TableHintOptions.PARTITION_PARALLELISM,
+          String.valueOf(tableOptions.getPartitionParallelism()));
+    }
+
+    newHints.add(builder.build());
+
+    return tableScan.withHints(newHints);
+  }
+
+  /**
+   * Creates a new table options hint based on the given table partition info 
and the explicit hint, if any.
+   *
+   * Any explicit hint will override the implicit hint obtained from the table 
partition info.
+   */
+  private static TableOptions calculateTableOptions(
+      @Nullable RelHint relHint, TableOptions implicitTableOptions, 
LogicalTableScan tableScan) {
+    if (relHint == null) {
+      return implicitTableOptions;
+    }
+
+    // there is a hint, check fill default data and obtain the partition 
parallelism if supplied
+    Map<String, String> kvOptions = relHint.kvOptions;
+
+    ImmutableTableOptions newTableOptions = 
ImmutableTableOptions.copyOf(implicitTableOptions);

Review Comment:
   Should we override here? I think we just want to validate if the explicit 
hint matches the implicit one, and always use the implicit one? Partition 
parallelism should always come from explicit hint



##########
pinot-query-planner/src/main/java/org/apache/pinot/query/QueryEnvironment.java:
##########
@@ -100,26 +111,64 @@ public class QueryEnvironment {
   private final FrameworkConfig _config;
   private final CalciteCatalogReader _catalogReader;
   private final HepProgram _optProgram;
-  private final HepProgram _traitProgram;
-
-  // Pinot extensions
-  private final TableCache _tableCache;
-  private final WorkerManager _workerManager;
+  private final Config _envConfig;
 
-  public QueryEnvironment(String database, TableCache tableCache, @Nullable 
WorkerManager workerManager) {
-    PinotCatalog catalog = new PinotCatalog(tableCache, database);
+  public QueryEnvironment(Config config) {
+    _envConfig = config;
+    String database = config.getDatabase();
+    PinotCatalog catalog = new PinotCatalog(config.getTableCache(), database);
     CalciteSchema rootSchema = CalciteSchema.createRootSchema(false, false, 
database, catalog);
     _config = 
Frameworks.newConfigBuilder().traitDefs().operatorTable(PinotOperatorTable.instance())
         
.defaultSchema(rootSchema.plus()).sqlToRelConverterConfig(PinotRuleUtils.PINOT_SQL_TO_REL_CONFIG).build();
     _catalogReader = new CalciteCatalogReader(rootSchema, List.of(database), 
_typeFactory, CONNECTION_CONFIG);
     _optProgram = getOptProgram();
-    _traitProgram = getTraitProgram();
-    _tableCache = tableCache;
-    _workerManager = workerManager;
   }
 
-  private PlannerContext getPlannerContext() {
-    return new PlannerContext(_config, _catalogReader, _typeFactory, 
_optProgram, _traitProgram);
+  public QueryEnvironment(String database, TableCache tableCache, @Nullable 
WorkerManager workerManager) {
+    this(configBuilder()
+        .database(database)
+        .tableCache(tableCache)
+        .workerManager(workerManager)
+        .build());
+  }
+
+  /**
+   * Returns a planner context that can be used to either parse, explain or 
execute a query.
+   */
+  private PlannerContext getPlannerContext(SqlNodeAndOptions 
sqlNodeAndOptions) {
+    WorkerManager workerManager = getWorkerManager(sqlNodeAndOptions);
+    HepProgram traitProgram = getTraitProgram(workerManager);
+    return new PlannerContext(_config, _catalogReader, _typeFactory, 
_optProgram, traitProgram);
+  }
+
+  @Nullable
+  private WorkerManager getWorkerManager(SqlNodeAndOptions sqlNodeAndOptions) {
+    String useImplicitColocatedOptionValue = sqlNodeAndOptions.getOptions()
+        
.get(CommonConstants.Broker.Request.QueryOptionKey.IMPLICIT_COLOCATE_JOIN);
+    WorkerManager workerManager = _envConfig.getWorkerManager();
+
+    if (useImplicitColocatedOptionValue == null) {
+      return _envConfig.useImplicitColocatedByDefault() ? workerManager : null;
+    }
+    switch (useImplicitColocatedOptionValue.toLowerCase()) {
+      case "true":
+        Objects.requireNonNull(workerManager, "WorkerManager is required for 
implicit colocated join");

Review Comment:
   Is it possible that we run into this query path without the worker manager 
being set? I still think this path should be handled the same way as instance 
level setting. Whether worker manager is set or not should be orthogonal to 
whether the setting is instance level or query level.



##########
pinot-query-planner/src/main/java/org/apache/pinot/calcite/rel/rules/PinotImplicitTableHintRule.java:
##########
@@ -0,0 +1,225 @@
+/**
+ * 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.calcite.rel.rules;
+
+import java.util.ArrayList;
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.hint.RelHint;
+import org.apache.calcite.rel.logical.LogicalTableScan;
+import org.apache.pinot.calcite.rel.hint.PinotHintOptions;
+import org.apache.pinot.query.planner.logical.RelToPlanNodeConverter;
+import org.apache.pinot.query.routing.WorkerManager;
+import org.immutables.value.Value;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Value.Enclosing
+public class PinotImplicitTableHintRule extends RelRule<RelRule.Config> {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PinotImplicitTableHintRule.class);
+  private final WorkerManager _workerManager;
+
+  private PinotImplicitTableHintRule(Config config) {
+    super(config);
+    _workerManager = config.getWorkerManager();
+  }
+
+  public static PinotImplicitTableHintRule withWorkerManager(WorkerManager 
workerManager) {
+    return new 
PinotImplicitTableHintRule(ImmutablePinotImplicitTableHintRule.Config.builder()
+        .operandSupplier(b0 -> b0.operand(LogicalTableScan.class).anyInputs())
+        .workerManager(workerManager)
+        .build()
+    );
+  }
+
+  @Override
+  public boolean matches(RelOptRuleCall call) {
+    LogicalTableScan tableScan = call.rel(0);
+
+    RelHint explicitHint = getTableOptionHint(tableScan);
+
+    if (explicitHint == null) {
+      return true;
+    }
+    // we don't want to apply this rule if the explicit hint is complete
+    Map<String, String> kvOptions = explicitHint.kvOptions;
+    return 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_KEY)
+        && 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_FUNCTION)
+        && 
kvOptions.containsKey(PinotHintOptions.TableHintOptions.PARTITION_SIZE);
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    LogicalTableScan tableScan = call.rel(0);
+
+    String tableName = 
RelToPlanNodeConverter.getTableNameFromTableScan(tableScan);
+    @Nullable
+    TableOptions implicitTableOptions = 
_workerManager.inferTableOptions(tableName);
+    if (implicitTableOptions == null) {
+      return;
+    }
+
+    @Nullable
+    RelHint explicitHint = getTableOptionHint(tableScan);
+    TableOptions tableOptions = calculateTableOptions(explicitHint, 
implicitTableOptions, tableScan);
+    RelNode newRel = withNewTableOptions(tableScan, tableOptions);
+    call.transformTo(newRel);
+  }
+
+  /**
+   * Get the table option hint from the table scan, if any.
+   */
+  @Nullable
+  private static RelHint getTableOptionHint(LogicalTableScan tableScan) {
+    return tableScan.getHints().stream()
+        .filter(relHint -> 
relHint.hintName.equals(PinotHintOptions.TABLE_HINT_OPTIONS))
+        .findAny()
+        .orElse(null);
+  }
+
+  /**
+   * Returns a new node which is a copy of the given table scan with the new 
table options hint.
+   */
+  private static RelNode withNewTableOptions(LogicalTableScan tableScan, 
TableOptions tableOptions) {
+    ArrayList<RelHint> newHints = new ArrayList<>(tableScan.getHints());
+
+    newHints.removeIf(relHint -> 
relHint.hintName.equals(PinotHintOptions.TABLE_HINT_OPTIONS));
+
+    RelHint.Builder builder = 
RelHint.builder(PinotHintOptions.TABLE_HINT_OPTIONS)
+        .hintOption(PinotHintOptions.TableHintOptions.PARTITION_KEY, 
tableOptions.getPartitionKey())
+        .hintOption(PinotHintOptions.TableHintOptions.PARTITION_FUNCTION, 
tableOptions.getPartitionFunction())
+        .hintOption(PinotHintOptions.TableHintOptions.PARTITION_SIZE, 
String.valueOf(tableOptions.getPartitionSize()));
+
+    if (tableOptions.getPartitionParallelism() != null) {
+      
builder.hintOption(PinotHintOptions.TableHintOptions.PARTITION_PARALLELISM,
+          String.valueOf(tableOptions.getPartitionParallelism()));
+    }
+
+    newHints.add(builder.build());
+
+    return tableScan.withHints(newHints);
+  }
+
+  /**
+   * Creates a new table options hint based on the given table partition info 
and the explicit hint, if any.
+   *
+   * Any explicit hint will override the implicit hint obtained from the table 
partition info.
+   */
+  private static TableOptions calculateTableOptions(
+      @Nullable RelHint relHint, TableOptions implicitTableOptions, 
LogicalTableScan tableScan) {
+    if (relHint == null) {
+      return implicitTableOptions;
+    }
+
+    // there is a hint, check fill default data and obtain the partition 
parallelism if supplied
+    Map<String, String> kvOptions = relHint.kvOptions;
+
+    ImmutableTableOptions newTableOptions = 
ImmutableTableOptions.copyOf(implicitTableOptions);
+    newTableOptions = overridePartitionKey(newTableOptions, tableScan, 
kvOptions);
+    newTableOptions = overridePartitionFunction(newTableOptions, tableScan, 
kvOptions);
+    newTableOptions = overridePartitionSize(newTableOptions, tableScan, 
kvOptions);
+    newTableOptions = overridePartitionParallelism(newTableOptions, tableScan, 
kvOptions);
+
+    return newTableOptions;
+  }
+
+  /**
+   * Returns a table options hint with the partition key overridden by the 
hint, if any.
+   */
+  private static ImmutableTableOptions 
overridePartitionKey(ImmutableTableOptions base, LogicalTableScan tableScan,
+      Map<String, String> kvOptions) {
+    String partitionKey = 
kvOptions.get(kvOptions.get(PinotHintOptions.TableHintOptions.PARTITION_KEY));
+    if (partitionKey == null || partitionKey.equals(base.getPartitionKey())) {
+      return base;
+    }
+    LOGGER.debug("Override implicit table hint for {} with explicit partition 
key: {}", tableScan, partitionKey);

Review Comment:
   Does `LogicalTableScan` have `toString()` implemented?



##########
pinot-query-planner/src/main/java/org/apache/pinot/query/routing/WorkerManager.java:
##########
@@ -76,6 +78,11 @@ public WorkerManager(String hostName, int port, 
RoutingManager routingManager) {
     _routingManager = routingManager;
   }
 
+  @Nullable
+  public TablePartitionInfo getTablePartitionInfo(String tableNameWithType) {

Review Comment:
   (minor) Seems not used



##########
pinot-spi/src/main/java/org/apache/pinot/spi/utils/CommonConstants.java:
##########
@@ -343,6 +343,8 @@ public static class Broker {
     public static final String CONFIG_OF_ENABLE_PARTITION_METADATA_MANAGER =
         "pinot.broker.enable.partition.metadata.manager";
     public static final boolean DEFAULT_ENABLE_PARTITION_METADATA_MANAGER = 
false;
+    public static final String IMPLICIT_COLOCATE_JOIN = 
"pinot.broker.multistage.implicit.colocate";

Review Comment:
   Remember to change this



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