Repository: tobymao/sqlglot

Issue:
Timezone in BigQuery function FORMAT_TIMESTAMP is not propagated
When transpiling my SQL string, the timezone argument of BigQuery’s [FORMAT_TIMESTAMP](https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#format_timestamp) function is unexpectedly removed. It should be preserved and propagated correctly.

I'm using sqlglot version 26.11.1.

**Example**

Running the following
```python
import sqlglot

print(sqlglot.transpile("SELECT FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', CURRENT_TIMESTAMP(), 'Europe/Berlin') AS ts", read="bigquery", write="bigquery")[0])
```
Produces:
```python
SELECT FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', CURRENT_TIMESTAMP()) AS ts
```

Expected output:
```python
SELECT FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', CURRENT_TIMESTAMP(), 'Europe/Berlin') AS ts
```

I searched a bit through the code but couldn't come up with a fix so far.

Any help would be much appreciated. Thanks!

Discussion:

Solution patch:
diff --git a/sqlglot/dialects/bigquery.py b/sqlglot/dialects/bigquery.py
index d68feb7631..331093e0a9 100644
--- a/sqlglot/dialects/bigquery.py
+++ b/sqlglot/dialects/bigquery.py
@@ -313,7 +313,11 @@ def _build_levenshtein(args: t.List) -> exp.Levenshtein:
 
 def _build_format_time(expr_type: t.Type[exp.Expression]) -> t.Callable[[t.List], exp.TimeToStr]:
     def _builder(args: t.List) -> exp.TimeToStr:
-        return exp.TimeToStr(this=expr_type(this=seq_get(args, 1)), format=seq_get(args, 0))
+        return exp.TimeToStr(
+            this=expr_type(this=seq_get(args, 1)),
+            format=seq_get(args, 0),
+            zone=seq_get(args, 2),
+        )
 
     return _builder
 
@@ -1174,7 +1178,9 @@ def timetostr_sql(self, expression: exp.TimeToStr) -> str:
                 if isinstance(this, (exp.TsOrDsToDatetime, exp.TsOrDsToTimestamp, exp.TsOrDsToDate))
                 else expression
             )
-            return self.func(func_name, self.format_time(expression), time_expr.this)
+            return self.func(
+                func_name, self.format_time(expression), time_expr.this, expression.args.get("zone")
+            )
 
         def eq_sql(self, expression: exp.EQ) -> str:
             # Operands of = cannot be NULL in BigQuery


Test patch:
diff --git a/tests/dialects/test_bigquery.py b/tests/dialects/test_bigquery.py
index 2cdc1059ff..7e8b3cb941 100644
--- a/tests/dialects/test_bigquery.py
+++ b/tests/dialects/test_bigquery.py
@@ -196,6 +196,9 @@ def test_bigquery(self):
         self.validate_identity("CAST(x AS TIMESTAMPTZ)", "CAST(x AS TIMESTAMP)")
         self.validate_identity("CAST(x AS RECORD)", "CAST(x AS STRUCT)")
         self.validate_identity("SELECT * FROM x WHERE x.y >= (SELECT MAX(a) FROM b-c) - 20")
+        self.validate_identity(
+            "SELECT FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', CURRENT_TIMESTAMP(), 'Europe/Berlin') AS ts"
+        )
         self.validate_identity(
             "SELECT cars, apples FROM some_table PIVOT(SUM(total_counts) FOR products IN ('general.cars' AS cars, 'food.apples' AS apples))"
         )