Search in sources :

Example 1 with LongColumn

use of tech.tablesaw.api.LongColumn 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;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) LongColumn(tech.tablesaw.api.LongColumn) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) DoubleColumn(tech.tablesaw.api.DoubleColumn) ShortColumn(tech.tablesaw.api.ShortColumn) BooleanColumn(tech.tablesaw.api.BooleanColumn) FloatColumn(tech.tablesaw.api.FloatColumn) IntColumn(tech.tablesaw.api.IntColumn)

Example 2 with LongColumn

use of tech.tablesaw.api.LongColumn in project symja_android_library by axkr.

the class XlsxReader method appendValue.

@SuppressWarnings("unchecked")
private Column<?> appendValue(Column<?> column, Cell cell) {
    CellType cellType = cell.getCellType() == FORMULA ? cell.getCachedFormulaResultType() : cell.getCellType();
    switch(cellType) {
        case STRING:
            column.appendCell(cell.getRichStringCellValue().getString());
            return null;
        case NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                // This will return inconsistent results across time zones, but that matches Excel's
                // behavior
                LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                if (column.type() == ColumnType.STRING) {
                    // If column has String type try to honor it and leave the value as an string as similar
                    // as posible as seen in Excel
                    String dataFormatStyle = cell.getCellStyle().getDataFormatString();
                    String val;
                    if ("general".equalsIgnoreCase(dataFormatStyle)) {
                        val = new CellGeneralFormatter().format(cell.getNumericCellValue());
                    } else {
                        val = new CellDateFormatter(dataFormatStyle).format(cell.getDateCellValue());
                    }
                    column.appendCell(val);
                } else {
                    column.appendCell(localDate.toString());
                }
                return null;
            } else {
                double num = cell.getNumericCellValue();
                if (column.type() == ColumnType.INTEGER) {
                    Column<Integer> intColumn = (Column<Integer>) column;
                    if ((int) num == num) {
                        intColumn.append((int) num);
                        return null;
                    } else if ((long) num == num) {
                        Column<Long> altColumn = LongColumn.create(column.name(), column.size());
                        altColumn = intColumn.mapInto(s -> (long) s, altColumn);
                        altColumn.append((long) num);
                        return altColumn;
                    } else {
                        Column<Double> altColumn = DoubleColumn.create(column.name(), column.size());
                        altColumn = intColumn.mapInto(s -> (double) s, altColumn);
                        altColumn.append(num);
                        return altColumn;
                    }
                } else if (column.type() == ColumnType.LONG) {
                    Column<Long> longColumn = (Column<Long>) column;
                    if ((long) num == num) {
                        longColumn.append((long) num);
                        return null;
                    } else {
                        Column<Double> altColumn = DoubleColumn.create(column.name(), column.size());
                        altColumn = longColumn.mapInto(s -> (double) s, altColumn);
                        altColumn.append(num);
                        return altColumn;
                    }
                } else if (column.type() == ColumnType.DOUBLE) {
                    Column<Double> doubleColumn = (Column<Double>) column;
                    doubleColumn.append(num);
                    return null;
                } else if (column.type() == ColumnType.STRING) {
                    // If column has String type try to honor it and leave the value as an string as similar
                    // as posible as seen in Excel
                    Column<String> stringColumn = (Column<String>) column;
                    String dataFormatStyle = cell.getCellStyle().getDataFormatString();
                    String val;
                    if ("general".equalsIgnoreCase(dataFormatStyle)) {
                        val = new CellGeneralFormatter().format(cell.getNumericCellValue());
                    } else {
                        val = new CellNumberFormatter(dataFormatStyle).format(cell.getNumericCellValue());
                    }
                    stringColumn.append(val);
                }
            }
            break;
        case BOOLEAN:
            if (column.type() == ColumnType.BOOLEAN) {
                Column<Boolean> booleanColumn = (Column<Boolean>) column;
                booleanColumn.append(cell.getBooleanCellValue());
                return null;
            } else if (column.type() == ColumnType.STRING) {
                // If column has String type try to honor it and leave the value as an string as similar
                // as posible as seen in Excel
                Column<String> stringColumn = (Column<String>) column;
                String val = new CellGeneralFormatter().format(cell.getBooleanCellValue());
                stringColumn.append(val);
            }
        default:
            break;
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) CellNumberFormatter(org.apache.poi.ss.format.CellNumberFormatter) Date(java.util.Date) CellDateFormatter(org.apache.poi.ss.format.CellDateFormatter) CellType(org.apache.poi.ss.usermodel.CellType) LongColumn(tech.tablesaw.api.LongColumn) DoubleColumn(tech.tablesaw.api.DoubleColumn) Column(tech.tablesaw.columns.Column) CellGeneralFormatter(org.apache.poi.ss.format.CellGeneralFormatter)

