use of org.apache.hop.core.row.value.ValueMetaNumber in project hop by apache.
the class SasInput method processRow.
@Override
public boolean processRow() throws HopException {
final Object[] fileRowData = getRow();
if (fileRowData == null) {
// No more work to do...
//
setOutputDone();
return false;
}
//
if (first) {
// The output row meta data, what does it look like?
//
data.outputRowMeta = new RowMeta();
// See if the input row contains the filename field...
//
int idx = getInputRowMeta().indexOfValue(meta.getAcceptingField());
if (idx < 0) {
throw new HopException(BaseMessages.getString(PKG, "SASInput.Log.Error.UnableToFindFilenameField", meta.getAcceptingField()));
}
// Determine the output row layout
//
data.outputRowMeta = getInputRowMeta().clone();
meta.getFields(data.outputRowMeta, getTransformName(), null, null, this, metadataProvider);
}
String rawFilename = getInputRowMeta().getString(fileRowData, meta.getAcceptingField(), null);
final String filename = resolve(rawFilename);
// Add this to the result file names...
//
ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, HopVfs.getFileObject(filename), getPipelineMeta().getName(), getTransformName());
resultFile.setComment(BaseMessages.getString(PKG, "SASInput.ResultFile.Comment"));
addResultFile(resultFile);
//
try (InputStream inputStream = HopVfs.getInputStream(filename)) {
SasFileReaderImpl sasFileReader = new SasFileReaderImpl(inputStream);
SasFileProperties sasFileProperties = sasFileReader.getSasFileProperties();
logBasic(BaseMessages.getString(PKG, "SASInput.Log.OpenedSASFile") + " : [" + filename + "]");
// What are the columns in the file?
//
List<Column> columns = sasFileReader.getColumns();
// Map this to the columns we want...
//
List<Integer> indexes = new ArrayList<>();
for (SasInputField field : meta.getOutputFields()) {
int index = -1;
for (int c = 0; c < columns.size(); c++) {
if (columns.get(c).getName().equalsIgnoreCase(field.getName())) {
index = c;
break;
}
}
if (index < 0) {
throw new HopException("Field '" + field.getName() + " could not be found in input file '" + filename);
}
indexes.add(index);
}
// Now we have the indexes of the output fields to grab.
// Let's grab them...
//
Object[] sasRow;
while ((sasRow = sasFileReader.readNext()) != null) {
Object[] outputRow = RowDataUtil.createResizedCopy(fileRowData, data.outputRowMeta.size());
for (int i = 0; i < meta.getOutputFields().size(); i++) {
SasInputField field = meta.getOutputFields().get(i);
int index = indexes.get(i);
Column column = columns.get(index);
ColumnFormat columnFormat = column.getFormat();
Object sasValue = sasRow[index];
Object value = null;
IValueMeta inputValueMeta = null;
String fieldName = Const.NVL(field.getRename(), field.getName());
int outputIndex = getInputRowMeta().size() + i;
if (sasValue instanceof byte[]) {
inputValueMeta = new ValueMetaString(fieldName);
if (sasFileProperties.getEncoding() != null) {
value = new String((byte[]) sasValue, sasFileProperties.getEncoding());
} else {
// TODO: user defined encoding.
value = new String((byte[]) sasValue);
}
}
if (sasValue instanceof String) {
inputValueMeta = new ValueMetaString(fieldName);
value = sasValue;
}
if (sasValue instanceof Double) {
inputValueMeta = new ValueMetaNumber(fieldName);
value = sasValue;
}
if (sasValue instanceof Float) {
inputValueMeta = new ValueMetaNumber(fieldName);
value = Double.valueOf((double) sasValue);
}
if (sasValue instanceof Long) {
inputValueMeta = new ValueMetaInteger(fieldName);
value = sasValue;
}
if (sasValue instanceof Integer) {
inputValueMeta = new ValueMetaInteger(fieldName);
value = Long.valueOf((int) sasValue);
}
if (sasValue instanceof Date) {
inputValueMeta = new ValueMetaDate(fieldName);
value = sasValue;
}
if (inputValueMeta != null) {
inputValueMeta.setLength(field.getLength());
inputValueMeta.setPrecision(field.getPrecision());
inputValueMeta.setConversionMask(field.getConversionMask());
IValueMeta outputValueMeta = data.outputRowMeta.getValueMeta(outputIndex);
outputRow[outputIndex] = outputValueMeta.convertData(inputValueMeta, value);
}
}
// Send the row on its way...
//
putRow(data.outputRowMeta, outputRow);
}
} catch (Exception e) {
throw new HopException("Error reading from file " + filename, e);
}
return true;
}
use of org.apache.hop.core.row.value.ValueMetaNumber in project hop by apache.
the class DatabaseTest method testGetQueryFieldsFromPreparedStatement.
@Test
public void testGetQueryFieldsFromPreparedStatement() throws Exception {
when(rsMetaData.getColumnCount()).thenReturn(1);
when(rsMetaData.getColumnName(1)).thenReturn(columnName);
when(rsMetaData.getColumnLabel(1)).thenReturn(columnName);
when(rsMetaData.getColumnType(1)).thenReturn(Types.DECIMAL);
when(meta.stripCR(anyString())).thenReturn(sql);
// MySQL specific ?
when(meta.getIDatabase()).thenReturn(new NoneDatabaseMeta());
when(conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)).thenReturn(ps);
when(ps.getMetaData()).thenReturn(rsMetaData);
Database db = new Database(log, variables, meta);
db.setConnection(conn);
IRowMeta iRowMeta = db.getQueryFieldsFromPreparedStatement(sql);
assertEquals(iRowMeta.size(), 1);
assertEquals(iRowMeta.getValueMeta(0).getName(), columnName);
assertTrue(iRowMeta.getValueMeta(0) instanceof ValueMetaNumber);
}
use of org.apache.hop.core.row.value.ValueMetaNumber in project hop by apache.
the class DriverCqlRowHandlerTest method testQueryRows.
@Test
public void testQueryRows() throws Exception {
List<Object[]> rowList = new ArrayList<>();
rowList.add(new Object[] { 1L, "a", 0.2d });
rowList.add(new Object[] { 2L, "b", 42d });
DriverKeyspace keyspace = mock(DriverKeyspace.class);
Session session = mock(Session.class);
ResultSet rs = mock(ResultSet.class);
mockColumnDefinitions(rs, DataType.cint(), DataType.text(), DataType.cdouble());
when(session.execute(anyString())).thenReturn(rs);
Iterator<Object[]> it = rowList.iterator();
when(rs.isExhausted()).then(invoc -> {
return !it.hasNext();
});
when(rs.one()).then(invocation -> {
Object[] rowArr = it.next();
Row row = mock(Row.class);
when(row.getObject(anyInt())).then(invoc -> {
return rowArr[(int) invoc.getArguments()[0]];
});
when(row.getLong(0)).thenReturn((long) rowArr[0]);
when(row.getDouble(2)).thenReturn((double) rowArr[2]);
return row;
});
DriverCqlRowHandler rowHandler = new DriverCqlRowHandler(keyspace, session, true);
IRowMeta rowMeta = new RowMeta();
rowMeta.addValueMeta(new ValueMetaInteger("a"));
rowMeta.addValueMeta(new ValueMetaString("b"));
rowMeta.addValueMeta(new ValueMetaNumber("c"));
rowHandler.newRowQuery(mock(ITransform.class), "tab", "select * from tab", null, null, mock(ILogChannel.class));
List<Object[]> resultRows = getNextOutputRows(rowHandler, rowMeta);
assertEquals(2, resultRows.size());
assertEquals(2L, resultRows.get(1)[0]);
}
use of org.apache.hop.core.row.value.ValueMetaNumber in project hop by apache.
the class DriverCqlRowHandlerTest method testExpandCollection.
@Test
public void testExpandCollection() throws Exception {
List<Object[]> rowList = new ArrayList<>();
ArrayList<Long> numList = new ArrayList<>();
numList.add(1L);
numList.add(2L);
numList.add(3L);
rowList.add(new Object[] { 1L, numList });
rowList.add(new Object[] { 2L, new ArrayList<Long>() });
Iterator<Object[]> it = rowList.iterator();
DriverKeyspace keyspace = mock(DriverKeyspace.class);
Session session = mock(Session.class);
ResultSet rs = mock(ResultSet.class);
when(session.execute(anyString())).thenReturn(rs);
when(rs.isExhausted()).then(invoc -> {
return !it.hasNext();
});
when(rs.one()).then(invocation -> {
Object[] rowArr = it.next();
Row row = mock(Row.class);
when(row.getObject(anyInt())).then(invoc -> {
return rowArr[(int) invoc.getArguments()[0]];
});
when(row.getLong(0)).thenReturn((long) rowArr[0]);
return row;
});
mockColumnDefinitions(rs, DataType.bigint(), DataType.list(DataType.bigint()));
DriverCqlRowHandler rowHandler = new DriverCqlRowHandler(keyspace, session, true);
IRowMeta rowMeta = new RowMeta();
rowMeta.addValueMeta(new ValueMetaInteger("id"));
rowMeta.addValueMeta(new ValueMetaNumber("nums"));
rowHandler.newRowQuery(mock(ITransform.class), "tab", "select * from tab", null, null, mock(ILogChannel.class));
List<Object[]> resultRows = getNextOutputRows(rowHandler, rowMeta);
assertEquals(4, resultRows.size());
assertEquals(1L, resultRows.get(0)[1]);
assertEquals(2L, resultRows.get(3)[0]);
assertNull(resultRows.get(3)[1]);
}
use of org.apache.hop.core.row.value.ValueMetaNumber in project hop by apache.
the class ConditionTest method testPdi13227.
@Test
public void testPdi13227() throws Exception {
IRowMeta rowMeta1 = new RowMeta();
rowMeta1.addValueMeta(new ValueMetaNumber("name1"));
rowMeta1.addValueMeta(new ValueMetaNumber("name2"));
rowMeta1.addValueMeta(new ValueMetaNumber("name3"));
IRowMeta rowMeta2 = new RowMeta();
rowMeta2.addValueMeta(new ValueMetaNumber("name2"));
rowMeta2.addValueMeta(new ValueMetaNumber("name1"));
rowMeta2.addValueMeta(new ValueMetaNumber("name3"));
String left = "name1";
String right = "name3";
Condition condition = new Condition(left, Condition.FUNC_EQUAL, right, null);
assertTrue(condition.evaluate(rowMeta1, new Object[] { 1.0, 2.0, 1.0 }));
assertTrue(condition.evaluate(rowMeta2, new Object[] { 2.0, 1.0, 1.0 }));
}
Aggregations