Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Unexpected behaviour in Python API: time should be in quotes in select #309

Closed
Jimilian opened this issue Mar 18, 2016 · 2 comments
Closed

Comments

@Jimilian
Copy link

I found that if you are using simple select query "select * from measurement where t = X" without quotes, result becomes unpredictable. For it's unclear why I should use quotes for time, if time is integer (or long).

from time import sleep
from influxdb import client as influxdb

db = influxdb.InfluxDBClient("localhost", 8086, "", "", "queue")

measurement = "wtr"

def drop():
    db.query("drop measurement " + measurement)

def show_all():
    print db.query("select * from " + measurement)

def check_one_way(t):
    result = db.query('select * from {0} where time="{1}"'.format(measurement, t))
    return bool(result)

def check_second_way(t):
    result = db.query('select * from {0} where time={1}'.format(measurement, t))
    return bool(result)

def write_to_influx(exists):
    t = 1458239131231000000
    points = [{'fields': {'value': 1}, 'time': t, 'measurement': measurement}]

    if exists(t):
        print "Exists!!!", t
        return

    db.write_points(points)

    sleep(1)

    if exists(t):
        print "OK"  # Expected behaviour
    else:
        print "WTF?!"  # Unexpected behaviour

print "With quotes"
write_to_influx(check_one_way)
show_all()
print "="*80

print "Without quotes"
drop()
write_to_influx(check_second_way)
show_all()
drop()
@Tomcat-Engineering
Copy link

  1. I see this behaviour in the InfluxDB web interface too, so I guess this is a general InfluxDB thing (as per your original ticket) rather than specific to the Python library?

  2. The documentation says that you should omit the quotes but append a code for the units of the timestamp... Sadly it doesn't appear to have a code for nanoseconds, but if you alter your code to look like this then it seems to work for your example:

result = db.query('select * from {0} where time={1}u'.format(measurement, t/1000))
  1. Further investigation suggests that InfluxDB actually ignores any timestamp that you put in quotes! For example, editing your check_one_way call to double the timestamp still returns the same result:
result = db.query('select * from {0} where time="{1}"'.format(measurement, t*2))

@sebito91
Copy link
Contributor

Addressed in #678

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants