Search in sources :

Example 1 with CreateTableStmtBuilder

use of org.apache.drill.exec.store.jdbc.utils.CreateTableStmtBuilder in project drill by apache.

the class JdbcRecordWriter method updateSchema.

@Override
public void updateSchema(VectorAccessible batch) {
    BatchSchema schema = batch.getSchema();
    String columnName;
    MinorType type;
    String sql;
    boolean nullable = false;
    CreateTableStmtBuilder queryBuilder = new CreateTableStmtBuilder(tableName, dialect);
    for (MaterializedField field : schema) {
        columnName = JdbcDDLQueryUtils.addBackTicksToField(field.getName());
        type = field.getType().getMinorType();
        logger.debug("Adding column {} of type {}.", columnName, type);
        if (field.getType().getMode() == DataMode.REPEATED) {
            throw UserException.dataWriteError().message("Drill does not yet support writing arrays to JDBC. " + columnName + " is an array.").build(logger);
        }
        if (field.getType().getMode() == DataMode.OPTIONAL) {
            nullable = true;
        }
        int precision = field.getPrecision();
        int scale = field.getScale();
        queryBuilder.addColumn(columnName, field.getType().getMinorType(), nullable, precision, scale);
    }
    sql = queryBuilder.build().getCreateTableQuery();
    sql = JdbcDDLQueryUtils.cleanDDLQuery(sql, dialect);
    logger.debug("Final query: {}", sql);
    // Execute the query to build the schema
    try (Statement statement = connection.createStatement()) {
        logger.debug("Executing CREATE query: {}", sql);
        statement.execute(sql);
    } catch (SQLException e) {
        throw UserException.dataReadError(e).message("The JDBC storage plugin failed while trying to create the schema. ").addContext("Sql", sql).build(logger);
    }
}
Also used : BatchSchema(org.apache.drill.exec.record.BatchSchema) SQLException(java.sql.SQLException) Statement(java.sql.Statement) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) MaterializedField(org.apache.drill.exec.record.MaterializedField) CreateTableStmtBuilder(org.apache.drill.exec.store.jdbc.utils.CreateTableStmtBuilder)

Aggregations

SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)1 BatchSchema (org.apache.drill.exec.record.BatchSchema)1 MaterializedField (org.apache.drill.exec.record.MaterializedField)1 CreateTableStmtBuilder (org.apache.drill.exec.store.jdbc.utils.CreateTableStmtBuilder)1