Skip to content

Commit

Permalink
Bugfix with syntax error when using fdw_convert_tz and fdw_casting on…
Browse files Browse the repository at this point in the history
… a date
  • Loading branch information
gabfl committed Oct 5, 2017
1 parent 814bf48 commit 4c60169
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/fdw.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,17 @@ def buildColumnList(self, columns, usage='SELECT'):
# Get column data type
dataType = self.getBigQueryDatatype(column)

# Save column original name
columnOriginalName = column

# If the data type is a date or a timestamp
if dataType in ['DATE', 'TIMESTAMP']:
column = self.setTimeZone(column, dataType, useAliases)
column = self.setTimeZone(column, dataType)

# Data type casting
casted = self.castColumn(column, dataType)
column = self.castColumn(column, columnOriginalName, dataType)

if casted:
clause += casted + " " + self.addColumnAlias(column, useAliases) + ", "
else:
clause += column + ", "
clause += column + " " + self.addColumnAlias(columnOriginalName, useAliases) + ", "

# Remove final `, `
clause = clause.strip(', ')
Expand All @@ -298,29 +298,29 @@ def buildColumnList(self, columns, usage='SELECT'):

return clause

def setTimeZone(self, column, dataType, useAliases=True):
def setTimeZone(self, column, dataType):
"""
If the option `fdw_convert_tz` is used, convert the time zone automatically from UTC to the desired time zone
"""

# Option is set
if self.convertToTz:
if dataType == 'DATE': # BigQuery column type is `DATE`
return 'DATE(' + column + ', "' + self.convertToTz + '") ' + self.addColumnAlias(column, useAliases)
return 'DATE(' + column + ', "' + self.convertToTz + '") '
else: # BigQuery column type is `TIMESTAMP`
return 'DATETIME(' + column + ', "' + self.convertToTz + '") ' + self.addColumnAlias(column, useAliases)
return 'DATETIME(' + column + ', "' + self.convertToTz + '") '

# Option is not set
return column

def castColumn(self, column, dataType):
def castColumn(self, column, columnOriginalName, dataType):
"""
If the option `fdw_casting` is used, this method will attempt to cast the column to the new type
"""

if self.castingRules and column in self.castingRules: # If we have casting rule for this column
if self.castingRules and columnOriginalName in self.castingRules: # If we have casting rule for this column
# Get desired casting
castTo = self.castingRules[column]
castTo = self.castingRules[columnOriginalName]

# Find if we have a matching rule
rule = [conversionRule for conversionRule in self.conversionRules if conversionRule.bq_standard_from == dataType.upper()]
Expand All @@ -334,6 +334,9 @@ def castColumn(self, column, dataType):
else:
log_to_postgres("Casting from the data type `" + dataType.upper() + "` is not permitted.", ERROR)

# Option is not set
return column

def addColumnAlias(self, alias, useAliases=True):
"""
Returns a string "as `alias`" if `useAliases` is `True`
Expand Down

0 comments on commit 4c60169

Please sign in to comment.