Skip to content

Commit 617a696

Browse files
author
Matti Remes
committed
Add exists check for dpt table
1 parent 109d47b commit 617a696

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

pandas_gbq/gbq.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ def delete_and_recreate_table(self, dataset_id, table_id, table_schema):
699699
table = _Table(self.project_id, dataset_id,
700700
private_key=self.private_key)
701701
table.delete(table_id)
702-
if _Table.partition_decorator not in table_id:
702+
if not _Table.is_date_partitioned(table_id):
703703
table.create(table_id, table_schema)
704704
sleep(delay)
705705

@@ -1010,6 +1010,9 @@ class _Table(GbqConnector):
10101010

10111011
partition_decorator = '$'
10121012

1013+
def is_date_partitioned(self, table_id):
1014+
return self.partition_decorator in table_id
1015+
10131016
def __init__(self, project_id, dataset_id, reauth=False, verbose=False,
10141017
private_key=None):
10151018
self.dataset_id = dataset_id
@@ -1031,8 +1034,12 @@ def exists(self, table_id):
10311034
from google.api_core.exceptions import NotFound
10321035

10331036
table_ref = self.client.dataset(self.dataset_id).table(table_id)
1037+
10341038
try:
1035-
self.client.get_table(table_ref)
1039+
table = self.client.get_table(table_ref)
1040+
if self.is_date_partitioned(table_id):
1041+
return table.num_rows > 0
1042+
10361043
return True
10371044
except NotFound:
10381045
return False
@@ -1067,7 +1074,7 @@ def create(self, table_id, schema, date_partitioned=False):
10671074
table_ref = self.client.dataset(self.dataset_id).table(table_id)
10681075
table = Table(table_ref)
10691076

1070-
if date_partitioned or '$' in table_id:
1077+
if date_partitioned or self.is_date_partitioned(table_id):
10711078
table.partitioning_type = 'DAY'
10721079

10731080
# Manually create the schema objects, adding NULLABLE mode

0 commit comments

Comments
 (0)