-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Support SQL MERGE in the Trino engine and five connectors #7933
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
Merged
electrum
merged 12 commits into
trinodb:master
from
djsagain:david.stryker/support-sql-merge-final
Aug 5, 2022
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5689dfb
Update Kudu Toxiproxy to 2.1.4
electrum 1516b6d
Allow non-standard schemas for UPDATE smoke tests
electrum 0912cbd
Add support for TINYINT and REAL types in Raptor
electrum 660ceb8
Add CatalogHandle utility method in NodePartitioningManager
electrum 6b4c901
Add default implementation of getSplitBucketFunction
electrum e7d11c2
Make ConnectorBucketNodeMap optional
electrum cee96c3
Support SQL MERGE in the Trino engine
djsagain e906548
Support SQL MERGE in the Hive connector
djsagain cf5c25c
Support SQL MERGE in the Kudu connector
djsagain 435d100
Support SQL MERGE in the Raptor connector
electrum 6cb188b
Support SQL MERGE in the Iceberg connector
electrum 53a4500
Support SQL MERGE in the Delta Lake connector
electrum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
core/trino-main/src/main/java/io/trino/metadata/MergeHandle.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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 io.trino.metadata; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import io.trino.spi.connector.ConnectorMergeTableHandle; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public final class MergeHandle | ||
| { | ||
| private final TableHandle tableHandle; | ||
| private final ConnectorMergeTableHandle connectorMergeHandle; | ||
|
|
||
| @JsonCreator | ||
| public MergeHandle( | ||
| @JsonProperty("tableHandle") TableHandle tableHandle, | ||
| @JsonProperty("connectorMergeHandle") ConnectorMergeTableHandle connectorMergeHandle) | ||
| { | ||
| this.tableHandle = requireNonNull(tableHandle, "tableHandle is null"); | ||
| this.connectorMergeHandle = requireNonNull(connectorMergeHandle, "connectorMergeHandle is null"); | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public TableHandle getTableHandle() | ||
| { | ||
| return tableHandle; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public ConnectorMergeTableHandle getConnectorMergeHandle() | ||
| { | ||
| return connectorMergeHandle; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
core/trino-main/src/main/java/io/trino/operator/ChangeOnlyUpdatedColumnsMergeProcessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * 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 io.trino.operator; | ||
|
|
||
| import io.trino.spi.Page; | ||
| import io.trino.spi.block.Block; | ||
| import io.trino.spi.block.ColumnarRow; | ||
| import io.trino.spi.block.RunLengthEncodedBlock; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static io.trino.spi.block.ColumnarRow.toColumnarRow; | ||
| import static io.trino.spi.predicate.Utils.nativeValueToBlock; | ||
| import static io.trino.spi.type.TinyintType.TINYINT; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * The transformPage() method in this class does two things: | ||
| * <ul> | ||
| * <li>Transform the input page into an "update" page format</li> | ||
| * <li>Removes all rows whose operation number is DEFAULT_CASE_OPERATION_NUMBER</li> | ||
| * </ul> | ||
| */ | ||
| public class ChangeOnlyUpdatedColumnsMergeProcessor | ||
| implements MergeRowChangeProcessor | ||
| { | ||
| private static final Block INSERT_FROM_UPDATE_BLOCK = nativeValueToBlock(TINYINT, 0L); | ||
|
|
||
| private final int rowIdChannel; | ||
| private final int mergeRowChannel; | ||
| private final List<Integer> dataColumnChannels; | ||
| private final int writeRedistributionColumnCount; | ||
|
|
||
| public ChangeOnlyUpdatedColumnsMergeProcessor( | ||
| int rowIdChannel, | ||
| int mergeRowChannel, | ||
| List<Integer> dataColumnChannels, | ||
| List<Integer> redistributionColumnChannels) | ||
| { | ||
| this.rowIdChannel = rowIdChannel; | ||
| this.mergeRowChannel = mergeRowChannel; | ||
| this.dataColumnChannels = requireNonNull(dataColumnChannels, "dataColumnChannels is null"); | ||
| this.writeRedistributionColumnCount = redistributionColumnChannels.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public Page transformPage(Page inputPage) | ||
| { | ||
| requireNonNull(inputPage, "inputPage is null"); | ||
| int inputChannelCount = inputPage.getChannelCount(); | ||
| checkArgument(inputChannelCount >= 2 + writeRedistributionColumnCount, "inputPage channelCount (%s) should be >= 2 + %s", inputChannelCount, writeRedistributionColumnCount); | ||
| int positionCount = inputPage.getPositionCount(); | ||
| // TODO: Check with Karol to see if we can get empty pages | ||
| checkArgument(positionCount > 0, "positionCount should be > 0, but is %s", positionCount); | ||
|
|
||
| ColumnarRow mergeRow = toColumnarRow(inputPage.getBlock(mergeRowChannel)); | ||
|
djsagain marked this conversation as resolved.
Outdated
|
||
| checkArgument(!mergeRow.mayHaveNull(), "The mergeRow may not have null rows"); | ||
|
|
||
| // We've verified that the mergeRow block has no null rows, so it's okay to get the field blocks | ||
|
|
||
| List<Block> builder = new ArrayList<>(dataColumnChannels.size() + 3); | ||
|
|
||
| for (int channel : dataColumnChannels) { | ||
| builder.add(mergeRow.getField(channel)); | ||
| } | ||
| Block operationChannelBlock = mergeRow.getField(mergeRow.getFieldCount() - 2); | ||
| builder.add(operationChannelBlock); | ||
|
djsagain marked this conversation as resolved.
Outdated
|
||
| builder.add(inputPage.getBlock(rowIdChannel)); | ||
| builder.add(new RunLengthEncodedBlock(INSERT_FROM_UPDATE_BLOCK, positionCount)); | ||
|
|
||
| Page result = new Page(builder.toArray(Block[]::new)); | ||
|
|
||
| int defaultCaseCount = 0; | ||
| for (int position = 0; position < positionCount; position++) { | ||
| if (TINYINT.getLong(operationChannelBlock, position) == DEFAULT_CASE_OPERATION_NUMBER) { | ||
| defaultCaseCount++; | ||
| } | ||
| } | ||
| if (defaultCaseCount == 0) { | ||
| return result; | ||
| } | ||
|
|
||
| int usedCases = 0; | ||
| int[] positions = new int[positionCount - defaultCaseCount]; | ||
| for (int position = 0; position < positionCount; position++) { | ||
| if (TINYINT.getLong(operationChannelBlock, position) != DEFAULT_CASE_OPERATION_NUMBER) { | ||
| positions[usedCases] = position; | ||
| usedCases++; | ||
| } | ||
| } | ||
|
|
||
| checkArgument(usedCases + defaultCaseCount == positionCount, "usedCases (%s) + defaultCaseCount (%s) != positionCount (%s)", usedCases, defaultCaseCount, positionCount); | ||
|
|
||
| return result.getPositions(positions, 0, usedCases); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.