use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class SqlResultSetReader method read.
/**
* Returns a new table with the given tableName, constructed from the given result set
*
* @throws SQLException if there is a problem detected in the database
*/
public static Table read(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
Table table = Table.create();
// Setup the columns and add to the table
for (int i = 1; i <= metaData.getColumnCount(); i++) {
ColumnType type = getColumnType(metaData.getColumnType(i), metaData.getScale(i), metaData.getPrecision(i));
Preconditions.checkState(type != null, "No column type found for %s as specified for column %s", metaData.getColumnType(i), metaData.getColumnName(i));
Column<?> newColumn = type.create(metaData.getColumnLabel(i));
table.addColumns(newColumn);
}
// Add the rows
while (resultSet.next()) {
for (int i = 1; i <= metaData.getColumnCount(); i++) {
Column<?> column = // subtract 1 because results sets originate at 1 not 0
table.column(i - 1);
if (column instanceof ShortColumn) {
appendToColumn(column, resultSet, resultSet.getShort(i));
} else if (column instanceof IntColumn) {
appendToColumn(column, resultSet, resultSet.getInt(i));
} else if (column instanceof LongColumn) {
appendToColumn(column, resultSet, resultSet.getLong(i));
} else if (column instanceof FloatColumn) {
appendToColumn(column, resultSet, resultSet.getFloat(i));
} else if (column instanceof DoubleColumn) {
appendToColumn(column, resultSet, resultSet.getDouble(i));
} else if (column instanceof BooleanColumn) {
appendToColumn(column, resultSet, resultSet.getBoolean(i));
} else {
column.appendObj(resultSet.getObject(i));
}
}
}
return table;
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class TableSliceGroup method aggregate.
/**
* Applies the given aggregations to the given columns. The apply and combine steps of a
* split-apply-combine.
*
* @param functions map from column name to aggregation to apply on that function
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public Table aggregate(ListMultimap<String, AggregateFunction<?, ?>> functions) {
Table groupTable = summaryTableName(sourceTable);
StringColumn groupColumn = StringColumn.create("Group");
groupTable.addColumns(groupColumn);
boolean firstFunction = true;
for (Map.Entry<String, Collection<AggregateFunction<?, ?>>> entry : functions.asMap().entrySet()) {
String columnName = entry.getKey();
for (AggregateFunction function : entry.getValue()) {
String colName = aggregateColumnName(columnName, function.functionName());
ColumnType type = function.returnType();
Column resultColumn = type.create(colName);
for (TableSlice subTable : getSlices()) {
Object result = function.summarize(subTable.column(columnName));
if (firstFunction) {
groupColumn.append(subTable.name());
}
if (function.returnType().equals(ColumnType.DOUBLE)) {
Number number = (Number) result;
resultColumn.append(number.doubleValue());
} else {
resultColumn.append(result);
}
}
groupTable.addColumns(resultColumn);
firstFunction = false;
}
}
return splitGroupingColumn(groupTable);
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class XlsxReader method readMultiple.
/**
* Read at most a table from every sheet.
*
* @param includeNulls include nulls for sheets without a table
* @return a list of tables, at most one for every sheet
*/
protected List<Table> readMultiple(XlsxReadOptions options, boolean includeNulls) throws IOException {
byte[] bytes = null;
InputStream input = getInputStream(options, bytes);
List<Table> tables = new ArrayList<>();
try (XSSFWorkbook workbook = new XSSFWorkbook(input)) {
for (Sheet sheet : workbook) {
TableRange tableArea = findTableArea(sheet);
if (tableArea != null) {
Table table = createTable(sheet, tableArea, options);
tables.add(table);
} else if (includeNulls) {
tables.add(null);
}
}
return tables;
} finally {
if (options.source().reader() == null) {
// if we get a reader back from options it means the client opened it, so let
// the client close it
// if it's null, we close it here.
input.close();
}
}
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class XlsxReader method read.
@Override
public Table read(XlsxReadOptions options) throws IOException {
List<Table> tables = readMultiple(options, true);
if (options.sheetIndex() != null) {
int index = options.sheetIndex();
if (index < 0 || index >= tables.size()) {
throw new IndexOutOfBoundsException(String.format("Sheet index %d outside bounds. %d sheets found.", index, tables.size()));
}
Table table = tables.get(index);
if (table == null) {
throw new IllegalArgumentException(String.format("No table found at sheet index %d.", index));
}
return table;
}
// since no specific sheetIndex asked, return first table
return tables.stream().filter(t -> t != null).findFirst().orElseThrow(() -> new IllegalArgumentException("No tables found."));
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class Relation method summary.
public Table summary() {
Table summaryTable = Table.create(this.name());
if (this.columnCount() == 0) {
return summaryTable;
}
summaryTable.addColumns(StringColumn.create("Measure"));
for (int i = 0; i < this.columnCount(); i++) {
Table columnSummary = this.column(i).summary();
columnSummary.column(1).setName(this.column(i).name());
summaryTable = summaryTable.joinOn("Measure").fullOuter(columnSummary, columnSummary.column(0).name());
}
summaryTable.column(0).setName("Summary");
return summaryTable;
}
Aggregations