Skip to content

Commit 88d1901

Browse files
committed
fix value parsing in event shovel
1 parent 20baa49 commit 88d1901

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

scraper_service/shovel_events/utils.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,28 @@
99

1010

1111
def format_value(value):
12-
"""
13-
SQL requires strings to be wrapped in single quotes
14-
Complex types like lists with dictionaries are serialized to JSON
15-
"""
1612
if value is None:
1713
return "NULL"
14+
elif isinstance(value, (int, float)):
15+
return value
1816
elif isinstance(value, str):
19-
return f"'{value}'"
17+
escaped_value = value.replace("'", "\\'")
18+
return f"'{escaped_value}'"
2019
elif isinstance(value, list):
21-
if len(value) > 0 and all(isinstance(x, (str, int, float)) for x in value):
22-
return value
20+
if len(value) > 0 and all(isinstance(x, (int, float)) for x in value):
21+
return f"[{','.join(str(x) for x in value)}]"
22+
elif len(value) > 0 and all(isinstance(x, str) for x in value):
23+
escaped_strings = []
24+
for x in value:
25+
escaped = x.replace("'", "\\'")
26+
escaped_strings.append(f"'{escaped}'")
27+
return f"[{','.join(escaped_strings)}]"
2328
else:
24-
return f"'{json.dumps(value)}'"
29+
json_str = json.dumps(value).replace("'", "\\'")
30+
return f"'{json_str}'"
2531
else:
26-
return value
32+
escaped = str(value).replace("'", "\\'")
33+
return f"'{escaped}'"
2734

2835

2936
def get_column_type(value):
@@ -34,20 +41,7 @@ def get_column_type(value):
3441
elif isinstance(value, float):
3542
return "Float64"
3643
elif isinstance(value, list):
37-
if len(value) > 0:
38-
inner = value[0]
39-
# Only use Array if the inner value will have a proper type, otherwise, just
40-
# stringify it.
41-
if (
42-
isinstance(inner, str)
43-
or isinstance(inner, int)
44-
or isinstance(inner, float)
45-
):
46-
return f"Array({get_column_type(inner)})"
47-
else:
48-
return "String"
49-
else:
50-
return "String"
44+
return "String"
5145
elif value is None:
5246
return None
5347
else:
@@ -101,8 +95,7 @@ def create_clickhouse_table(table_name, column_names, column_types, values):
10195

10296
columns = list(
10397
map(
104-
lambda x, y: f"{escape_column_name(x)} {
105-
y}",
98+
lambda x, y: f"{escape_column_name(x)} {y}",
10699
column_names,
107100
column_types,
108101
)

0 commit comments

Comments
 (0)