Skip to content

Commit

Permalink
Merge pull request #6 from gabfl/multi-partition-select
Browse files Browse the repository at this point in the history
Multi partition select
  • Loading branch information
gabfl authored Nov 14, 2017
2 parents 2817318 + b21dfd1 commit fa9026f
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions src/fdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class ConstantForeignDataWrapper(ForeignDataWrapper):
clients = {} # Dictionnary of clients
bq = None # BqClient instance
partitionPseudoColumn = 'partition_date' # Name of the partition pseudo column
partitionPseudoColumnValue = None # If a partition is used, its value will be stored in this variable to return it to PostgreSQL
countPseudoColumn = '_fdw_count' # Pseudo column to fetch `count(*)` when using the remote counting and grouping feature
castingRules = None # Dict of casting rules when using the `fdw_casting` option

Expand Down Expand Up @@ -229,16 +228,10 @@ def execute(self, quals, columns):
# Example: `OrderedDict([('column1', 'value1'), ('column2', value2)])`
line = OrderedDict()
for column in columns:
if column != self.partitionPseudoColumn: # Except for the partition pseudo column
line[column] = row[column]
else: # Fallback for partition pseudo column
line[column] = self.partitionPseudoColumnValue
line[column] = row[column]

yield line

# Reset partition pseudo column value
self.partitionPseudoColumnValue = None

def buildQuery(self, quals, columns):
"""
Builds a BigQuery query
Expand Down Expand Up @@ -286,9 +279,8 @@ def buildColumnList(self, columns, usage='SELECT'):
if column == self.countPseudoColumn: # Pseudo column to count grouped rows
if usage == 'SELECT':
clause += "count(*) " + self.addColumnAlias(column, useAliases) + ", "
elif column == self.partitionPseudoColumn: # Partition pseudo column (for SELECT only)
if usage == 'SELECT':
clause += "null " + self.addColumnAlias(column, useAliases) + ", " # Partition pseudo column is forced to return `null`
elif column == self.partitionPseudoColumn: # Partition pseudo column
clause += "_PARTITIONTIME " + self.addColumnAlias(column, useAliases) + ", "
else: # Any other column
# Get column data type
dataType = self.getBigQueryDatatype(column)
Expand Down Expand Up @@ -381,9 +373,6 @@ def buildWhereClause(self, quals):
value = qual.value.strftime("%Y-%m-%d 00:00:00")

parameters.append(self.setParameter(qual.field_name, 'TIMESTAMP', value)) # Force data type to `TIMESTAMP`

# Store the value to return it to PostgreSQL
self.partitionPseudoColumnValue = self.bq.varToString(qual.value)
else:
clause += str(qual.field_name) + " " + str(self.getOperator(qual.operator)) + " ?"
parameters.append(self.setParameter(qual.field_name, self.getBigQueryDatatype(qual.field_name), qual.value))
Expand Down

0 comments on commit fa9026f

Please sign in to comment.