Search in sources :

Example 26 with QualifiedObjectName

use of io.trino.metadata.QualifiedObjectName in project trino by trinodb.

the class LogicalPlanner method createTableCreationPlan.

private RelationPlan createTableCreationPlan(Analysis analysis, Query query) {
    Analysis.Create create = analysis.getCreate().orElseThrow();
    QualifiedObjectName destination = create.getDestination().orElseThrow();
    RelationPlan plan = createRelationPlan(analysis, query);
    if (!create.isCreateTableAsSelectWithData()) {
        PlanNode root = new LimitNode(idAllocator.getNextId(), plan.getRoot(), 0L, false);
        plan = new RelationPlan(root, plan.getScope(), plan.getFieldMappings(), Optional.empty());
    }
    ConnectorTableMetadata tableMetadata = create.getMetadata().orElseThrow();
    Optional<TableLayout> newTableLayout = create.getLayout();
    List<String> columnNames = tableMetadata.getColumns().stream().filter(// todo this filter is redundant
    column -> !column.isHidden()).map(ColumnMetadata::getName).collect(toImmutableList());
    TableStatisticsMetadata statisticsMetadata = metadata.getStatisticsCollectionMetadataForWrite(session, destination.getCatalogName(), tableMetadata);
    return createTableWriterPlan(analysis, plan.getRoot(), visibleFields(plan), new CreateReference(destination.getCatalogName(), tableMetadata, newTableLayout), columnNames, tableMetadata.getColumns(), newTableLayout, statisticsMetadata);
}
Also used : TableStatisticsMetadata(io.trino.spi.statistics.TableStatisticsMetadata) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) PlanNode(io.trino.sql.planner.plan.PlanNode) LimitNode(io.trino.sql.planner.plan.LimitNode) Analysis(io.trino.sql.analyzer.Analysis) CreateReference(io.trino.sql.planner.plan.TableWriterNode.CreateReference) TableLayout(io.trino.metadata.TableLayout) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata)

Example 27 with QualifiedObjectName

use of io.trino.metadata.QualifiedObjectName in project trino by trinodb.

the class TestHiveProjectionPushdownIntoTableScan method testDereferencePushdown.

