Column Rename node: how to pass old_column_name via flow variable

Hi everybody,
I’m using Knime 4.7.4. I need to rename a column with a new name using the node Column Rename. I want to choose the “old_column_name” via a workflow variable created with a String Configuration node.
Why I’m doing this: I have a table that is part of a workflows and I am interested in one of its columns, whose name may vary. So, I want to parametrize “a priori” what’s the name of the column I will target (the old_column_name) and I want the “name-to-be” stored in the node, or at most in another parameter.
I just want something that works as "if you find a column whose name is the one given by the flow variable, then change it to “final_name”…
I’m trying every possibile thing and I can’t seem to achieve this simple thing.
Problems are:

  1. the Column Rename node requires me to manually select at least ONE column to rename, before allowing me to use the flow variables
  2. I don’t know “a priori” how many variables are going to be in the table, and in which order
  3. I can do the opposite, i.e.: I can pass the new_name via flow variables in order to replace a manually-selected old_name in the UI, but I actually need to avoid using the UI.

Thanks a lot

Hi @alessioorlandi

What might help you in this case is to first evaluate how the flow variables behave. Assume this example:

I want to rename Universe_0_0 to Universe_final. I first apply the rename via UI. In the flow variables tab, fill in placeholder names in the white box after each variable. This will output the current config as set via the UI as flow variables.

image

Variables

image

Now regarding your issue, something like this should work.

  1. the Column Rename node requires me to manually select at least ONE column to rename, before allowing me to use the flow variables

This does not matter, the node is executed regardless. If you have a String Configuration for the old name, create a variable for the new name (which seems to be hardcoded) and merge them. Select a random column and override old_column_name and new_column_name with the flow variables.

image

Put this all in a component. If I type Universe_2_0 (not present in the table), nothing changes to in the table

If I type Universe_1_0 (present in the table), only that column gets changed.

  1. I don’t know “a priori” how many variables are going to be in the table, and in which order

As per the above, this also does not matter for the rename node. If you would like to have more renames to be performed, add the desired amount of string configs and final names in the variable expression and increase the number of renames in the node.

Flow variables

Same principle applies: if one or more of the columns that you filled it in the component are not found, it is disregarded and the table is outputted “as is”.

See WF:
Column Rename node how to pass old_column_name via flow variable.knwf (33.2 KB)

Hope this helps!

3 Likes

Thanks for the detailed explaination. It was quite enlightening and let me work out a slightly different solution.
Just one more clarification: the “variable expressions” node has been put there in order to reduce the needed number of nodes? I mean… One could use String Configuration to set both the old_name and the new_name. If I have 15 columns to be renamed, then this would require 30 nodes of string config.
By what I understand, one can use Variable Expressions to synthetize the 15 new_names with just one node, so that in the end I only have 15 String Configs and 1 Variable Expressions instead of 30 String Configs. Am I right?

Thanks a lot again

Indeed, but with the pre-condition that the new_names will be fixed. If you want to change it often then it’s either more String Configs or updating the Variable Expression.

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