Search in sources :

Example 66 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Formula method calcFields.

private Object[] calcFields(RowMetaInterface rowMeta, Object[] r) throws KettleValueException {
    try {
        Object[] outputRowData = RowDataUtil.createResizedCopy(r, data.outputRowMeta.size());
        int tempIndex = rowMeta.size();
        // Assign this tempRowData to the formula context
        // 
        data.context.setRowData(outputRowData);
        // 
        if (data.formulas == null) {
            // Create a set of LValues to put the parsed results in...
            data.formulas = new org.pentaho.reporting.libraries.formula.Formula[meta.getFormula().length];
            for (int i = 0; i < meta.getFormula().length; i++) {
                FormulaMetaFunction fn = meta.getFormula()[i];
                if (!Utils.isEmpty(fn.getFieldName())) {
                    data.formulas[i] = data.createFormula(meta.getFormula()[i].getFormula());
                } else {
                    throw new KettleException("Unable to find field name for formula [" + Const.NVL(fn.getFormula(), "") + "]");
                }
            }
        }
        for (int i = 0; i < meta.getFormula().length; i++) {
            FormulaMetaFunction fn = meta.getFormula()[i];
            if (!Utils.isEmpty(fn.getFieldName())) {
                if (data.formulas[i] == null) {
                    data.formulas[i] = data.createFormula(meta.getFormula()[i].getFormula());
                }
                // this is main part of all this step: calculate formula
                Object formulaResult = data.formulas[i].evaluate();
                if (formulaResult instanceof LibFormulaErrorValue) {
                    // inspect why it is happens to get clear error message.
                    throw new KettleException("Error calculate formula. Formula " + fn.getFormula() + " output field: " + fn.getFieldName() + ", error is: " + formulaResult.toString());
                }
                // for most cases we can try to convert data on a fly.
                if (data.returnType[i] < 0) {
                    if (formulaResult instanceof String) {
                        data.returnType[i] = FormulaData.RETURN_TYPE_STRING;
                        fn.setNeedDataConversion(fn.getValueType() != ValueMetaInterface.TYPE_STRING);
                    } else if (formulaResult instanceof Integer) {
                        data.returnType[i] = FormulaData.RETURN_TYPE_INTEGER;
                        fn.setNeedDataConversion(fn.getValueType() != ValueMetaInterface.TYPE_INTEGER);
                    } else if (formulaResult instanceof Long) {
                        data.returnType[i] = FormulaData.RETURN_TYPE_LONG;
                        fn.setNeedDataConversion(fn.getValueType() != ValueMetaInterface.TYPE_INTEGER);
                    } else if (formulaResult instanceof Date) {
                        data.returnType[i] = FormulaData.RETURN_TYPE_DATE;
                        fn.setNeedDataConversion(fn.getValueType() != ValueMetaInterface.TYPE_DATE);
                    } else if (formulaResult instanceof BigDecimal) {
                        data.returnType[i] = FormulaData.RETURN_TYPE_BIGDECIMAL;
                        fn.setNeedDataConversion(fn.getValueType() != ValueMetaInterface.TYPE_BIGNUMBER);
                    } else if (formulaResult instanceof Number) {
                        data.returnType[i] = FormulaData.RETURN_TYPE_NUMBER;
                        fn.setNeedDataConversion(fn.getValueType() != ValueMetaInterface.TYPE_NUMBER);
                    // this types we will not make attempt to auto-convert
                    } else if (formulaResult instanceof byte[]) {
                        data.returnType[i] = FormulaData.RETURN_TYPE_BYTE_ARRAY;
                        if (fn.getValueType() != ValueMetaInterface.TYPE_BINARY) {
                            throw new KettleValueException("Please specify a Binary type for field [" + fn.getFieldName() + "] as a result of formula [" + fn.getFormula() + "]");
                        }
                    } else if (formulaResult instanceof Boolean) {
                        data.returnType[i] = FormulaData.RETURN_TYPE_BOOLEAN;
                        if (fn.getValueType() != ValueMetaInterface.TYPE_BOOLEAN) {
                            throw new KettleValueException("Please specify a Boolean type for field [" + fn.getFieldName() + "] as a result of formula [" + fn.getFormula() + "]");
                        }
                    } else {
                        data.returnType[i] = FormulaData.RETURN_TYPE_STRING;
                        fn.setNeedDataConversion(fn.getValueType() != ValueMetaInterface.TYPE_STRING);
                    }
                }
                int realIndex = (data.replaceIndex[i] < 0) ? tempIndex++ : data.replaceIndex[i];
                outputRowData[realIndex] = getReturnValue(formulaResult, data.returnType[i], realIndex, fn);
            }
        }
        return outputRowData;
    } catch (Throwable e) {
        throw new KettleValueException(e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) LibFormulaErrorValue(org.pentaho.reporting.libraries.formula.LibFormulaErrorValue) Date(java.util.Date) BigDecimal(java.math.BigDecimal) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 67 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Janino method calcFields.

private Object[] calcFields(RowMetaInterface rowMeta, Object[] r) throws KettleValueException {
    try {
        Object[] outputRowData = RowDataUtil.createResizedCopy(r, data.outputRowMeta.size());
        int tempIndex = rowMeta.size();
        // 
        if (data.expressionEvaluators == null) {
            data.expressionEvaluators = new ExpressionEvaluator[meta.getFormula().length];
            data.argumentIndexes = new ArrayList<List<Integer>>();
            for (int i = 0; i < meta.getFormula().length; i++) {
                List<Integer> argIndexes = new ArrayList<Integer>();
                data.argumentIndexes.add(argIndexes);
            }
            for (int m = 0; m < meta.getFormula().length; m++) {
                List<Integer> argIndexes = data.argumentIndexes.get(m);
                List<String> parameterNames = new ArrayList<String>();
                List<Class<?>> parameterTypes = new ArrayList<Class<?>>();
                for (int i = 0; i < data.outputRowMeta.size(); i++) {
                    ValueMetaInterface valueMeta = data.outputRowMeta.getValueMeta(i);
                    // 
                    if (meta.getFormula()[m].getFormula().contains(valueMeta.getName())) {
                        // If so, add it to the indexes...
                        argIndexes.add(i);
                        parameterTypes.add(valueMeta.getNativeDataTypeClass());
                        parameterNames.add(valueMeta.getName());
                    }
                }
                JaninoMetaFunction fn = meta.getFormula()[m];
                if (!Utils.isEmpty(fn.getFieldName())) {
                    // Create the expression evaluator: is relatively slow so we do it only for the first row...
                    // 
                    data.expressionEvaluators[m] = new ExpressionEvaluator();
                    data.expressionEvaluators[m].setParameters(parameterNames.toArray(new String[parameterNames.size()]), parameterTypes.toArray(new Class<?>[parameterTypes.size()]));
                    data.expressionEvaluators[m].setReturnType(Object.class);
                    data.expressionEvaluators[m].setThrownExceptions(new Class<?>[] { Exception.class });
                    data.expressionEvaluators[m].cook(fn.getFormula());
                } else {
                    throw new KettleException("Unable to find field name for formula [" + Const.NVL(fn.getFormula(), "") + "]");
                }
            }
        }
        for (int i = 0; i < meta.getFormula().length; i++) {
            List<Integer> argumentIndexes = data.argumentIndexes.get(i);
            // This method can only accept the specified number of values...
            // 
            Object[] argumentData = new Object[argumentIndexes.size()];
            for (int x = 0; x < argumentIndexes.size(); x++) {
                int index = argumentIndexes.get(x);
                ValueMetaInterface outputValueMeta = data.outputRowMeta.getValueMeta(index);
                argumentData[x] = outputValueMeta.convertToNormalStorageType(outputRowData[index]);
            }
            Object formulaResult = data.expressionEvaluators[i].evaluate(argumentData);
            Object value = null;
            if (formulaResult == null) {
                value = null;
            } else {
                ValueMetaInterface valueMeta = data.returnType[i];
                if (valueMeta.getNativeDataTypeClass().isAssignableFrom(formulaResult.getClass())) {
                    value = formulaResult;
                } else if (formulaResult instanceof Integer && valueMeta.getType() == ValueMetaInterface.TYPE_INTEGER) {
                    value = ((Integer) formulaResult).longValue();
                } else {
                    throw new KettleValueException(BaseMessages.getString(PKG, "Janino.Error.ValueTypeMismatch", valueMeta.getTypeDesc(), meta.getFormula()[i].getFieldName(), formulaResult.getClass(), meta.getFormula()[i].getFormula()));
                }
            }
            // 
            if (data.replaceIndex[i] < 0) {
                outputRowData[tempIndex++] = value;
            } else {
                outputRowData[data.replaceIndex[i]] = value;
            }
        }
        return outputRowData;
    } catch (Exception e) {
        throw new KettleValueException(e);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) ArrayList(java.util.ArrayList) ExpressionEvaluator(org.codehaus.janino.ExpressionEvaluator) KettleException(org.pentaho.di.core.exception.KettleException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) ArrayList(java.util.ArrayList) List(java.util.List) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 68 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class Database method getNextValue.

public synchronized Long getNextValue(Hashtable<String, Counter> counters, String schemaName, String tableName, String val_key) throws KettleDatabaseException {
    Long nextValue = null;
    String schemaTable = databaseMeta.getQuotedSchemaTableCombination(schemaName, tableName);
    String lookup = schemaTable + "." + databaseMeta.quoteField(val_key);
    // Try to find the previous sequence value...
    Counter counter = null;
    if (counters != null) {
        counter = counters.get(lookup);
    }
    if (counter == null) {
        RowMetaAndData rmad = getOneRow("SELECT MAX(" + databaseMeta.quoteField(val_key) + ") FROM " + schemaTable);
        if (rmad != null) {
            long previous;
            try {
                Long tmp = rmad.getRowMeta().getInteger(rmad.getData(), 0);
                // null.
                if (tmp != null) {
                    previous = tmp.longValue();
                } else {
                    previous = 0L;
                }
            } catch (KettleValueException e) {
                throw new KettleDatabaseException("Error getting the first long value from the max value returned from table : " + schemaTable);
            }
            counter = new Counter(previous + 1, 1);
            nextValue = Long.valueOf(counter.next());
            if (counters != null) {
                counters.put(lookup, counter);
            }
        } else {
            throw new KettleDatabaseException("Couldn't find maximum key value from table " + schemaTable);
        }
    } else {
        nextValue = Long.valueOf(counter.next());
    }
    return nextValue;
}
Also used : Counter(org.pentaho.di.core.Counter) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) KettleValueException(org.pentaho.di.core.exception.KettleValueException)

Example 69 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class ValueMetaBase method convertStringToBigNumber.

protected synchronized BigDecimal convertStringToBigNumber(String string) throws KettleValueException {
    // see if trimming needs
    string = Const.trimToType(string, getTrimType());
    if (Utils.isEmpty(string)) {
        return null;
    }
    try {
        DecimalFormat format = getDecimalFormat(bigNumberFormatting);
        Number number;
        if (lenientStringToNumber) {
            number = format.parse(string);
        } else {
            ParsePosition parsePosition = new ParsePosition(0);
            number = format.parse(string, parsePosition);
            if (parsePosition.getIndex() < string.length()) {
                throw new KettleValueException(toString() + " : couldn't convert String to number : non-numeric character found at position " + (parsePosition.getIndex() + 1) + " for value [" + string + "]");
            }
        }
        return (BigDecimal) number;
    } catch (Exception e) {
        // 
        try {
            return new BigDecimal(string);
        } catch (NumberFormatException ex) {
            throw new KettleValueException(toString() + " : couldn't convert string value '" + string + "' to a big number.", ex);
        }
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) KettleValueException(org.pentaho.di.core.exception.KettleValueException) BigDecimal(java.math.BigDecimal) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ParseException(java.text.ParseException) KettleEOFException(org.pentaho.di.core.exception.KettleEOFException) EOFException(java.io.EOFException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ParsePosition(java.text.ParsePosition)

Example 70 with KettleValueException

use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.

the class ValueMetaBase method compare.

/**
 * Compare 2 values of the same data type
 *
 * @param data1
 *          the first value
 * @param meta2
 *          the second value's metadata
 * @param data2
 *          the second value
 * @return 0 if the values are equal, -1 if data1 is smaller than data2 and +1 if it's larger.
 * @throws KettleValueException
 *           In case we get conversion errors
 */
@Override
public int compare(Object data1, ValueMetaInterface meta2, Object data2) throws KettleValueException {
    if (meta2 == null) {
        throw new KettleValueException(toStringMeta() + " : Second meta data (meta2) is null, please check one of the previous steps.");
    }
    try {
        // 
        if (getType() == meta2.getType()) {
            if (getStorageType() == meta2.getStorageType()) {
                return compare(data1, data2);
            }
            // 
            switch(getStorageType()) {
                case STORAGE_TYPE_NORMAL:
                    return compare(data1, meta2.convertToNormalStorageType(data2));
                case STORAGE_TYPE_BINARY_STRING:
                    if (storageMetadata != null && storageMetadata.getConversionMask() != null) {
                        // BACKLOG-18754 - if there is a storage conversion mask, we should use
                        // it as the mask for meta2 (meta2 can have specific storage type and type, so
                        // it can't be used directly to convert data2 to binary string)
                        ValueMetaInterface meta2StorageMask = meta2.clone();
                        meta2StorageMask.setConversionMask(storageMetadata.getConversionMask());
                        return compare(data1, meta2StorageMask.convertToBinaryStringStorageType(data2));
                    } else {
                        return compare(data1, meta2.convertToBinaryStringStorageType(data2));
                    }
                case STORAGE_TYPE_INDEXED:
                    switch(meta2.getStorageType()) {
                        case STORAGE_TYPE_INDEXED:
                            // not accessible, just to make sure.
                            return compare(data1, data2);
                        case STORAGE_TYPE_NORMAL:
                            return -meta2.compare(data2, convertToNormalStorageType(data1));
                        case STORAGE_TYPE_BINARY_STRING:
                            return -meta2.compare(data2, convertToBinaryStringStorageType(data1));
                        default:
                            throw new KettleValueException(meta2.toStringMeta() + " : Unknown storage type : " + meta2.getStorageType());
                    }
                default:
                    throw new KettleValueException(toStringMeta() + " : Unknown storage type : " + getStorageType());
            }
        } else if (ValueMetaInterface.TYPE_INTEGER == getType() && ValueMetaInterface.TYPE_NUMBER == meta2.getType()) {
            // compare Double to Integer
            return -meta2.compare(data2, meta2.convertData(this, data1));
        }
        // 
        return compare(data1, convertData(meta2, data2));
    } catch (Exception e) {
        throw new KettleValueException(toStringMeta() + " : Unable to compare with value [" + meta2.toStringMeta() + "]", e);
    }
}
Also used : KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) ParseException(java.text.ParseException) KettleEOFException(org.pentaho.di.core.exception.KettleEOFException) EOFException(java.io.EOFException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Aggregations

KettleValueException (org.pentaho.di.core.exception.KettleValueException)127 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)35 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)35 KettleException (org.pentaho.di.core.exception.KettleException)25 KettleStepException (org.pentaho.di.core.exception.KettleStepException)17 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)16 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)13 IOException (java.io.IOException)12 Test (org.junit.Test)11 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)11 KettleFileException (org.pentaho.di.core.exception.KettleFileException)11 ParseException (java.text.ParseException)10 Date (java.util.Date)9 EOFException (java.io.EOFException)8 SQLException (java.sql.SQLException)8 ArrayList (java.util.ArrayList)8 Calendar (java.util.Calendar)8 KettleEOFException (org.pentaho.di.core.exception.KettleEOFException)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 BigDecimal (java.math.BigDecimal)6