Skip to content
Merged
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 @@ -16,6 +16,7 @@
import com.facebook.airlift.json.JsonCodec;
import com.facebook.airlift.json.JsonCodecFactory;
import com.facebook.airlift.json.ObjectMapperProvider;
import com.facebook.airlift.log.Logger;
import com.facebook.presto.Session;
import com.facebook.presto.block.BlockEncodingManager;
import com.facebook.presto.common.CatalogSchemaName;
Expand Down Expand Up @@ -92,6 +93,7 @@
import java.util.concurrent.ConcurrentMap;

import static com.facebook.airlift.concurrent.MoreFutures.toListenableFuture;
import static com.facebook.presto.SystemSessionProperties.isIgnoreStatsCalculatorFailures;
import static com.facebook.presto.common.function.OperatorType.BETWEEN;
import static com.facebook.presto.common.function.OperatorType.EQUAL;
import static com.facebook.presto.common.function.OperatorType.GREATER_THAN;
Expand Down Expand Up @@ -119,6 +121,8 @@
public class MetadataManager
implements Metadata
{
private static final Logger log = Logger.get(MetadataManager.class);

private final FunctionManager functions;
private final ProcedureRegistry procedures;
private final TypeManager typeManager;
Expand Down Expand Up @@ -502,9 +506,18 @@ public TableMetadata getTableMetadata(Session session, TableHandle tableHandle)
@Override
public TableStatistics getTableStatistics(Session session, TableHandle tableHandle, List<ColumnHandle> columnHandles, Constraint<ColumnHandle> constraint)
{
ConnectorId connectorId = tableHandle.getConnectorId();
ConnectorMetadata metadata = getMetadata(session, connectorId);
return metadata.getTableStatistics(session.toConnectorSession(connectorId), tableHandle.getConnectorHandle(), tableHandle.getLayout(), columnHandles, constraint);
try {
ConnectorId connectorId = tableHandle.getConnectorId();
ConnectorMetadata metadata = getMetadata(session, connectorId);
return metadata.getTableStatistics(session.toConnectorSession(connectorId), tableHandle.getConnectorHandle(), tableHandle.getLayout(), columnHandles, constraint);
}
catch (RuntimeException e) {
if (isIgnoreStatsCalculatorFailures(session)) {
log.error(e, "Error occurred when computing stats for query %s", session.getQueryId());
return TableStatistics.empty();
}
throw e;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.facebook.presto.spi.connector.ConnectorRecordSetProvider;
import com.facebook.presto.spi.connector.ConnectorSplitManager;
import com.facebook.presto.spi.connector.ConnectorTransactionHandle;
import com.facebook.presto.spi.statistics.TableStatistics;
import com.facebook.presto.spi.transaction.IsolationLevel;
import com.facebook.presto.tpch.TpchColumnHandle;
import com.facebook.presto.tpch.TpchHandleResolver;
Expand All @@ -47,6 +48,7 @@
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.IntStream;

import static com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType;
Expand All @@ -61,17 +63,20 @@ public class MockConnectorFactory
private final BiFunction<ConnectorSession, String, List<SchemaTableName>> listTables;
private final BiFunction<ConnectorSession, SchemaTablePrefix, Map<SchemaTableName, ConnectorViewDefinition>> getViews;
private final BiFunction<ConnectorSession, ConnectorTableHandle, Map<String, TpchColumnHandle>> getColumnHandles;
private final Supplier<TableStatistics> getTableStatistics;

private MockConnectorFactory(
Function<ConnectorSession, List<String>> listSchemaNames,
BiFunction<ConnectorSession, String, List<SchemaTableName>> listTables,
BiFunction<ConnectorSession, SchemaTablePrefix, Map<SchemaTableName, ConnectorViewDefinition>> getViews,
BiFunction<ConnectorSession, ConnectorTableHandle, Map<String, TpchColumnHandle>> getColumnHandles)
BiFunction<ConnectorSession, ConnectorTableHandle, Map<String, TpchColumnHandle>> getColumnHandles,
Supplier<TableStatistics> getTableStatistics)
{
this.listSchemaNames = requireNonNull(listSchemaNames, "listSchemaNames is null");
this.listTables = requireNonNull(listTables, "listTables is null");
this.getViews = requireNonNull(getViews, "getViews is null");
this.getColumnHandles = requireNonNull(getColumnHandles, "getColumnHandles is null");
this.getTableStatistics = requireNonNull(getTableStatistics, "getTableStatistics is null");
}

@Override
Expand All @@ -89,7 +94,7 @@ public ConnectorHandleResolver getHandleResolver()
@Override
public Connector create(String catalogName, Map<String, String> config, ConnectorContext context)
{
return new MockConnector(context, listSchemaNames, listTables, getViews, getColumnHandles);
return new MockConnector(context, listSchemaNames, listTables, getViews, getColumnHandles, getTableStatistics);
}

public static Builder builder()
Expand All @@ -105,19 +110,22 @@ private static class MockConnector
private final BiFunction<ConnectorSession, String, List<SchemaTableName>> listTables;
private final BiFunction<ConnectorSession, SchemaTablePrefix, Map<SchemaTableName, ConnectorViewDefinition>> getViews;
private final BiFunction<ConnectorSession, ConnectorTableHandle, Map<String, TpchColumnHandle>> getColumnHandles;
private final Supplier<TableStatistics> getTableStatistics;

public MockConnector(
ConnectorContext context,
Function<ConnectorSession, List<String>> listSchemaNames,
BiFunction<ConnectorSession, String, List<SchemaTableName>> listTables,
BiFunction<ConnectorSession, SchemaTablePrefix, Map<SchemaTableName, ConnectorViewDefinition>> getViews,
BiFunction<ConnectorSession, ConnectorTableHandle, Map<String, TpchColumnHandle>> getColumnHandles)
BiFunction<ConnectorSession, ConnectorTableHandle, Map<String, TpchColumnHandle>> getColumnHandles,
Supplier<TableStatistics> getTableStatistics)
{
this.context = requireNonNull(context, "context is null");
this.listSchemaNames = requireNonNull(listSchemaNames, "listSchemaNames is null");
this.listTables = requireNonNull(listTables, "listTables is null");
this.getViews = requireNonNull(getViews, "getViews is null");
this.getColumnHandles = requireNonNull(getColumnHandles, "getColumnHandles is null");
this.getTableStatistics = requireNonNull(getTableStatistics, "getTableStatistics is null");
}

@Override
Expand Down Expand Up @@ -211,6 +219,12 @@ public Map<SchemaTableName, ConnectorViewDefinition> getViews(ConnectorSession s
{
return getViews.apply(session, prefix);
}

@Override
public TableStatistics getTableStatistics(ConnectorSession session, ConnectorTableHandle tableHandle, Optional<ConnectorTableLayoutHandle> tableLayoutHandle, List<ColumnHandle> columnHandles, Constraint<ColumnHandle> constraint)
{
return getTableStatistics.get();
}
}
}

