Search in sources :

Example 36 with VarCharHolder

use of org.apache.drill.exec.expr.holders.VarCharHolder in project drill by apache.

the class JdbcRecordWriter method endRecord.

@Override
public void endRecord() throws IOException {
    logger.debug("Ending record");
    // Add values to rowString
    for (int i = 0; i < rowList.size(); i++) {
        if (i > 0) {
            insertQueryBuilder.append(",");
        }
        // Add null value to rowstring
        if (rowList.get(i) instanceof String && ((String) rowList.get(i)).equalsIgnoreCase("null")) {
            insertQueryBuilder.append("null");
            continue;
        }
        JdbcWriterField currentField = fields.get(i);
        if (currentField.getDataType() == MinorType.VARCHAR) {
            String value = null;
            // Get the string value
            if (currentField.getMode() == DataMode.REQUIRED) {
                VarCharHolder varCharHolder = (VarCharHolder) rowList.get(i);
                value = StringFunctionHelpers.getStringFromVarCharHolder(varCharHolder);
            } else {
                try {
                    NullableVarCharHolder nullableVarCharHolder = (NullableVarCharHolder) rowList.get(i);
                    value = StringFunctionHelpers.getStringFromVarCharHolder(nullableVarCharHolder);
                } catch (ClassCastException e) {
                    logger.error("Unable to read field: {}", rowList.get(i));
                }
            }
            // Add to value string
            insertQueryBuilder.literal(value);
        } else if (currentField.getDataType() == MinorType.DATE) {
            String dateString = formatDateForInsertQuery((Long) rowList.get(i));
            insertQueryBuilder.literal(dateString);
        } else if (currentField.getDataType() == MinorType.TIME) {
            String timeString = formatTimeForInsertQuery((Integer) rowList.get(i));
            insertQueryBuilder.literal(timeString);
        } else if (currentField.getDataType() == MinorType.TIMESTAMP) {
            String timeString = formatTimeStampForInsertQuery((Long) rowList.get(i));
            insertQueryBuilder.literal(timeString);
        } else {
            if (Strings.isNullOrEmpty(rowList.get(i).toString())) {
                insertQueryBuilder.append("null");
            } else {
                insertQueryBuilder.append(rowList.get(i).toString());
            }
        }
    }
    recordCount++;
    firstRecord = false;
    insertQueryBuilder.append(")");
    if (recordCount >= config.getPlugin().getConfig().getWriterBatchSize()) {
        // Execute the insert query
        String insertQuery = insertQueryBuilder.toString();
        executeInsert(insertQuery);
        // Reset the batch
        recordCount = 0;
        firstRecord = true;
        insertQueryBuilder = initializeInsertQuery();
    }
    rowList.clear();
}
Also used : NullableVarCharHolder(org.apache.drill.exec.expr.holders.NullableVarCharHolder) VarCharHolder(org.apache.drill.exec.expr.holders.VarCharHolder) NullableVarCharHolder(org.apache.drill.exec.expr.holders.NullableVarCharHolder)

Aggregations

VarCharHolder (org.apache.drill.exec.expr.holders.VarCharHolder)36 Test (org.junit.Test)23 UnlikelyTest (org.apache.drill.categories.UnlikelyTest)20 VectorTest (org.apache.drill.categories.VectorTest)20 ExecTest (org.apache.drill.exec.ExecTest)20 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)5 FunctionImplementationRegistry (org.apache.drill.exec.expr.fn.FunctionImplementationRegistry)4 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)4 BaseWriter (org.apache.drill.exec.vector.complex.writer.BaseWriter)4 SchemaPath (org.apache.drill.common.expression.SchemaPath)3 PhysicalPlan (org.apache.drill.exec.physical.PhysicalPlan)3 FragmentRoot (org.apache.drill.exec.physical.base.FragmentRoot)3 PhysicalPlanReader (org.apache.drill.exec.planner.PhysicalPlanReader)3 VarCharVector (org.apache.drill.exec.vector.VarCharVector)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 BitHolder (org.apache.drill.exec.expr.holders.BitHolder)2 VarDecimalHolder (org.apache.drill.exec.expr.holders.VarDecimalHolder)2 DrillBuf (io.netty.buffer.DrillBuf)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1