Create a clustered table in BigQuery from existing table with _PARTITIONTIME












0















I am trying to make a new clustered table, db.new_table, that have the same data and schema as an existing table, db.old_table, in BigQuery. The existing table have a pseudo column _PARTITIONTIME, and I would like the new table to have this _PARTITIONTIME pseudo column as well.



I have tried using DDL, with query like:



CREATE TABLE `db.new_table`
PARTITION BY DATE(_PARTITIONTIME)
CLUSTER BY field1, field2
AS SELECT * FROM `db.old_table`
WHERE _PARTITIONTIME > '1990-01-01'


However it failed because we cannot use PARTITION BY DATE(_PARTITIONTIME) followed by AS SELECT .... as stated in https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language



Is there a methods to do this? (create a new clustered table with the same exact schema and data from an old table partitioned by pseudo column _PARTITIONTIME )



Any answer and comment is appreciated, thanks.





Notes:
I can create a similar table without _PARTITIONTIME with query like:



CREATE TABLE `db.new_table`
PARTITION BY partition_date
CLUSTER BY field1, field2
AS SELECT DATE(_PARTITIONTIME) AS partition_date, * FROM `db.old_table`
WHERE _PARTITIONTIME > '1990-01-01'


However since a lot of things in the system depend on db.old_table, the change in partition field from _PARTITIONTIME to partition_date would cause a lot of query changes... Therefore it would be much preferable if we can create the clustered table with exactly same schema and data.










