use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class StreamLookup method getFromCache.
private Object[] getFromCache(RowMetaInterface keyMeta, Object[] keyData) throws KettleValueException {
if (meta.isMemoryPreservationActive()) {
if (meta.isUsingSortedList()) {
KeyValue keyValue = new KeyValue(keyData, null);
int idx = Collections.binarySearch(data.list, keyValue, data.comparator);
if (idx < 0) {
// nothing found
return null;
}
keyValue = data.list.get(idx);
return keyValue.getValue();
} else {
if (meta.isUsingIntegerPair()) {
Long value = data.longIndex.get(keyMeta.getInteger(keyData, 0));
if (value == null) {
return null;
}
return new Object[] { value };
} else {
try {
byte[] value = data.hashIndex.get(RowMeta.extractData(keyMeta, keyData));
if (value == null) {
return null;
}
return RowMeta.getRow(data.cacheValueMeta, value);
} catch (Exception e) {
logError("Oops", e);
throw new RuntimeException(e);
}
}
}
} else {
return data.look.get(new RowMetaAndData(keyMeta, keyData));
}
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class SelectValues method metadataValues.
/**
* Change the meta-data of certain fields.
* <p/>
* This, we can do VERY fast.
* <p/>
*
* @param row The row to manipulate
* @return the altered RowData array
* @throws KettleValueException
*/
@VisibleForTesting
synchronized Object[] metadataValues(RowMetaInterface rowMeta, Object[] rowData) throws KettleException {
if (data.firstmetadata) {
data.firstmetadata = false;
data.metanrs = new int[meta.getMeta().length];
for (int i = 0; i < data.metanrs.length; i++) {
data.metanrs[i] = rowMeta.indexOfValue(meta.getMeta()[i].getName());
if (data.metanrs[i] < 0) {
logError(BaseMessages.getString(PKG, "SelectValues.Log.CouldNotFindField", meta.getMeta()[i].getName()));
setErrors(1);
stopAll();
return null;
}
}
// Check for doubles in the selected fields...
int[] cnt = new int[meta.getMeta().length];
for (int i = 0; i < meta.getMeta().length; i++) {
cnt[i] = 0;
for (int j = 0; j < meta.getMeta().length; j++) {
if (meta.getMeta()[i].getName().equals(meta.getMeta()[j].getName())) {
cnt[i]++;
}
if (cnt[i] > 1) {
logError(BaseMessages.getString(PKG, "SelectValues.Log.FieldCouldNotSpecifiedMoreThanTwice2", meta.getMeta()[i].getName()));
setErrors(1);
stopAll();
return null;
}
}
}
//
for (int i = 0; i < data.metanrs.length; i++) {
SelectMetadataChange change = meta.getMeta()[i];
ValueMetaInterface valueMeta = rowMeta.getValueMeta(data.metanrs[i]);
if (!Utils.isEmpty(change.getConversionMask())) {
valueMeta.setConversionMask(change.getConversionMask());
}
valueMeta.setDateFormatLenient(change.isDateFormatLenient());
valueMeta.setDateFormatLocale(EnvUtil.createLocale(change.getDateFormatLocale()));
valueMeta.setDateFormatTimeZone(EnvUtil.createTimeZone(change.getDateFormatTimeZone()));
valueMeta.setLenientStringToNumber(change.isLenientStringToNumber());
if (!Utils.isEmpty(change.getEncoding())) {
valueMeta.setStringEncoding(change.getEncoding());
}
if (!Utils.isEmpty(change.getDecimalSymbol())) {
valueMeta.setDecimalSymbol(change.getDecimalSymbol());
}
if (!Utils.isEmpty(change.getGroupingSymbol())) {
valueMeta.setGroupingSymbol(change.getGroupingSymbol());
}
if (!Utils.isEmpty(change.getCurrencySymbol())) {
valueMeta.setCurrencySymbol(change.getCurrencySymbol());
}
}
}
//
for (int i = 0; i < data.metanrs.length; i++) {
int index = data.metanrs[i];
ValueMetaInterface fromMeta = rowMeta.getValueMeta(index);
ValueMetaInterface toMeta = data.metadataRowMeta.getValueMeta(index);
//
try {
if (fromMeta.isStorageBinaryString() && meta.getMeta()[i].getStorageType() == ValueMetaInterface.STORAGE_TYPE_NORMAL) {
rowData[index] = fromMeta.convertBinaryStringToNativeType((byte[]) rowData[index]);
}
if (meta.getMeta()[i].getType() != ValueMetaInterface.TYPE_NONE && fromMeta.getType() != toMeta.getType()) {
rowData[index] = toMeta.convertData(fromMeta, rowData[index]);
}
} catch (KettleValueException e) {
throw new KettleConversionException(e.getMessage(), Collections.<Exception>singletonList(e), Collections.singletonList(toMeta), rowData);
}
}
return rowData;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class SortRows method getBuffer.
// get sorted rows from available files in iterative manner.
// that means call to this method will continue to return rows
// till all temp files will not be read to the end.
Object[] getBuffer() throws KettleValueException {
Object[] retval;
// Open all files at once and read one row from each file...
if (data.files.size() > 0 && (data.dis.size() == 0 || data.fis.size() == 0)) {
if (log.isBasic()) {
logBasic(BaseMessages.getString(PKG, "SortRows.Basic.OpeningTempFiles", data.files.size()));
}
try {
for (int f = 0; f < data.files.size() && !isStopped(); f++) {
FileObject fileObject = data.files.get(f);
String filename = KettleVFS.getFilename(fileObject);
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "SortRows.Detailed.OpeningTempFile", filename));
}
InputStream fi = KettleVFS.getInputStream(fileObject);
DataInputStream di;
data.fis.add(fi);
if (data.compressFiles) {
di = getDataInputStream(new GZIPInputStream(new BufferedInputStream(fi)));
} else {
di = new DataInputStream(new BufferedInputStream(fi, 50000));
}
data.dis.add(di);
// How long is the buffer?
int buffersize = data.bufferSizes.get(f);
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "SortRows.Detailed.FromFileExpectingRows", filename, buffersize));
}
if (buffersize > 0) {
Object[] row = data.outputRowMeta.readData(di);
// new row from input stream
data.rowbuffer.add(row);
data.tempRows.add(new RowTempFile(row, f));
}
}
// Sort the data row buffer
Collections.sort(data.tempRows, data.comparator);
} catch (Exception e) {
logError(BaseMessages.getString(PKG, "SortRows.Error.ErrorReadingBackTempFiles"), e);
}
}
if (data.files.size() == 0) {
if (data.getBufferIndex < data.buffer.size()) {
retval = data.buffer.get(data.getBufferIndex);
data.getBufferIndex++;
} else {
retval = null;
}
} else {
if (data.rowbuffer.size() == 0) {
retval = null;
} else {
//
if (log.isRowLevel()) {
for (int i = 0; i < data.rowbuffer.size() && !isStopped(); i++) {
Object[] b = data.rowbuffer.get(i);
logRowlevel(BaseMessages.getString(PKG, "SortRows.RowLevel.PrintRow", i, data.outputRowMeta.getString(b)));
}
}
RowTempFile rowTempFile = data.tempRows.remove(0);
retval = rowTempFile.row;
int smallest = rowTempFile.fileNumber;
// now get another Row for position smallest
FileObject file = data.files.get(smallest);
DataInputStream di = data.dis.get(smallest);
InputStream fi = data.fis.get(smallest);
try {
Object[] row2 = data.outputRowMeta.readData(di);
RowTempFile extra = new RowTempFile(row2, smallest);
int index = Collections.binarySearch(data.tempRows, extra, data.comparator);
if (index < 0) {
data.tempRows.add(index * (-1) - 1, extra);
} else {
data.tempRows.add(index, extra);
}
} catch (KettleFileException fe) {
// empty file or EOF mostly
GZIPInputStream gzfi = (data.compressFiles) ? data.gzis.get(smallest) : null;
try {
di.close();
fi.close();
if (gzfi != null) {
gzfi.close();
}
file.delete();
} catch (IOException e) {
logError(BaseMessages.getString(PKG, "SortRows.Error.UnableToCloseFile", smallest, file.toString()));
setErrors(1);
stopAll();
return null;
}
data.files.remove(smallest);
data.dis.remove(smallest);
data.fis.remove(smallest);
if (gzfi != null) {
data.gzis.remove(smallest);
}
//
for (RowTempFile rtf : data.tempRows) {
if (rtf.fileNumber > smallest) {
rtf.fileNumber--;
}
}
} catch (SocketTimeoutException e) {
// should never happen on local files
throw new KettleValueException(e);
}
}
}
return retval;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Script method addValues.
private boolean addValues(RowMetaInterface rowMeta, Object[] row) throws KettleException {
if (first) {
first = false;
// What is the output row looking like?
//
data.outputRowMeta = getInputRowMeta().clone();
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
// Determine the indexes of the fields used!
//
determineUsedFields(rowMeta);
// Get the indexes of the replaced fields...
//
data.replaceIndex = new int[meta.getFieldname().length];
for (int i = 0; i < meta.getFieldname().length; i++) {
if (meta.getReplace()[i]) {
data.replaceIndex[i] = rowMeta.indexOfValue(meta.getFieldname()[i]);
if (data.replaceIndex[i] < 0) {
if (Utils.isEmpty(meta.getFieldname()[i])) {
throw new KettleStepException(BaseMessages.getString(PKG, "ScriptValuesMetaMod.Exception.FieldToReplaceNotFound", meta.getFieldname()[i]));
}
data.replaceIndex[i] = rowMeta.indexOfValue(meta.getRename()[i]);
if (data.replaceIndex[i] < 0) {
throw new KettleStepException(BaseMessages.getString(PKG, "ScriptValuesMetaMod.Exception.FieldToReplaceNotFound", meta.getRename()[i]));
}
}
} else {
data.replaceIndex[i] = -1;
}
}
data.cx = ScriptMeta.createNewScriptEngine(getStepname());
data.scope = data.cx.getBindings(ScriptContext.ENGINE_SCOPE);
bFirstRun = true;
data.scope.put("_step_", this);
//
for (int i = 0; i < meta.getNumberOfJSScripts(); i++) {
data.scope.put(jsScripts[i].getScriptName(), jsScripts[i].getScript());
}
// Adding the Name of the Transformation to the Context
//
data.scope.put("_TransformationName_", this.getStepname());
try {
// add these now (they will be re-added later) to make
// compilation succeed
//
// Add the old style row object for compatibility reasons...
//
data.scope.put("row", row);
//
for (int i = 0; i < data.fields_used.length; i++) {
ValueMetaInterface valueMeta = rowMeta.getValueMeta(data.fields_used[i]);
Object valueData = row[data.fields_used[i]];
Object normalStorageValueData = valueMeta.convertToNormalStorageType(valueData);
data.scope.put(valueMeta.getName(), normalStorageValueData);
}
// also add the meta information for the whole row
//
data.scope.put("rowMeta", rowMeta);
//
try {
if (meta.getAddClasses() != null) {
for (int i = 0; i < meta.getAddClasses().length; i++) {
// TODO AKRETION ensure it works
data.scope.put(meta.getAddClasses()[i].getJSName(), meta.getAddClasses()[i].getAddObject());
// Object jsOut =
// Context.javaToJS(meta.getAddClasses()[i].getAddObject(),
// data.scope);
// ScriptableObject.putProperty(data.scope,
// meta.getAddClasses()[i].getJSName(), jsOut);
}
}
} catch (Exception e) {
throw new KettleValueException(BaseMessages.getString(PKG, "Script.Log.CouldNotAttachAdditionalScripts"), e);
}
// Adding some Constants to the JavaScript
try {
data.scope.put("SKIP_TRANSFORMATION", Integer.valueOf(SKIP_TRANSFORMATION));
data.scope.put("ABORT_TRANSFORMATION", Integer.valueOf(ABORT_TRANSFORMATION));
data.scope.put("ERROR_TRANSFORMATION", Integer.valueOf(ERROR_TRANSFORMATION));
data.scope.put("CONTINUE_TRANSFORMATION", Integer.valueOf(CONTINUE_TRANSFORMATION));
} catch (Exception ex) {
// ex.toString());
throw new KettleValueException(BaseMessages.getString(PKG, "Script.Log.CouldNotAddDefaultConstants"), ex);
}
try {
// Checking for StartScript
if (strStartScript != null && strStartScript.length() > 0) {
CompiledScript startScript = ((Compilable) data.cx).compile(strStartScript);
startScript.eval(data.scope);
if (log.isDetailed()) {
logDetailed(("Start Script found!"));
}
} else {
if (log.isDetailed()) {
logDetailed(("No starting Script found!"));
}
}
} catch (Exception es) {
// es.toString());
throw new KettleValueException(BaseMessages.getString(PKG, "Script.Log.ErrorProcessingStartScript"), es);
}
// Now Compile our Script
// alternatively you could also support non compilable JSR223
// languages, see how we were doing before:
// http://github.com/rvalyi/jripple/blob/e6190fd89014a49b0faffae68c75762be124d899/
// src/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesMod.java
data.script = ((Compilable) data.cx).compile(strTransformScript);
} catch (Exception e) {
throw new KettleValueException(BaseMessages.getString(PKG, "Script.Log.CouldNotCompileJavascript"), e);
}
}
// Filling the defined TranVars with the Values from the Row
//
Object[] outputRow = RowDataUtil.resizeArray(row, data.outputRowMeta.size());
// Keep an index...
int outputIndex = rowMeta.size();
try {
try {
data.scope.put("row", row);
for (int i = 0; i < data.fields_used.length; i++) {
ValueMetaInterface valueMeta = rowMeta.getValueMeta(data.fields_used[i]);
Object valueData = row[data.fields_used[i]];
Object normalStorageValueData = valueMeta.convertToNormalStorageType(valueData);
data.scope.put(valueMeta.getName(), normalStorageValueData);
}
// also add the meta information for the hole row
//
data.scope.put("rowMeta", rowMeta);
} catch (Exception e) {
throw new KettleValueException(BaseMessages.getString(PKG, "Script.Log.UnexpectedeError"), e);
}
data.script.eval(data.scope);
if (bFirstRun) {
bFirstRun = false;
// Check if we had a Transformation Status
Object tran_stat = data.scope.get("trans_Status");
if (tran_stat != null) {
// TODO AKRETION not sure: !=
// ScriptableObject.NOT_FOUND
bWithTransStat = true;
if (log.isDetailed()) {
logDetailed(("tran_Status found. Checking transformation status while script execution."));
}
} else {
if (log.isDetailed()) {
logDetailed(("No tran_Status found. Transformation status checking not available."));
}
bWithTransStat = false;
}
}
if (bWithTransStat) {
// TODO
iTranStat = (Integer) data.scope.get("trans_Status");
// ARETION
// not
// sure
// the
// casting
// is
// correct
} else {
iTranStat = CONTINUE_TRANSFORMATION;
}
if (iTranStat == CONTINUE_TRANSFORMATION) {
bRC = true;
for (int i = 0; i < meta.getFieldname().length; i++) {
Object result = data.scope.get(meta.getFieldname()[i]);
Object valueData = getValueFromJScript(result, i);
if (data.replaceIndex[i] < 0) {
outputRow[outputIndex++] = valueData;
} else {
outputRow[data.replaceIndex[i]] = valueData;
}
}
putRow(data.outputRowMeta, outputRow);
} else {
switch(iTranStat) {
case SKIP_TRANSFORMATION:
// eat this row.
bRC = true;
break;
case ABORT_TRANSFORMATION:
if (data.cx != null) {
// Context.exit(); TODO AKRETION not sure
stopAll();
}
setOutputDone();
bRC = false;
break;
case ERROR_TRANSFORMATION:
if (data.cx != null) {
// Context.exit(); TODO AKRETION not sure
setErrors(1);
}
stopAll();
bRC = false;
break;
default:
break;
}
// TODO: kick this "ERROR handling" junk out now that we have
// solid error handling in place.
//
}
} catch (ScriptException e) {
throw new KettleValueException(BaseMessages.getString(PKG, "Script.Log.JavascriptError"), e);
}
return bRC;
}
use of org.pentaho.di.core.exception.KettleValueException in project pentaho-kettle by pentaho.
the class Calculator method calcFields.
/**
* @param inputRowMeta
* the input row metadata
* @param r
* the input row (data)
* @return A row including the calculations, excluding the temporary values
* @throws KettleValueException
* in case there is a calculation error.
*/
private Object[] calcFields(RowMetaInterface inputRowMeta, Object[] r) throws KettleValueException {
// First copy the input data to the new result...
Object[] calcData = RowDataUtil.resizeArray(r, data.getCalcRowMeta().size());
for (int i = 0, index = inputRowMeta.size() + i; i < meta.getCalculation().length; i++, index++) {
CalculatorMetaFunction fn = meta.getCalculation()[i];
if (!Utils.isEmpty(fn.getFieldName())) {
ValueMetaInterface targetMeta = data.getCalcRowMeta().getValueMeta(index);
// Get the metadata & the data...
// ValueMetaInterface metaTarget = data.calcRowMeta.getValueMeta(i);
ValueMetaInterface metaA = null;
Object dataA = null;
if (data.getFieldIndexes()[i].indexA >= 0) {
metaA = data.getCalcRowMeta().getValueMeta(data.getFieldIndexes()[i].indexA);
dataA = calcData[data.getFieldIndexes()[i].indexA];
}
ValueMetaInterface metaB = null;
Object dataB = null;
if (data.getFieldIndexes()[i].indexB >= 0) {
metaB = data.getCalcRowMeta().getValueMeta(data.getFieldIndexes()[i].indexB);
dataB = calcData[data.getFieldIndexes()[i].indexB];
}
ValueMetaInterface metaC = null;
Object dataC = null;
if (data.getFieldIndexes()[i].indexC >= 0) {
metaC = data.getCalcRowMeta().getValueMeta(data.getFieldIndexes()[i].indexC);
dataC = calcData[data.getFieldIndexes()[i].indexC];
}
int calcType = fn.getCalcType();
// The data types are those of the first argument field, convert to the target field.
// Exceptions:
// - multiply can be string
// - constant is string
// - all date functions except add days/months
// - hex encode / decodes
int resultType;
if (metaA != null) {
resultType = metaA.getType();
} else {
resultType = ValueMetaInterface.TYPE_NONE;
}
switch(calcType) {
case CalculatorMetaFunction.CALC_NONE:
break;
case // Create a copy of field A
CalculatorMetaFunction.CALC_COPY_OF_FIELD:
calcData[index] = dataA;
break;
case // A + B
CalculatorMetaFunction.CALC_ADD:
calcData[index] = ValueDataUtil.plus(metaA, dataA, metaB, dataB);
if (metaA.isString() || metaB.isString()) {
resultType = ValueMetaInterface.TYPE_STRING;
}
break;
case // A - B
CalculatorMetaFunction.CALC_SUBTRACT:
calcData[index] = ValueDataUtil.minus(metaA, dataA, metaB, dataB);
if (metaA.isDate()) {
resultType = ValueMetaInterface.TYPE_INTEGER;
}
break;
case // A * B
CalculatorMetaFunction.CALC_MULTIPLY:
calcData[index] = ValueDataUtil.multiply(metaA, dataA, metaB, dataB);
if (metaA.isString() || metaB.isString()) {
resultType = ValueMetaInterface.TYPE_STRING;
}
break;
case // A / B
CalculatorMetaFunction.CALC_DIVIDE:
calcData[index] = ValueDataUtil.divide(metaA, dataA, metaB, dataB);
break;
case // A * A
CalculatorMetaFunction.CALC_SQUARE:
calcData[index] = ValueDataUtil.multiply(metaA, dataA, metaA, dataA);
break;
case // SQRT( A )
CalculatorMetaFunction.CALC_SQUARE_ROOT:
calcData[index] = ValueDataUtil.sqrt(metaA, dataA);
break;
case // 100 * A / B
CalculatorMetaFunction.CALC_PERCENT_1:
calcData[index] = ValueDataUtil.percent1(metaA, dataA, metaB, dataB);
break;
case // A - ( A * B / 100 )
CalculatorMetaFunction.CALC_PERCENT_2:
calcData[index] = ValueDataUtil.percent2(metaA, dataA, metaB, dataB);
break;
case // A + ( A * B / 100 )
CalculatorMetaFunction.CALC_PERCENT_3:
calcData[index] = ValueDataUtil.percent3(metaA, dataA, metaB, dataB);
break;
case // A + B * C
CalculatorMetaFunction.CALC_COMBINATION_1:
calcData[index] = ValueDataUtil.combination1(metaA, dataA, metaB, dataB, metaC, dataC);
break;
case // SQRT( A*A + B*B )
CalculatorMetaFunction.CALC_COMBINATION_2:
calcData[index] = ValueDataUtil.combination2(metaA, dataA, metaB, dataB);
break;
case // ROUND( A )
CalculatorMetaFunction.CALC_ROUND_1:
calcData[index] = ValueDataUtil.round(metaA, dataA);
break;
case // ROUND( A , B )
CalculatorMetaFunction.CALC_ROUND_2:
calcData[index] = ValueDataUtil.round(metaA, dataA, metaB, dataB);
break;
case // ROUND( A , B )
CalculatorMetaFunction.CALC_ROUND_CUSTOM_1:
calcData[index] = ValueDataUtil.round(metaA, dataA, metaB.getNumber(dataB).intValue());
break;
case // ROUND( A , B, C )
CalculatorMetaFunction.CALC_ROUND_CUSTOM_2:
calcData[index] = ValueDataUtil.round(metaA, dataA, metaB, dataB, metaC.getNumber(dataC).intValue());
break;
case // ROUND( A )
CalculatorMetaFunction.CALC_ROUND_STD_1:
calcData[index] = ValueDataUtil.round(metaA, dataA, java.math.BigDecimal.ROUND_HALF_UP);
break;
case // ROUND( A , B )
CalculatorMetaFunction.CALC_ROUND_STD_2:
calcData[index] = ValueDataUtil.round(metaA, dataA, metaB, dataB, java.math.BigDecimal.ROUND_HALF_UP);
break;
case // CEIL( A )
CalculatorMetaFunction.CALC_CEIL:
calcData[index] = ValueDataUtil.ceil(metaA, dataA);
break;
case // FLOOR( A )
CalculatorMetaFunction.CALC_FLOOR:
calcData[index] = ValueDataUtil.floor(metaA, dataA);
break;
case // Set field to constant value...
CalculatorMetaFunction.CALC_CONSTANT:
// A string
calcData[index] = fn.getFieldA();
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Replace null values with another value
CalculatorMetaFunction.CALC_NVL:
calcData[index] = ValueDataUtil.nvl(metaA, dataA, metaB, dataB);
break;
case // Add B days to date field A
CalculatorMetaFunction.CALC_ADD_DAYS:
calcData[index] = ValueDataUtil.addDays(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Add B hours to date field A
CalculatorMetaFunction.CALC_ADD_HOURS:
calcData[index] = ValueDataUtil.addHours(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Add B minutes to date field A
CalculatorMetaFunction.CALC_ADD_MINUTES:
calcData[index] = ValueDataUtil.addMinutes(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the year (Integer) of a date?
CalculatorMetaFunction.CALC_YEAR_OF_DATE:
calcData[index] = ValueDataUtil.yearOfDate(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the month (Integer) of a date?
CalculatorMetaFunction.CALC_MONTH_OF_DATE:
calcData[index] = ValueDataUtil.monthOfDate(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the day of year (Integer) of a date?
CalculatorMetaFunction.CALC_DAY_OF_YEAR:
calcData[index] = ValueDataUtil.dayOfYear(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the day of month (Integer) of a date?
CalculatorMetaFunction.CALC_DAY_OF_MONTH:
calcData[index] = ValueDataUtil.dayOfMonth(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the day of week (Integer) of a date?
CalculatorMetaFunction.CALC_DAY_OF_WEEK:
calcData[index] = ValueDataUtil.dayOfWeek(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the week of year (Integer) of a date?
CalculatorMetaFunction.CALC_WEEK_OF_YEAR:
calcData[index] = ValueDataUtil.weekOfYear(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the week of year (Integer) of a date ISO8601
CalculatorMetaFunction.CALC_WEEK_OF_YEAR_ISO8601:
// style?
calcData[index] = ValueDataUtil.weekOfYearISO8601(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the year (Integer) of a date ISO8601 style?
CalculatorMetaFunction.CALC_YEAR_OF_DATE_ISO8601:
calcData[index] = ValueDataUtil.yearOfDateISO8601(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Byte to Hex encode string field A
CalculatorMetaFunction.CALC_BYTE_TO_HEX_ENCODE:
calcData[index] = ValueDataUtil.byteToHexEncode(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Hex to Byte decode string field A
CalculatorMetaFunction.CALC_HEX_TO_BYTE_DECODE:
calcData[index] = ValueDataUtil.hexToByteDecode(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Char to Hex encode string field A
CalculatorMetaFunction.CALC_CHAR_TO_HEX_ENCODE:
calcData[index] = ValueDataUtil.charToHexEncode(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Hex to Char decode string field A
CalculatorMetaFunction.CALC_HEX_TO_CHAR_DECODE:
calcData[index] = ValueDataUtil.hexToCharDecode(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // CRC32
CalculatorMetaFunction.CALC_CRC32:
calcData[index] = ValueDataUtil.ChecksumCRC32(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // ADLER32
CalculatorMetaFunction.CALC_ADLER32:
calcData[index] = ValueDataUtil.ChecksumAdler32(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // MD5
CalculatorMetaFunction.CALC_MD5:
calcData[index] = ValueDataUtil.createChecksum(metaA, dataA, "MD5");
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // SHA-1
CalculatorMetaFunction.CALC_SHA1:
calcData[index] = ValueDataUtil.createChecksum(metaA, dataA, "SHA-1");
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // LEVENSHTEIN DISTANCE
CalculatorMetaFunction.CALC_LEVENSHTEIN_DISTANCE:
calcData[index] = ValueDataUtil.getLevenshtein_Distance(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // METAPHONE
CalculatorMetaFunction.CALC_METAPHONE:
calcData[index] = ValueDataUtil.get_Metaphone(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Double METAPHONE
CalculatorMetaFunction.CALC_DOUBLE_METAPHONE:
calcData[index] = ValueDataUtil.get_Double_Metaphone(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // ABS( A )
CalculatorMetaFunction.CALC_ABS:
calcData[index] = ValueDataUtil.abs(metaA, dataA);
break;
case // Remove Time from field A
CalculatorMetaFunction.CALC_REMOVE_TIME_FROM_DATE:
calcData[index] = ValueDataUtil.removeTimeFromDate(metaA, dataA);
break;
case // DateA - DateB
CalculatorMetaFunction.CALC_DATE_DIFF:
calcData[index] = ValueDataUtil.DateDiff(metaA, dataA, metaB, dataB, "d");
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // A + B + C
CalculatorMetaFunction.CALC_ADD3:
calcData[index] = ValueDataUtil.plus3(metaA, dataA, metaB, dataB, metaC, dataC);
if (metaA.isString() || metaB.isString() || metaC.isString()) {
resultType = ValueMetaInterface.TYPE_STRING;
}
break;
case // InitCap( A )
CalculatorMetaFunction.CALC_INITCAP:
calcData[index] = ValueDataUtil.initCap(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // UpperCase( A )
CalculatorMetaFunction.CALC_UPPER_CASE:
calcData[index] = ValueDataUtil.upperCase(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // UpperCase( A )
CalculatorMetaFunction.CALC_LOWER_CASE:
calcData[index] = ValueDataUtil.lowerCase(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // escapeXML( A )
CalculatorMetaFunction.CALC_MASK_XML:
calcData[index] = ValueDataUtil.escapeXML(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // CDATA( A )
CalculatorMetaFunction.CALC_USE_CDATA:
calcData[index] = ValueDataUtil.useCDATA(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // REMOVE CR FROM A
CalculatorMetaFunction.CALC_REMOVE_CR:
calcData[index] = ValueDataUtil.removeCR(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // REMOVE LF FROM A
CalculatorMetaFunction.CALC_REMOVE_LF:
calcData[index] = ValueDataUtil.removeLF(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // REMOVE CRLF FROM A
CalculatorMetaFunction.CALC_REMOVE_CRLF:
calcData[index] = ValueDataUtil.removeCRLF(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // REMOVE TAB FROM A
CalculatorMetaFunction.CALC_REMOVE_TAB:
calcData[index] = ValueDataUtil.removeTAB(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // GET ONLY DIGITS FROM A
CalculatorMetaFunction.CALC_GET_ONLY_DIGITS:
calcData[index] = ValueDataUtil.getDigits(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // REMOVE DIGITS FROM A
CalculatorMetaFunction.CALC_REMOVE_DIGITS:
calcData[index] = ValueDataUtil.removeDigits(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // RETURN THE LENGTH OF A
CalculatorMetaFunction.CALC_STRING_LEN:
calcData[index] = ValueDataUtil.stringLen(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // LOAD CONTENT OF A FILE A IN A BLOB
CalculatorMetaFunction.CALC_LOAD_FILE_CONTENT_BINARY:
calcData[index] = ValueDataUtil.loadFileContentInBinary(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Add time B to a date A
CalculatorMetaFunction.CALC_ADD_TIME_TO_DATE:
calcData[index] = ValueDataUtil.addTimeToDate(metaA, dataA, metaB, dataB, metaC, dataC);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // What is the quarter (Integer) of a date?
CalculatorMetaFunction.CALC_QUARTER_OF_DATE:
calcData[index] = ValueDataUtil.quarterOfDate(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // variable substitution in string
CalculatorMetaFunction.CALC_SUBSTITUTE_VARIABLE:
calcData[index] = environmentSubstitute(dataA.toString());
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // UnescapeXML( A )
CalculatorMetaFunction.CALC_UNESCAPE_XML:
calcData[index] = ValueDataUtil.unEscapeXML(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // EscapeHTML( A )
CalculatorMetaFunction.CALC_ESCAPE_HTML:
calcData[index] = ValueDataUtil.escapeHTML(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // UnescapeHTML( A )
CalculatorMetaFunction.CALC_UNESCAPE_HTML:
calcData[index] = ValueDataUtil.unEscapeHTML(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // EscapeSQL( A )
CalculatorMetaFunction.CALC_ESCAPE_SQL:
calcData[index] = ValueDataUtil.escapeSQL(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // DateWorkingDiff( A , B)
CalculatorMetaFunction.CALC_DATE_WORKING_DIFF:
calcData[index] = ValueDataUtil.DateWorkingDiff(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Add B months to date field A
CalculatorMetaFunction.CALC_ADD_MONTHS:
calcData[index] = ValueDataUtil.addMonths(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Check if file A is well formed
CalculatorMetaFunction.CALC_CHECK_XML_FILE_WELL_FORMED:
calcData[index] = ValueDataUtil.isXMLFileWellFormed(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Check if xml A is well formed
CalculatorMetaFunction.CALC_CHECK_XML_WELL_FORMED:
calcData[index] = ValueDataUtil.isXMLWellFormed(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Get file encoding from a file A
CalculatorMetaFunction.CALC_GET_FILE_ENCODING:
calcData[index] = ValueDataUtil.getFileEncoding(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // DAMERAULEVENSHTEIN DISTANCE
CalculatorMetaFunction.CALC_DAMERAU_LEVENSHTEIN:
calcData[index] = ValueDataUtil.getDamerauLevenshtein_Distance(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // NEEDLEMANWUNSH DISTANCE
CalculatorMetaFunction.CALC_NEEDLEMAN_WUNSH:
calcData[index] = ValueDataUtil.getNeedlemanWunsch_Distance(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Jaro DISTANCE
CalculatorMetaFunction.CALC_JARO:
calcData[index] = ValueDataUtil.getJaro_Similitude(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Jaro DISTANCE
CalculatorMetaFunction.CALC_JARO_WINKLER:
calcData[index] = ValueDataUtil.getJaroWinkler_Similitude(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // SOUNDEX
CalculatorMetaFunction.CALC_SOUNDEX:
calcData[index] = ValueDataUtil.get_SoundEx(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // REFINEDSOUNDEX
CalculatorMetaFunction.CALC_REFINED_SOUNDEX:
calcData[index] = ValueDataUtil.get_RefinedSoundEx(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // DateA - DateB (ms)
CalculatorMetaFunction.CALC_DATE_DIFF_MSEC:
calcData[index] = ValueDataUtil.DateDiff(metaA, dataA, metaB, dataB, "ms");
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // DateA - DateB (s)
CalculatorMetaFunction.CALC_DATE_DIFF_SEC:
calcData[index] = ValueDataUtil.DateDiff(metaA, dataA, metaB, dataB, "s");
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // DateA - DateB (mn)
CalculatorMetaFunction.CALC_DATE_DIFF_MN:
calcData[index] = ValueDataUtil.DateDiff(metaA, dataA, metaB, dataB, "mn");
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // DateA - DateB (h)
CalculatorMetaFunction.CALC_DATE_DIFF_HR:
calcData[index] = ValueDataUtil.DateDiff(metaA, dataA, metaB, dataB, "h");
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case CalculatorMetaFunction.CALC_HOUR_OF_DAY:
calcData[index] = ValueDataUtil.hourOfDay(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case CalculatorMetaFunction.CALC_MINUTE_OF_HOUR:
calcData[index] = ValueDataUtil.minuteOfHour(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case CalculatorMetaFunction.CALC_SECOND_OF_MINUTE:
calcData[index] = ValueDataUtil.secondOfMinute(metaA, dataA);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case // Add B seconds to date field A
CalculatorMetaFunction.CALC_ADD_SECONDS:
calcData[index] = ValueDataUtil.addSeconds(metaA, dataA, metaB, dataB);
resultType = CalculatorMetaFunction.calcDefaultResultType[calcType];
break;
case CalculatorMetaFunction.CALC_REMAINDER:
if (targetMeta.getType() != metaA.getType() || targetMeta.getType() != metaB.getType()) {
dataA = targetMeta.convertData(metaA, dataA);
metaA = targetMeta.clone();
dataB = targetMeta.convertData(metaB, dataB);
metaB = targetMeta.clone();
}
calcData[index] = ValueDataUtil.remainder(metaA, dataA, metaB, dataB);
resultType = targetMeta.getType();
break;
default:
throw new KettleValueException(BaseMessages.getString(PKG, "Calculator.Log.UnknownCalculationType") + fn.getCalcType());
}
//
if (targetMeta.getType() == ValueMetaInterface.TYPE_NONE) {
throw new KettleValueException(BaseMessages.getString(PKG, "Calculator.Log.NoType") + (i + 1) + " : " + fn.getFieldName() + " = " + fn.getCalcTypeDesc() + " / " + fn.getCalcTypeLongDesc());
}
//
if (calcData[index] != null) {
if (targetMeta.getType() != resultType) {
ValueMetaInterface resultMeta;
try {
// clone() is not necessary as one data instance belongs to one step instance and no race condition occurs
resultMeta = data.getValueMetaFor(resultType, "result");
} catch (Exception exception) {
throw new KettleValueException("Error creating value");
}
resultMeta.setConversionMask(fn.getConversionMask());
resultMeta.setGroupingSymbol(fn.getGroupingSymbol());
resultMeta.setDecimalSymbol(fn.getDecimalSymbol());
resultMeta.setCurrencySymbol(fn.getCurrencySymbol());
try {
calcData[index] = targetMeta.convertData(resultMeta, calcData[index]);
} catch (Exception ex) {
throw new KettleValueException("resultType: " + resultType + "; targetMeta: " + targetMeta.getType(), ex);
}
}
}
}
}
//
return RowDataUtil.removeItems(calcData, data.getTempIndexes());
}
Aggregations