Search in sources :

Example 41 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method testCreatePartitionedTableAs.

private void testCreatePartitionedTableAs(Session session, HiveStorageFormat storageFormat) {
    @Language("SQL") String createTable = "" + "CREATE TABLE test_create_partitioned_table_as " + "WITH (" + "format = '" + storageFormat + "', " + "partitioned_by = ARRAY[ 'SHIP_PRIORITY', 'ORDER_STATUS' ]" + ") " + "AS " + "SELECT orderkey AS order_key, shippriority AS ship_priority, orderstatus AS order_status " + "FROM tpch.tiny.orders";
    assertUpdate(session, createTable, "SELECT count(*) from orders");
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_create_partitioned_table_as");
    assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
    assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), ImmutableList.of("ship_priority", "order_status"));
    List<?> partitions = getPartitions("test_create_partitioned_table_as");
    assertEquals(partitions.size(), 3);
    assertQuery(session, "SELECT * from test_create_partitioned_table_as", "SELECT orderkey, shippriority, orderstatus FROM orders");
    assertUpdate(session, "DROP TABLE test_create_partitioned_table_as");
    assertFalse(getQueryRunner().tableExists(session, "test_create_partitioned_table_as"));
}
Also used : TableMetadata(com.facebook.presto.metadata.TableMetadata) Language(org.intellij.lang.annotations.Language)

Example 42 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method testBucketHiddenColumn.

@Test
public void testBucketHiddenColumn() {
    @Language("SQL") String createTable = "CREATE TABLE test_bucket_hidden_column " + "WITH (" + "bucketed_by = ARRAY['col0']," + "bucket_count = 2" + ") AS " + "SELECT * FROM (VALUES " + "(0, 11), (1, 12), (2, 13), " + "(3, 14), (4, 15), (5, 16), " + "(6, 17), (7, 18), (8, 19)" + " ) t (col0, col1) ";
    assertUpdate(createTable, 9);
    assertTrue(getQueryRunner().tableExists(getSession(), "test_bucket_hidden_column"));
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_bucket_hidden_column");
    assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKETED_BY_PROPERTY), ImmutableList.of("col0"));
    assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKET_COUNT_PROPERTY), 2);
    List<String> columnNames = ImmutableList.of("col0", "col1", PATH_COLUMN_NAME, BUCKET_COLUMN_NAME, FILE_SIZE_COLUMN_NAME, FILE_MODIFIED_TIME_COLUMN_NAME);
    List<ColumnMetadata> columnMetadatas = tableMetadata.getColumns();
    assertEquals(columnMetadatas.size(), columnNames.size());
    for (int i = 0; i < columnMetadatas.size(); i++) {
        ColumnMetadata columnMetadata = columnMetadatas.get(i);
        assertEquals(columnMetadata.getName(), columnNames.get(i));
        if (columnMetadata.getName().equals(BUCKET_COLUMN_NAME)) {
            // $bucket_number should be hidden column
            assertTrue(columnMetadata.isHidden());
        }
    }
    assertEquals(getBucketCount("test_bucket_hidden_column"), 2);
    MaterializedResult results = computeActual(format("SELECT *, \"%1$s\" FROM test_bucket_hidden_column WHERE \"%1$s\" = 1", BUCKET_COLUMN_NAME));
    for (int i = 0; i < results.getRowCount(); i++) {
        MaterializedRow row = results.getMaterializedRows().get(i);
        int col0 = (int) row.getField(0);
        int col1 = (int) row.getField(1);
        int bucket = (int) row.getField(2);
        assertEquals(col1, col0 + 11);
        assertTrue(col1 % 2 == 0);
        // Because Hive's hash function for integer n is h(n) = n.
        assertEquals(bucket, col0 % 2);
    }
    assertEquals(results.getRowCount(), 4);
    assertUpdate("DROP TABLE test_bucket_hidden_column");
    assertFalse(getQueryRunner().tableExists(getSession(), "test_bucket_hidden_column"));
}
Also used : TableMetadata(com.facebook.presto.metadata.TableMetadata) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Language(org.intellij.lang.annotations.Language) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Constraint(com.facebook.presto.spi.Constraint) ColumnConstraint(com.facebook.presto.sql.planner.planPrinter.IOPlanPrinter.ColumnConstraint) MaterializedRow(com.facebook.presto.testing.MaterializedRow) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Example 43 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method testPartialAggregatePushdownORC.

