diff --git a/ibis/backends/bigquery/tests/unit/snapshots/test_compiler/test_time_from_hms_with_micros/micros.sql b/ibis/backends/bigquery/tests/unit/snapshots/test_compiler/test_time_from_hms_with_micros/micros.sql new file mode 100644 index 000000000000..383e1bc5102b --- /dev/null +++ b/ibis/backends/bigquery/tests/unit/snapshots/test_compiler/test_time_from_hms_with_micros/micros.sql @@ -0,0 +1,2 @@ +SELECT + TIME_ADD(TIME(12, 34, 56), INTERVAL 789101 MICROSECOND) AS `datetime_time_12_34_56_789101` \ No newline at end of file diff --git a/ibis/backends/bigquery/tests/unit/snapshots/test_compiler/test_time_from_hms_with_micros/no_micros.sql b/ibis/backends/bigquery/tests/unit/snapshots/test_compiler/test_time_from_hms_with_micros/no_micros.sql new file mode 100644 index 000000000000..53f8aed1c25d --- /dev/null +++ b/ibis/backends/bigquery/tests/unit/snapshots/test_compiler/test_time_from_hms_with_micros/no_micros.sql @@ -0,0 +1,2 @@ +SELECT + TIME(12, 34, 56) AS `datetime_time_12_34_56` \ No newline at end of file diff --git a/ibis/backends/bigquery/tests/unit/test_compiler.py b/ibis/backends/bigquery/tests/unit/test_compiler.py index e66451316d88..94ddb123f36d 100644 --- a/ibis/backends/bigquery/tests/unit/test_compiler.py +++ b/ibis/backends/bigquery/tests/unit/test_compiler.py @@ -667,3 +667,13 @@ def test_subquery_scalar_params(snapshot): ) result = ibis.to_sql(expr, params={p: "20140101"}, dialect="bigquery") snapshot.assert_match(result, "out.sql") + + +def test_time_from_hms_with_micros(snapshot): + literal = ibis.literal(datetime.time(12, 34, 56, 789101)) + result = ibis.to_sql(literal, dialect="bigquery") + snapshot.assert_match(result, "micros.sql") + + literal = ibis.literal(datetime.time(12, 34, 56)) + result = ibis.to_sql(literal, dialect="bigquery") + snapshot.assert_match(result, "no_micros.sql") diff --git a/ibis/backends/sql/compilers/bigquery.py b/ibis/backends/sql/compilers/bigquery.py index 10c405492c9b..b37a91d7d72b 100644 --- a/ibis/backends/sql/compilers/bigquery.py +++ b/ibis/backends/sql/compilers/bigquery.py @@ -283,7 +283,15 @@ def visit_NonNullLiteral(self, op, *, value, dtype): elif dtype.is_date(): return self.f.date_from_parts(value.year, value.month, value.day) elif dtype.is_time(): - return self.f.time_from_parts(value.hour, value.minute, value.second) + time = self.f.time_from_parts(value.hour, value.minute, value.second) + if micros := value.microsecond: + # bigquery doesn't support `time(12, 34, 56.789101)`, AKA a + # float seconds specifier, so add any non-zero micros to the + # time value + return sge.TimeAdd( + this=time, expression=sge.convert(micros), unit=self.v.MICROSECOND + ) + return time elif dtype.is_binary(): return sge.Cast( this=sge.convert(value.hex()),