Example 3 with LongColumn

use of tech.tablesaw.api.LongColumn in project symja_android_library by axkr.

the class DateTimeMapFunctions method timeWindow.

/**
 * Returns a column containing integers representing the nth group (0-based) that a date falls
 * into.
 *
 * <p>Example: When Unit = ChronoUnit.DAY and n = 5, we form 5 day groups. a Date that is 2 days
 * after the start is assigned to the first ("0") group. A day 7 days after the start is assigned
 * to the second ("1") group.
 *
 * @param unit A ChronoUnit greater than or equal to a minute
 * @param n The number of units in each group.
 * @param start The starting point of the first group; group boundaries are offsets from this
 *     point
 */
default LongColumn timeWindow(ChronoUnit unit, int n, LocalDateTime start) {
    String newColumnName = "" + n + " " + unit.toString() + " window [" + name() + "]";
    long packedStartDate = pack(start);
    LongColumn numberColumn = LongColumn.create(newColumnName, size());
    for (int i = 0; i < size(); i++) {
        long packedDate = getLongInternal(i);
        long result;
        switch(unit) {
            case MINUTES:
                result = minutesUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case HOURS:
                result = hoursUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case DAYS:
                result = daysUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case WEEKS:
                result = weeksUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case MONTHS:
                result = monthsUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            case YEARS:
                result = yearsUntil(packedDate, packedStartDate) / n;
                numberColumn.set(i, result);
                break;
            default:
                throw new UnsupportedTemporalTypeException("The ChronoUnit " + unit + " is not supported for timeWindows on dates");
        }
    }
    numberColumn.setPrintFormatter(NumberColumnFormatter.ints());
    return numberColumn;
}
Also used : LongColumn(tech.tablesaw.api.LongColumn) UnsupportedTemporalTypeException(java.time.temporal.UnsupportedTemporalTypeException)

Example 4 with LongColumn

use of tech.tablesaw.api.LongColumn in project symja_android_library by axkr.

the class TemporalMapFunctions method difference.

default LongColumn difference(TemporalColumn<T> column2, ChronoUnit unit) {
    LongColumn newColumn = LongColumn.create(name() + " - " + column2.name() + "[" + unit.name() + "]");
    for (int r = 0; r < size(); r++) {
        if (this.isMissing(r) || column2.isMissing(r)) {
            newColumn.appendMissing();
        } else {
            long c1 = this.getLongInternal(r);
            long c2 = column2.getLongInternal(r);
            LocalDateTime value1 = asLocalDateTime(c1);
            LocalDateTime value2 = asLocalDateTime(c2);
            if (value1 != null && value2 != null) {
                newColumn.append(unit.between(value1, value2));
            } else {
                newColumn.appendMissing();
            }
        }
    }
    return newColumn;
}
Also used : PackedLocalDateTime.asLocalDateTime(tech.tablesaw.columns.datetimes.PackedLocalDateTime.asLocalDateTime) LocalDateTime(java.time.LocalDateTime) LongColumn(tech.tablesaw.api.LongColumn)

Aggregations

LongColumn (tech.tablesaw.api.LongColumn)4 LocalDateTime (java.time.LocalDateTime)2 DoubleColumn (tech.tablesaw.api.DoubleColumn)2 ResultSetMetaData (java.sql.ResultSetMetaData)1 UnsupportedTemporalTypeException (java.time.temporal.UnsupportedTemporalTypeException)1 Date (java.util.Date)1 CellDateFormatter (org.apache.poi.ss.format.CellDateFormatter)1 CellGeneralFormatter (org.apache.poi.ss.format.CellGeneralFormatter)1 CellNumberFormatter (org.apache.poi.ss.format.CellNumberFormatter)1 CellType (org.apache.poi.ss.usermodel.CellType)1 BooleanColumn (tech.tablesaw.api.BooleanColumn)1 ColumnType (tech.tablesaw.api.ColumnType)1 FloatColumn (tech.tablesaw.api.FloatColumn)1 IntColumn (tech.tablesaw.api.IntColumn)1 ShortColumn (tech.tablesaw.api.ShortColumn)1 Table (tech.tablesaw.api.Table)1 Column (tech.tablesaw.columns.Column)1 PackedLocalDateTime.asLocalDateTime (tech.tablesaw.columns.datetimes.PackedLocalDateTime.asLocalDateTime)1