@Test
public void testPartialAggregatePushdownORC() {
    @Language("SQL") String createTable = "" + "CREATE TABLE test_orc_table (" + " _boolean BOOLEAN" + ", _tinyint TINYINT" + ", _smallint SMALLINT" + ", _integer INTEGER" + ", _bigint BIGINT" + ", _real REAL" + ", _double DOUBLE" + ", _shortdecimal DECIMAL(8,3)" + ", _longdecimal DECIMAL(25,2)" + ", _string VARCHAR" + ", _varchar VARCHAR(10)" + ", _singlechar CHAR" + ", _char CHAR(10)" + ", _varbinary VARBINARY" + ", _date DATE" + ", _timestamp TIMESTAMP" + ")" + "WITH (format = 'orc')";
    Session session = Session.builder(getSession()).setCatalogSessionProperty(catalog, "partial_aggregation_pushdown_enabled", "true").setCatalogSessionProperty(catalog, "partial_aggregation_pushdown_for_variable_length_datatypes_enabled", "true").build();
    try {
        assertUpdate(session, createTable);
        TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_orc_table");
        assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), HiveStorageFormat.ORC);
        assertUpdate(session, "INSERT INTO test_orc_table VALUES (" + "true" + ", cast(1 as tinyint)" + ", cast(2 as smallint)" + ", 3" + ", 4" + ", 1.2" + ", 2.3" + ", 4.5" + ", 55555555555555.32" + ", 'abc'" + ", 'def'" + ", 'g'" + ", 'hij'" + ", cast('klm' as varbinary)" + ", cast('2020-05-01' as date)" + ", cast('2020-06-04 16:55:40.777' as timestamp)" + ")", 1);
        assertUpdate(session, "INSERT INTO test_orc_table VALUES (" + "false" + ", cast(10 as tinyint)" + ", cast(20 as smallint)" + ", 30" + ", 40" + ", 10.25" + ", 25.334" + ", 465.523" + ", 88888888555555.91" + ", 'foo'" + ", 'bar'" + ", 'b'" + ", 'baz'" + ", cast('qux' as varbinary)" + ", cast('2020-06-02' as date)" + ", cast('2020-05-01 18:34:23.88' as timestamp)" + ")", 1);
        String rowCount = "SELECT 2";
        assertQuery(session, "SELECT COUNT(*) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_boolean) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_tinyint) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_smallint) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_integer) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_bigint) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_real) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_double) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_shortdecimal) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_longdecimal) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_string) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_varchar) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_singlechar) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_char) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_varbinary) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_date) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT COUNT(_timestamp) FROM test_orc_table", rowCount);
        assertQuery(session, "SELECT MIN(_boolean), MAX(_boolean) FROM test_orc_table", "select false, true");
        assertQuery(session, "SELECT MIN(_tinyint), MAX(_tinyint) FROM test_orc_table", "select 1, 10");
        assertQuery(session, "SELECT MIN(_smallint), MAX(_smallint) FROM test_orc_table", "select 2, 20");
        assertQuery(session, "SELECT MIN(_integer), MAX(_integer) FROM test_orc_table", "select 3, 30");
        assertQuery(session, "SELECT MIN(_bigint), MAX(_bigint) FROM test_orc_table", "select 4, 40");
        assertQuery(session, "SELECT MIN(_real), MAX(_real) FROM test_orc_table", "select 1.2, 10.25");
        assertQuery(session, "SELECT MIN(_double), MAX(_double) FROM test_orc_table", "select 2.3, 25.334");
        assertQuery(session, "SELECT MIN(_shortdecimal), MAX(_shortdecimal) FROM test_orc_table", "select 4.5, 465.523");
        assertQuery(session, "SELECT MIN(_longdecimal), MAX(_longdecimal) FROM test_orc_table", "select 55555555555555.32, 88888888555555.91");
        assertQuery(session, "SELECT MIN(_string), MAX(_string) FROM test_orc_table", "select 'abc', 'foo'");
        assertQuery(session, "SELECT MIN(_varchar), MAX(_varchar) FROM test_orc_table", "select 'bar', 'def'");
        assertQuery(session, "SELECT MIN(_singlechar), MAX(_singlechar) FROM test_orc_table", "select 'b', 'g'");
        assertQuery(session, "SELECT MIN(_char), MAX(_char) FROM test_orc_table", "select 'baz', 'hij'");
        assertQuery(session, "SELECT MIN(_varbinary), MAX(_varbinary) FROM test_orc_table", "select X'6b6c6d', X'717578'");
        assertQuery(session, "SELECT MIN(_date), MAX(_date) FROM test_orc_table", "select cast('2020-05-01' as date), cast('2020-06-02' as date)");
        assertQuery(session, "SELECT MIN(_timestamp), MAX(_timestamp) FROM test_orc_table", "select cast('2020-05-01 18:34:23.88' as timestamp), cast('2020-06-04 16:55:40.777' as timestamp)");
    } finally {
        assertUpdate(session, "DROP TABLE test_orc_table");
    }
    assertFalse(getQueryRunner().tableExists(session, "test_orc_table"));
}
Also used : TableMetadata(com.facebook.presto.metadata.TableMetadata) Language(org.intellij.lang.annotations.Language) ConnectorSession(com.facebook.presto.spi.ConnectorSession) HiveQueryRunner.createBucketedSession(com.facebook.presto.hive.HiveQueryRunner.createBucketedSession) Session(com.facebook.presto.Session) HiveQueryRunner.createMaterializeExchangesSession(com.facebook.presto.hive.HiveQueryRunner.createMaterializeExchangesSession) Test(org.testng.annotations.Test) AbstractTestIntegrationSmokeTest(com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)

