Search in sources :

Example 6 with ColumnDefinition

use of com.facebook.presto.sql.tree.ColumnDefinition in project presto by prestodb.

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(com.facebook.presto.security.AllowAllAccessControl) CreateTable(com.facebook.presto.sql.tree.CreateTable) ColumnDefinition(com.facebook.presto.sql.tree.ColumnDefinition) Test(org.testng.annotations.Test)

Example 7 with ColumnDefinition

use of com.facebook.presto.sql.tree.ColumnDefinition in project presto by prestodb.

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(com.facebook.presto.security.AllowAllAccessControl) CreateTable(com.facebook.presto.sql.tree.CreateTable) PrestoException(com.facebook.presto.spi.PrestoException) ColumnDefinition(com.facebook.presto.sql.tree.ColumnDefinition) Test(org.testng.annotations.Test)

Example 8 with ColumnDefinition

use of com.facebook.presto.sql.tree.ColumnDefinition in project presto by prestodb.

the class AddColumnTask method execute.

@Override
public ListenableFuture<?> execute(AddColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getName());
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        if (!statement.isTableExists()) {
            throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
        }
        return immediateFuture(null);
    }
    Optional<ConnectorMaterializedViewDefinition> optionalMaterializedView = metadata.getMaterializedView(session, tableName);
    if (optionalMaterializedView.isPresent()) {
        if (!statement.isTableExists()) {
            throw new SemanticException(NOT_SUPPORTED, statement, "'%s' is a materialized view, and add column is not supported", tableName);
        }
        return immediateFuture(null);
    }
    ConnectorId connectorId = metadata.getCatalogHandle(session, tableName.getCatalogName()).orElseThrow(() -> new PrestoException(NOT_FOUND, "Catalog does not exist: " + tableName.getCatalogName()));
    accessControl.checkCanAddColumns(session.getRequiredTransactionId(), session.getIdentity(), session.getAccessControlContext(), tableName);
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle.get());
    ColumnDefinition element = statement.getColumn();
    Type type;
    try {
        type = metadata.getType(parseTypeSignature(element.getType()));
    } catch (IllegalArgumentException e) {
        throw new SemanticException(TYPE_MISMATCH, element, "Unknown type '%s' for column '%s'", element.getType(), element.getName());
    }
    if (type.equals(UNKNOWN)) {
        throw new SemanticException(TYPE_MISMATCH, element, "Unknown type '%s' for column '%s'", element.getType(), element.getName());
    }
    if (columnHandles.containsKey(element.getName().getValue().toLowerCase(ENGLISH))) {
        if (!statement.isColumnNotExists()) {
            throw new SemanticException(COLUMN_ALREADY_EXISTS, statement, "Column '%s' already exists", element.getName());
        }
        return immediateFuture(null);
    }
    if (!element.isNullable() && !metadata.getConnectorCapabilities(session, connectorId).contains(NOT_NULL_COLUMN_CONSTRAINT)) {
        throw new SemanticException(NOT_SUPPORTED, element, "Catalog '%s' does not support NOT NULL for column '%s'", connectorId.getCatalogName(), element.getName());
    }
    Map<String, Expression> sqlProperties = mapFromProperties(element.getProperties());
    Map<String, Object> columnProperties = metadata.getColumnPropertyManager().getProperties(connectorId, tableName.getCatalogName(), sqlProperties, session, metadata, parameters);
    ColumnMetadata column = new ColumnMetadata(element.getName().getValue(), type, element.isNullable(), element.getComment().orElse(null), null, false, columnProperties);
    metadata.addColumn(session, tableHandle.get(), column);
    return immediateFuture(null);
}
Also used : ColumnHandle(com.facebook.presto.spi.ColumnHandle) ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) ConnectorMaterializedViewDefinition(com.facebook.presto.spi.ConnectorMaterializedViewDefinition) PrestoException(com.facebook.presto.spi.PrestoException) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName) ColumnDefinition(com.facebook.presto.sql.tree.ColumnDefinition) Type(com.facebook.presto.common.type.Type) Expression(com.facebook.presto.sql.tree.Expression) TableHandle(com.facebook.presto.spi.TableHandle) SemanticException(com.facebook.presto.sql.analyzer.SemanticException) ConnectorId(com.facebook.presto.spi.ConnectorId)

