@@ -870,22 +870,6 @@ def _process_time_partitioning(
870870 },
871871 }
872872
873- def parse_sqltypes (coltype , partitioning_period ):
874- """Returns the default value OR the truncation function to be used
875- and the allowed partitioning periods.
876- """
877-
878- if coltype in {"_PARTITIONDATE" , "_PARTITIONTIME" }:
879- return sqltypes [coltype ]
880-
881- # by this point, value must be a nested dict
882- if partitioning_period is None :
883- # use "no_period" key
884- return sqltypes [coltype ]["no_period" ]
885- else :
886- # use "period" key
887- return sqltypes [coltype ]["period" ]
888-
889873 # Extract field (i.e <column_name> or _PARTITIONDATE)
890874 # AND extract the name of the column_type (i.e. "TIMESTAMP", "DATE",
891875 # "DATETIME", "_PARTITIONDATE")
@@ -903,14 +887,37 @@ def parse_sqltypes(coltype, partitioning_period):
903887 # immediately overwritten by python-bigquery to a default of DAY.
904888 partitioning_period = time_partitioning .type_
905889
890+ # TODO: move dict outside the function or to top of function
891+ sqltypes_w_no_partitioning_period = {
892+ # Keys are columns, values are functions
893+ "_PARTITIONDATE" : None ,
894+ "_PARTITIONTIME" : "DATE" , # <date function>
895+ "DATE" : None , # 'DATE' is a <date_column> not a function
896+ "DATETIME" : "DATE" , # <date function>
897+ "TIMESTAMP" : "DATE" , # <date function>
898+ }
899+
900+ # TODO: move dict outside the function or to top of function
901+ sqltypes_w_partitioning_period = {
902+ # Keys are columns, values are (functions, {allowed_partioning_periods})
903+ "DATE" : ("DATE_TRUNC" , {"MONTH" , "YEAR" }),
904+ "DATETIME" : ("DATETIME_TRUNC" , {"DAY" , "HOUR" , "MONTH" , "YEAR" }),
905+ "TIMESTAMP" : ("TIMESTAMP_TRUNC" , {"DAY" , "HOUR" , "MONTH" , "YEAR" }),
906+ }
907+
906908 # Extract the default value or truncation_function (i.e. DATE_TRUNC())
907909 # and the set of allowable partition_periods
908910 # that can be used in that function
909- trunc_fn , allowed_partitions = parse_sqltypes (column_type , partitioning_period )
911+ if partitioning_period is None :
912+ # do stuff via swnpp
913+ function = sqltypes_w_no_partitioning_period [column_type ]
914+ else :
915+ # do different stuff via swpp
916+ function , allowed_partitions = sqltypes_w_partitioning_period [column_type , partitioning_period ]
910917
911918 # Create output:
912919 # Special Case: _PARTITIONDATE does NOT use a function or partitioning_period
913- if trunc_fn is None or trunc_fn in { "_PARTITIONDATE" } :
920+ if function is None :
914921 return f"PARTITION BY { field } "
915922
916923 # Special Case: BigQuery will not accept DAY as partitioning_period for
@@ -919,7 +926,7 @@ def parse_sqltypes(coltype, partitioning_period):
919926 # is DAY. This case overwrites that to avoid making a breaking change in
920927 # python-bigquery.
921928 # https://github.com/googleapis/python-bigquery/blob/a4d9534a900f13ae7355904cda05097d781f27e3/google/cloud/bigquery/table.py#L2916
922- if trunc_fn == "DATE_TRUNC" and partitioning_period == "DAY" :
929+ if function == "DATE_TRUNC" and partitioning_period == "DAY" :
923930 raise ValueError (
924931 "The TimePartitioning.type_ must be one of: "
925932 f"{ allowed_partitions } , received { partitioning_period } ."
@@ -935,7 +942,7 @@ def parse_sqltypes(coltype, partitioning_period):
935942 f"{ allowed_partitions } , received { partitioning_period } ."
936943 )
937944
938- return f"PARTITION BY { trunc_fn } ({ field } , { partitioning_period } )"
945+ return f"PARTITION BY { function } ({ field } , { partitioning_period } )"
939946
940947 def _process_range_partitioning (
941948 self , table : Table , range_partitioning : RangePartitioning
0 commit comments