Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use enum values in JSON without explicit casts #3439

Closed
lukaseder opened this issue Feb 16, 2022 · 2 comments · Fixed by #3440
Closed

Cannot use enum values in JSON without explicit casts #3439

lukaseder opened this issue Feb 16, 2022 · 2 comments · Fixed by #3440

Comments

@lukaseder
Copy link
Contributor

Try this:

create domain e as enum ('a', 'b');
select cast ('a' as e);
select json_array(cast('a' as e));

I would expect the query to produce ["a"], but it fails with:

SQL Error [22018] [22018]: Data conversion error converting "ENUM to JSON"

As a workaround, I can cast the enum value back to VARCHAR explicitly, but I think that should work out of the box?

select json_array(cast(cast('a' as e) as varchar));

Is there any other reasonable auto conversion from an enum value to a json value that I might be overlooking?

@katzyn
Copy link
Contributor

katzyn commented Feb 16, 2022

Is there any other reasonable auto conversion from an enum value to a json value that I might be overlooking?

There are at least two other possible conversions to 1-based JSON number or 0-based JSON number, but they aren't reasonable from my point of view; people who need them can use a cast and arithmetic operation.

JSON string looks like a reasonable default.

@lukaseder
Copy link
Contributor Author

There are at least two other possible conversions to 1-based JSON number or 0-based JSON number

I thought about it too. The 0-based JSON number wouldn't work, I think?

select cast (cast ('a' as e) as int);

Produces 1, not 0, so it would have to be consistent with the CAST behaviour.

but they aren't reasonable from my point of view; people who need them can use a cast and arithmetic operation.

Indeed, most people use the enum's string value, e.g. also when inserting into a table containing an enum type:

create table t (e e);
insert into t values ('a');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants