Redshift regression: failed to find conversion function from "unknown" to numeric

0

On Redshift 1.0.60353, I'm getting the following error for a query that previously worked:

SQL Error [XX000]: ERROR: failed to find conversion function from "unknown" to numeric

According to this list, XX000 is an internal error.

Using DBeaver 23.2.5, I am able to reproduce the error with the following query:

with
    a as (select cast(0 as decimal(15, 4)) as test),
    b as (select null as test),
    c as (
        select * from a
        union all
        select * from b
    )
select * from c
Curtis
질문됨 6달 전1349회 조회
1개 답변
1

The error you're encountering in Redshift, ERROR: failed to find conversion function from "unknown" to numeric, suggests that the query engine is having difficulty inferring the correct data type for the null value in your UNION query.

In your query, you have two Common Table Expressions (CTEs), a and b. a returns a column test of type decimal(15, 4), while b returns a column test with null values but doesn't explicitly define the data type for null. When you try to UNION these two CTEs in c, Redshift struggles to reconcile the unknown data type of the null in b with the decimal(15, 4) type in a.

To resolve this, you should explicitly cast the null in CTE b to the same data type as the corresponding column in CTE a.

with
    a as (select cast(0 as decimal(15, 4)) as test),
    b as (select cast(null as decimal(15, 4)) as test),
    c as (
        select * from a
        union all
        select * from b
    )
select * from c;

Always ensure that your queries explicitly define data types, especially when dealing with null values and set operations like UNION

답변함 6달 전
  • Thanks for the quick response, Jesus. The implicit cast works for union all when no CTEs are used. Seems like this is a regression because the implicit cast used to work for union all inside a CTE as well.

    select cast(0 as decimal(15, 4)) as test
    union all
    select null as test
    

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