Redshift UNION ALL handling of re-defined data types

0

I'm using the following ctas syntax to create a temp table:

create temp table test as
select 'Value1'::varchar(100) as field1
union all 
select 'Value2'::varchar(100) as field1

The expected result is that field1 will have the data type varchar(100) but it instead has varchar(6). Whereas running the following does result in field1 being varchar(100).

create temp table test as
select 'Value1'::varchar(100) as field1

Is this expected behaviour, and if so, is there some syntax I can use to have field1 varchar(100) using a union all statement?

Zan
질문됨 일 년 전383회 조회
2개 답변
1
수락된 답변

For UNION ALL and UNION, the final field size is determined by the maximum length of the actual string values present. In the above case it is 6 because both ‘Value1’ and ‘Value2’ have 6 characters.

If you change the example to:

create temp table test3 as
select 'Value12345’::varchar(100) as field1
union  
select 'Value2'::varchar(100) as field1;

, then field1 column will be defined as VARCHAR(10).

To get the desired outcome of varchar(100) for field1, you can do the following:

create temp table test5 as
	with cte as(
		select 'Value1'::varchar(100) as field1
			union all 
		select 'Value2'::varchar(100) as field1
	) 
select field1::varchar(100) from cte;
AWS
지원 엔지니어
Regis_M
답변함 일 년 전
profile picture
전문가
검토됨 일 년 전
1

Redshift will decide column length based on actual data. If you have a need to define column length longer than actual data then you can follow above answer, OR another option is to manually create the table first and use the no backup option. This being a permanent table has one advantage - system catalog tables do not need to be updated every time like in creating temp table. This could lead to performance issues on a busy system with lots of tables being created and dropped as the system catalog tables can bloat, and these need a system restart to be cleaned up.

profile pictureAWS
답변함 일 년 전

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

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

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

관련 콘텐츠