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 @@ -21,11 +21,6 @@
import io.trino.execution.warnings.WarningCollector;
import io.trino.metadata.TableHandle;
import io.trino.metadata.TableMetadata;
import io.trino.plugin.hive.RecordingMetastoreConfig;
import io.trino.plugin.hive.TestingHiveConnectorFactory;
import io.trino.plugin.hive.metastore.UnimplementedHiveMetastore;
import io.trino.plugin.hive.metastore.recording.HiveMetastoreRecording;
import io.trino.plugin.hive.metastore.recording.RecordingHiveMetastore;
import io.trino.spi.connector.ConnectorFactory;
import io.trino.sql.planner.OptimizerConfig.JoinDistributionType;
import io.trino.sql.planner.OptimizerConfig.JoinReorderingStrategy;
Expand All @@ -40,25 +35,23 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Verify.verify;
import static com.google.common.collect.MoreCollectors.onlyElement;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.io.Files.createParentDirs;
import static com.google.common.io.Files.write;
import static com.google.common.io.Resources.getResource;
import static io.trino.Session.SessionBuilder;
import static io.trino.SystemSessionProperties.JOIN_DISTRIBUTION_TYPE;
import static io.trino.SystemSessionProperties.JOIN_REORDERING_STRATEGY;
import static io.trino.plugin.hive.metastore.recording.TestRecordingHiveMetastore.createJsonCodec;
import static io.trino.sql.planner.LogicalPlanner.Stage.OPTIMIZED_AND_VALIDATED;
import static io.trino.sql.planner.plan.JoinNode.DistributionType.REPLICATED;
import static io.trino.sql.planner.plan.JoinNode.Type.INNER;
Expand All @@ -72,15 +65,26 @@
import static java.util.stream.Collectors.joining;
import static org.testng.Assert.assertEquals;