@Test
public void testDereferencePushdown() {
    String testTable = "test_simple_projection_pushdown";
    QualifiedObjectName completeTableName = new QualifiedObjectName(HIVE_CATALOG_NAME, SCHEMA_NAME, testTable);
    getQueryRunner().execute(format("CREATE TABLE %s (col0, col1) AS" + " SELECT cast(row(5, 6) as row(x bigint, y bigint)) AS col0, 5 AS col1 WHERE false", testTable));
    Session session = getQueryRunner().getDefaultSession();
    Optional<TableHandle> tableHandle = getTableHandle(session, completeTableName);
    assertTrue(tableHandle.isPresent(), "expected the table handle to be present");
    Map<String, ColumnHandle> columns = getColumnHandles(session, completeTableName);
    HiveColumnHandle column0Handle = (HiveColumnHandle) columns.get("col0");
    HiveColumnHandle column1Handle = (HiveColumnHandle) columns.get("col1");
    HiveColumnHandle columnX = createProjectedColumnHandle(column0Handle, ImmutableList.of(0));
    HiveColumnHandle columnY = createProjectedColumnHandle(column0Handle, ImmutableList.of(1));
    // Simple Projection pushdown
    assertPlan("SELECT col0.x expr_x, col0.y expr_y FROM " + testTable, any(tableScan(((HiveTableHandle) tableHandle.get().getConnectorHandle()).withProjectedColumns(ImmutableSet.of(columnX, columnY))::equals, TupleDomain.all(), ImmutableMap.of("col0#x", columnX::equals, "col0#y", columnY::equals))));
    // Projection and predicate pushdown
    assertPlan(format("SELECT col0.x FROM %s WHERE col0.x = col1 + 3 and col0.y = 2", testTable), anyTree(filter("col0_y = BIGINT '2' AND (col0_x =  cast((col1 + 3) as BIGINT))", tableScan(table -> {
        HiveTableHandle hiveTableHandle = (HiveTableHandle) table;
        return hiveTableHandle.getCompactEffectivePredicate().equals(TupleDomain.withColumnDomains(ImmutableMap.of(columnY, Domain.singleValue(BIGINT, 2L)))) && hiveTableHandle.getProjectedColumns().equals(ImmutableSet.of(column1Handle, columnX, columnY));
    }, TupleDomain.all(), ImmutableMap.of("col0_y", columnY::equals, "col0_x", columnX::equals, "col1", column1Handle::equals)))));
    // Projection and predicate pushdown with overlapping columns
    assertPlan(format("SELECT col0, col0.y expr_y FROM %s WHERE col0.x = 5", testTable), anyTree(filter("col0_x = BIGINT '5'", tableScan(table -> {
        HiveTableHandle hiveTableHandle = (HiveTableHandle) table;
        return hiveTableHandle.getCompactEffectivePredicate().equals(TupleDomain.withColumnDomains(ImmutableMap.of(columnX, Domain.singleValue(BIGINT, 5L)))) && hiveTableHandle.getProjectedColumns().equals(ImmutableSet.of(column0Handle, columnX));
    }, TupleDomain.all(), ImmutableMap.of("col0", column0Handle::equals, "col0_x", columnX::equals)))));
    // Projection and predicate pushdown with joins
    assertPlan(format("SELECT T.col0.x, T.col0, T.col0.y FROM %s T join %s S on T.col1 = S.col1 WHERE (T.col0.x = 2)", testTable, testTable), anyTree(project(ImmutableMap.of("expr_0_x", expression("expr_0[1]"), "expr_0", expression("expr_0"), "expr_0_y", expression("expr_0[2]")), join(INNER, ImmutableList.of(equiJoinClause("t_expr_1", "s_expr_1")), anyTree(filter("expr_0_x = BIGINT '2'", tableScan(table -> ((HiveTableHandle) table).getCompactEffectivePredicate().getDomains().get().equals(ImmutableMap.of(columnX, Domain.singleValue(BIGINT, 2L))), TupleDomain.all(), ImmutableMap.of("expr_0_x", columnX::equals, "expr_0", column0Handle::equals, "t_expr_1", column1Handle::equals)))), anyTree(tableScan(((HiveTableHandle) tableHandle.get().getConnectorHandle()).withProjectedColumns(ImmutableSet.of(column1Handle))::equals, TupleDomain.all(), ImmutableMap.of("s_expr_1", column1Handle::equals)))))));
}
Also used : MoreFiles.deleteRecursively(com.google.common.io.MoreFiles.deleteRecursively) PlanMatchPattern.any(io.trino.sql.planner.assertions.PlanMatchPattern.any) Database(io.trino.plugin.hive.metastore.Database) Test(org.testng.annotations.Test) NoHdfsAuthentication(io.trino.plugin.hive.authentication.NoHdfsAuthentication) Map(java.util.Map) PlanMatchPattern.equiJoinClause(io.trino.sql.planner.assertions.PlanMatchPattern.equiJoinClause) HiveHdfsConfiguration(io.trino.plugin.hive.HiveHdfsConfiguration) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle) TestHiveReaderProjectionsUtil.createProjectedColumnHandle(io.trino.plugin.hive.TestHiveReaderProjectionsUtil.createProjectedColumnHandle) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) ImmutableSet(com.google.common.collect.ImmutableSet) HdfsEnvironment(io.trino.plugin.hive.HdfsEnvironment) ImmutableMap(com.google.common.collect.ImmutableMap) Domain(io.trino.spi.predicate.Domain) FileHiveMetastore(io.trino.plugin.hive.metastore.file.FileHiveMetastore) TestingHiveConnectorFactory(io.trino.plugin.hive.TestingHiveConnectorFactory) String.format(java.lang.String.format) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) PlanMatchPattern.anyTree(io.trino.sql.planner.assertions.PlanMatchPattern.anyTree) BIGINT(io.trino.spi.type.BigintType.BIGINT) HdfsConfig(io.trino.plugin.hive.HdfsConfig) HdfsConfigurationInitializer(io.trino.plugin.hive.HdfsConfigurationInitializer) Optional(java.util.Optional) Session(io.trino.Session) INNER(io.trino.sql.planner.plan.JoinNode.Type.INNER) MetastoreConfig(io.trino.plugin.hive.metastore.MetastoreConfig) PlanMatchPattern.filter(io.trino.sql.planner.assertions.PlanMatchPattern.filter) HiveMetastore(io.trino.plugin.hive.metastore.HiveMetastore) ALLOW_INSECURE(com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE) ImmutableList(com.google.common.collect.ImmutableList) Files(com.google.common.io.Files) NodeVersion(io.trino.plugin.hive.NodeVersion) HiveTableHandle(io.trino.plugin.hive.HiveTableHandle) BasePushdownPlanTest(io.trino.sql.planner.assertions.BasePushdownPlanTest) LocalQueryRunner(io.trino.testing.LocalQueryRunner) ColumnHandle(io.trino.spi.connector.ColumnHandle) PlanMatchPattern.join(io.trino.sql.planner.assertions.PlanMatchPattern.join) AfterClass(org.testng.annotations.AfterClass) PrincipalType(io.trino.spi.security.PrincipalType) HdfsConfiguration(io.trino.plugin.hive.HdfsConfiguration) TupleDomain(io.trino.spi.predicate.TupleDomain) File(java.io.File) TableHandle(io.trino.metadata.TableHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) Assert.assertTrue(org.testng.Assert.assertTrue) FileHiveMetastoreConfig(io.trino.plugin.hive.metastore.file.FileHiveMetastoreConfig) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle) TestHiveReaderProjectionsUtil.createProjectedColumnHandle(io.trino.plugin.hive.TestHiveReaderProjectionsUtil.createProjectedColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) HiveTableHandle(io.trino.plugin.hive.HiveTableHandle) HiveTableHandle(io.trino.plugin.hive.HiveTableHandle) TableHandle(io.trino.metadata.TableHandle) HiveColumnHandle(io.trino.plugin.hive.HiveColumnHandle) Session(io.trino.Session) Test(org.testng.annotations.Test) BasePushdownPlanTest(io.trino.sql.planner.assertions.BasePushdownPlanTest)

