Skip to content

Commit

Permalink
Remove Type compareTo with block and position
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Oct 7, 2020
1 parent 8f8cfdf commit b3308a1
Show file tree
Hide file tree
Showing 181 changed files with 1,325 additions and 971 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Optional;

import static io.prestosql.benchmark.BenchmarkQueryRunner.createLocalQueryRunner;
import static io.prestosql.spi.block.SortOrder.ASC_NULLS_LAST;
import static io.prestosql.spi.connector.SortOrder.ASC_NULLS_LAST;

public class OrderByBenchmark
extends AbstractSimpleOperatorBenchmark
Expand Down Expand Up @@ -58,7 +58,7 @@ protected List<? extends OperatorFactory> createOperatorFactories()
new PagesIndex.TestingFactory(false),
false,
Optional.empty(),
new OrderingCompiler());
new OrderingCompiler(localQueryRunner.getTypeOperators()));

return ImmutableList.of(tableScanOperator, limitOperator, orderByOperator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;

import static io.prestosql.benchmark.BenchmarkQueryRunner.createLocalQueryRunner;
import static io.prestosql.spi.block.SortOrder.ASC_NULLS_LAST;
import static io.prestosql.spi.connector.SortOrder.ASC_NULLS_LAST;

public class Top100Benchmark
extends AbstractSimpleOperatorBenchmark
Expand All @@ -44,7 +44,8 @@ protected List<? extends OperatorFactory> createOperatorFactories()
tableScanTypes,
100,
ImmutableList.of(0),
ImmutableList.of(ASC_NULLS_LAST));
ImmutableList.of(ASC_NULLS_LAST),
localQueryRunner.getTypeOperators());
return ImmutableList.of(tableScanOperator, topNOperator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import io.prestosql.spi.Page;
import io.prestosql.spi.PageSorter;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.SortOrder;
import io.prestosql.spi.connector.ConnectorSession;
import io.prestosql.spi.connector.SortOrder;
import io.prestosql.spi.session.PropertyMetadata;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.TypeManager;
Expand Down Expand Up @@ -551,6 +551,7 @@ else if (insertExistingPartitionsBehavior == InsertExistingPartitionsBehavior.ER
sortFields,
sortOrders,
pageSorter,
typeManager.getTypeOperators(),
OrcFileWriterFactory::createOrcDataSink);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
import io.prestosql.spi.Page;
import io.prestosql.spi.PageSorter;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.SortOrder;
import io.prestosql.spi.connector.SortOrder;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.TypeOperators;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.openjdk.jol.info.ClassLayout;
Expand Down Expand Up @@ -75,6 +76,7 @@ public class SortingFileWriter
private final TempFileSinkFactory tempFileSinkFactory;
private final Queue<TempFile> tempFiles = new PriorityQueue<>(comparing(TempFile::getSize));
private final AtomicLong nextFileId = new AtomicLong();
private final TypeOperators typeOperators;

public SortingFileWriter(
FileSystem fileSystem,
Expand All @@ -86,6 +88,7 @@ public SortingFileWriter(
List<Integer> sortFields,
List<SortOrder> sortOrders,
PageSorter pageSorter,
TypeOperators typeOperators,
TempFileSinkFactory tempFileSinkFactory)
{
checkArgument(maxOpenTempFiles >= 2, "maxOpenTempFiles must be at least two");
Expand All @@ -98,6 +101,7 @@ public SortingFileWriter(
this.outputWriter = requireNonNull(outputWriter, "outputWriter is null");
this.sortBuffer = new SortBuffer(maxMemory, types, sortFields, sortOrders, pageSorter);
this.tempFileSinkFactory = tempFileSinkFactory;
this.typeOperators = requireNonNull(typeOperators, "typeOperators is null");
}

@Override
Expand Down Expand Up @@ -218,7 +222,7 @@ private void mergeFiles(Iterable<TempFile> files, Consumer<Page> consumer)
iterators.add(new TempFileReader(types, dataSource));
}

new MergingPageIterator(iterators, types, sortFields, sortOrders)
new MergingPageIterator(iterators, types, sortFields, sortOrders, typeOperators)
.forEachRemaining(consumer);

for (TempFile tempFile : files) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.SortOrder;
import io.prestosql.spi.connector.SortOrder;

import javax.annotation.concurrent.Immutable;

import java.util.Objects;

import static com.google.common.base.MoreObjects.toStringHelper;
import static io.prestosql.plugin.hive.HiveErrorCode.HIVE_INVALID_METADATA;
import static io.prestosql.spi.block.SortOrder.ASC_NULLS_FIRST;
import static io.prestosql.spi.block.SortOrder.DESC_NULLS_LAST;
import static io.prestosql.spi.connector.SortOrder.ASC_NULLS_FIRST;
import static io.prestosql.spi.connector.SortOrder.DESC_NULLS_LAST;
import static java.util.Objects.requireNonNull;

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,60 @@
import com.google.common.collect.ImmutableList;
import io.prestosql.spi.Page;
import io.prestosql.spi.PageBuilder;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.SortOrder;
import io.prestosql.spi.connector.SortOrder;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.TypeOperators;

import java.lang.invoke.MethodHandle;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Throwables.throwIfUnchecked;
import static com.google.common.collect.Iterators.concat;
import static com.google.common.collect.Iterators.mergeSorted;
import static com.google.common.collect.Iterators.transform;
import static io.prestosql.plugin.hive.util.SortBuffer.appendPositionTo;
import static io.prestosql.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
import static io.prestosql.spi.function.InvocationConvention.InvocationArgumentConvention.BLOCK_POSITION;
import static io.prestosql.spi.function.InvocationConvention.InvocationReturnConvention.FAIL_ON_NULL;
import static io.prestosql.spi.function.InvocationConvention.simpleConvention;
import static java.util.Comparator.naturalOrder;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;

public class MergingPageIterator
extends AbstractIterator<Page>
{
private final List<Type> types;
private final List<Integer> sortFields;
private final List<SortOrder> sortOrders;
private final List<MethodHandle> orderingOperators;
private final PageBuilder pageBuilder;
private final Iterator<PagePosition> pagePositions;

public MergingPageIterator(
Collection<Iterator<Page>> iterators,
List<Type> types,
List<Integer> sortFields,
List<SortOrder> sortOrders)
List<SortOrder> sortOrders,
TypeOperators typeOperators)
{
requireNonNull(sortFields, "sortFields is null");
requireNonNull(sortOrders, "sortOrders is null");
checkArgument(sortFields.size() == sortOrders.size(), "sortFields and sortOrders size must match");

this.types = ImmutableList.copyOf(requireNonNull(types, "types is null"));
this.sortFields = ImmutableList.copyOf(sortFields);
this.sortOrders = ImmutableList.copyOf(sortOrders);

ImmutableList.Builder<MethodHandle> orderingOperators = ImmutableList.builder();
for (int index = 0; index < sortFields.size(); index++) {
Type type = types.get(sortFields.get(index));
SortOrder sortOrder = sortOrders.get(index);
orderingOperators.add(typeOperators.getOrderingOperator(type, sortOrder, simpleConvention(FAIL_ON_NULL, BLOCK_POSITION, BLOCK_POSITION)));
}
this.orderingOperators = orderingOperators.build();

this.pageBuilder = new PageBuilder(types);
this.pagePositions = mergeSorted(
iterators.stream()
Expand Down Expand Up @@ -123,20 +138,25 @@ public void appendTo(PageBuilder pageBuilder)
@Override
public int compareTo(PagePosition other)
{
for (int i = 0; i < sortFields.size(); i++) {
int channel = sortFields.get(i);
SortOrder order = sortOrders.get(i);
Type type = types.get(channel);

Block block = page.getBlock(channel);
Block otherBlock = other.page.getBlock(channel);

int result = order.compareBlockValue(type, block, position, otherBlock, other.position);
if (result != 0) {
return result;
try {
for (int i = 0; i < sortFields.size(); i++) {
int channel = sortFields.get(i);
MethodHandle orderingOperator = orderingOperators.get(i);

Block block = page.getBlock(channel);
Block otherBlock = other.page.getBlock(channel);

int result = (int) orderingOperator.invokeExact(block, position, otherBlock, other.position);
if (result != 0) {
return result;
}
}
return 0;
}
catch (Throwable throwable) {
throwIfUnchecked(throwable);
throw new PrestoException(GENERIC_INTERNAL_ERROR, throwable);
}
return 0;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import io.prestosql.spi.PageSorter;
import io.prestosql.spi.block.Block;
import io.prestosql.spi.block.BlockBuilder;
import io.prestosql.spi.block.SortOrder;
import io.prestosql.spi.connector.SortOrder;
import io.prestosql.spi.type.Type;
import org.openjdk.jol.info.ClassLayout;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.prestosql.spi.security.PrincipalType;
import io.prestosql.spi.type.RowType;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.TypeOperators;
import io.prestosql.sql.planner.iterative.rule.PushPredicateIntoTableScan;
import io.prestosql.sql.planner.iterative.rule.PushProjectionIntoTableScan;
import io.prestosql.sql.planner.iterative.rule.test.BaseRuleTest;
Expand Down Expand Up @@ -187,7 +188,7 @@ public void testPredicatePushdown()
String tableName = "predicate_test";
tester().getQueryRunner().execute(format("CREATE TABLE %s (a, b) AS SELECT 5, 6", tableName));

PushPredicateIntoTableScan pushPredicateIntoTableScan = new PushPredicateIntoTableScan(tester().getMetadata(), tester().getTypeAnalyzer());
PushPredicateIntoTableScan pushPredicateIntoTableScan = new PushPredicateIntoTableScan(tester().getMetadata(), new TypeOperators(), tester().getTypeAnalyzer());

HiveTableHandle hiveTable = new HiveTableHandle(SCHEMA_NAME, tableName, ImmutableMap.of(), ImmutableList.of(), Optional.empty());
TableHandle table = new TableHandle(new CatalogName(HIVE_CATALOG_NAME), hiveTable, new HiveTransactionHandle(), Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
import com.google.common.collect.ImmutableList;
import io.prestosql.spi.Page;
import io.prestosql.spi.PageBuilder;
import io.prestosql.spi.block.SortOrder;
import io.prestosql.spi.connector.SortOrder;
import io.prestosql.spi.type.Type;
import io.prestosql.spi.type.TypeOperators;
import org.testng.annotations.Test;

import java.util.ArrayList;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void testMerging()
List<Iterator<Page>> pages = pageLists.stream()
.map(List::iterator)
.collect(toList());
Iterator<Page> iterator = new MergingPageIterator(pages, types, sortIndexes, sortOrders);
Iterator<Page> iterator = new MergingPageIterator(pages, types, sortIndexes, sortOrders, new TypeOperators());

List<Long> values = new ArrayList<>();
while (iterator.hasNext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import io.prestosql.operator.PagesIndex;
import io.prestosql.spi.Page;
import io.prestosql.spi.PageSorter;
import io.prestosql.spi.block.SortOrder;
import io.prestosql.spi.connector.SortOrder;
import io.prestosql.spi.type.Type;

import javax.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import io.prestosql.Session;
import io.prestosql.matching.Pattern;
import io.prestosql.spi.block.SortOrder;
import io.prestosql.spi.connector.SortOrder;
import io.prestosql.sql.planner.Symbol;
import io.prestosql.sql.planner.TypeProvider;
import io.prestosql.sql.planner.iterative.Lookup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.prestosql.server.protocol.Slug;
import io.prestosql.spi.PrestoException;
import io.prestosql.spi.QueryId;
import io.prestosql.spi.type.TypeOperators;
import io.prestosql.split.SplitManager;
import io.prestosql.split.SplitSource;
import io.prestosql.sql.analyzer.Analysis;
Expand Down Expand Up @@ -104,6 +105,7 @@ public class SqlQueryExecution
private final QueryStateMachine stateMachine;
private final Slug slug;
private final Metadata metadata;
private final TypeOperators typeOperators;
private final SqlParser sqlParser;
private final SplitManager splitManager;
private final NodePartitioningManager nodePartitioningManager;
Expand Down Expand Up @@ -131,7 +133,7 @@ private SqlQueryExecution(
QueryStateMachine stateMachine,
Slug slug,
Metadata metadata,
AccessControl accessControl,
TypeOperators typeOperators, AccessControl accessControl,
SqlParser sqlParser,
SplitManager splitManager,
NodePartitioningManager nodePartitioningManager,
Expand All @@ -155,6 +157,7 @@ private SqlQueryExecution(
try (SetThreadName ignored = new SetThreadName("Query-%s", stateMachine.getQueryId())) {
this.slug = requireNonNull(slug, "slug is null");
this.metadata = requireNonNull(metadata, "metadata is null");
this.typeOperators = requireNonNull(typeOperators, "typeOperators is null");
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null");
this.splitManager = requireNonNull(splitManager, "splitManager is null");
this.nodePartitioningManager = requireNonNull(nodePartitioningManager, "nodePartitioningManager is null");
Expand Down Expand Up @@ -429,7 +432,15 @@ private PlanRoot doPlanQuery()
{
// plan query
PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator();
LogicalPlanner logicalPlanner = new LogicalPlanner(stateMachine.getSession(), planOptimizers, idAllocator, metadata, new TypeAnalyzer(sqlParser, metadata), statsCalculator, costCalculator, stateMachine.getWarningCollector());
LogicalPlanner logicalPlanner = new LogicalPlanner(stateMachine.getSession(),
planOptimizers,
idAllocator,
metadata,
typeOperators,
new TypeAnalyzer(sqlParser, metadata),
statsCalculator,
costCalculator,
stateMachine.getWarningCollector());
Plan plan = logicalPlanner.plan(analysis);
queryPlan.set(plan);

Expand Down Expand Up @@ -667,6 +678,7 @@ public static class SqlQueryExecutionFactory
private final SplitSchedulerStats schedulerStats;
private final int scheduleSplitBatchSize;
private final Metadata metadata;
private final TypeOperators typeOperators;
private final AccessControl accessControl;
private final SqlParser sqlParser;
private final SplitManager splitManager;
Expand All @@ -689,6 +701,7 @@ public static class SqlQueryExecutionFactory
SqlQueryExecutionFactory(
QueryManagerConfig config,
Metadata metadata,
TypeOperators typeOperators,
AccessControl accessControl,
SqlParser sqlParser,
SplitManager splitManager,
Expand All @@ -712,6 +725,7 @@ public static class SqlQueryExecutionFactory
this.schedulerStats = requireNonNull(schedulerStats, "schedulerStats is null");
this.scheduleSplitBatchSize = config.getScheduleSplitBatchSize();
this.metadata = requireNonNull(metadata, "metadata is null");
this.typeOperators = requireNonNull(typeOperators, "typeOperators is null");
this.accessControl = requireNonNull(accessControl, "accessControl is null");
this.sqlParser = requireNonNull(sqlParser, "sqlParser is null");
this.splitManager = requireNonNull(splitManager, "splitManager is null");
Expand Down Expand Up @@ -747,6 +761,7 @@ public QueryExecution createQueryExecution(
stateMachine,
slug,
metadata,
typeOperators,
accessControl,
sqlParser,
splitManager,
Expand Down
Loading

0 comments on commit b3308a1

Please sign in to comment.