Expand All @@ -220,6 +234,7 @@ public static final class Builder
private BiFunction<ConnectorSession, String, List<SchemaTableName>> listTables = (session, schemaName) -> ImmutableList.of();
private BiFunction<ConnectorSession, SchemaTablePrefix, Map<SchemaTableName, ConnectorViewDefinition>> getViews = (session, schemaTablePrefix) -> ImmutableMap.of();
private BiFunction<ConnectorSession, ConnectorTableHandle, Map<String, TpchColumnHandle>> getColumnHandles = (session, tableHandle) -> notSupported();
private Supplier<TableStatistics> getTableStatistics = TableStatistics::empty;

public Builder withListSchemaNames(Function<ConnectorSession, List<String>> listSchemaNames)
{
Expand All @@ -245,9 +260,15 @@ public Builder withGetColumnHandles(BiFunction<ConnectorSession, ConnectorTableH
return this;
}

public Builder withGetTableStatistics(Supplier<TableStatistics> getTableStatitics)
{
this.getTableStatistics = requireNonNull(getTableStatitics, "getTableStatistics is null");
return this;
}

public MockConnectorFactory build()
{
return new MockConnectorFactory(listSchemaNames, listTables, getViews, getColumnHandles);
return new MockConnectorFactory(listSchemaNames, listTables, getViews, getColumnHandles, getTableStatistics);
}

private static <T> T notSupported()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
*/
package com.facebook.presto.tests;

import com.facebook.presto.Session;
import com.facebook.presto.connector.MockConnectorFactory;
import com.facebook.presto.dispatcher.DispatchManager;
import com.facebook.presto.execution.TestingSessionContext;
import com.facebook.presto.metadata.MetadataManager;
import com.facebook.presto.server.BasicQueryInfo;
import com.facebook.presto.spi.ConnectorId;
import com.facebook.presto.spi.ConnectorTableHandle;
import com.facebook.presto.spi.Plugin;
import com.facebook.presto.spi.QueryId;
import com.facebook.presto.spi.TableHandle;
import com.facebook.presto.spi.connector.ConnectorFactory;
import com.facebook.presto.spi.statistics.TableStatistics;
import com.facebook.presto.testing.TestingTransactionHandle;
import com.facebook.presto.tests.tpch.TpchQueryRunnerBuilder;
import com.facebook.presto.transaction.TransactionBuilder;
import com.google.common.collect.ImmutableList;
Expand All @@ -31,10 +37,14 @@
import org.testng.annotations.Test;

import java.util.List;
import java.util.Optional;

import static com.facebook.presto.SessionTestUtils.TEST_SESSION;
import static com.facebook.presto.SystemSessionProperties.IGNORE_STATS_CALCULATOR_FAILURES;
import static com.facebook.presto.execution.QueryState.FAILED;
import static com.facebook.presto.execution.QueryState.RUNNING;
import static com.facebook.presto.spi.Constraint.alwaysTrue;
import static com.facebook.presto.testing.TestingSession.testSessionBuilder;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

Expand Down Expand Up @@ -69,6 +79,9 @@ public Iterable<ConnectorFactory> getConnectorFactories()
.withGetColumnHandles((session, tableHandle) -> {
throw new UnsupportedOperationException();
})
.withGetTableStatistics(() -> {
throw new UnsupportedOperationException();
})
.build();
return ImmutableList.of(connectorFactory);
}
Expand Down Expand Up @@ -152,4 +165,36 @@ public void testUpperCaseSchemaIsChangedToLowerCase()
return null;
});
}

