Skip to content

Commit

Permalink
Fix bug in selective page source when row IDs are needed but row numb…
Browse files Browse the repository at this point in the history
…ers aren't
  • Loading branch information
elharo committed Jul 3, 2024
1 parent fd57488 commit 3a31ddc
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,18 @@ public boolean isFinished()
public Page getNextPage()
{
try {
Page page = recordReader.getNextPage();
Page page;
if (supplyRowIDs) {
page = recordReader.getNextPage(true);
page = fillInRowIDs(page);
}
else {
page = recordReader.getNextPage();
}

if (page == null) {
close();
}
else if (supplyRowIDs) { // If we need a row ID block, synthesize it here.
page = fillInRowIDs(page);
}
return page;
}
catch (InvalidFunctionArgumentException e) {
Expand Down Expand Up @@ -147,7 +152,7 @@ private Page fillInRowIDs(Page page)
// figure out which block is the row ID and replace it
page = page.replaceColumn(rowIDColumnIndex.getAsInt(), rowIDs);

if (!appendRowNumberEnabled) {
if (!this.appendRowNumberEnabled) {
// remove the row number block now that the row IDs have been constructed unless it was also requested
page = page.dropColumn(page.getChannelCount() - 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,12 @@ public long getReadPositions()

public Page getNextPage()
throws IOException
{
return getNextPage(this.appendRowNumber);
}

public Page getNextPage(boolean withRowNumbers)
throws IOException
{
if (constantFilterIsFalse) {
return null;
Expand Down Expand Up @@ -721,7 +727,7 @@ public Page getNextPage()
}
}

Block[] blocks = new Block[ appendRowNumber ? outputColumns.size() + 1 : outputColumns.size()];
Block[] blocks = new Block[ withRowNumbers ? outputColumns.size() + 1 : outputColumns.size()];
for (int i = 0; i < outputColumns.size(); i++) {
int columnIndex = outputColumns.get(i);
if (constantValues[columnIndex] != null) {
Expand All @@ -742,7 +748,7 @@ else if (!hasAnyFilter(columnIndex)) {
}
}

if (appendRowNumber) {
if (withRowNumbers) {
blocks[outputColumns.size()] = createRowNumbersBlock(positionsToRead, positionCount, this.getFilePosition());
}
Page page = new Page(positionCount, blocks);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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.orc;

import com.facebook.presto.common.Page;
import com.facebook.presto.common.block.Block;
import com.facebook.presto.common.type.Type;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.testng.annotations.Test;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.IntStream;

import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.orc.NoOpOrcWriterStats.NOOP_WRITER_STATS;
import static com.facebook.presto.orc.OrcTester.Format.DWRF;
import static com.facebook.presto.orc.OrcTester.Format.ORC_12;
import static com.facebook.presto.orc.OrcTester.createCustomOrcSelectiveRecordReader;
import static com.facebook.presto.orc.OrcTester.writeOrcColumnsPresto;
import static com.facebook.presto.orc.TestingOrcPredicate.createOrcPredicate;
import static com.facebook.presto.orc.metadata.CompressionKind.NONE;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static org.testng.Assert.assertEquals;

public class TestOrcSelectiveRecordReader
{
@Test
public void testGetNextPage_withRowNumbers()
throws Exception
{
List<Type> types = ImmutableList.of(VARCHAR);
List<List<?>> values = ImmutableList.of(ImmutableList.of("a", ""));

TempFile tempFile = new TempFile();
writeOrcColumnsPresto(tempFile.getFile(), ORC_12, NONE, Optional.empty(), types, values, NOOP_WRITER_STATS);

OrcPredicate orcPredicate = createOrcPredicate(types, values, DWRF, false);
Map<Integer, Type> includedColumns = IntStream.range(0, types.size())
.boxed()
.collect(toImmutableMap(Function.identity(), types::get));
List<Integer> outputColumns = IntStream.range(0, types.size())
.boxed()
.collect(toImmutableList());
OrcAggregatedMemoryContext systemMemoryUsage = new TestingHiveOrcAggregatedMemoryContext();
try (OrcSelectiveRecordReader recordReader = createCustomOrcSelectiveRecordReader(
tempFile.getFile(),
ORC_12.getOrcEncoding(),
orcPredicate,
types,
1,
ImmutableMap.of(),
ImmutableList.of(),
ImmutableMap.of(),
OrcTester.OrcReaderSettings.builder().build().getRequiredSubfields(),
ImmutableMap.of(),
ImmutableMap.of(),
includedColumns,
outputColumns,
false,
systemMemoryUsage,
false)) {
assertEquals(recordReader.getReaderPosition(), 0);
assertEquals(recordReader.getFilePosition(), 0);

Page page = recordReader.getNextPage(true);
// One VARCHAR column and one row number column
assertEquals(2, page.getChannelCount());
Block block = page.getBlock(1);
assertEquals(block.getPositionCount(), 1);
assertEquals(block.getLong(0), 0, "First row number is not zero");
}
}

@Test
public void testGetNextPage_withoutRowNumbers()
throws Exception
{
List<Type> types = ImmutableList.of(VARCHAR);
List<List<?>> values = ImmutableList.of(ImmutableList.of("a", ""));

TempFile tempFile = new TempFile();
writeOrcColumnsPresto(tempFile.getFile(), ORC_12, NONE, Optional.empty(), types, values, NOOP_WRITER_STATS);

OrcPredicate orcPredicate = createOrcPredicate(types, values, DWRF, false);
Map<Integer, Type> includedColumns = IntStream.range(0, types.size())
.boxed()
.collect(toImmutableMap(Function.identity(), types::get));
List<Integer> outputColumns = IntStream.range(0, types.size())
.boxed()
.collect(toImmutableList());
OrcAggregatedMemoryContext systemMemoryUsage = new TestingHiveOrcAggregatedMemoryContext();
try (OrcSelectiveRecordReader recordReader = createCustomOrcSelectiveRecordReader(
tempFile.getFile(),
ORC_12.getOrcEncoding(),
orcPredicate,
types,
1,
ImmutableMap.of(),
ImmutableList.of(),
ImmutableMap.of(),
OrcTester.OrcReaderSettings.builder().build().getRequiredSubfields(),
ImmutableMap.of(),
ImmutableMap.of(),
includedColumns,
outputColumns,
false,
systemMemoryUsage,
false)) {
assertEquals(recordReader.getReaderPosition(), 0);
assertEquals(recordReader.getFilePosition(), 0);

Page page = recordReader.getNextPage();
// One VARCHAR column, no row number column
assertEquals(1, page.getChannelCount());
Block block = page.getBlock(0);
assertEquals(block.getPositionCount(), 1);
}
}
}

0 comments on commit 3a31ddc

Please sign in to comment.