Search in sources :

Example 1 with CreateTable

use of io.prestosql.sql.tree.CreateTable in project hetu-core by openlookeng.

the class TestSqlParser method testCreateTable.

@Test
public void testCreateTable() {
    assertStatement("CREATE TABLE foo (a VARCHAR, b BIGINT COMMENT 'hello world', c IPADDRESS)", new CreateTable(QualifiedName.of("foo"), ImmutableList.of(new ColumnDefinition(identifier("a"), "VARCHAR", true, emptyList(), Optional.empty()), new ColumnDefinition(identifier("b"), "BIGINT", true, emptyList(), Optional.of("hello world")), new ColumnDefinition(identifier("c"), "IPADDRESS", true, emptyList(), Optional.empty())), false, ImmutableList.of(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", true, emptyList(), Optional.empty())), true, ImmutableList.of(), Optional.empty()));
    // with properties
    assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP WITH (nullable = true, compression = 'LZ4'))", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", true, ImmutableList.of(new Property(new Identifier("nullable"), BooleanLiteral.TRUE_LITERAL), new Property(new Identifier("compression"), new StringLiteral("LZ4"))), Optional.empty())), true, ImmutableList.of(), Optional.empty()));
    // with LIKE
    assertStatement("CREATE TABLE IF NOT EXISTS bar (LIKE like_table)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new LikeClause(QualifiedName.of("like_table"), Optional.empty())), true, ImmutableList.of(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP, LIKE like_table)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", true, emptyList(), Optional.empty()), new LikeClause(QualifiedName.of("like_table"), Optional.empty())), true, ImmutableList.of(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP, LIKE like_table, d DATE)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", true, emptyList(), Optional.empty()), new LikeClause(QualifiedName.of("like_table"), Optional.empty()), new ColumnDefinition(identifier("d"), "DATE", true, emptyList(), Optional.empty())), true, ImmutableList.of(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS bar (LIKE like_table INCLUDING PROPERTIES)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new LikeClause(QualifiedName.of("like_table"), Optional.of(LikeClause.PropertiesOption.INCLUDING))), true, ImmutableList.of(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP, LIKE like_table EXCLUDING PROPERTIES)", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", true, emptyList(), Optional.empty()), new LikeClause(QualifiedName.of("like_table"), Optional.of(LikeClause.PropertiesOption.EXCLUDING))), true, ImmutableList.of(), Optional.empty()));
    assertStatement("CREATE TABLE IF NOT EXISTS bar (c TIMESTAMP, LIKE like_table EXCLUDING PROPERTIES) COMMENT 'test'", new CreateTable(QualifiedName.of("bar"), ImmutableList.of(new ColumnDefinition(identifier("c"), "TIMESTAMP", true, emptyList(), Optional.empty()), new LikeClause(QualifiedName.of("like_table"), Optional.of(LikeClause.PropertiesOption.EXCLUDING))), true, ImmutableList.of(), Optional.of("test")));
}
Also used : LikeClause(io.prestosql.sql.tree.LikeClause) Identifier(io.prestosql.sql.tree.Identifier) QueryUtil.quotedIdentifier(io.prestosql.sql.QueryUtil.quotedIdentifier) StringLiteral(io.prestosql.sql.tree.StringLiteral) CreateTable(io.prestosql.sql.tree.CreateTable) Property(io.prestosql.sql.tree.Property) ColumnDefinition(io.prestosql.sql.tree.ColumnDefinition) Test(org.testng.annotations.Test)

Example 2 with CreateTable

use of io.prestosql.sql.tree.CreateTable in project hetu-core by openlookeng.

the class QueryRewriter method rewriteInsertQuery.

private Query rewriteInsertQuery(Connection connection, Query query, Insert statement) throws SQLException, QueryRewriteException {
    QualifiedName temporaryTableName = generateTemporaryTableName(statement.getTarget());
    Statement createTemporaryTable = new CreateTable(temporaryTableName, ImmutableList.of(new LikeClause(statement.getTarget(), Optional.of(INCLUDING))), true, ImmutableList.of(), Optional.empty());
    String createTemporaryTableSql = formatSql(createTemporaryTable, Optional.empty());
    String insertSql = formatSql(new Insert(temporaryTableName, statement.getColumns(), statement.getQuery(), statement.getOverwrite()), Optional.empty());
    String checksumSql = checksumSql(getColumnsForTable(connection, query.getCatalog(), query.getSchema(), statement.getTarget().toString()), temporaryTableName);
    String dropTableSql = dropTableSql(temporaryTableName);
    return new Query(query.getCatalog(), query.getSchema(), ImmutableList.of(createTemporaryTableSql, insertSql), checksumSql, ImmutableList.of(dropTableSql), query.getUsername(), query.getPassword(), query.getSessionProperties());
}
Also used : LikeClause(io.prestosql.sql.tree.LikeClause) Statement(io.prestosql.sql.tree.Statement) QualifiedName(io.prestosql.sql.tree.QualifiedName) CreateTable(io.prestosql.sql.tree.CreateTable) Insert(io.prestosql.sql.tree.Insert)

