Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void simpleQueryLogical() {
explainLogical("SELECT 1 FROM mytable",
"Execution Plan\n"
+ "LogicalProject(EXPR$0=[1])\n"
+ " LogicalTableScan(table=[[default, mytable]])\n");
+ " PinotLogicalTableScan(table=[[default, mytable]])\n");
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3485,7 +3485,7 @@ public void testExplainPlanQueryV2()
+ " PinotLogicalAggregate(group=[{0}], agg#0=[COUNT($1)], aggType=[FINAL])\n"
+ " PinotLogicalExchange(distribution=[hash[0]])\n"
+ " PinotLogicalAggregate(group=[{17}], agg#0=[COUNT()], aggType=[LEAF])\n"
+ " LogicalTableScan(table=[[default, mytable]])\n");
+ " PinotLogicalTableScan(table=[[default, mytable]])\n");
assertEquals(response1Json.get("rows").get(0).get(2).asText(), "Rule Execution Times\n"
+ "Rule: AggregateProjectMergeRule -> Time:*\n"
+ "Rule: Project -> Time:*\n"
Expand All @@ -3512,7 +3512,7 @@ public void testExplainPlanQueryV2()
"Execution Plan\n"
+ "LogicalProject\\(.*\\)\n"
+ " LogicalFilter\\(condition=\\[<\\(.*, 0\\)]\\)\n"
+ " LogicalTableScan\\(table=\\[\\[default, mytable]]\\)\n"
+ " PinotLogicalTableScan\\(table=\\[\\[default, mytable]]\\)\n"
).matcher(response2Json.get("rows").get(0).get(1).asText()).find());
assertEquals(response2Json.get("rows").get(0).get(2).asText(),
"Rule Execution Times\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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.logical;

import java.util.List;
import org.apache.calcite.plan.RelOptCluster;
import org.apache.calcite.plan.RelOptTable;
import org.apache.calcite.plan.RelTraitSet;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.TableScan;
import org.apache.calcite.rel.hint.RelHint;
import org.apache.calcite.rel.logical.LogicalTableScan;


/**
* Same as {@link org.apache.calcite.rel.logical.LogicalTableScan}, except that it doesn't drop provided traits
* in the {@link #copy} method.
*/
public class PinotLogicalTableScan extends TableScan {
protected PinotLogicalTableScan(RelOptCluster cluster, RelTraitSet traitSet, List<RelHint> hints, RelOptTable table) {
super(cluster, traitSet, hints, table);
}

@Override
public RelNode withHints(List<RelHint> hintList) {
return new PinotLogicalTableScan(getCluster(), traitSet, hintList, table);
}

@Override
public RelNode copy(RelTraitSet traitSet, List<RelNode> inputs) {
assert inputs.isEmpty();
return new PinotLogicalTableScan(getCluster(), traitSet, hints, table);
}

public static PinotLogicalTableScan create(LogicalTableScan logicalTableScan) {
return new PinotLogicalTableScan(logicalTableScan.getCluster(), logicalTableScan.getTraitSet(),
logicalTableScan.getHints(), logicalTableScan.getTable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.plan.RelRule;
import org.apache.calcite.rel.RelNode;
import org.apache.calcite.rel.core.TableScan;
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.calcite.rel.hint.PinotHintStrategyTable;
import org.apache.pinot.query.planner.logical.RelToPlanNodeConverter;
Expand All @@ -47,23 +47,23 @@ private PinotImplicitTableHintRule(Config config) {

public static PinotImplicitTableHintRule withWorkerManager(WorkerManager workerManager) {
return new PinotImplicitTableHintRule(ImmutablePinotImplicitTableHintRule.Config.builder()
.operandSupplier(b0 -> b0.operand(LogicalTableScan.class).anyInputs())
.operandSupplier(b0 -> b0.operand(TableScan.class).anyInputs())
.workerManager(workerManager)
.build()
);
}

@Override
public boolean matches(RelOptRuleCall call) {
LogicalTableScan tableScan = call.rel(0);
TableScan tableScan = call.rel(0);

// we don't want to apply this rule if the explicit hint is complete
return !isHintComplete(getTableOptionHint(tableScan));
}

@Override
public void onMatch(RelOptRuleCall call) {
LogicalTableScan tableScan = call.rel(0);
TableScan tableScan = call.rel(0);

String tableName = RelToPlanNodeConverter.getTableNameFromTableScan(tableScan);
@Nullable
Expand Down Expand Up @@ -97,14 +97,14 @@ private boolean isHintComplete(@Nullable RelHint hint) {
* Get the table option hint from the table scan, if any.
*/
@Nullable
private static RelHint getTableOptionHint(LogicalTableScan tableScan) {
private static RelHint getTableOptionHint(TableScan tableScan) {
return PinotHintStrategyTable.getHint(tableScan, PinotHintOptions.TABLE_HINT_OPTIONS);
}

/**
* 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) {
private static RelNode withNewTableOptions(TableScan tableScan, TableOptions tableOptions) {
ArrayList<RelHint> newHints = new ArrayList<>(tableScan.getHints());

newHints.removeIf(relHint -> relHint.hintName.equals(PinotHintOptions.TABLE_HINT_OPTIONS));
Expand All @@ -130,7 +130,7 @@ private static RelNode withNewTableOptions(LogicalTableScan tableScan, TableOpti
* 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) {
@Nullable RelHint relHint, TableOptions implicitTableOptions, TableScan tableScan) {
if (relHint == null) {
return implicitTableOptions;
}
Expand All @@ -150,7 +150,7 @@ private static TableOptions calculateTableOptions(
/**
* Returns a table options hint with the partition key overridden by the hint, if any.
*/
private static ImmutableTableOptions overridePartitionKey(ImmutableTableOptions base, LogicalTableScan tableScan,
private static ImmutableTableOptions overridePartitionKey(ImmutableTableOptions base, TableScan tableScan,
Map<String, String> kvOptions) {
String partitionKey = kvOptions.get(kvOptions.get(PinotHintOptions.TableHintOptions.PARTITION_KEY));
if (partitionKey == null || partitionKey.equals(base.getPartitionKey())) {
Expand All @@ -163,8 +163,8 @@ private static ImmutableTableOptions overridePartitionKey(ImmutableTableOptions
/**
* Returns a table options hint with the partition function overridden by the hint, if any.
*/
private static ImmutableTableOptions overridePartitionFunction(ImmutableTableOptions base, LogicalTableScan tableScan,
Map<String, String> kvOptions) {
private static ImmutableTableOptions overridePartitionFunction(ImmutableTableOptions base,
TableScan tableScan, Map<String, String> kvOptions) {
String partitionFunction = kvOptions.get(kvOptions.get(PinotHintOptions.TableHintOptions.PARTITION_FUNCTION));
if (partitionFunction == null || partitionFunction.equals(base.getPartitionFunction())) {
return base;
Expand All @@ -178,7 +178,7 @@ private static ImmutableTableOptions overridePartitionFunction(ImmutableTableOpt
* Returns a table options hint with the partition parallelism overridden by the hint, if any.
*/
private static ImmutableTableOptions overridePartitionParallelism(ImmutableTableOptions base,
LogicalTableScan tableScan, Map<String, String> kvOptions) {
TableScan tableScan, Map<String, String> kvOptions) {
String partitionParallelismStr = kvOptions.get(PinotHintOptions.TableHintOptions.PARTITION_PARALLELISM);
if (partitionParallelismStr == null) {
return base;
Expand All @@ -197,8 +197,8 @@ private static ImmutableTableOptions overridePartitionParallelism(ImmutableTable
/**
* Returns a table options hint with the partition size overridden by the hint, if any.
*/
private static ImmutableTableOptions overridePartitionSize(ImmutableTableOptions base, LogicalTableScan tableScan,
Map<String, String> kvOptions) {
private static ImmutableTableOptions overridePartitionSize(ImmutableTableOptions base,
TableScan tableScan, Map<String, String> kvOptions) {
String partitionSizeStr = kvOptions.get(PinotHintOptions.TableHintOptions.PARTITION_SIZE);
if (partitionSizeStr == null) {
return base;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ private PinotQueryRuleSets() {
PinotExchangeEliminationRule.INSTANCE,

// Evaluate the Literal filter nodes
CoreRules.FILTER_REDUCE_EXPRESSIONS
CoreRules.FILTER_REDUCE_EXPRESSIONS,
PinotTableScanConverterRule.INSTANCE
);
//@formatter:on
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.plan.RelOptRuleCall;
Expand All @@ -35,7 +36,6 @@
import org.apache.calcite.rel.logical.LogicalFilter;
import org.apache.calcite.rel.logical.LogicalJoin;
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.logical.LogicalTableScan;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.tools.RelBuilderFactory;
import org.apache.calcite.util.mapping.IntPair;
Expand All @@ -46,6 +46,7 @@
import org.apache.pinot.calcite.rel.hint.PinotHintStrategyTable;
import org.apache.pinot.calcite.rel.logical.PinotLogicalAggregate;
import org.apache.pinot.calcite.rel.logical.PinotLogicalExchange;
import org.apache.pinot.calcite.rel.logical.PinotLogicalTableScan;
import org.apache.pinot.query.planner.logical.RelToPlanNodeConverter;
import org.apache.pinot.query.planner.plannode.AggregateNode;
import org.slf4j.Logger;
Expand Down Expand Up @@ -95,9 +96,9 @@ private static RelNode attachTrait(RelNode relNode, RelTrait trait) {
return new LogicalJoin(join.getCluster(), clusterTraitSet.plus(trait), join.getLeft(), join.getRight(),
join.getCondition(), join.getVariablesSet(), join.getJoinType(), join.isSemiJoinDone(),
ImmutableList.copyOf(join.getSystemFieldList()));
} else if (relNode instanceof LogicalTableScan) {
LogicalTableScan tableScan = (LogicalTableScan) relNode;
return new LogicalTableScan(tableScan.getCluster(), clusterTraitSet.plus(trait), tableScan.getTable());
} else if (relNode instanceof PinotLogicalTableScan) {
PinotLogicalTableScan tableScan = (PinotLogicalTableScan) relNode;
return tableScan.copy(clusterTraitSet.plus(trait), Collections.emptyList());
} else {
return relNode.copy(clusterTraitSet.plus(trait), relNode.getInputs());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 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 org.apache.calcite.plan.RelOptRule;
import org.apache.calcite.plan.RelOptRuleCall;
import org.apache.calcite.rel.core.TableScan;
import org.apache.calcite.rel.logical.LogicalTableScan;
import org.apache.calcite.tools.RelBuilderFactory;
import org.apache.pinot.calcite.rel.logical.PinotLogicalTableScan;


public class PinotTableScanConverterRule extends RelOptRule {
public static final PinotTableScanConverterRule INSTANCE =
new PinotTableScanConverterRule(PinotRuleUtils.PINOT_REL_FACTORY);

public PinotTableScanConverterRule(RelBuilderFactory factory) {
super(operand(TableScan.class, none()), factory, null);
}

@Override
public void onMatch(RelOptRuleCall call) {
TableScan tableScan = call.rel(0);
if (tableScan instanceof LogicalTableScan) {
call.transformTo(PinotLogicalTableScan.create(call.rel(0)));
} else if (!(tableScan instanceof PinotLogicalTableScan)) {
throw new IllegalStateException("Unknown table scan in PinotTableScanConverterRule: " + tableScan.getClass());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a QueryException here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thrown exception here would get caught in MultiStageBrokerRequestHandler which will set the QUERY_PLANNING_ERROR_CODE. I think the idea behind this approach is that the planner etc. can throw whatever exception seems right in their context (e.g. illegal state in this case), and the broker request handler will convert it to a structured form with an error code.

I don't think QueryException is used for throwing. It is mainly used for getting a ProcessingException which shows up nicely in broker response (we should ideally make the contract explicit in QueryException javadocs)

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That has recently changed in #15037. Now QueryException is an actual exception. The idea is that you can throw it anywhere during query processing and that way you can define the QueryErrorCode being used. That is not 100% true because there are still some paths that haven't been updated, but it is pretty safe to think that way.

For example, here we know that the error is not related to the query but a bug in our code, so it could be marked as QueryErrorCode.INTERNAL instead of QueryErrorCode.QUERY_PLANNING.

Anyway, it is completely valid to delegate the caller's responsibility to catch the exception and set the error code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah damn saw this comment after merging. I am working on another PR.. can update this there. Thanks for sharing this. This looks like a better approach to me.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 34, you can directly match LogicalTableScan. I don't think we need to protect it from getting a different TableScan node

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.apache.calcite.rel.logical.LogicalJoin;
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rel.logical.LogicalSort;
import org.apache.calcite.rel.logical.LogicalTableScan;
import org.apache.calcite.rel.logical.LogicalValues;
import org.apache.calcite.rel.logical.LogicalWindow;
import org.apache.calcite.rel.type.RelDataType;
Expand All @@ -56,6 +55,7 @@
import org.apache.pinot.calcite.rel.logical.PinotLogicalAggregate;
import org.apache.pinot.calcite.rel.logical.PinotLogicalExchange;
import org.apache.pinot.calcite.rel.logical.PinotLogicalSortExchange;
import org.apache.pinot.calcite.rel.logical.PinotLogicalTableScan;
import org.apache.pinot.calcite.rel.logical.PinotRelExchangeType;
import org.apache.pinot.calcite.rel.rules.PinotRuleUtils;
import org.apache.pinot.common.config.provider.TableCache;
Expand Down Expand Up @@ -107,8 +107,8 @@ public RelToPlanNodeConverter(@Nullable TransformationTracker.Builder<PlanNode,
*/
public PlanNode toPlanNode(RelNode node) {
PlanNode result;
if (node instanceof LogicalTableScan) {
result = convertLogicalTableScan((LogicalTableScan) node);
if (node instanceof PinotLogicalTableScan) {
result = convertPinotLogicalTableScan((PinotLogicalTableScan) node);
} else if (node instanceof LogicalProject) {
result = convertLogicalProject((LogicalProject) node);
} else if (node instanceof LogicalFilter) {
Expand Down Expand Up @@ -289,7 +289,7 @@ private FilterNode convertLogicalFilter(LogicalFilter node) {
convertInputs(node.getInputs()), RexExpressionUtils.fromRexNode(node.getCondition()));
}

private TableScanNode convertLogicalTableScan(LogicalTableScan node) {
private TableScanNode convertPinotLogicalTableScan(PinotLogicalTableScan node) {
String tableName = _tableCache.getActualTableName(getTableNameFromTableScan(node));
List<RelDataTypeField> fields = node.getRowType().getFieldList();
List<String> columns = new ArrayList<>(fields.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testAggregateCaseToFilter() {
+ " PinotLogicalExchange(distribution=[hash])\n"
+ " PinotLogicalAggregate(group=[{}], agg#0=[COUNT() FILTER $0], agg#1=[COUNT()], aggType=[LEAF])\n"
+ " LogicalProject($f1=[=($0, _UTF-8'a')])\n"
+ " LogicalTableScan(table=[[default, a]])\n");
+ " PinotLogicalTableScan(table=[[default, a]])\n");
//@formatter:on
}

Expand Down Expand Up @@ -592,13 +592,13 @@ private Object[][] provideQueriesWithExplainedLogicalPlan() {
+ " \"rels\": [\n"
+ " {\n"
+ " \"id\": \"0\",\n"
+ " \"relOp\": \"LogicalTableScan\",\n"
+ " \"relOp\": \"org.apache.pinot.calcite.rel.logical.PinotLogicalTableScan\",\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are required because RelJson only converts to short names for calcite specific packages.

+ " \"table\": [\n"
+ " \"default\",\n"
+ " \"a\"\n"
+ " ],\n"
+ " \"inputs\": [],\n"
+ " \"type\": \"LogicalTableScan\"\n"
+ " \"type\": \"PinotLogicalTableScan\"\n"
+ " },\n"
+ " {\n"
+ " \"id\": \"1\",\n"
Expand Down Expand Up @@ -626,7 +626,7 @@ private Object[][] provideQueriesWithExplainedLogicalPlan() {
+ "digraph {\n"
+ "\"PinotLogicalExchange\\n\" -> \"PinotLogicalAggregat\\ne\\n\" [label=\"0\"]\n"
+ "\"PinotLogicalAggregat\\ne\\n\" -> \"PinotLogicalExchange\\n\" [label=\"0\"]\n"
+ "\"LogicalTableScan\\n\" -> \"PinotLogicalAggregat\\ne\\n\" [label=\"0\"]\n"
+ "\"PinotLogicalTableSca\\nn\\n\" -> \"PinotLogicalAggregat\\ne\\n\" [label=\"0\"]\n"
+ "}\n"
},
new Object[]{"EXPLAIN PLAN FOR SELECT a.col1, b.col3 FROM a JOIN b ON a.col1 = b.col1",
Expand All @@ -635,10 +635,10 @@ private Object[][] provideQueriesWithExplainedLogicalPlan() {
+ " LogicalJoin(condition=[=($0, $1)], joinType=[inner])\n"
+ " PinotLogicalExchange(distribution=[hash[0]])\n"
+ " LogicalProject(col1=[$0])\n"
+ " LogicalTableScan(table=[[default, a]])\n"
+ " PinotLogicalTableScan(table=[[default, a]])\n"
+ " PinotLogicalExchange(distribution=[hash[0]])\n"
+ " LogicalProject(col1=[$0], col3=[$2])\n"
+ " LogicalTableScan(table=[[default, b]])\n"
+ " PinotLogicalTableScan(table=[[default, b]])\n"
},
};
//@formatter:on
Expand Down
Loading
Loading