Search in sources :

Example 56 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class DatabaseHelper method createTableSpec.

/**
 * Creates <code>DataTableSpec</code> from the given <code>ResultSetMetaData</code>
 * @param meta the <code>ResultSetMetaData</code> used to create <code>DataTableSpec</code>
 * @return a {@link DataTableSpec} created from the given {@link ResultSetMetaData}
 * @throws SQLException
 */
protected DataTableSpec createTableSpec(final ResultSetMetaData meta) throws SQLException {
    int cols = meta.getColumnCount();
    if (cols == 0) {
        return new DataTableSpec("database");
    }
    StatementManipulator manipulator = m_conn.getUtility().getStatementManipulator();
    DataTableSpec spec = null;
    for (int i = 0; i < cols; i++) {
        int dbIdx = i + 1;
        String name = manipulator.unquoteColumn(meta.getColumnLabel(dbIdx));
        int type = meta.getColumnType(dbIdx);
        DataType newType = getKNIMEType(type, meta, dbIdx);
        if (spec == null) {
            spec = new DataTableSpec("database", new DataColumnSpecCreator(name, newType).createSpec());
        } else {
            name = DataTableSpec.getUniqueColumnName(spec, name);
            spec = new DataTableSpec("database", spec, new DataTableSpec(new DataColumnSpecCreator(name, newType).createSpec()));
        }
    }
    return spec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataType(org.knime.core.data.DataType)

Example 57 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class DatabaseReaderConnection method createTableSpec.

private DataTableSpec createTableSpec(final ResultSetMetaData meta) throws SQLException {
    int cols = meta.getColumnCount();
    if (cols == 0) {
        return new DataTableSpec("database");
    }
    StatementManipulator manipulator = m_conn.getUtility().getStatementManipulator();
    DataTableSpec spec = null;
    for (int i = 0; i < cols; i++) {
        int dbIdx = i + 1;
        String name = manipulator.unquoteColumn(meta.getColumnLabel(dbIdx));
        int type = meta.getColumnType(dbIdx);
        DataType newType;
        switch(type) {
            // all types that can be interpreted as integer
            case Types.BIT:
            case Types.BOOLEAN:
                newType = BooleanCell.TYPE;
                break;
            // all types that can be interpreted as integer
            case Types.TINYINT:
            case Types.SMALLINT:
            case Types.INTEGER:
                newType = IntCell.TYPE;
                break;
            // all types that can be interpreted as long
            case Types.BIGINT:
                newType = LongCell.TYPE;
                break;
            // all types that can be interpreted as double
            case Types.FLOAT:
            case Types.DOUBLE:
            case Types.NUMERIC:
            case Types.DECIMAL:
            case Types.REAL:
                newType = DoubleCell.TYPE;
                break;
            // all types that can be interpreted as data-and-time
            case Types.TIME:
            case Types.DATE:
            case Types.TIMESTAMP:
                newType = DateAndTimeCell.TYPE;
                break;
            // all types that can be interpreted as binary object
            case Types.BLOB:
            case Types.LONGVARBINARY:
            case Types.BINARY:
                newType = BinaryObjectDataCell.TYPE;
                break;
            // fallback string
            default:
                newType = StringCell.TYPE;
        }
        if (spec == null) {
            spec = new DataTableSpec("database", new DataColumnSpecCreator(name, newType).createSpec());
        } else {
            name = DataTableSpec.getUniqueColumnName(spec, name);
            spec = new DataTableSpec("database", spec, new DataTableSpec(new DataColumnSpecCreator(name, newType).createSpec()));
        }
    }
    return spec;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataType(org.knime.core.data.DataType)

Example 58 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class DataContainer method addRowToTableWrite.

private void addRowToTableWrite(final DataRow row) {
    // let's do every possible sanity check
    int numCells = row.getNumCells();
    RowKey key = row.getKey();
    if (numCells != m_spec.getNumColumns()) {
        throw new IllegalArgumentException("Cell count in row \"" + key + "\" is not equal to length of column names " + "array: " + numCells + " vs. " + m_spec.getNumColumns());
    }
    for (int c = 0; c < numCells; c++) {
        DataType columnClass = m_spec.getColumnSpec(c).getType();
        DataCell value;
        DataType runtimeType;
        if (row instanceof BlobSupportDataRow) {
            BlobSupportDataRow bsvalue = (BlobSupportDataRow) row;
            value = bsvalue.getRawCell(c);
        } else {
            value = row.getCell(c);
        }
        if (value instanceof BlobWrapperDataCell) {
            BlobWrapperDataCell bw = (BlobWrapperDataCell) value;
            runtimeType = bw.getBlobDataType();
        } else {
            runtimeType = value.getType();
        }
        if (!columnClass.isASuperTypeOf(runtimeType)) {
            String valString = value.toString();
            // avoid too long string representations
            if (valString.length() > 30) {
                valString = valString.substring(0, 30) + "...";
            }
            throw new IllegalArgumentException("Runtime class of object \"" + valString + "\" (index " + c + ") in " + "row \"" + key + "\" is " + runtimeType.toString() + " and does " + "not comply with its supposed superclass " + columnClass.toString());
        }
    }
    // for all cells
    m_domainCreator.updateDomain(row);
    addRowKeyForDuplicateCheck(key);
    m_buffer.addRow(row, false, m_forceCopyOfBlobs);
}
Also used : RowKey(org.knime.core.data.RowKey) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell)

Example 59 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class DefaultTableStoreWriter method writeMetaInfoAfterWrite.

/**
 * {@inheritDoc}
 */
@Override
public void writeMetaInfoAfterWrite(final NodeSettingsWO settings) {
    settings.addString(DefaultTableStoreFormat.CFG_COMPRESSION, m_compressionFormat.name());
    // unreported bug fix: NPE when the table only contains missing values.
    if (m_typeShortCuts == null) {
        m_typeShortCuts = new HashMap<CellClassInfo, Byte>();
    }
    CellClassInfo[] shortCutsLookup = new CellClassInfo[m_typeShortCuts.size()];
    for (Map.Entry<CellClassInfo, Byte> e : m_typeShortCuts.entrySet()) {
        byte shortCut = e.getValue();
        CellClassInfo type = e.getKey();
        shortCutsLookup[shortCut - BYTE_TYPE_START] = type;
    }
    m_shortCutsLookup = shortCutsLookup;
    NodeSettingsWO typeSubSettings = settings.addNodeSettings(DefaultTableStoreFormat.CFG_CELL_CLASSES);
    for (int i = 0; i < shortCutsLookup.length; i++) {
        CellClassInfo info = shortCutsLookup[i];
        NodeSettingsWO single = typeSubSettings.addNodeSettings("element_" + i);
        single.addString(DefaultTableStoreFormat.CFG_CELL_SINGLE_CLASS, info.getCellClass().getName());
        DataType elementType = info.getCollectionElementType();
        if (elementType != null) {
            NodeSettingsWO subTypeConfig = single.addNodeSettings(DefaultTableStoreFormat.CFG_CELL_SINGLE_ELEMENT_TYPE);
            elementType.save(subTypeConfig);
        }
    }
}
Also used : NodeSettingsWO(org.knime.core.node.NodeSettingsWO) DataType(org.knime.core.data.DataType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 60 with DataType

use of org.knime.core.data.DataType in project knime-core by knime.

the class JavaToDataCellConverterRegistry method getConverterFactories.

/**
 * Get all {@link JavaToDataCellConverterFactory converter factories} which create {@link JavaToDataCellConverter}s
 * that convert <code>sourceType</code> into <code>destType</code>. If you do not require more than one converter
 * factory, you should consider using {@link #getPreferredConverterFactory(Class, DataType)} instead.
 *
 * @param sourceType Source type to convert
 * @param destType {@link DataType} to convert to
 * @return collection of {@link JavaToDataCellConverterFactory converter factories} which create converters which
 *         convert from <code>sourceType</code> to <code>destType</code>
 * @param <S> A JavaToDataCellConverter type (letting java infer the type is highly recommended)
 */
// we only put JavaToDataCellConverter<T> into the map for Class<T>
public <S> Collection<JavaToDataCellConverterFactory<S>> getConverterFactories(final Class<S> sourceType, final DataType destType) {
    final LinkedBlockingQueue<Class<?>> classes = new LinkedBlockingQueue<>();
    classes.add(sourceType);
    Class<?> curClass = null;
    final ArrayList<JavaToDataCellConverterFactory<S>> factories = new ArrayList<>();
    while ((curClass = classes.poll()) != null) {
        final ArrayList<JavaToDataCellConverterFactory<?>> newFactories = m_converterFactories.get(new ConversionKey(curClass, destType));
        if (newFactories != null) {
            factories.addAll((Collection<? extends JavaToDataCellConverterFactory<S>>) newFactories);
        }
        /* check if a supertype has a compatible converter factory */
        classes.addAll(Arrays.asList(curClass.getInterfaces()));
        if (curClass.getSuperclass() != null) {
            classes.add(curClass.getSuperclass());
        }
    }
    if (destType.isCollectionType() && sourceType.isArray()) {
        final Collection<? extends JavaToDataCellConverterFactory<S>> elementFactories = (Collection<? extends JavaToDataCellConverterFactory<S>>) getConverterFactories(sourceType.getComponentType(), destType.getCollectionElementType());
        final List<?> arrayFactories = elementFactories.stream().map((elementFactory) -> getArrayConverterFactory((JavaToDataCellConverterFactory<?>) elementFactory)).collect(Collectors.toList());
        factories.addAll((Collection<? extends JavaToDataCellConverterFactory<S>>) arrayFactories);
    }
    return factories;
}
Also used : Arrays(java.util.Arrays) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Pair(org.knime.core.util.Pair) HashSet(java.util.HashSet) DataCellFactoryMethod(org.knime.core.data.convert.DataCellFactoryMethod) DataCellToJavaConverterFactory(org.knime.core.data.convert.java.DataCellToJavaConverterFactory) DataCellFactory(org.knime.core.data.DataCellFactory) DataTypeRegistry(org.knime.core.data.DataTypeRegistry) NodeLogger(org.knime.core.node.NodeLogger) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) ConversionKey(org.knime.core.data.convert.ConversionKey) Method(java.lang.reflect.Method) LinkedHashSet(java.util.LinkedHashSet) ClassUtil(org.knime.core.data.convert.util.ClassUtil) IncompleteAnnotationException(java.lang.annotation.IncompleteAnnotationException) Collection(java.util.Collection) Set(java.util.Set) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) DataCellToJavaConverterRegistry(org.knime.core.data.convert.java.DataCellToJavaConverterRegistry) List(java.util.List) Optional(java.util.Optional) Platform(org.eclipse.core.runtime.Platform) Pattern(java.util.regex.Pattern) DataType(org.knime.core.data.DataType) Collections(java.util.Collections) ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ConversionKey(org.knime.core.data.convert.ConversionKey) Collection(java.util.Collection)

Aggregations

DataType (org.knime.core.data.DataType)330 DataColumnSpec (org.knime.core.data.DataColumnSpec)142 DataTableSpec (org.knime.core.data.DataTableSpec)101 DataCell (org.knime.core.data.DataCell)96 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)95 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)71 DoubleValue (org.knime.core.data.DoubleValue)67 DataRow (org.knime.core.data.DataRow)61 ArrayList (java.util.ArrayList)55 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)34 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)32 DefaultRow (org.knime.core.data.def.DefaultRow)24 HashSet (java.util.HashSet)23 HashMap (java.util.HashMap)20 StringCell (org.knime.core.data.def.StringCell)20 NominalValue (org.knime.core.data.NominalValue)18 DoubleCell (org.knime.core.data.def.DoubleCell)18 IntCell (org.knime.core.data.def.IntCell)18 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)18 ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)18