NULL emements lost when casting result of unnest()

后端 未结 1 957
不思量自难忘°
不思量自难忘° 2021-01-18 03:43

I stumbled upon very odd behavior with unnest(), when casting after expanding an array.

Introduction

There are three basic syntax variants to use unnest():

相关标签:
1条回答
  • 2021-01-18 04:18

    Casting SRF function (in FROM clause) is not supported - you cannot use any operator there. Only function call is allowed.

    a cast is possible only in column list:

    postgres=# SELECT * FROM unnest('{2,NULL,1}'::int[])::text;
    ERROR:  syntax error at or near "::"
    LINE 1: SELECT * FROM unnest('{2,NULL,1}'::int[])::text;
                                                     ^
    postgres=# SELECT v::text FROM unnest('{2,NULL,1}'::int[]) g(v);
       v    
    ────────
          2
     [null]
          1
    (3 rows)
    

    Missing row from NULL is probably bug and should be reported

    postgres=# SELECT unnest('{1,NULL,4}'::int[])::text;
     unnest 
    ────────
          1
     [null]
          4
    (3 rows)
    
    postgres=# SELECT unnest('{1,NULL,4}'::int[])::numeric;
     unnest 
    ────────
          1
          4
    (2 rows)
    

    There is not reason, why NULL rows should be dropped, I think

    0 讨论(0)
提交回复
热议问题