Example 44 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method testInsertUnpartitionedTable.

private void testInsertUnpartitionedTable(Session session, HiveStorageFormat storageFormat) {
    String tableName = "test_insert_unpartitioned_table";
    @Language("SQL") String createTable = "" + "CREATE TABLE " + tableName + " " + "(" + "  order_key BIGINT," + "  comment VARCHAR," + "  order_status VARCHAR" + ") " + "WITH (" + "format = '" + storageFormat + "'" + ") ";
    assertUpdate(session, createTable);
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
    assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
    for (int i = 0; i < 3; i++) {
        assertUpdate(session, format("INSERT INTO " + tableName + " " + "SELECT orderkey, comment, orderstatus " + "FROM tpch.tiny.orders " + "WHERE orderkey %% 3 = %d", i), format("SELECT count(*) from orders where orderkey %% 3 = %d", i));
    }
    assertQuery(session, "SELECT * from " + tableName, "SELECT orderkey, comment, orderstatus FROM orders");
    assertUpdate(session, "DROP TABLE " + tableName);
    assertFalse(getQueryRunner().tableExists(session, tableName));
}
Also used : TableMetadata(com.facebook.presto.metadata.TableMetadata) Language(org.intellij.lang.annotations.Language) Constraint(com.facebook.presto.spi.Constraint) ColumnConstraint(com.facebook.presto.sql.planner.planPrinter.IOPlanPrinter.ColumnConstraint)

Example 45 with Language

use of org.intellij.lang.annotations.Language in project presto by prestodb.

the class TestHiveIntegrationSmokeTest method testCreatePartitionedTable.