public abstract class AbstractHiveCostBasedPlanTest
public abstract class BaseCostBasedPlanTest
extends BasePlanTest
{
public static final List<String> TPCH_SQL_FILES = IntStream.rangeClosed(1, 22)
.mapToObj(i -> format("q%02d", i))
.map(queryId -> format("/sql/presto/tpch/%s.sql", queryId))
.collect(toImmutableList());

public static final List<String> TPCDS_SQL_FILES = IntStream.range(1, 100)
.mapToObj(i -> format("q%02d", i))
.map(queryId -> format("/sql/presto/tpcds/%s.sql", queryId))
.collect(toImmutableList());

private static final String CATALOG_NAME = "local";

@Override
protected LocalQueryRunner createLocalQueryRunner()
{
String catalog = "local";
SessionBuilder sessionBuilder = testSessionBuilder()
.setCatalog(catalog)
.setCatalog(CATALOG_NAME)
.setSchema(getSchema())
.setSystemProperty("task_concurrency", "1") // these tests don't handle exchanges from local parallel
.setSystemProperty(JOIN_REORDERING_STRATEGY, JoinReorderingStrategy.AUTOMATIC.name())
Expand All @@ -89,56 +93,15 @@ protected LocalQueryRunner createLocalQueryRunner()
.withNodeCountForStats(8)
.build();
queryRunner.createCatalog(
catalog,
CATALOG_NAME,
createConnectorFactory(),
ImmutableMap.of());
return queryRunner;
}

protected ConnectorFactory createConnectorFactory()
{
RecordingMetastoreConfig recordingConfig = new RecordingMetastoreConfig()
.setRecordingPath(getRecordingPath())
.setReplay(true);
try {
// The RecordingHiveMetastore loads the metadata files generated through HiveMetadataRecorder
// which essentially helps to generate the optimal query plans for validation purposes. These files
// contains all the metadata including statistics.
RecordingHiveMetastore metastore = new RecordingHiveMetastore(
new UnimplementedHiveMetastore(),
new HiveMetastoreRecording(recordingConfig, createJsonCodec()));
return new TestingHiveConnectorFactory(metastore);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private String getSchema()
{
String fileName = Paths.get(getRecordingPath()).getFileName().toString();
return fileName.split("\\.")[0];
}

private String getRecordingPath()
{
URL resource = getClass().getResource(getMetadataDir());
if (resource == null) {
throw new RuntimeException("Hive metadata directory doesn't exist: " + getMetadataDir());
}

File[] files = new File(resource.getPath()).listFiles();
if (files == null) {
throw new RuntimeException("Hive metadata recording file doesn't exist in directory: " + getMetadataDir());
}

return Arrays.stream(files)
.filter(f -> !f.isDirectory())
.collect(onlyElement())
.getPath();
}
protected abstract ConnectorFactory createConnectorFactory();

protected abstract String getMetadataDir();
protected abstract String getSchema();

protected abstract Stream<String> getQueryResourcePaths();

Expand All @@ -157,9 +120,10 @@ public void test(String queryResourcePath)

private String getQueryPlanResourcePath(String queryResourcePath)
{
String connectorName = getQueryRunner().getCatalogManager().getCatalog(CATALOG_NAME).orElseThrow().getConnectorName();
String subDir = isPartitioned() ? "partitioned" : "unpartitioned";
java.nio.file.Path tempPath = Paths.get(queryResourcePath.replaceAll("\\.sql$", ".plan.txt"));
return Paths.get(tempPath.getParent().toString(), subDir, tempPath.getFileName().toString()).toString();
Path tempPath = Paths.get(queryResourcePath.replaceAll("\\.sql$", ".plan.txt"));
return Paths.get(tempPath.getParent().toString(), connectorName, subDir, tempPath.getFileName().toString()).toString();
}

protected abstract boolean isPartitioned();
Expand Down Expand Up @@ -201,7 +165,7 @@ public static String readQuery(String resource)
private static String read(String resource)
{
try {
return Resources.toString(getResource(AbstractHiveCostBasedPlanTest.class, resource), UTF_8);
return Resources.toString(getResource(BaseCostBasedPlanTest.class, resource), UTF_8);
}
catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.sql.planner;

import io.trino.plugin.hive.RecordingMetastoreConfig;
import io.trino.plugin.hive.TestingHiveConnectorFactory;
import io.trino.plugin.hive.metastore.UnimplementedHiveMetastore;
import io.trino.plugin.hive.metastore.recording.HiveMetastoreRecording;
import io.trino.plugin.hive.metastore.recording.RecordingHiveMetastore;
import io.trino.spi.connector.ConnectorFactory;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Arrays;

import static com.google.common.collect.MoreCollectors.onlyElement;
import static io.trino.plugin.hive.metastore.recording.TestRecordingHiveMetastore.createJsonCodec;

public abstract class BaseHiveCostBasedPlanTest
extends BaseCostBasedPlanTest
{
@Override
protected ConnectorFactory createConnectorFactory()
{
RecordingMetastoreConfig recordingConfig = new RecordingMetastoreConfig()
.setRecordingPath(getRecordingPath())
.setReplay(true);
try {
// The RecordingHiveMetastore loads the metadata files generated through HiveMetadataRecorder
// which essentially helps to generate the optimal query plans for validation purposes. These files
// contain all the metadata including statistics.
RecordingHiveMetastore metastore = new RecordingHiveMetastore(
new UnimplementedHiveMetastore(),
new HiveMetastoreRecording(recordingConfig, createJsonCodec()));
return new TestingHiveConnectorFactory(metastore);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
protected String getSchema()
{
String fileName = Paths.get(getRecordingPath()).getFileName().toString();
return fileName.split("\\.")[0];
}

private String getRecordingPath()
{
URL resource = getClass().getResource(getMetadataDir());
if (resource == null) {
throw new RuntimeException("Hive metadata directory doesn't exist: " + getMetadataDir());
}

File[] files = new File(resource.getPath()).listFiles();
if (files == null) {
throw new RuntimeException("Hive metadata recording file doesn't exist in directory: " + getMetadataDir());
}

return Arrays.stream(files)
.filter(f -> !f.isDirectory())
.collect(onlyElement())
.getPath();
}

protected abstract String getMetadataDir();
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public static void main(String[] args)
TPCH_METADATA_DIR, "tpch_sf1000_orc",
PARTITIONED_TPCH_METADATA_DIR, "tpch_sf1000_orc_part"),
TPCH_SQL_FILES.stream()
.map(AbstractHiveCostBasedPlanTest::readQuery)
.map(BaseHiveCostBasedPlanTest::readQuery)
.collect(toImmutableList()));
recordMetadata(
ImmutableMap.of(
TPCDS_METADATA_DIR, "tpcds_sf1000_orc",
PARTITIONED_TPCDS_METADATA_DIR, "tpcds_sf1000_orc_part"),
TPCDS_SQL_FILES.stream()
.map(AbstractHiveCostBasedPlanTest::readQuery)
.map(BaseHiveCostBasedPlanTest::readQuery)
.collect(toImmutableList()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@

import java.util.stream.Stream;

import static io.trino.sql.planner.TestHiveTpcdsCostBasedPlan.TPCDS_SQL_FILES;

/**
* This class tests cost-based optimization rules related to joins. It contains unmodified TPCDS queries.
* This class is using Hive connector with mocked in memory thrift metastore with partitioned TPCDS tables.
*/
public class TestHivePartitionedTpcdsCostBasedPlan
extends AbstractHiveCostBasedPlanTest
extends BaseHiveCostBasedPlanTest
{
/*
* CAUTION: The expected plans here are not necessarily optimal yet. Their role is to prevent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@

import java.util.stream.Stream;

import static io.trino.sql.planner.TestHiveTpchCostBasedPlan.TPCH_SQL_FILES;

/**
* This class tests cost-based optimization rules related to joins. It contains unmodified TPCH queries.
* This class is using Hive connector with mocked in memory thrift metastore with partitioned TPCH tables.
*/
public class TestHivePartitionedTpchCostBasedPlan
extends AbstractHiveCostBasedPlanTest
extends BaseHiveCostBasedPlanTest
{
/*
* CAUTION: The expected plans here are not necessarily optimal yet. Their role is to prevent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@

package io.trino.sql.planner;

import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.String.format;

/**
* This class tests cost-based optimization rules related to joins. It contains unmodified TPCDS queries.
* This class is using Hive connector with mocked in memory thrift metastore with un-partitioned TPCDS tables.
*/
public class TestHiveTpcdsCostBasedPlan
extends AbstractHiveCostBasedPlanTest
extends BaseHiveCostBasedPlanTest
{
/*
* CAUTION: The expected plans here are not necessarily optimal yet. Their role is to prevent
Expand All @@ -36,10 +31,6 @@ public class TestHiveTpcdsCostBasedPlan
*/

public static final String TPCDS_METADATA_DIR = "/hive_metadata/unpartitioned_tpcds";
public static final List<String> TPCDS_SQL_FILES = IntStream.range(1, 100)
.mapToObj(i -> format("q%02d", i))
.map(queryId -> format("/sql/presto/tpcds/%s.sql", queryId))
.collect(toImmutableList());

@Override
protected String getMetadataDir()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,14 @@

package io.trino.sql.planner;

import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.String.format;

/**
* This class tests cost-based optimization rules related to joins. It contains unmodified TPCH queries.
* This class is using Hive connector with mocked in memory thrift metastore with un-partitioned TPCH tables.
*/
public class TestHiveTpchCostBasedPlan
extends AbstractHiveCostBasedPlanTest
extends BaseHiveCostBasedPlanTest
{
/*
* CAUTION: The expected plans here are not necessarily optimal yet. Their role is to prevent
Expand All @@ -36,10 +31,6 @@ public class TestHiveTpchCostBasedPlan
*/

public static final String TPCH_METADATA_DIR = "/hive_metadata/unpartitioned_tpch";
public static final List<String> TPCH_SQL_FILES = IntStream.rangeClosed(1, 22)
.mapToObj(i -> format("q%02d", i))
.map(queryId -> format("/sql/presto/tpch/%s.sql", queryId))
.collect(toImmutableList());

@Override
protected String getMetadataDir()
Expand Down