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);
}
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);
}
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);
}
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());
}
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()));
}
Aggregations