private void testCreatePartitionedTable(Session session, HiveStorageFormat storageFormat) {
    if (!insertOperationsSupported(storageFormat)) {
        return;
    }
    @Language("SQL") String createTable = "" + "CREATE TABLE test_partitioned_table (" + "  _string VARCHAR" + ",  _varchar VARCHAR(65535)" + ", _char CHAR(10)" + ", _bigint BIGINT" + ", _integer INTEGER" + ", _smallint SMALLINT" + ", _tinyint TINYINT" + ", _real REAL" + ", _double DOUBLE" + ", _boolean BOOLEAN" + ", _decimal_short DECIMAL(3,2)" + ", _decimal_long DECIMAL(30,10)" + ", _partition_string VARCHAR" + ", _partition_varchar VARCHAR(65535)" + ", _partition_char CHAR(10)" + ", _partition_tinyint TINYINT" + ", _partition_smallint SMALLINT" + ", _partition_integer INTEGER" + ", _partition_bigint BIGINT" + ", _partition_boolean BOOLEAN" + ", _partition_decimal_short DECIMAL(3,2)" + ", _partition_decimal_long DECIMAL(30,10)" + ", _partition_date DATE" + ", _partition_timestamp TIMESTAMP" + ") " + "WITH (" + "format = '" + storageFormat + "', " + "partitioned_by = ARRAY[ '_partition_string', '_partition_varchar', '_partition_char', '_partition_tinyint', '_partition_smallint', '_partition_integer', '_partition_bigint', '_partition_boolean', '_partition_decimal_short', '_partition_decimal_long', '_partition_date', '_partition_timestamp']" + ") ";
    if (storageFormat == HiveStorageFormat.AVRO) {
        createTable = createTable.replace(" _smallint SMALLINT,", " _smallint INTEGER,");
        createTable = createTable.replace(" _tinyint TINYINT,", " _tinyint INTEGER,");
    }
    assertUpdate(session, createTable);
    TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table");
    assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
    List<String> partitionedBy = ImmutableList.of("_partition_string", "_partition_varchar", "_partition_char", "_partition_tinyint", "_partition_smallint", "_partition_integer", "_partition_bigint", "_partition_boolean", "_partition_decimal_short", "_partition_decimal_long", "_partition_date", "_partition_timestamp");
    assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), partitionedBy);
    for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
        boolean partitionKey = partitionedBy.contains(columnMetadata.getName());
        assertEquals(columnMetadata.getExtraInfo(), columnExtraInfo(partitionKey));
    }
    assertColumnType(tableMetadata, "_string", createUnboundedVarcharType());
    assertColumnType(tableMetadata, "_varchar", createVarcharType(65535));
    assertColumnType(tableMetadata, "_char", createCharType(10));
    assertColumnType(tableMetadata, "_partition_string", createUnboundedVarcharType());
    assertColumnType(tableMetadata, "_partition_varchar", createVarcharType(65535));
    MaterializedResult result = computeActual("SELECT * from test_partitioned_table");
    assertEquals(result.getRowCount(), 0);
    @Language("SQL") String select = "" + "SELECT" + " 'foo' _string" + ", 'bar' _varchar" + ", CAST('boo' AS CHAR(10)) _char" + ", CAST(1 AS BIGINT) _bigint" + ", 2 _integer" + ", CAST (3 AS SMALLINT) _smallint" + ", CAST (4 AS TINYINT) _tinyint" + ", CAST('123.45' AS REAL) _real" + ", CAST('3.14' AS DOUBLE) _double" + ", true _boolean" + ", CAST('3.14' AS DECIMAL(3,2)) _decimal_short" + ", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _decimal_long" + ", 'foo' _partition_string" + ", 'bar' _partition_varchar" + ", CAST('boo' AS CHAR(10)) _partition_char" + ", CAST(1 AS TINYINT) _partition_tinyint" + ", CAST(1 AS SMALLINT) _partition_smallint" + ", 1 _partition_integer" + ", CAST (1 AS BIGINT) _partition_bigint" + ", true _partition_boolean" + ", CAST('3.14' AS DECIMAL(3,2)) _partition_decimal_short" + ", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _partition_decimal_long" + ", CAST('2017-05-01' AS DATE) _partition_date" + ", CAST('2017-05-01 10:12:34' AS TIMESTAMP) _partition_timestamp";
    if (storageFormat == HiveStorageFormat.AVRO) {
        select = select.replace(" CAST (3 AS SMALLINT) _smallint,", " 3 _smallint,");
        select = select.replace(" CAST (4 AS TINYINT) _tinyint,", " 4 _tinyint,");
    }
    assertUpdate(session, "INSERT INTO test_partitioned_table " + select, 1);
    assertQuery(session, "SELECT * from test_partitioned_table", select);
    assertQuery(session, "SELECT * from test_partitioned_table WHERE" + " 'foo' = _partition_string" + " AND 'bar' = _partition_varchar" + " AND CAST('boo' AS CHAR(10)) = _partition_char" + " AND CAST(1 AS TINYINT) = _partition_tinyint" + " AND CAST(1 AS SMALLINT) = _partition_smallint" + " AND 1 = _partition_integer" + " AND CAST(1 AS BIGINT) = _partition_bigint" + " AND true = _partition_boolean" + " AND CAST('3.14' AS DECIMAL(3,2)) = _partition_decimal_short" + " AND CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) = _partition_decimal_long" + " AND CAST('2017-05-01' AS DATE) = _partition_date" + " AND CAST('2017-05-01 10:12:34' AS TIMESTAMP) = _partition_timestamp", select);
    assertUpdate(session, "DROP TABLE test_partitioned_table");
    assertFalse(getQueryRunner().tableExists(session, "test_partitioned_table"));
}
Also used : TableMetadata(com.facebook.presto.metadata.TableMetadata) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) Language(org.intellij.lang.annotations.Language) MaterializedResult(com.facebook.presto.testing.MaterializedResult)

Aggregations

Language (org.intellij.lang.annotations.Language)111 TableMetadata (com.facebook.presto.metadata.TableMetadata)25 Test (org.testng.annotations.Test)24 MaterializedResult (com.facebook.presto.testing.MaterializedResult)19 PsiFile (com.intellij.psi.PsiFile)18 MaterializedRow (com.facebook.presto.testing.MaterializedRow)11 List (java.util.List)11 Optional (java.util.Optional)11 Test (org.junit.Test)11 ImmutableList (com.google.common.collect.ImmutableList)10 Session (com.facebook.presto.Session)9 Constraint (com.facebook.presto.spi.Constraint)9 AbstractTestIntegrationSmokeTest (com.facebook.presto.tests.AbstractTestIntegrationSmokeTest)9 BasePlanTest (com.facebook.presto.sql.planner.assertions.BasePlanTest)8 ColumnConstraint (com.facebook.presto.sql.planner.planPrinter.IOPlanPrinter.ColumnConstraint)8 ImmutableMap (com.google.common.collect.ImmutableMap)8 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)7 ExpectedValueProvider (com.facebook.presto.sql.planner.assertions.ExpectedValueProvider)7 PlanMatchPattern (com.facebook.presto.sql.planner.assertions.PlanMatchPattern)7 SortOrder (com.facebook.presto.common.block.SortOrder)6