# Column Expression: Dynamically Extract and Define Type

**URL:** <https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681>\
**Category:** KNIME Analytics Platform\
**Tags:** flow-variables, node\
**Created:** [January 7, 2023, 1:21pm UTC](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681 "2023-01-07T13:21:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwiegand](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mwiegand/32/66896_2.png) [@mwiegand](https://forum.knime.com/u/mwiegand)\
**Post date:** [January 7, 2023, 1:21pm UTC](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/1 "2023-01-07T13:21:25Z")

</div>

Hi,

picking up my original topic:

> [@Column Expressions Guidance: Dynamically set Column Type](https://forum.knime.com/t/column-expressions-guidance-dynamically-set-column-type/47003):
>
> Hi all, while working with the [Column Expression Node](https://hub.knime.com/knime/extensions/org.knime.features.expressions/latest/org.knime.expressions.base.node.formulas.FormulasNodeFactory) I struggle a lot, maybe because of the lack of documentation, to dynamically define the column type (i.e. string, int etc.). I overwrite via a flow variable expressions \> element 0 \> outputType \> cell-class but to no avail. I tried with the: exact type declaration like “Number (integer)” or “String” an array index (starting at 0) like 12 for “Number (integer)” or 20 for “String” a cleaned version …

I want to extract the column type to save this alongside i.e. a CSV file in order to once after reading CSV again, reinstate the original column type(s). Reason for that is that data might get shared / transmitted in human readable text files types. But, reinterpreting column types is time consuming and failure prone.

I am trying to accomplish this by looping over the table of data types derived from aforementioned workflow.

| Binary Object | org.knime.core.data.blob.BinaryObjectDataCell |
| --- | --- |
| Bit vector | org.knime.core.data.vector.bitvector.DenseBitVectorCell |
| Bit vector (sparse) | org.knime.core.data.vector.bitvector.SparseBitVectorCell |
| Boolean Value | org.knime.core.data.def.BooleanCell |
| Byte vector | org.knime.core.data.vector.bytevector.DenseByteVectorCell |
| Duration | org.knime.core.data.time.duration.DurationCell |
| Image | org.knime.knip.base.data.img.ImgPlusCell |
| JSON | org.knime.core.data.json.JSONCell |
| Local Date | org.knime.core.data.time.localdate.LocalDateCell |
| Local Date Time | org.knime.core.data.time.localdatetime.LocalDateTimeCell |
| Local Time | org.knime.core.data.time.localtime.LocalTimeCell |
| Number (double) | org.knime.core.data.def.DoubleCell |
| Number (integer) | org.knime.core.data.def.IntCell |
| Number (long) | org.knime.core.data.def.LongCell |
| PMML | org.knime.core.data.def.LongCell |
| PNG Image | org.knime.core.data.image.png.PNGImageCell |
| Path | org.knime.filehandling.core.data.location.cell.SimpleFSLocationCell |
| Period | org.knime.core.data.time.period.PeriodCell |
| RDKit Molecule | org.rdkit.knime.types.RDKitMolCell2 |
| SVG Image | org.knime.base.data.xml.SvgCell |
| String | org.knime.core.data.def.StringCell |
| URI | org.knime.core.data.uri.URIDataCell |
| XML | org.knime.core.data.xml.XMLCell |
| Zone Date Time | org.knime.core.data.time.zoneddatetime.ZonedDateTimeCell |

Not really an elegant but more a brute force approach (due lack of ideas currently). However, it seems the type can not be managed through variables, can it?

 ![Bildschirm­foto 2023-01-07 um 14.02.40](https://forum-cdn.knime.com/uploads/default/original/3X/3/5/35f374204572dd00b64dc2255cdb69e3936033ad.jpeg)  
 ![Bildschirm­foto 2023-01-07 um 13.58.48](https://forum-cdn.knime.com/uploads/default/original/3X/3/4/34029ca3f5ae58ec4da96c895094a1469f780eb4.jpeg)

> **[Column Expression Dynamically Extract and Define Type – mwiegand](https://hub.knime.com/mwiegand/spaces/Public/Knime%20Forum%20Support/Column%20Expression%20Dynamically%20Extract%20and%20Define%20Type~HypQQFLOX6vZUT_1/current-state)**
>
> Solutions for data science: find workflows, nodes and components, and collaborate in spaces.

Best  
Mike

---

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [January 7, 2023, 1:33pm UTC](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/2 "2023-01-07T13:33:51Z")

</div>

> [@mwiegand](#):
>
> Reason for that is that data might get shared / transmitted in human readable text files types.

If you absolutely must have text files that are still readable you could try and use the [ARFF format](https://datahub.io/blog/attribute-relation-file-format-arff) which is also supported by KNIME

> [@Export data from KNIME for processing in Python (externaly)](https://forum.knime.com/t/export-data-from-knime-for-processing-in-python-externaly/47495/2):
>
> @TheLeo you could use [Parquet](https://kni.me/n/jita8ax2pW9UyCI8) to keep the column types. Or SQLite database: ORC could also be an option but it might be more complicated. Both ORC and Parquet would allow to read and write data in chunks. If you absolutely must have a text only file. [ARFF](https://datahub.io/blog/attribute-relation-file-format-arff) is another option: If you want to explore more about exchange between KNIME and Python you might have a look here:

> **[use the new (KNIME 4.6) integrated Python extension to read and write ARFF...](https://hub.knime.com/mlauber71/spaces/Public/kn_example_python_read_arff_file~mp7Pv607rD0biVI5/current-state)**
>
> use the new (KNIME 4.6) integrated Python extension to read and write ARFF file into KNIME, export it again as Parquet, put it into SQLite database and read it…

> **[ARFF Format - Weka Wiki](https://waikato.github.io/weka-wiki/formats_and_processing/arff/)**
>
> Documention on Weka that can also be found in the manual and the example archive that comes with each Weka download (zip or installer).

---

<div class="post-metadata">

**Author:** ![mwiegand](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mwiegand/32/66896_2.png) [@mwiegand](https://forum.knime.com/u/mwiegand)\
**Post date:** [January 7, 2023, 1:55pm UTC](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/3 "2023-01-07T13:55:42Z")

</div>

Nice node but it’s not quite cutting it:

`WARN ARFF Writer 5:1708 Class List (Collection of: String) not supported.`

There are some complex data types I am curious they can exist outside of Knime and still being readable. Adding more to my explantion / the reason why, not everyone will or can use Knime. Having a human readable but still interchangeable format would make adoption easier. Some only trust, understand and therefore support what they can actually see.

Anyways, sometimes it’s necessary to leverage meta information such as the column type or date time format in order to properly make systems / processes understand each other. Having to guess / try & error each time, is one of the root causes of potentially nice solutions being never adopted.

---

<div class="post-metadata">

**Author:** ![mlauber71](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mlauber71/32/73590_2.png) [@mlauber71](https://forum.knime.com/u/mlauber71)\
**Post date:** [January 7, 2023, 8:48pm UTC](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/4 "2023-01-07T20:48:29Z")

</div>

@mwiegand if we are into interesting (re-)engineerings concerning text files I can add the case of a MySQL Dump being imported into H2 … 🙂

> [@Reading Mysql dump(.sql)](https://forum.knime.com/t/reading-mysql-dump-sql/20578/4):
>
> OK, I built something since the mentioned examples are not straightforward (if you want to play around with them further in a subfolder of the workflow there is a /script/ folder containing some tests with R and Python scrips mentioned in the links but the results are insufficient if you have more complex INSERTS statements). As an example I use the [Mondial](https://www.dbis.informatik.uni-goettingen.de/Mondial/) MySQL dump mentioned here: [https://blog.twineworks.com/converting-a-mysql-dump-to-csv-files-b5e92d7cc5dd](https://blog.twineworks.com/converting-a-mysql-dump-to-csv-files-b5e92d7cc5dd) (which is also included in the w…

I think the most transferable file format still is Parquet. Another one might be ORC though with KNIME not exactly as flexible. Also SQLite and H2 are quite common to exchange information. But admittedly they are not text-only. KNIME itself supports a very wider arrange of data formats that you will not see in most other applications.

---

<div class="post-metadata">

**Author:** ![mwiegand](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mwiegand/32/66896_2.png) [@mwiegand](https://forum.knime.com/u/mwiegand)\
**Post date:** [January 8, 2023, 7:17am UTC](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/5 "2023-01-08T07:17:47Z")

</div>

Good morning @mlauber71,

that sounds like a nightmare but pretty much falls into the nice exercise of “will it blend” 😉

The Parquet nodes I became aware about recently too and thanks for the reminder. It feels like these are one if not the only possible solution. Got to familiarize with Parquet more. Will check ORC as well.

If I find a solution to ensure column types are preserved as some sort of meta file, I will circle back.

Best  
Mike

---

<div class="post-metadata">

**Author:** ![mwiegand](https://forum-cdn.knime.com/user_avatar/forum.knime.com/mwiegand/32/66896_2.png) [@mwiegand](https://forum.knime.com/u/mwiegand)\
**Post date:** [January 8, 2023, 11:15am UTC](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/6 "2023-01-08T11:15:06Z")

</div>

Unfortunately both nodes do not write a human readable format nor could they be leveraged to extract the meta information from each column type.

 ![Bildschirm­foto 2023-01-08 um 12.01.26](https://forum-cdn.knime.com/uploads/default/original/3X/0/a/0a6f407ebfbb90b6adc3de0888618ca16c7117fe.jpeg)

The ARFF Writer does (yet) not support paths (to point to storage location, column type untested) and the column type Collection. However, it looks more promising as it’s kind of human readable

 ![Bildschirm­foto 2023-01-08 um 12.09.06](https://forum-cdn.knime.com/uploads/default/original/3X/5/4/544a30ce3e4286cffc2333df0ea220de3fa5dc7d.jpeg)

Unfortunately, the writer node seems to miss proper quote treatment causing the ARFF Reader to break:  
`ERROR ARFF Reader 5:1726 Configure failed (TokenizerException): New line in quoted string (or closing quote missing). In line 9.`

 ![Bildschirm­foto 2023-01-08 um 12.14.42](https://forum-cdn.knime.com/uploads/default/original/3X/c/c/ccc89bde162e6178c4c63052f4751c58aa8a118c.jpeg)

Raised this ticket

> [@ARFF Writer lacks Quote Treatment causing ARFF Reader to break](https://forum.knime.com/t/arff-writer-lacks-quote-treatment-causing-arff-reader-to-break/58768):
>
> Hi, while working on this [https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/6?u=mwiegand](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/6) I seem to have recognized an issue, presumably about quote treatment, with [https://hub.knime.com/knime/extensions/org.knime.features.base/latest/org.knime.base.node.io.arffwriter.ARFFWriterNodeFactory](https://hub.knime.com/knime/extensions/org.knime.features.base/latest/org.knime.base.node.io.arffwriter.ARFFWriterNodeFactory) ERROR ARFF Reader 5:1726 Configure failed (TokenizerException): New line in quoted string (or closing quote missing). In line 9. [Bildschirm­foto 2023-01-0…

Best  
Mike

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [April 8, 2023, 11:15am UTC](https://forum.knime.com/t/column-expression-dynamically-extract-and-define-type/58681/7 "2023-04-08T11:15:07Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