share|improve this question



























    0















    I am trying to make a new clustered table, db.new_table, that have the same data and schema as an existing table, db.old_table, in BigQuery. The existing table have a pseudo column _PARTITIONTIME, and I would like the new table to have this _PARTITIONTIME pseudo column as well.



    I have tried using DDL, with query like:



    CREATE TABLE `db.new_table`
    PARTITION BY DATE(_PARTITIONTIME)
    CLUSTER BY field1, field2
    AS SELECT * FROM `db.old_table`
    WHERE _PARTITIONTIME > '1990-01-01'


    However it failed because we cannot use PARTITION BY DATE(_PARTITIONTIME) followed by AS SELECT .... as stated in https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language



    Is there a methods to do this? (create a new clustered table with the same exact schema and data from an old table partitioned by pseudo column _PARTITIONTIME )



    Any answer and comment is appreciated, thanks.





    Notes:
    I can create a similar table without _PARTITIONTIME with query like:



    CREATE TABLE `db.new_table`
    PARTITION BY partition_date
    CLUSTER BY field1, field2
    AS SELECT DATE(_PARTITIONTIME) AS partition_date, * FROM `db.old_table`
    WHERE _PARTITIONTIME > '1990-01-01'


    However since a lot of things in the system depend on db.old_table, the change in partition field from _PARTITIONTIME to partition_date would cause a lot of query changes... Therefore it would be much preferable if we can create the clustered table with exactly same schema and data.










    share|improve this question

























      0












      0








      0








      I am trying to make a new clustered table, db.new_table, that have the same data and schema as an existing table, db.old_table, in BigQuery. The existing table have a pseudo column _PARTITIONTIME, and I would like the new table to have this _PARTITIONTIME pseudo column as well.



      I have tried using DDL, with query like:



      CREATE TABLE `db.new_table`
      PARTITION BY DATE(_PARTITIONTIME)
      CLUSTER BY field1, field2
      AS SELECT * FROM `db.old_table`
      WHERE _PARTITIONTIME > '1990-01-01'


      However it failed because we cannot use PARTITION BY DATE(_PARTITIONTIME) followed by AS SELECT .... as stated in https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language



      Is there a methods to do this? (create a new clustered table with the same exact schema and data from an old table partitioned by pseudo column _PARTITIONTIME )



      Any answer and comment is appreciated, thanks.





      Notes:
      I can create a similar table without _PARTITIONTIME with query like:



      CREATE TABLE `db.new_table`
      PARTITION BY partition_date
      CLUSTER BY field1, field2
      AS SELECT DATE(_PARTITIONTIME) AS partition_date, * FROM `db.old_table`
      WHERE _PARTITIONTIME > '1990-01-01'


      However since a lot of things in the system depend on db.old_table, the change in partition field from _PARTITIONTIME to partition_date would cause a lot of query changes... Therefore it would be much preferable if we can create the clustered table with exactly same schema and data.










      share|improve this question














      I am trying to make a new clustered table, db.new_table, that have the same data and schema as an existing table, db.old_table, in BigQuery. The existing table have a pseudo column _PARTITIONTIME, and I would like the new table to have this _PARTITIONTIME pseudo column as well.



      I have tried using DDL, with query like:



      CREATE TABLE `db.new_table`
      PARTITION BY DATE(_PARTITIONTIME)
      CLUSTER BY field1, field2
      AS SELECT * FROM `db.old_table`
      WHERE _PARTITIONTIME > '1990-01-01'


      However it failed because we cannot use PARTITION BY DATE(_PARTITIONTIME) followed by AS SELECT .... as stated in https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language



      Is there a methods to do this? (create a new clustered table with the same exact schema and data from an old table partitioned by pseudo column _PARTITIONTIME )



      Any answer and comment is appreciated, thanks.





      Notes:
      I can create a similar table without _PARTITIONTIME with query like:



      CREATE TABLE `db.new_table`
      PARTITION BY partition_date
      CLUSTER BY field1, field2
      AS SELECT DATE(_PARTITIONTIME) AS partition_date, * FROM `db.old_table`
      WHERE _PARTITIONTIME > '1990-01-01'


      However since a lot of things in the system depend on db.old_table, the change in partition field from _PARTITIONTIME to partition_date would cause a lot of query changes... Therefore it would be much preferable if we can create the clustered table with exactly same schema and data.







      google-bigquery ddl






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 9:15









      Yosua MichaelYosua Michael

      223




      223
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can just pre-create your day-partitioned, clustered table (on whatever fields) named db.new_table using either BQ UI or bq command.



          Once the table is there, you can populate "for each day" as:



          bq query --allow_large_results --append_table --noflatten_results --destination_table 'db.new_table$19900101' "select field1, field2, field3 from db.old_table where _PARTITIONTIME = '1990-01-01'";


          Notice two things:




          • You have to run this query for each day separately (which will cost you pretty much the same so don't worry about it).

          • The db.new_table$19900101 points to the partition of 1990-01-01 in db.new_table.






          share|improve this answer























            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',
            autoActivateHeartbeat: false,
            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%2f53315974%2fcreate-a-clustered-table-in-bigquery-from-existing-table-with-partitiontime%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









            0














            You can just pre-create your day-partitioned, clustered table (on whatever fields) named db.new_table using either BQ UI or bq command.



            Once the table is there, you can populate "for each day" as:



            bq query --allow_large_results --append_table --noflatten_results --destination_table 'db.new_table$19900101' "select field1, field2, field3 from db.old_table where _PARTITIONTIME = '1990-01-01'";


            Notice two things:




            • You have to run this query for each day separately (which will cost you pretty much the same so don't worry about it).

            • The db.new_table$19900101 points to the partition of 1990-01-01 in db.new_table.






            share|improve this answer




























              0














              You can just pre-create your day-partitioned, clustered table (on whatever fields) named db.new_table using either BQ UI or bq command.



              Once the table is there, you can populate "for each day" as:



              bq query --allow_large_results --append_table --noflatten_results --destination_table 'db.new_table$19900101' "select field1, field2, field3 from db.old_table where _PARTITIONTIME = '1990-01-01'";


              Notice two things:




              • You have to run this query for each day separately (which will cost you pretty much the same so don't worry about it).

              • The db.new_table$19900101 points to the partition of 1990-01-01 in db.new_table.






              share|improve this answer


























                0












                0








                0







                You can just pre-create your day-partitioned, clustered table (on whatever fields) named db.new_table using either BQ UI or bq command.



                Once the table is there, you can populate "for each day" as:



                bq query --allow_large_results --append_table --noflatten_results --destination_table 'db.new_table$19900101' "select field1, field2, field3 from db.old_table where _PARTITIONTIME = '1990-01-01'";


                Notice two things:




                • You have to run this query for each day separately (which will cost you pretty much the same so don't worry about it).

                • The db.new_table$19900101 points to the partition of 1990-01-01 in db.new_table.






                share|improve this answer













                You can just pre-create your day-partitioned, clustered table (on whatever fields) named db.new_table using either BQ UI or bq command.



                Once the table is there, you can populate "for each day" as:



                bq query --allow_large_results --append_table --noflatten_results --destination_table 'db.new_table$19900101' "select field1, field2, field3 from db.old_table where _PARTITIONTIME = '1990-01-01'";


                Notice two things:




                • You have to run this query for each day separately (which will cost you pretty much the same so don't worry about it).

                • The db.new_table$19900101 points to the partition of 1990-01-01 in db.new_table.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 '18 at 19:00









                khankhan

                1,97683052




                1,97683052
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315974%2fcreate-a-clustered-table-in-bigquery-from-existing-table-with-partitiontime%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







                    Popular posts from this blog

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)