Search in sources :

Example 61 with QualifiedObjectName

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

the class PhoenixQueryRunner method copyTable.

private static void copyTable(QueryRunner queryRunner, String catalog, Session session, String schema, TpchTable<?> table) {
    QualifiedObjectName source = new QualifiedObjectName(catalog, schema, table.getTableName());
    String target = table.getTableName();
    String tableProperties = "";
    if (LINE_ITEM.getTableName().equals(target)) {
        tableProperties = "WITH (ROWKEYS = 'ORDERKEY,LINENUMBER', SALT_BUCKETS=10)";
    } else if (ORDERS.getTableName().equals(target)) {
        tableProperties = "WITH (SALT_BUCKETS=10)";
    } else if (PART_SUPPLIER.getTableName().equals(target)) {
        tableProperties = "WITH (ROWKEYS = 'PARTKEY,SUPPKEY')";
    }
    @Language("SQL") String sql = format("CREATE TABLE %s %s AS SELECT * FROM %s", target, tableProperties, source);
    LOG.debug("Running import for %s %s", target, sql);
    long rows = queryRunner.execute(session, sql).getUpdateCount().getAsLong();
    LOG.debug("%s rows loaded into %s", rows, target);
}
Also used : Language(org.intellij.lang.annotations.Language) QualifiedObjectName(io.trino.metadata.QualifiedObjectName)

Example 62 with QualifiedObjectName

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

the class RaptorQueryRunner method copyTable.

private static void copyTable(QueryRunner queryRunner, String catalog, Session session, String schema, TpchTable<?> table, String properties) {
    QualifiedObjectName source = new QualifiedObjectName(catalog, schema, table.getTableName());
    String target = table.getTableName();
    String with = properties.isEmpty() ? "" : format(" WITH (%s)", properties);
    @Language("SQL") String sql = format("CREATE TABLE %s%s AS SELECT * FROM %s", target, with, source);
    log.info("Running import for %s", target);
    long start = System.nanoTime();
    long rows = queryRunner.execute(session, sql).getUpdateCount().getAsLong();
    log.info("Imported %s rows for %s in %s", rows, target, nanosSince(start));
}
Also used : Language(org.intellij.lang.annotations.Language) QualifiedObjectName(io.trino.metadata.QualifiedObjectName)

Example 63 with QualifiedObjectName

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

the class BaseConnectorTest method testRenameMaterializedView.

@Test
public void testRenameMaterializedView() {
    skipTestUnless(hasBehavior(SUPPORTS_CREATE_MATERIALIZED_VIEW));
    String schema = "rename_mv_test";
    Session session = Session.builder(getSession()).setSchema(schema).build();
    QualifiedObjectName originalMaterializedView = new QualifiedObjectName(session.getCatalog().orElseThrow(), session.getSchema().orElseThrow(), "test_materialized_view_rename_" + randomTableSuffix());
    createTestingMaterializedView(originalMaterializedView, Optional.empty());
    String renamedMaterializedView = "test_materialized_view_rename_new_" + randomTableSuffix();
    if (!hasBehavior(SUPPORTS_RENAME_MATERIALIZED_VIEW)) {
        assertQueryFails(session, "ALTER MATERIALIZED VIEW " + originalMaterializedView + " RENAME TO " + renamedMaterializedView, "This connector does not support renaming materialized views");
        assertUpdate(session, "DROP MATERIALIZED VIEW " + originalMaterializedView);
        return;
    }
    // simple rename
    assertUpdate(session, "ALTER MATERIALIZED VIEW " + originalMaterializedView + " RENAME TO " + renamedMaterializedView);
    assertTestingMaterializedViewQuery(schema, renamedMaterializedView);
    // verify new name in the system.metadata.materialized_views
    assertQuery(session, "SELECT catalog_name, schema_name FROM system.metadata.materialized_views WHERE name = '" + renamedMaterializedView + "'", format("VALUES ('%s', '%s')", originalMaterializedView.getCatalogName(), originalMaterializedView.getSchemaName()));
    assertQueryReturnsEmptyResult(session, listMaterializedViewsSql("name = '" + originalMaterializedView.getObjectName() + "'"));
    // rename with IF EXISTS on existing materialized view
    String testExistsMaterializedViewName = "test_materialized_view_rename_exists_" + randomTableSuffix();
    assertUpdate(session, "ALTER MATERIALIZED VIEW IF EXISTS " + renamedMaterializedView + " RENAME TO " + testExistsMaterializedViewName);
    assertTestingMaterializedViewQuery(schema, testExistsMaterializedViewName);
    // rename with upper-case, not delimited identifier
    String uppercaseName = "TEST_MATERIALIZED_VIEW_RENAME_UPPERCASE_" + randomTableSuffix();
    assertUpdate(session, "ALTER MATERIALIZED VIEW " + testExistsMaterializedViewName + " RENAME TO " + uppercaseName);
    // Ensure select allows for lower-case, not delimited identifier
    assertTestingMaterializedViewQuery(schema, uppercaseName.toLowerCase(ENGLISH));
    String otherSchema = "rename_mv_other_schema";
    assertUpdate(format("CREATE SCHEMA IF NOT EXISTS %s", otherSchema));
    if (hasBehavior(SUPPORTS_RENAME_MATERIALIZED_VIEW_ACROSS_SCHEMAS)) {
        assertUpdate(session, "ALTER MATERIALIZED VIEW " + uppercaseName + " RENAME TO " + otherSchema + "." + originalMaterializedView.getObjectName());
        assertTestingMaterializedViewQuery(otherSchema, originalMaterializedView.getObjectName());
        assertUpdate(session, "DROP MATERIALIZED VIEW " + otherSchema + "." + originalMaterializedView.getObjectName());
    } else {
        assertQueryFails(session, "ALTER MATERIALIZED VIEW " + uppercaseName + " RENAME TO " + otherSchema + "." + originalMaterializedView.getObjectName(), "Materialized View rename across schemas is not supported");
        assertUpdate(session, "DROP MATERIALIZED VIEW " + uppercaseName);
    }
    assertFalse(getQueryRunner().tableExists(session, originalMaterializedView.toString()));
    assertFalse(getQueryRunner().tableExists(session, renamedMaterializedView));
    assertFalse(getQueryRunner().tableExists(session, testExistsMaterializedViewName));
    // rename with IF EXISTS on NOT existing materialized view
    assertUpdate(session, "ALTER TABLE IF EXISTS " + originalMaterializedView + " RENAME TO " + renamedMaterializedView);
    assertQueryReturnsEmptyResult(session, listMaterializedViewsSql("name = '" + originalMaterializedView.getObjectName() + "'"));
    assertQueryReturnsEmptyResult(session, listMaterializedViewsSql("name = '" + renamedMaterializedView + "'"));
}
Also used : QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Session(io.trino.Session) Test(org.testng.annotations.Test)

Example 64 with QualifiedObjectName

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

the class BaseConnectorTest method testMaterializedView.