Example 28 with QualifiedObjectName

use of io.trino.metadata.QualifiedObjectName in project trino by trinodb.

the class TestCreateViewTask method setUp.

@Override
@BeforeMethod
public void setUp() {
    super.setUp();
    parser = new SqlParser();
    analyzerFactory = new AnalyzerFactory(createTestingStatementAnalyzerFactory(plannerContext, new AllowAllAccessControl(), new TablePropertyManager(), new AnalyzePropertyManager()), new StatementRewrite(ImmutableSet.of()));
    QualifiedObjectName tableName = qualifiedObjectName("mock_table");
    metadata.createTable(testSession, CATALOG_NAME, someTable(tableName), false);
}
Also used : AllowAllAccessControl(io.trino.security.AllowAllAccessControl) StatementRewrite(io.trino.sql.rewrite.StatementRewrite) SqlParser(io.trino.sql.parser.SqlParser) TablePropertyManager(io.trino.metadata.TablePropertyManager) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) AnalyzerFactory(io.trino.sql.analyzer.AnalyzerFactory) StatementAnalyzerFactory.createTestingStatementAnalyzerFactory(io.trino.sql.analyzer.StatementAnalyzerFactory.createTestingStatementAnalyzerFactory) AnalyzePropertyManager(io.trino.metadata.AnalyzePropertyManager) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 29 with QualifiedObjectName

use of io.trino.metadata.QualifiedObjectName in project trino by trinodb.

the class TestCreateViewTask method testCreateViewOnMaterializedView.

@Test
public void testCreateViewOnMaterializedView() {
    QualifiedObjectName viewName = qualifiedObjectName("existing_materialized_view");
    metadata.createMaterializedView(testSession, viewName, someMaterializedView(), false, false);
    assertTrinoExceptionThrownBy(() -> getFutureValue(executeCreateView(asQualifiedName(viewName), false))).hasErrorCode(TABLE_ALREADY_EXISTS).hasMessage("Materialized view already exists: '%s'", viewName);
}
Also used : QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Example 30 with QualifiedObjectName

use of io.trino.metadata.QualifiedObjectName in project trino by trinodb.

the class TestCreateViewTask method testCreateViewOnViewIfExists.

@Test
public void testCreateViewOnViewIfExists() {
    QualifiedObjectName viewName = qualifiedObjectName("existing_view");
    metadata.createView(testSession, viewName, someView(), false);
    assertTrinoExceptionThrownBy(() -> getFutureValue(executeCreateView(asQualifiedName(viewName), false))).hasErrorCode(TABLE_ALREADY_EXISTS).hasMessage("View already exists: '%s'", viewName);
}
Also used : QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Aggregations

QualifiedObjectName (io.trino.metadata.QualifiedObjectName)142 ViewExpression (io.trino.spi.security.ViewExpression)51 Test (org.testng.annotations.Test)51 Test (org.junit.jupiter.api.Test)41 Session (io.trino.Session)40 TableHandle (io.trino.metadata.TableHandle)33 MetadataUtil.createQualifiedObjectName (io.trino.metadata.MetadataUtil.createQualifiedObjectName)24 Optional (java.util.Optional)20 Metadata (io.trino.metadata.Metadata)17 Map (java.util.Map)17 Objects.requireNonNull (java.util.Objects.requireNonNull)16 ImmutableList (com.google.common.collect.ImmutableList)15 List (java.util.List)15 ImmutableMap (com.google.common.collect.ImmutableMap)14 TrinoException (io.trino.spi.TrinoException)14 ImmutableSet (com.google.common.collect.ImmutableSet)13 ColumnHandle (io.trino.spi.connector.ColumnHandle)13 Type (io.trino.spi.type.Type)11 Set (java.util.Set)11 CatalogName (io.trino.connector.CatalogName)10