mirror of
https://github.com/PostHog/posthog.git
synced 2024-12-01 04:04:16 +01:00
8e5347b4e1
* change parsing to include operators' * make properties test into factory * add clickhouse test implementation and fix another test * add custom test to clickhouse filter tests * all tests besides json filtering * add json test * fix tests * fix type errors
54 lines
1.6 KiB
Python
54 lines
1.6 KiB
Python
import json
|
|
|
|
from posthog.models.property import Property
|
|
|
|
|
|
def get_operator(prop: Property, arg: str):
|
|
operator = prop.operator
|
|
|
|
if operator == "is_not":
|
|
return "(trim(BOTH '\"' FROM ep.value) = %({})s)".format(arg), prop.value
|
|
elif operator == "icontains" or operator == "not_icontains":
|
|
value = "%{}%".format(prop.value)
|
|
return "(trim(BOTH '\"' FROM ep.value) LIKE %({})s)".format(arg), value
|
|
elif operator == "regex" or operator == "not_regex":
|
|
return "match(trim(BOTH '\"' FROM ep.value), %({})s)".format(arg), prop.value
|
|
elif operator == "is_set":
|
|
return "", prop.value
|
|
elif operator == "is_not_set":
|
|
return "", prop.value
|
|
elif operator == "gt":
|
|
return (
|
|
"(toInt64(trim(BOTH '\"' FROM ep.value)) > %({})s)".format(arg),
|
|
prop.value,
|
|
)
|
|
elif operator == "lt":
|
|
return (
|
|
"(toInt64(trim(BOTH '\"' FROM ep.value)) < %({})s)".format(arg),
|
|
prop.value,
|
|
)
|
|
else:
|
|
if is_json(prop.value):
|
|
return (
|
|
"replaceRegexpAll(trim(BOTH '\"' FROM ep.value),' ', '') = replaceRegexpAll(toString(%({})s),' ', '')".format(
|
|
arg
|
|
),
|
|
prop.value,
|
|
)
|
|
else:
|
|
return (
|
|
"(trim(BOTH '\"' FROM ep.value) = toString(%({})s))".format(arg),
|
|
prop.value,
|
|
)
|
|
|
|
|
|
def is_json(val):
|
|
if isinstance(val, int):
|
|
return False
|
|
|
|
try:
|
|
json.loads(val)
|
|
except ValueError:
|
|
return False
|
|
return True
|