Unnesting and reforming a big query array seems to destroy null











up vote
1
down vote

favorite












SELECT ARRAY(SELECT foo FROM UNNEST(CAST(NULL as ARRAY<STRING>)) as foo) is null 


returns false



vs.



SELECT CAST(NULL as ARRAY<STRING>) is null 


returns true



My specific situation is that I'd like to sort an array using



SELECT ARRAY(SELECT foo from UNNEST(bar) as foo Order by foo) as arr 


and keep the array as null if it was originally null. I'm open to alternatives for sorting the array but preserving null (meaning I'm not hung up on my implementation).










share|improve this question


























    up vote
    1
    down vote

    favorite












    SELECT ARRAY(SELECT foo FROM UNNEST(CAST(NULL as ARRAY<STRING>)) as foo) is null 


    returns false



    vs.



    SELECT CAST(NULL as ARRAY<STRING>) is null 


    returns true



    My specific situation is that I'd like to sort an array using



    SELECT ARRAY(SELECT foo from UNNEST(bar) as foo Order by foo) as arr 


    and keep the array as null if it was originally null. I'm open to alternatives for sorting the array but preserving null (meaning I'm not hung up on my implementation).










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      SELECT ARRAY(SELECT foo FROM UNNEST(CAST(NULL as ARRAY<STRING>)) as foo) is null 


      returns false



      vs.



      SELECT CAST(NULL as ARRAY<STRING>) is null 


      returns true



      My specific situation is that I'd like to sort an array using



      SELECT ARRAY(SELECT foo from UNNEST(bar) as foo Order by foo) as arr 


      and keep the array as null if it was originally null. I'm open to alternatives for sorting the array but preserving null (meaning I'm not hung up on my implementation).










      share|improve this question













      SELECT ARRAY(SELECT foo FROM UNNEST(CAST(NULL as ARRAY<STRING>)) as foo) is null 


      returns false



      vs.



      SELECT CAST(NULL as ARRAY<STRING>) is null 


      returns true



      My specific situation is that I'd like to sort an array using



      SELECT ARRAY(SELECT foo from UNNEST(bar) as foo Order by foo) as arr 


      and keep the array as null if it was originally null. I'm open to alternatives for sorting the array but preserving null (meaning I'm not hung up on my implementation).







      sql google-bigquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 20:32









      Andrew Cassidy

      1,377724




      1,377724
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          An empty array is different from a NULL with an array type. An empty array is an array with no elements. That is what you get from the UNNEST(). So, the comparison to NULL is false.



          The second is returning NULL typed as an array. This is equivalent to NULL, so the comparison is true.






          share|improve this answer





















          • Got it. I'm used to a world where any operation on a NULL would result in a NULL....
            – Andrew Cassidy
            Nov 7 at 21:16










          • This is still true. But an array with a null element is not itself null.
            – Elliott Brossard
            Nov 7 at 22:30










          • @ElliottBrossard . . . Arrays in BQ do not have NULL elements. An empty array is not NULL.
            – Gordon Linoff
            Nov 7 at 22:32










          • Yes, that's right. I was trying to explain to the OP that operations can involve nulls (e.g. ARRAY_LENGTH applied to an array with a null element) but not result in null.
            – Elliott Brossard
            Nov 7 at 23:03











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197380%2funnesting-and-reforming-a-big-query-array-seems-to-destroy-null%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          An empty array is different from a NULL with an array type. An empty array is an array with no elements. That is what you get from the UNNEST(). So, the comparison to NULL is false.



          The second is returning NULL typed as an array. This is equivalent to NULL, so the comparison is true.






          share|improve this answer





















          • Got it. I'm used to a world where any operation on a NULL would result in a NULL....
            – Andrew Cassidy
            Nov 7 at 21:16










          • This is still true. But an array with a null element is not itself null.
            – Elliott Brossard
            Nov 7 at 22:30










          • @ElliottBrossard . . . Arrays in BQ do not have NULL elements. An empty array is not NULL.
            – Gordon Linoff
            Nov 7 at 22:32










          • Yes, that's right. I was trying to explain to the OP that operations can involve nulls (e.g. ARRAY_LENGTH applied to an array with a null element) but not result in null.
            – Elliott Brossard
            Nov 7 at 23:03















          up vote
          2
          down vote



          accepted










          An empty array is different from a NULL with an array type. An empty array is an array with no elements. That is what you get from the UNNEST(). So, the comparison to NULL is false.



          The second is returning NULL typed as an array. This is equivalent to NULL, so the comparison is true.






          share|improve this answer





















          • Got it. I'm used to a world where any operation on a NULL would result in a NULL....
            – Andrew Cassidy
            Nov 7 at 21:16










          • This is still true. But an array with a null element is not itself null.
            – Elliott Brossard
            Nov 7 at 22:30










          • @ElliottBrossard . . . Arrays in BQ do not have NULL elements. An empty array is not NULL.
            – Gordon Linoff
            Nov 7 at 22:32










          • Yes, that's right. I was trying to explain to the OP that operations can involve nulls (e.g. ARRAY_LENGTH applied to an array with a null element) but not result in null.
            – Elliott Brossard
            Nov 7 at 23:03













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          An empty array is different from a NULL with an array type. An empty array is an array with no elements. That is what you get from the UNNEST(). So, the comparison to NULL is false.



          The second is returning NULL typed as an array. This is equivalent to NULL, so the comparison is true.






          share|improve this answer












          An empty array is different from a NULL with an array type. An empty array is an array with no elements. That is what you get from the UNNEST(). So, the comparison to NULL is false.



          The second is returning NULL typed as an array. This is equivalent to NULL, so the comparison is true.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 21:05









          Gordon Linoff

          746k33285390




          746k33285390












          • Got it. I'm used to a world where any operation on a NULL would result in a NULL....
            – Andrew Cassidy
            Nov 7 at 21:16










          • This is still true. But an array with a null element is not itself null.
            – Elliott Brossard
            Nov 7 at 22:30










          • @ElliottBrossard . . . Arrays in BQ do not have NULL elements. An empty array is not NULL.
            – Gordon Linoff
            Nov 7 at 22:32










          • Yes, that's right. I was trying to explain to the OP that operations can involve nulls (e.g. ARRAY_LENGTH applied to an array with a null element) but not result in null.
            – Elliott Brossard
            Nov 7 at 23:03


















          • Got it. I'm used to a world where any operation on a NULL would result in a NULL....
            – Andrew Cassidy
            Nov 7 at 21:16










          • This is still true. But an array with a null element is not itself null.
            – Elliott Brossard
            Nov 7 at 22:30










          • @ElliottBrossard . . . Arrays in BQ do not have NULL elements. An empty array is not NULL.
            – Gordon Linoff
            Nov 7 at 22:32










          • Yes, that's right. I was trying to explain to the OP that operations can involve nulls (e.g. ARRAY_LENGTH applied to an array with a null element) but not result in null.
            – Elliott Brossard
            Nov 7 at 23:03
















          Got it. I'm used to a world where any operation on a NULL would result in a NULL....
          – Andrew Cassidy
          Nov 7 at 21:16




          Got it. I'm used to a world where any operation on a NULL would result in a NULL....
          – Andrew Cassidy
          Nov 7 at 21:16












          This is still true. But an array with a null element is not itself null.
          – Elliott Brossard
          Nov 7 at 22:30




          This is still true. But an array with a null element is not itself null.
          – Elliott Brossard
          Nov 7 at 22:30












          @ElliottBrossard . . . Arrays in BQ do not have NULL elements. An empty array is not NULL.
          – Gordon Linoff
          Nov 7 at 22:32




          @ElliottBrossard . . . Arrays in BQ do not have NULL elements. An empty array is not NULL.
          – Gordon Linoff
          Nov 7 at 22:32












          Yes, that's right. I was trying to explain to the OP that operations can involve nulls (e.g. ARRAY_LENGTH applied to an array with a null element) but not result in null.
          – Elliott Brossard
          Nov 7 at 23:03




          Yes, that's right. I was trying to explain to the OP that operations can involve nulls (e.g. ARRAY_LENGTH applied to an array with a null element) but not result in null.
          – Elliott Brossard
          Nov 7 at 23:03


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197380%2funnesting-and-reforming-a-big-query-array-seems-to-destroy-null%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings