Search in sources :

Example 11 with ColumnDefinition

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

the class AddColumnTask method execute.

@Override
public ListenableFuture<?> execute(AddColumn statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, QueryStateMachine stateMachine, List<Expression> parameters, HeuristicIndexerManager heuristicIndexerManager) {
    Session session = stateMachine.getSession();
    QualifiedObjectName tableName = createQualifiedObjectName(session, statement, statement.getName());
    Optional<CubeMetaStore> optionalCubeMetaStore = this.cubeManager.getMetaStore(STAR_TREE);
    if (optionalCubeMetaStore.isPresent() && optionalCubeMetaStore.get().getMetadataFromCubeName(tableName.toString()).isPresent()) {
        throw new SemanticException(ALTER_TABLE_ON_CUBE, statement, "Operation not permitted. %s is a Cube table, use CUBE statements instead.", tableName);
    }
    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
        throw new SemanticException(MISSING_TABLE, statement, "Table '%s' does not exist", tableName);
    }
    CatalogName catalogName = metadata.getCatalogHandle(session, tableName.getCatalogName()).orElseThrow(() -> new PrestoException(NOT_FOUND, "Catalog does not exist: " + tableName.getCatalogName()));
    accessControl.checkCanAddColumns(session.getRequiredTransactionId(), session.getIdentity(), tableName);
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle.get());
    ColumnDefinition element = statement.getColumn();
    Type type;
    try {
        type = metadata.getType(parseTypeSignature(element.getType()));
    } catch (TypeNotFoundException 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))) {
        throw new SemanticException(COLUMN_ALREADY_EXISTS, statement, "Column '%s' already exists", element.getName());
    }
    if (!element.isNullable() && !metadata.getConnectorCapabilities(session, catalogName).contains(NOT_NULL_COLUMN_CONSTRAINT)) {
        throw new SemanticException(NOT_SUPPORTED, element, "Catalog '%s' does not support NOT NULL for column '%s'", catalogName.getCatalogName(), element.getName());
    }
    Map<String, Expression> sqlProperties = mapFromProperties(element.getProperties());
    Map<String, Object> columnProperties = metadata.getColumnPropertyManager().getProperties(catalogName, 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(io.prestosql.spi.connector.ColumnHandle) ColumnMetadata(io.prestosql.spi.connector.ColumnMetadata) CubeMetaStore(io.hetu.core.spi.cube.io.CubeMetaStore) PrestoException(io.prestosql.spi.PrestoException) QualifiedObjectName(io.prestosql.spi.connector.QualifiedObjectName) MetadataUtil.createQualifiedObjectName(io.prestosql.metadata.MetadataUtil.createQualifiedObjectName) ColumnDefinition(io.prestosql.sql.tree.ColumnDefinition) Type(io.prestosql.spi.type.Type) Expression(io.prestosql.sql.tree.Expression) TypeNotFoundException(io.prestosql.spi.type.TypeNotFoundException) TableHandle(io.prestosql.spi.metadata.TableHandle) CatalogName(io.prestosql.spi.connector.CatalogName) Session(io.prestosql.Session) SemanticException(io.prestosql.sql.analyzer.SemanticException)

Example 12 with ColumnDefinition

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

the class ImpalaAstBuilder method visitAddColumns.

@Override
public Node visitAddColumns(ImpalaSqlParser.AddColumnsContext context) {
    if (context.EXISTS() != null) {
        addDiff(DiffType.UNSUPPORTED, context.EXISTS().getText(), "[IF NOT EXISTS] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "[IF NOT EXISTS] is not supported", context);
    }
    if (context.columnSpecWithKudu().size() > 1) {
        // does not support add multiple column
        addDiff(DiffType.UNSUPPORTED, context.columnSpecWithKudu(1).getText(), "adding [Multiple columns] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "adding [Multiple columns] is not supported", context);
    }
    if (context.columnSpecWithKudu(0).kuduAttributes() != null) {
        addDiff(DiffType.UNSUPPORTED, context.columnSpecWithKudu(0).kuduAttributes().getText(), "[KUDU Attribute] is not supported");
        throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "[KUDU Attribute] is not supported", context);
    }
    Optional<String> comment = Optional.empty();
    if (context.columnSpecWithKudu(0).COMMENT() != null) {
        comment = Optional.of(((StringLiteral) visit(context.columnSpecWithKudu(0).string())).getValue());
    }
    addDiff(DiffType.MODIFIED, context.COLUMNS().getText(), "COLUMN", "[COLUMNS] is modified to column");
    ColumnDefinition columnDefinition = new ColumnDefinition((Identifier) visit(context.columnSpecWithKudu(0).identifier()), getType(context.columnSpecWithKudu(0).type()), true, new ArrayList<>(), comment);
    return new AddColumn(getLocation(context), getQualifiedName(context.qualifiedName()), columnDefinition);
}
Also used : StringLiteral(io.prestosql.sql.tree.StringLiteral) ColumnDefinition(io.prestosql.sql.tree.ColumnDefinition) AddColumn(io.prestosql.sql.tree.AddColumn)

Example 13 with ColumnDefinition

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

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(io.prestosql.spi.connector.ColumnMetadata) 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

ColumnDefinition (io.prestosql.sql.tree.ColumnDefinition)13 CreateTable (io.prestosql.sql.tree.CreateTable)8 StringLiteral (io.prestosql.sql.tree.StringLiteral)6 Test (org.testng.annotations.Test)6 TableElement (io.prestosql.sql.tree.TableElement)5 AllowAllAccessControl (io.prestosql.security.AllowAllAccessControl)4 AddColumn (io.prestosql.sql.tree.AddColumn)4 Expression (io.prestosql.sql.tree.Expression)4 Identifier (io.prestosql.sql.tree.Identifier)4 PrestoException (io.prestosql.spi.PrestoException)3 ColumnMetadata (io.prestosql.spi.connector.ColumnMetadata)3 Property (io.prestosql.sql.tree.Property)3 HiveSqlParser (io.hetu.core.migration.source.hive.HiveSqlParser)2 Session (io.prestosql.Session)2 MetadataUtil.createQualifiedObjectName (io.prestosql.metadata.MetadataUtil.createQualifiedObjectName)2 CatalogName (io.prestosql.spi.connector.CatalogName)2 QualifiedObjectName (io.prestosql.spi.connector.QualifiedObjectName)2 TableHandle (io.prestosql.spi.metadata.TableHandle)2 Type (io.prestosql.spi.type.Type)2 TypeNotFoundException (io.prestosql.spi.type.TypeNotFoundException)2