influxdata / influxdb-client-python

InfluxDB 2.0 python client
https://influxdb-client.readthedocs.io/en/stable/
MIT License
705 stars 185 forks source link

Empty string in database becomes None value in Python #570

Open hy144328 opened 1 year ago

hy144328 commented 1 year ago

Specifications

Code sample to reproduce problem

import influxdb_client

with influxdb_client.InfluxDBClient(
    org = ...,
    token = ...,
    url = ...,
) as client:
    q = f"""
        from(bucket: ...)
        //|> filter(fn: (r) => exists r._value)
        //|> filter(fn: (r) => r._value != "")
        |> last()
    """
    res = [
        record_it.values
        for table_it in client.query_api().query(q)
        for record_it in table_it.records
    ]
    print(res)

Expected behavior

[{'result': '_result', 'table': 0, '_time': datetime.datetime(2023, 3, 2, 18, 47, 7, 547036, tzinfo=tzutc()), '_value': ''}]

Actual behavior

[{'result': '_result', 'table': 0, '_time': datetime.datetime(2023, 3, 2, 18, 47, 7, 547036, tzinfo=tzutc()), '_value': None}]

Additional info

last() function:

If [_value] column is null in the last record, last() returns the previous record with a non-null value.

My last record with a non-null _value has the empty string for _value. So far, the row is correctly identified. But by the time I read _value from the result returned in Python, the record does not hold the empty string "" but None.

I verified that the value of _value is really the empty string in the database and not None by experimenting with filters (see commented code). I find it more intuitive to leave the empty string as empty string in Python.