Upgrading an int column to enum type in postgresql - postgresql

Following this question I was wondering if there as in elegant way to do the same for int values.
More precisely given an Integer column with a fixed number of values (not necessarily contiguous), how would I go about mapping each number to each enum value. And when I say map I mean migrate
For example:
Lets assume the enum is as
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
We would like to map:
0 -> 'sad'
1 -> 'ok'
10 -> 'happy'
Where, let's say, we have table which contains a column called mood with values {0,1,10} only.
Also, I can't see that the answer here helps me.
I use Postgres 9.5

The USING clause is an expression to tell postgresql how to transform the value. This is an expression just as you might use in SELECT. So if you need to specify a mapping then you can just use a CASE statement.
alter table foo
alter bar type mood
using
case bar
when 0 then 'sad'
when 1 then 'ok'
when 10 then 'happy'
end :: mood;

Related

Set randomly chosen string as default value in sql column (Postgress)

I have this column on my user table hat_color in my PostgreSQL database.
I would like to set a randomly chosen color value as the default value if no value is provided when creating the column.
So the default value should be one of ['red', 'blue', 'green']
I was thinking something like
['red','blue','green'][floor(random() * (h-l+1) + l)::int]::text
but that does does not work. Is this possible to do and if so how?
The proper syntax:
(array['red','blue','green'])[floor(random()* 3+ 1)::int]
-- or
('{red,blue,green}'::text[])[floor(random()* 3+ 1)::int]
Test it in db<>fiddle.

Laravel8 migration change ENUM to BOOLEAN

I have in an old project a gender column using an ENUM type (0 and 1 values)... yes I know not my smartest idea.
I have to make a change to this table so I want to simply use a boolean type instead of an enum (since I only need 0 and 1 values). But when I try to change my colum this way :
Schema::table('users', function (Blueprint $table) {
$table->boolean('gender')->nullable()->change();
});
The migration works but it changes the values (0 are converted to 1 and 1 are converted to 2)...
I suppose there is some force casting underway but I don't get why it is converted this way.
Any idea how I can convert my enum type to a tinyint(1) without changing my values? I guess I could add a second column with the right type, copy my values into it, drop my first column and rename my newly created column, but that seems complex for something that I thought would be simple.
Thank you for your help.
Edit: After reading some more posts about that I guess that it's using the value index instead of casting the value, that's why my 0 is converted to 1, etc..
I guess I'll have to use a temp column to do it.

Grafana Postgres ERROR when using Variables with ALL in WHERE clause multi value case when did not work

In Grafana Postgres Datasource has been integrated. For the report Generation, I have added Variables, for example, type. I have used case when clause in this variable filter.
case when type 0 then "ZERO" when type 1 then "ONE", when type 2 then "TWO"
Variable filter works fine in the reporting. In the variable, there is an option to choose All and multi-value.
Again in the Postgres query where clause, I need to convert this to integer,
case when type "ZERO" then 0 when type "ONE" then 1, when type "TWO" then 2.
When I select single value in the filter, ONE or TWO it works fine.
When I select All in the filter, it appends ZERO, ONE, TWO in the value.
The query became
case when type "ZERO, ONE, TWO" then 0
Is there any possible way or remove the option to choose multi value and all from the Grafana variable?
Thank you in advance.

How to store different types in a column?

I am a novice programmer currently working on how to store PostgreSQL column types in a table column. Specifically, I would like to create a table with a column named 'type' that stores one of the following types: boolean, integer, text and enum. Conceptually, however, I would like a user to store which values (s)he would like to use. An integer, a piece of text, or a list of options should all be possible. See the sample table below.
id | type | default value
---------------------------------------------------------------
1 | integer | 5
2 | boolean |
3 | enum("red", "blue", "yellow") | red
The last case confuses me. With my limited knowledge I know that creating a table with a column named type with type enum("boolean", "integer", "text", "enum") is possible, but I don't see how that allows for the last case in the sample table. It seems like this only allows for a type enum, but in that case I cannot specify a default value, such as red, because red is not listed in the options of the enum type.
I am left with two questions:
On creating the table, what type do I give the column named type?
How do I store the third record in the sample table, such that I can also store a default value and such that I can still check whether the default value is actually appropriate for the chosen type?
Any answer or help is greatly appreciated. Thanks!
I think you are confusing/mixing a lot of different things.
Regarding enum type
You can declare a column of a table to be of enumerated type. For this you need to define the type:
CREATE TYPE my_color_enum AS ENUM ( 'red', 'blue', 'yellow' );
Then use it in table just like any other type:
CREATE TABLE test ( column1 my_color_enum );
If you need to assign a default value to column1 it only lets you define a default value that exists in your ENUM type, so for example while this is valid:
CREATE TABLE test ( column1 my_color_enum default 'red');
This is not and will yield an error:
CREATE TABLE test ( column1 my_color_enum default 'green' );
Because "green" value is not present in your type. The error message will look something like this (this is a loose translation, not exact error message):
Invalid input value for enum my_color_enum: "green"
Storing different datatypes in one column
This seems like a bad design, but if you really need it, and would rather avoid text datatype and then casting back and forth, you could use json, jsonb and so forth...
It will come to bite you if you decide to have a lookup table which stores data types.

Functions are not appearing while using TFilterRow in Talend

I am using a tFilterRow to avoid empty rows. While trying to use it I am getting only one function value 'absolute value'.
I want to filter values with a length greater than 0.
Why I am not getting any other functions?
As mentioned in the comments, the length function is only available to schema columns that have the String data type.
To filter out any rows that have a null value in a column you can use a tFilterRow but configured so that the column being checked is not equal to null like so:
In the case you are dealing with the primitive int (rather than the Integer class) then the primitive can never be null and instead defaults to 0 so you'll want to set it as not equal to 0 instead.