Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.operator;

import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.errorprone.annotations.ThreadSafe;
import io.airlift.slice.Slice;
Expand All @@ -29,14 +30,18 @@
import io.trino.spi.catalog.CatalogName;
import io.trino.spi.connector.CatalogVersion;
import io.trino.spi.exchange.ExchangeId;
import io.trino.spi.type.Type;
import io.trino.split.RemoteSplit;
import io.trino.sql.planner.plan.PlanNodeId;
import io.trino.util.Ciphers;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static io.trino.SystemSessionProperties.isSourcePagesValidationEnabled;
import static io.trino.connector.CatalogHandle.createRootCatalogHandle;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
Expand All @@ -51,6 +56,7 @@ public static class ExchangeOperatorFactory
{
private final int operatorId;
private final PlanNodeId sourceId;
private final List<Type> outputTypes;
private final DirectExchangeClientSupplier directExchangeClientSupplier;
private final PagesSerdeFactory serdeFactory;
private final RetryPolicy retryPolicy;
Expand All @@ -64,13 +70,15 @@ public static class ExchangeOperatorFactory
public ExchangeOperatorFactory(
int operatorId,
PlanNodeId sourceId,
List<Type> outputTypes,
DirectExchangeClientSupplier directExchangeClientSupplier,
PagesSerdeFactory serdeFactory,
RetryPolicy retryPolicy,
ExchangeManagerRegistry exchangeManagerRegistry)
{
this.operatorId = operatorId;
this.sourceId = requireNonNull(sourceId, "sourceId is null");
this.outputTypes = ImmutableList.copyOf(requireNonNull(outputTypes, "outputTypes is null"));
this.directExchangeClientSupplier = requireNonNull(directExchangeClientSupplier, "directExchangeClientSupplier is null");
this.serdeFactory = requireNonNull(serdeFactory, "serdeFactory is null");
this.retryPolicy = requireNonNull(retryPolicy, "retryPolicy is null");
Expand Down Expand Up @@ -115,6 +123,10 @@ public SourceOperator createOperator(DriverContext driverContext)
noMoreSplitsTracker,
operatorInstanceId);
noMoreSplitsTracker.operatorAdded(operatorInstanceId);

if (isSourcePagesValidationEnabled(taskContext.getSession())) {
return new OutputValidatingSourceOperator(exchangeOperator, outputTypes);
}
return exchangeOperator;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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 com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListenableFuture;
import io.trino.metadata.Split;
import io.trino.spi.Page;
import io.trino.spi.type.Type;
import io.trino.split.PageValidations;
import io.trino.sql.planner.plan.PlanNodeId;

import java.util.List;
import java.util.function.Supplier;

import static java.util.Objects.requireNonNull;

public class OutputValidatingSourceOperator
implements SourceOperator
{
private final SourceOperator delegate;
private final List<Type> outputTypes;
private final Supplier<String> debugContextSupplier;

public OutputValidatingSourceOperator(SourceOperator delegate, List<Type> outputTypes)
{
this.delegate = requireNonNull(delegate, "delegate is null");
this.outputTypes = ImmutableList.copyOf(requireNonNull(outputTypes, "outputTypes is null"));
this.debugContextSupplier = () -> "operator=%s, taskId/operatorId=%s/%s,".formatted(
delegate.getClass().getSimpleName(),
delegate.getOperatorContext().getDriverContext().getTaskId(),
delegate.getOperatorContext().getOperatorId());
}

@Override
public PlanNodeId getSourceId()
{
return delegate.getSourceId();
}

@Override
public void addSplit(Split split)
{
delegate.addSplit(split);
}

@Override
public void noMoreSplits()
{
delegate.noMoreSplits();
}

@Override
public OperatorContext getOperatorContext()
{
return delegate.getOperatorContext();
}

@Override
public ListenableFuture<Void> isBlocked()
{
return delegate.isBlocked();
}

@Override
public boolean needsInput()
{
return delegate.needsInput();
}

@Override
public void addInput(Page page)
{
delegate.addInput(page);
}

@Override
public Page getOutput()
{
Page page = delegate.getOutput();
if (page != null) {
PageValidations.validateOutputPageTypes(page, outputTypes, debugContextSupplier);
}
return page;
}

@Override
public ListenableFuture<Void> startMemoryRevoke()
{
return delegate.startMemoryRevoke();
}

@Override
public void finishMemoryRevoke()
{
delegate.finishMemoryRevoke();
}

@Override
public void finish()
{
delegate.finish();
}

@Override
public boolean isFinished()
{
return delegate.isFinished();
}

@Override
public void close()
throws Exception
{
delegate.close();
}
}
44 changes: 44 additions & 0 deletions core/trino-main/src/main/java/io/trino/split/PageValidations.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.trino.split;

import com.google.common.base.Joiner;
import io.trino.spi.Page;
import io.trino.spi.TrinoException;
import io.trino.spi.block.Block;
import io.trino.spi.block.ValueBlock;
Expand Down Expand Up @@ -64,6 +65,40 @@ public static void validateOutputPageTypes(SourcePage page, List<Type> expectedT
}
}

