-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Fix Native Plan Checker for CTAS and Insert queries #25115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,23 +21,36 @@ | |
| import com.facebook.presto.spi.PrestoException; | ||
| import com.facebook.presto.spi.TableHandle; | ||
| import com.facebook.presto.spi.WarningCollector; | ||
| import com.facebook.presto.spi.plan.Assignments; | ||
| import com.facebook.presto.spi.plan.PlanChecker; | ||
| import com.facebook.presto.spi.plan.PlanNode; | ||
| import com.facebook.presto.spi.plan.PlanVisitor; | ||
| import com.facebook.presto.spi.plan.ProjectNode; | ||
| import com.facebook.presto.spi.plan.SimplePlanFragment; | ||
| import com.facebook.presto.spi.plan.TableScanNode; | ||
| import com.facebook.presto.spi.plan.TableWriterNode; | ||
| import com.facebook.presto.spi.relation.ConstantExpression; | ||
| import com.facebook.presto.spi.relation.RowExpression; | ||
| import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
| import okhttp3.MediaType; | ||
| import okhttp3.OkHttpClient; | ||
| import okhttp3.Request; | ||
| import okhttp3.RequestBody; | ||
| import okhttp3.Response; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static com.facebook.presto.common.type.BigintType.BIGINT; | ||
| import static com.facebook.presto.common.type.VarcharType.VARCHAR; | ||
| import static com.facebook.presto.sidecar.nativechecker.NativePlanCheckerErrorCode.NATIVEPLANCHECKER_CONNECTION_ERROR; | ||
| import static com.facebook.presto.sidecar.nativechecker.NativePlanCheckerErrorCode.NATIVEPLANCHECKER_UNKNOWN_CONVERSION_FAILURE; | ||
| import static com.google.common.base.MoreObjects.firstNonNull; | ||
| import static com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static io.airlift.slice.Slices.utf8Slice; | ||
| import static java.util.Objects.requireNonNull; | ||
| import static java.util.stream.Collectors.toMap; | ||
|
|
||
| /** | ||
| * Uses the native sidecar to check verify a plan can be run on a native worker. | ||
|
|
@@ -70,12 +83,34 @@ public void validate(PlanNode planNode, WarningCollector warningCollector, Conne | |
| @Override | ||
| public void validateFragment(SimplePlanFragment planFragment, WarningCollector warningCollector, ConnectorSession session) | ||
| { | ||
| if (!planFragment.getPartitioning().isCoordinatorOnly() && !isInternalSystemConnector(planFragment.getRoot())) { | ||
| runValidation(planFragment); | ||
| } | ||
| else { | ||
| if (planFragment.getPartitioning().isCoordinatorOnly() | ||
| || isInternalSystemConnector(planFragment.getRoot())) { | ||
| LOG.debug("Skipping native plan validation [fragment: %s, root: %s]", planFragment.getId(), planFragment.getRoot().getId()); | ||
| return; | ||
| } | ||
| runValidation(removeTableWriter(planFragment)); | ||
| } | ||
|
|
||
| /** | ||
| * HACK: Replace TableWriterNode from the plan fragment with a ProjectNode because validating a TableWriterNode | ||
| * is unsupported by the native sidecar. They are unsupported because they contain information only determined | ||
| * during scheduling. | ||
| */ | ||
| private SimplePlanFragment removeTableWriter(SimplePlanFragment planFragment) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be good to add a note as to why the TableWriterNode gets replaced
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, PTAL |
||
| { | ||
| // Remove TableWriterNode from the plan fragment | ||
| PlanNode root = planFragment.getRoot().accept(new TableWriterNodeReplacer(), null); | ||
| requireNonNull(root, "TableWriterNode removal resulted in null root"); | ||
|
|
||
| return new SimplePlanFragment( | ||
| planFragment.getId(), | ||
| root, | ||
| planFragment.getVariables(), | ||
| planFragment.getPartitioning(), | ||
| planFragment.getTableScanSchedulingOrder(), | ||
| planFragment.getPartitioningScheme(), | ||
| planFragment.getStageExecutionDescriptor(), | ||
| planFragment.isOutputTableWriterFragment()); | ||
| } | ||
|
|
||
| private boolean isInternalSystemConnector(PlanNode planNode) | ||
|
|
@@ -152,4 +187,40 @@ public Boolean visitPlan(PlanNode node, Void context) | |
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static class TableWriterNodeReplacer | ||
| extends PlanVisitor<PlanNode, Void> | ||
| { | ||
| @Override | ||
| public PlanNode visitTableWriter(TableWriterNode tableWriter, Void context) | ||
| { | ||
| // Create dummy assignments for the ProjectNode | ||
| Map<VariableReferenceExpression, RowExpression> assignmentsMap = tableWriter.getOutputVariables() | ||
| .subList(3, tableWriter.getOutputVariables().size()) | ||
| .stream() | ||
| .collect(toMap(i -> i, i -> i)); | ||
| assignmentsMap.put(tableWriter.getRowCountVariable(), new ConstantExpression(0L, BIGINT)); | ||
| assignmentsMap.put(tableWriter.getFragmentVariable(), new ConstantExpression(utf8Slice(""), VARCHAR)); | ||
| assignmentsMap.put(tableWriter.getTableCommitContextVariable(), new ConstantExpression(utf8Slice(""), VARCHAR)); | ||
| Assignments assignments = Assignments.builder().putAll(assignmentsMap).build(); | ||
|
|
||
| // Replace TableWriterNode with a ProjectNode | ||
| return new ProjectNode( | ||
| tableWriter.getId(), | ||
| tableWriter.getSource(), | ||
| Assignments.builder().putAll(assignmentsMap).build()); | ||
| } | ||
|
|
||
| @Override | ||
| public PlanNode visitPlan(PlanNode node, Void context) | ||
| { | ||
| // Recursively process child nodes | ||
| List<PlanNode> prunedChildren = node.getSources().stream() | ||
| .map(child -> child.accept(this, context)) | ||
| .collect(toImmutableList()); | ||
|
|
||
| // Replace the current node's children with the pruned children | ||
| return node.replaceChildren(prunedChildren); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * Licensed 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 com.facebook.presto.sidecar; | ||
|
|
||
| import com.facebook.presto.nativeworker.PrestoNativeQueryRunnerUtils; | ||
| import com.facebook.presto.testing.QueryRunner; | ||
| import com.facebook.presto.tests.AbstractTestQueryFramework; | ||
| import com.facebook.presto.tests.DistributedQueryRunner; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| import static com.facebook.presto.common.type.BigintType.BIGINT; | ||
| import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createLineitem; | ||
| import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createNation; | ||
| import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createOrders; | ||
| import static com.facebook.presto.nativeworker.NativeQueryRunnerUtils.createOrdersEx; | ||
| import static com.facebook.presto.sidecar.NativeSidecarPluginQueryRunnerUtils.setupNativeSidecarPlugin; | ||
| import static java.lang.String.format; | ||
|
|
||
| @Test(singleThreaded = true) | ||
| public class TestNativeSidecarPlanChecker | ||
| extends AbstractTestQueryFramework | ||
| { | ||
| @Override | ||
| protected void createTables() | ||
| { | ||
| QueryRunner queryRunner = (QueryRunner) getExpectedQueryRunner(); | ||
| createLineitem(queryRunner); | ||
| createNation(queryRunner); | ||
| createOrders(queryRunner); | ||
| createOrdersEx(queryRunner); | ||
| } | ||
|
|
||
| @Override | ||
| protected QueryRunner createQueryRunner() | ||
| throws Exception | ||
| { | ||
| DistributedQueryRunner queryRunner = (DistributedQueryRunner) PrestoNativeQueryRunnerUtils.nativeHiveQueryRunnerBuilder() | ||
| .setCoordinatorSidecarEnabled(true) | ||
| .setExtraProperties(ImmutableMap.of("http-server.http.port", "8089")) | ||
| .build(); | ||
| setupNativeSidecarPlugin(queryRunner); | ||
| queryRunner.getCoordinator().createCatalog("hive2", "hive"); | ||
| return queryRunner; | ||
| } | ||
|
|
||
| @Override | ||
| protected QueryRunner createExpectedQueryRunner() | ||
| throws Exception | ||
| { | ||
| return PrestoNativeQueryRunnerUtils.javaHiveQueryRunnerBuilder().build(); | ||
| } | ||
|
|
||
| @Test | ||
| public void createAndInsertUnbucketedTable() | ||
| { | ||
| String tableName = "tmp_presto_" + UUID.randomUUID().toString().replace("-", ""); | ||
| try { | ||
| assertUpdate(format("CREATE TABLE %s AS SELECT orderkey key1, comment value1 FROM orders", tableName), 15000); | ||
| assertUpdate(format("INSERT INTO %s SELECT orderkey key1, comment value1 FROM orders", tableName), 15000); | ||
| } | ||
| finally { | ||
| // Clean up the temporary tables | ||
| getExpectedQueryRunner().execute(getSession(), format("DROP TABLE IF EXISTS %s", tableName), ImmutableList.of(BIGINT)); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void selectFromUnbucketedTable() | ||
| { | ||
| assertQuery(format("SELECT orderkey key1, comment value1 FROM orders")); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.