@Test
public void testMaterializedView() {
    if (!hasBehavior(SUPPORTS_CREATE_MATERIALIZED_VIEW)) {
        assertQueryFails("CREATE MATERIALIZED VIEW nation_mv AS SELECT * FROM nation", "This connector does not support creating materialized views");
        return;
    }
    QualifiedObjectName view = new QualifiedObjectName(getSession().getCatalog().orElseThrow(), getSession().getSchema().orElseThrow(), "test_materialized_view_" + randomTableSuffix());
    QualifiedObjectName otherView = new QualifiedObjectName(getSession().getCatalog().orElseThrow(), "other_schema", "test_materialized_view_" + randomTableSuffix());
    QualifiedObjectName viewWithComment = new QualifiedObjectName(getSession().getCatalog().orElseThrow(), getSession().getSchema().orElseThrow(), "test_materialized_view_with_comment_" + randomTableSuffix());
    createTestingMaterializedView(view, Optional.empty());
    createTestingMaterializedView(otherView, Optional.of("sarcastic comment"));
    createTestingMaterializedView(viewWithComment, Optional.of("mv_comment"));
    // verify comment
    MaterializedResult materializedRows = computeActual("SHOW CREATE MATERIALIZED VIEW " + viewWithComment);
    assertThat((String) materializedRows.getOnlyValue()).contains("COMMENT 'mv_comment'");
    assertThat(query("SELECT table_name, comment FROM system.metadata.table_comments " + "WHERE catalog_name = '" + view.getCatalogName() + "' AND " + "schema_name = '" + view.getSchemaName() + "'")).skippingTypesCheck().containsAll("VALUES ('" + view.getObjectName() + "', null), ('" + viewWithComment.getObjectName() + "', 'mv_comment')");
    // reading
    assertThat(query("SELECT * FROM " + view)).skippingTypesCheck().matches("SELECT * FROM nation");
    assertThat(query("SELECT * FROM " + viewWithComment)).skippingTypesCheck().matches("SELECT * FROM nation");
    // table listing
    assertThat(query("SHOW TABLES")).skippingTypesCheck().containsAll("VALUES '" + view.getObjectName() + "'");
    // information_schema.tables without table_name filter
    assertThat(query("SELECT table_name, table_type FROM information_schema.tables " + "WHERE table_schema = '" + view.getSchemaName() + "'")).skippingTypesCheck().containsAll(// TODO table_type should probably be "* VIEW"
    "VALUES ('" + view.getObjectName() + "', 'BASE TABLE')");
    // information_schema.tables with table_name filter
    assertQuery("SELECT table_name, table_type FROM information_schema.tables " + "WHERE table_schema = '" + view.getSchemaName() + "' and table_name = '" + view.getObjectName() + "'", "VALUES ('" + view.getObjectName() + "', 'BASE TABLE')");
    // system.jdbc.tables without filter
    assertThat(query("SELECT table_schem, table_name, table_type FROM system.jdbc.tables")).skippingTypesCheck().containsAll("VALUES ('" + view.getSchemaName() + "', '" + view.getObjectName() + "', 'TABLE')");
    // system.jdbc.tables with table prefix filter
    assertQuery("SELECT table_schem, table_name, table_type " + "FROM system.jdbc.tables " + "WHERE table_cat = '" + view.getCatalogName() + "' AND " + "table_schem = '" + view.getSchemaName() + "' AND " + "table_name = '" + view.getObjectName() + "'", "VALUES ('" + view.getSchemaName() + "', '" + view.getObjectName() + "', 'TABLE')");
    // column listing
    assertThat(query("SHOW COLUMNS FROM " + view.getObjectName())).projected(// column types can very between connectors
    0).skippingTypesCheck().matches("VALUES 'nationkey', 'name', 'regionkey', 'comment'");
    assertThat(query("DESCRIBE " + view.getObjectName())).projected(// column types can very between connectors
    0).skippingTypesCheck().matches("VALUES 'nationkey', 'name', 'regionkey', 'comment'");
    // information_schema.columns without table_name filter
    assertThat(query("SELECT table_name, column_name " + "FROM information_schema.columns " + "WHERE table_schema = '" + view.getSchemaName() + "'")).skippingTypesCheck().containsAll("SELECT * FROM (VALUES '" + view.getObjectName() + "') " + "CROSS JOIN UNNEST(ARRAY['nationkey', 'name', 'regionkey', 'comment'])");
    // information_schema.columns with table_name filter
    assertThat(query("SELECT table_name, column_name " + "FROM information_schema.columns " + "WHERE table_schema = '" + view.getSchemaName() + "' and table_name = '" + view.getObjectName() + "'")).skippingTypesCheck().containsAll("SELECT * FROM (VALUES '" + view.getObjectName() + "') " + "CROSS JOIN UNNEST(ARRAY['nationkey', 'name', 'regionkey', 'comment'])");
    // view-specific listings
    checkInformationSchemaViewsForMaterializedView(view.getSchemaName(), view.getObjectName());
    // system.jdbc.columns without filter
    assertThat(query("SELECT table_schem, table_name, column_name FROM system.jdbc.columns")).skippingTypesCheck().containsAll("SELECT * FROM (VALUES ('" + view.getSchemaName() + "', '" + view.getObjectName() + "')) " + "CROSS JOIN UNNEST(ARRAY['nationkey', 'name', 'regionkey', 'comment'])");
    // system.jdbc.columns with schema filter
    assertThat(query("SELECT table_schem, table_name, column_name " + "FROM system.jdbc.columns " + "WHERE table_schem LIKE '%" + view.getSchemaName() + "%'")).skippingTypesCheck().containsAll("SELECT * FROM (VALUES ('" + view.getSchemaName() + "', '" + view.getObjectName() + "')) " + "CROSS JOIN UNNEST(ARRAY['nationkey', 'name', 'regionkey', 'comment'])");
    // system.jdbc.columns with table filter
    assertThat(query("SELECT table_schem, table_name, column_name " + "FROM system.jdbc.columns " + "WHERE table_name LIKE '%" + view.getObjectName() + "%'")).skippingTypesCheck().containsAll("SELECT * FROM (VALUES ('" + view.getSchemaName() + "', '" + view.getObjectName() + "')) " + "CROSS JOIN UNNEST(ARRAY['nationkey', 'name', 'regionkey', 'comment'])");
    // details
    assertThat(((String) computeScalar("SHOW CREATE MATERIALIZED VIEW " + view.getObjectName()))).matches("(?s)" + "CREATE MATERIALIZED VIEW \\Q" + view + "\\E" + ".* AS\n" + "SELECT \\*\n" + "FROM\n" + "  nation");
    // we only want to test filtering materialized views in different schemas,
    // `viewWithComment` is in the same schema as `view` so it is not needed
    assertUpdate("DROP MATERIALIZED VIEW " + viewWithComment);
    // test filtering materialized views in system metadata table
    assertThat(query(listMaterializedViewsSql("catalog_name = '" + view.getCatalogName() + "'"))).skippingTypesCheck().containsAll(getTestingMaterializedViewsResultRows(view, otherView));
    assertThat(query(listMaterializedViewsSql("catalog_name = '" + otherView.getCatalogName() + "'", "schema_name = '" + otherView.getSchemaName() + "'"))).skippingTypesCheck().containsAll(getTestingMaterializedViewsResultRow(otherView, "sarcastic comment"));
    assertThat(query(listMaterializedViewsSql("catalog_name = '" + view.getCatalogName() + "'", "schema_name = '" + view.getSchemaName() + "'", "name = '" + view.getObjectName() + "'"))).skippingTypesCheck().containsAll(getTestingMaterializedViewsResultRow(view, ""));
    assertThat(query(listMaterializedViewsSql("schema_name LIKE '%" + view.getSchemaName() + "%'"))).skippingTypesCheck().containsAll(getTestingMaterializedViewsResultRow(view, ""));
    assertThat(query(listMaterializedViewsSql("name LIKE '%" + view.getObjectName() + "%'"))).skippingTypesCheck().containsAll(getTestingMaterializedViewsResultRow(view, ""));
    // verify write in transaction
    if (!hasBehavior(SUPPORTS_MULTI_STATEMENT_WRITES)) {
        assertThatThrownBy(() -> inTransaction(session -> computeActual(session, "REFRESH MATERIALIZED VIEW " + view))).hasMessageMatching("Catalog only supports writes using autocommit: \\w+");
    }
    assertUpdate("DROP MATERIALIZED VIEW " + view);
    assertUpdate("DROP MATERIALIZED VIEW " + otherView);
    assertQueryReturnsEmptyResult(listMaterializedViewsSql("name = '" + view.getObjectName() + "'"));
    assertQueryReturnsEmptyResult(listMaterializedViewsSql("name = '" + otherView.getObjectName() + "'"));
    assertQueryReturnsEmptyResult(listMaterializedViewsSql("name = '" + viewWithComment.getObjectName() + "'"));
}
Also used : QualifiedObjectName(io.trino.metadata.QualifiedObjectName) Test(org.testng.annotations.Test)

Example 65 with QualifiedObjectName

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

the class QueryAssertions method copyTable.

public static void copyTable(QueryRunner queryRunner, String sourceCatalog, String sourceSchema, String sourceTable, Session session) {
    QualifiedObjectName table = new QualifiedObjectName(sourceCatalog, sourceSchema, sourceTable);
    copyTable(queryRunner, table, session);
}
Also used : QualifiedObjectName(io.trino.metadata.QualifiedObjectName)

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