public static void validateOutputPageTypes(Page page, List<Type> expectedTypes, Supplier<String> debugContextSupplier)
{
if (page.getChannelCount() != expectedTypes.size()) {
throw new TrinoException(
GENERIC_INTERNAL_ERROR,
"Invalid number of channels; got %s expected %s; context=%s; blocks=%s; types=%s".formatted(
page.getChannelCount(),
expectedTypes.size(),
debugContextSupplier.get(),
blocksDebugInfo(page),
expectedTypes));
}

List<String> mismatches = null;
for (int channel = 0; channel < expectedTypes.size(); channel++) {
Type type = expectedTypes.get(channel);
Block block = page.getBlock(channel);
if (!isBlockValidForType(block, type)) {
if (mismatches == null) {
mismatches = new ArrayList<>();
}
mismatches.add("Bad block %s for channel %s of type %s".formatted(blockDebugInfo(block), channel, type));
}
}
if (mismatches != null) {
throw new TrinoException(GENERIC_INTERNAL_ERROR,
"Bad block types found for context %s; blocks=%s; types=%s; mismatches=%s".formatted(
debugContextSupplier.get(),
blocksDebugInfo(page),
expectedTypes,
mismatches));
}
}

private static boolean isBlockValidForType(Block block, Type type)
{
ValueBlock underlyingValueBlock = block.getUnderlyingValueBlock();
Expand All @@ -79,6 +114,15 @@ private static String blocksDebugInfo(SourcePage page)
return Joiner.on(",").join(debugInfos);
}

private static String blocksDebugInfo(Page page)
{
ArrayList<String> debugInfos = new ArrayList<>();
for (int i = 0; i < page.getChannelCount(); i++) {
debugInfos.add(blockDebugInfo(page.getBlock(i)));
}
return Joiner.on(",").join(debugInfos);
}

private static String blockDebugInfo(Block block)
{
ValueBlock underlyingValueBlock = block.getUnderlyingValueBlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,9 +949,11 @@ private PhysicalOperation createRemoteSource(RemoteSourceNode node, LocalExecuti
context.setDriverInstanceCount(getTaskConcurrency(session));
}

List<Type> types = node.getOutputSymbols().stream().map(Symbol::type).collect(toImmutableList());
OperatorFactory operatorFactory = new ExchangeOperatorFactory(
context.getNextOperatorId(),
node.getId(),
types,
directExchangeClientSupplier,
createExchangePagesSerdeFactory(plannerContext.getBlockEncodingSerde(), session),
node.getRetryPolicy(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ private SourceOperator createExchangeOperator()
ExchangeOperatorFactory operatorFactory = new ExchangeOperatorFactory(
0,
new PlanNodeId("test"),
TYPES,
directExchangeClientSupplier,
SERDE_FACTORY,
RetryPolicy.NONE,
Expand Down