Create a clustered table in BigQuery from existing table with _PARTITIONTIME
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
add a comment |
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
add a comment |
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
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
google-bigquery ddl
asked Nov 15 '18 at 9:15
Yosua MichaelYosua Michael
223
223
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 of1990-01-01
indb.new_table
.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 of1990-01-01
indb.new_table
.
add a comment |
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 of1990-01-01
indb.new_table
.
add a comment |
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 of1990-01-01
indb.new_table
.
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 of1990-01-01
indb.new_table
.
answered Nov 19 '18 at 19:00
khankhan
1,97683052
1,97683052
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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