From 712fef03fded973784d05d18f082ce7fcf53a1ba Mon Sep 17 00:00:00 2001 From: NguyenHoangSon96 Date: Tue, 24 Mar 2026 15:04:29 +0700 Subject: [PATCH 1/5] test: add escape for key when serialize --- .../write/polars_dataframe_serializer.py | 15 +++++++------ tests/test_polars.py | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/influxdb_client_3/write_client/client/write/polars_dataframe_serializer.py b/influxdb_client_3/write_client/client/write/polars_dataframe_serializer.py index 61f272c..7c2e4cf 100644 --- a/influxdb_client_3/write_client/client/write/polars_dataframe_serializer.py +++ b/influxdb_client_3/write_client/client/write/polars_dataframe_serializer.py @@ -92,12 +92,15 @@ def to_line_protocol(self, row): # add escape symbols for special characters to tags fields = ",".join( - f"{col}=\"{self.escape_value(row[self.column_indices[col]])}\"" if isinstance(row[self.column_indices[col]], - str) - else f"{col}={str(row[self.column_indices[col]]).lower()}" if isinstance(row[self.column_indices[col]], - bool) # Check for bool first - else f"{col}={row[self.column_indices[col]]}i" if isinstance(row[self.column_indices[col]], int) - else f"{col}={row[self.column_indices[col]]}" + f"{self.escape_key(col)}=\"{self.escape_value(row[self.column_indices[col]])}\"" if isinstance( + row[self.column_indices[col]], + str) + else f"{self.escape_key(col)}={str(row[self.column_indices[col]]).lower()}" if isinstance( + row[self.column_indices[col]], + bool) # Check for bool first + else f"{self.escape_key(col)}={row[self.column_indices[col]]}i" if isinstance(row[self.column_indices[col]], + int) + else f"{self.escape_key(col)}={row[self.column_indices[col]]}" for col in self.column_indices if col not in self.tag_columns + [self.timestamp_column] and row[self.column_indices[col]] is not None and row[self.column_indices[col]] != "" diff --git a/tests/test_polars.py b/tests/test_polars.py index 61ff206..ac98eee 100644 --- a/tests/test_polars.py +++ b/tests/test_polars.py @@ -123,6 +123,27 @@ def test_to_list_of_points_with_precision_variants(self): data_frame_measurement_name='iot-devices', data_frame_timestamp_column='time') + def test_escape_for_key(self): + import polars as pl + ps = PointSettings(tag="prod") + df = pl.DataFrame(data={ + "name whitespace": ['iot-devices'], + "tag1 whitespace": "something", + "building": ['5a'], + "temperature": [72.3], + "time": pl.Series(["2022-10-01T12:01:00Z"]).str.to_datetime(time_unit='ns') + }) + + actual = polars_data_frame_to_list_of_points( + data_frame=df, point_settings=ps, + data_frame_measurement_name='iot-devices', + data_frame_tag_columns=['building', 'tag1 whitespace'], + data_frame_timestamp_column='time', + ) + + expected = [ + 'iot-devices,building=5a,tag1\\ whitespace=something,tag=prod name\\ whitespace="iot-devices",temperature=72.3 1664625660000000000'] + self.assertEqual(expected, actual) @unittest.skipIf(importlib.util.find_spec("polars") is None, 'Polars package not installed') class TestWritePolars(unittest.TestCase): From 7e7c585b5463f330a09aa5a036c833befbc2a7bd Mon Sep 17 00:00:00 2001 From: NguyenHoangSon96 Date: Tue, 24 Mar 2026 15:08:51 +0700 Subject: [PATCH 2/5] chore: lint --- tests/test_polars.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_polars.py b/tests/test_polars.py index ac98eee..b3fc524 100644 --- a/tests/test_polars.py +++ b/tests/test_polars.py @@ -142,7 +142,9 @@ def test_escape_for_key(self): ) expected = [ - 'iot-devices,building=5a,tag1\\ whitespace=something,tag=prod name\\ whitespace="iot-devices",temperature=72.3 1664625660000000000'] + 'iot-devices,building=5a,tag1\\ whitespace=something,tag=prod name\\ whitespace="iot-devices",' + 'temperature=72.3 1664625660000000000' + ] self.assertEqual(expected, actual) @unittest.skipIf(importlib.util.find_spec("polars") is None, 'Polars package not installed') From cd1107d4baaa7dfd29d76421b79ad5fab8f9566b Mon Sep 17 00:00:00 2001 From: NguyenHoangSon96 Date: Tue, 24 Mar 2026 15:11:23 +0700 Subject: [PATCH 3/5] chore: CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 839b845..7be9360 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ 1. [#198](https://github.com/InfluxCommunity/influxdb3-python/pull/198): Support custom tag order via `tag_order` write option. See [Sort tags by priority](https://docs.influxdata.com/influxdb3/enterprise/write-data/best-practices/schema-design/#sort-tags-by-query-priority) for more. +1. [#202](https://github.com/InfluxCommunity/influxdb3-python/pull/202): Add escape for fields when serializing to line protocol in `PolarsDataframeSerializer`. ## 0.18.0 [2026-02-19] From ca2e19b3197f3bd2571045f1ec7dd4d0510c144a Mon Sep 17 00:00:00 2001 From: NguyenHoangSon96 Date: Tue, 24 Mar 2026 15:12:55 +0700 Subject: [PATCH 4/5] chore: linter --- tests/test_polars.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_polars.py b/tests/test_polars.py index b3fc524..44350e1 100644 --- a/tests/test_polars.py +++ b/tests/test_polars.py @@ -147,6 +147,7 @@ def test_escape_for_key(self): ] self.assertEqual(expected, actual) + @unittest.skipIf(importlib.util.find_spec("polars") is None, 'Polars package not installed') class TestWritePolars(unittest.TestCase): def setUp(self): From cdfde4e37ae46bfd60c950d9ff16d3fca519fc43 Mon Sep 17 00:00:00 2001 From: sonnh <46211823+NguyenHoangSon96@users.noreply.github.com> Date: Tue, 24 Mar 2026 20:56:45 +0700 Subject: [PATCH 5/5] chore: Update CHANGELOG.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7be9360..54c278f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ 1. [#198](https://github.com/InfluxCommunity/influxdb3-python/pull/198): Support custom tag order via `tag_order` write option. See [Sort tags by priority](https://docs.influxdata.com/influxdb3/enterprise/write-data/best-practices/schema-design/#sort-tags-by-query-priority) for more. -1. [#202](https://github.com/InfluxCommunity/influxdb3-python/pull/202): Add escape for fields when serializing to line protocol in `PolarsDataframeSerializer`. +1. [#202](https://github.com/InfluxCommunity/influxdb3-python/pull/202): Add escape for field keys when serializing to line protocol in `PolarsDataframeSerializer`. ## 0.18.0 [2026-02-19]