Example 3 with CreateTable

use of io.prestosql.sql.tree.CreateTable in project hetu-core by openlookeng.

the class TestCreateTableTask method testCreateTableNotExistsTrue.

@Test
public void testCreateTableNotExistsTrue() {
    CreateTable statement = new CreateTable(QualifiedName.of("test_table"), ImmutableList.of(new ColumnDefinition(identifier("a"), "BIGINT", true, emptyList(), Optional.empty())), true, ImmutableList.of(), Optional.empty());
    getFutureValue(new CreateTableTask().internalExecute(statement, metadata, new AllowAllAccessControl(), testSession, emptyList()));
    assertEquals(metadata.getCreateTableCallCount(), 1);
}
Also used : AllowAllAccessControl(io.prestosql.security.AllowAllAccessControl) CreateTable(io.prestosql.sql.tree.CreateTable) ColumnDefinition(io.prestosql.sql.tree.ColumnDefinition) Test(org.testng.annotations.Test)

Example 4 with CreateTable

use of io.prestosql.sql.tree.CreateTable in project hetu-core by openlookeng.

the class TestCreateTableTask method testCreateTableNotExistsFalse.

@Test
public void testCreateTableNotExistsFalse() {
    CreateTable statement = new CreateTable(QualifiedName.of("test_table"), ImmutableList.of(new ColumnDefinition(identifier("a"), "BIGINT", true, emptyList(), Optional.empty())), false, ImmutableList.of(), Optional.empty());
    try {
        getFutureValue(new CreateTableTask().internalExecute(statement, metadata, new AllowAllAccessControl(), testSession, emptyList()));
        fail("expected exception");
    } catch (RuntimeException e) {
        // Expected
        assertTrue(e instanceof PrestoException);
        PrestoException prestoException = (PrestoException) e;
        assertEquals(prestoException.getErrorCode(), ALREADY_EXISTS.toErrorCode());
    }
    assertEquals(metadata.getCreateTableCallCount(), 1);
}
Also used : AllowAllAccessControl(io.prestosql.security.AllowAllAccessControl) CreateTable(io.prestosql.sql.tree.CreateTable) PrestoException(io.prestosql.spi.PrestoException) ColumnDefinition(io.prestosql.sql.tree.ColumnDefinition) Test(org.testng.annotations.Test)

Example 5 with CreateTable

use of io.prestosql.sql.tree.CreateTable in project hetu-core by openlookeng.

the class TestCreateTableTask method testCreateWithUnsupportedConnectorThrowsWhenNotNull.

@Test(expectedExceptions = SemanticException.class, expectedExceptionsMessageRegExp = ".*does not support non-null column for column name 'b'")
public void testCreateWithUnsupportedConnectorThrowsWhenNotNull() {
    List<TableElement> inputColumns = ImmutableList.of(new ColumnDefinition(identifier("a"), "DATE", true, emptyList(), Optional.empty()), new ColumnDefinition(identifier("b"), "VARCHAR", false, emptyList(), Optional.empty()), new ColumnDefinition(identifier("c"), "VARBINARY", false, emptyList(), Optional.empty()));
    CreateTable statement = new CreateTable(QualifiedName.of("test_table"), inputColumns, true, ImmutableList.of(), Optional.empty());
    getFutureValue(new CreateTableTask().internalExecute(statement, metadata, new AllowAllAccessControl(), testSession, emptyList()));
}
Also used : AllowAllAccessControl(io.prestosql.security.AllowAllAccessControl) CreateTable(io.prestosql.sql.tree.CreateTable) TableElement(io.prestosql.sql.tree.TableElement) ColumnDefinition(io.prestosql.sql.tree.ColumnDefinition) Test(org.testng.annotations.Test)

Aggregations

CreateTable (io.prestosql.sql.tree.CreateTable)12 ColumnDefinition (io.prestosql.sql.tree.ColumnDefinition)8 TableElement (io.prestosql.sql.tree.TableElement)7 Identifier (io.prestosql.sql.tree.Identifier)6 Test (org.testng.annotations.Test)6 Expression (io.prestosql.sql.tree.Expression)5 LikeClause (io.prestosql.sql.tree.LikeClause)5 Property (io.prestosql.sql.tree.Property)5 StringLiteral (io.prestosql.sql.tree.StringLiteral)5 AllowAllAccessControl (io.prestosql.security.AllowAllAccessControl)4 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)4 ArithmeticUnaryExpression (io.prestosql.sql.tree.ArithmeticUnaryExpression)4 CoalesceExpression (io.prestosql.sql.tree.CoalesceExpression)4 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)4 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)4 IfExpression (io.prestosql.sql.tree.IfExpression)4 InListExpression (io.prestosql.sql.tree.InListExpression)4 LogicalBinaryExpression (io.prestosql.sql.tree.LogicalBinaryExpression)4 NotExpression (io.prestosql.sql.tree.NotExpression)4 NullIfExpression (io.prestosql.sql.tree.NullIfExpression)4