0

Does postgres support the following?

SELECT ARRAY<INT64>[1,2];

Instead of just:

SELECT ARRAY[1,2]

enter image description here


As an example from BigQuery of what I mean:

enter image description here

Or is the only way to cast it after declaring a literal, such as:

select ARRAY[1,2]::int[];
4
  • 1
    What happend when you tried? dbfiddle.uk/Ho4tsJ0s Commented Sep 6, 2022 at 19:15
  • @FrankHeikens added to question. Commented Sep 6, 2022 at 19:17
  • Well, that's pretty clear, it's not valid syntax for PostgreSQL Commented Sep 6, 2022 at 19:18
  • @FrankHeikens sure, my question is if there is any way to specify the array type in Postgres? Is the answer that it's not and only via a cast? Commented Sep 6, 2022 at 19:20

1 Answer 1

1

specify the array type in Postgres

Manual 8.15.1. Declaration of Array Types
when you create an table, you can declare the array type. The following is manual example.

CREATE TABLE sal_emp (
    name            text,
    pay_by_quarter  integer[],
    schedule        text[][]
);

When you do a select then you need cast. Because it will be resolved to a certain type. Then you can cast to the type you want.

SELECT ARRAY[1,2,3], pg_typeof(ARRAY[1,2,3])
union
SELECT ARRAY[1,2,3]::smallint[], pg_typeof(ARRAY[1,2,3])
union
SELECT ARRAY[1,2,3]::bigint[], pg_typeof(ARRAY[1,2,3])
union all
SELECT ARRAY[1,2,3]::[numeric], pg_typeof(ARRAY[1,2,3]);

Quote from manual:

By default, the array element type is the common type of the member expressions, determined using the same rules as for UNION or CASE constructs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.