Example 9 with ColumnDefinition

use of com.facebook.presto.sql.tree.ColumnDefinition in project presto by prestodb.

the class TestCreateTableTask method testCreateWithNotNullColumns.

@Test
public void testCreateWithNotNullColumns() {
    metadata.setConnectorCapabilities(NOT_NULL_COLUMN_CONSTRAINT);
    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()));
    assertEquals(metadata.getCreateTableCallCount(), 1);
    List<ColumnMetadata> columns = metadata.getReceivedTableMetadata().get(0).getColumns();
    assertEquals(columns.size(), 3);
    assertEquals(columns.get(0).getName(), "a");
    assertEquals(columns.get(0).getType().getDisplayName().toUpperCase(ENGLISH), "DATE");
    assertTrue(columns.get(0).isNullable());
    assertEquals(columns.get(1).getName(), "b");
    assertEquals(columns.get(1).getType().getDisplayName().toUpperCase(ENGLISH), "VARCHAR");
    assertFalse(columns.get(1).isNullable());
    assertEquals(columns.get(2).getName(), "c");
    assertEquals(columns.get(2).getType().getDisplayName().toUpperCase(ENGLISH), "VARBINARY");
    assertFalse(columns.get(2).isNullable());
}
Also used : ColumnMetadata(com.facebook.presto.spi.ColumnMetadata) AllowAllAccessControl(com.facebook.presto.security.AllowAllAccessControl) CreateTable(com.facebook.presto.sql.tree.CreateTable) TableElement(com.facebook.presto.sql.tree.TableElement) ColumnDefinition(com.facebook.presto.sql.tree.ColumnDefinition) Test(org.testng.annotations.Test)

Example 10 with ColumnDefinition

use of com.facebook.presto.sql.tree.ColumnDefinition in project presto by prestodb.

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(com.facebook.presto.security.AllowAllAccessControl) CreateTable(com.facebook.presto.sql.tree.CreateTable) TableElement(com.facebook.presto.sql.tree.TableElement) ColumnDefinition(com.facebook.presto.sql.tree.ColumnDefinition) Test(org.testng.annotations.Test)

Aggregations

ColumnDefinition (com.facebook.presto.sql.tree.ColumnDefinition)10 CreateTable (com.facebook.presto.sql.tree.CreateTable)6 Test (org.testng.annotations.Test)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)5 MetadataUtil.createQualifiedObjectName (com.facebook.presto.metadata.MetadataUtil.createQualifiedObjectName)4 AllowAllAccessControl (com.facebook.presto.security.AllowAllAccessControl)4 PrestoException (com.facebook.presto.spi.PrestoException)4 SemanticException (com.facebook.presto.sql.analyzer.SemanticException)4 TableElement (com.facebook.presto.sql.tree.TableElement)4 Session (com.facebook.presto.Session)3 LikeClause (com.facebook.presto.sql.tree.LikeClause)3 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)2 Type (com.facebook.presto.common.type.Type)2 QualifiedObjectName (com.facebook.presto.metadata.QualifiedObjectName)2 TableHandle (com.facebook.presto.metadata.TableHandle)2 TableMetadata (com.facebook.presto.metadata.TableMetadata)2 ColumnHandle (com.facebook.presto.spi.ColumnHandle)2 ConnectorId (com.facebook.presto.spi.ConnectorId)2 ConnectorTableMetadata (com.facebook.presto.spi.ConnectorTableMetadata)2 TableHandle (com.facebook.presto.spi.TableHandle)2