@Test
public void testGetTableStatisticsDoesNotThrow()
{
Session session = testSessionBuilder()
.setSystemProperty("ignore_stats_calculator_failures", "true")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps, use com.facebook.presto.SystemSessionProperties#IGNORE_STATS_CALCULATOR_FAILURES constant

.build();
TableHandle tableHandle = new TableHandle(new ConnectorId("upper_case_schema_catalog"), new ConnectorTableHandle() {}, TestingTransactionHandle.create(), Optional.empty());
TransactionBuilder.transaction(queryRunner.getTransactionManager(), queryRunner.getAccessControl())
.execute(
session,
transactionSession -> {
queryRunner.getMetadata().getCatalogHandle(transactionSession, "upper_case_schema_catalog");
assertEquals(queryRunner.getMetadata().getTableStatistics(transactionSession, tableHandle, ImmutableList.of(), alwaysTrue()), TableStatistics.empty());
});
}

@Test(expectedExceptions = UnsupportedOperationException.class)
public void testGetTableStatisticsThrows()
{
Session session = testSessionBuilder()
.setSystemProperty(IGNORE_STATS_CALCULATOR_FAILURES, "false")
.build();
TableHandle tableHandle = new TableHandle(new ConnectorId("upper_case_schema_catalog"), new ConnectorTableHandle() {}, TestingTransactionHandle.create(), Optional.empty());
TransactionBuilder.transaction(queryRunner.getTransactionManager(), queryRunner.getAccessControl())
.execute(
session,
transactionSession -> {
queryRunner.getMetadata().getCatalogHandle(transactionSession, "upper_case_schema_catalog");
assertEquals(queryRunner.getMetadata().getTableStatistics(transactionSession, tableHandle, ImmutableList.of(), alwaysTrue()), TableStatistics.empty());
});
}
}