-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unnest array of row to multiple columns
- Loading branch information
Showing
10 changed files
with
272 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
92 changes: 92 additions & 0 deletions
92
presto-main/src/main/java/com/facebook/presto/operator/ArrayOfRowsUnnester.java
This file contains 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,92 @@ | ||
/* | ||
* 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.operator; | ||
|
||
import com.facebook.presto.spi.PageBuilder; | ||
import com.facebook.presto.spi.block.Block; | ||
import com.facebook.presto.spi.block.BlockBuilder; | ||
import com.facebook.presto.spi.block.ColumnarRow; | ||
import com.facebook.presto.spi.type.RowType; | ||
import com.facebook.presto.spi.type.Type; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.List; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class ArrayOfRowsUnnester | ||
implements Unnester | ||
{ | ||
private final List<Type> fieldTypes; | ||
private ColumnarRow columnarRow; | ||
private int position; | ||
private int nonNullPosition; | ||
private int positionCount; | ||
|
||
public ArrayOfRowsUnnester(Type elementType) | ||
{ | ||
requireNonNull(elementType, "elementType is null"); | ||
checkArgument(elementType instanceof RowType, "elementType is not of RowType"); | ||
this.fieldTypes = ImmutableList.copyOf(elementType.getTypeParameters()); | ||
} | ||
|
||
@Override | ||
public int getChannelCount() | ||
{ | ||
return fieldTypes.size(); | ||
} | ||
|
||
@Override | ||
public void appendNext(PageBuilder pageBuilder, int outputChannelOffset) | ||
{ | ||
checkState(columnarRow != null, "columnarRow is null"); | ||
|
||
for (int i = 0; i < fieldTypes.size(); i++) { | ||
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(outputChannelOffset + i); | ||
if (columnarRow.isNull(position)) { | ||
blockBuilder.appendNull(); | ||
} | ||
else { | ||
fieldTypes.get(i).appendTo(columnarRow.getField(i), nonNullPosition, blockBuilder); | ||
} | ||
} | ||
if (!columnarRow.isNull(position)) { | ||
nonNullPosition++; | ||
} | ||
position++; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() | ||
{ | ||
return position < positionCount; | ||
} | ||
|
||
@Override | ||
public void setBlock(Block block) | ||
{ | ||
this.position = 0; | ||
this.nonNullPosition = 0; | ||
if (block == null) { | ||
this.columnarRow = null; | ||
this.positionCount = 0; | ||
} | ||
else { | ||
this.columnarRow = ColumnarRow.toColumnarRow(block); | ||
this.positionCount = block.getPositionCount(); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.