9
9
10
10
11
11
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
- """
16
12
if value is None :
17
13
return "NULL"
14
+ elif isinstance (value , (int , float )):
15
+ return value
18
16
elif isinstance (value , str ):
19
- return f"'{ value } '"
17
+ escaped_value = value .replace ("'" , "\\ '" )
18
+ return f"'{ escaped_value } '"
20
19
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 )} ]"
23
28
else :
24
- return f"'{ json .dumps (value )} '"
29
+ json_str = json .dumps (value ).replace ("'" , "\\ '" )
30
+ return f"'{ json_str } '"
25
31
else :
26
- return value
32
+ escaped = str (value ).replace ("'" , "\\ '" )
33
+ return f"'{ escaped } '"
27
34
28
35
29
36
def get_column_type (value ):
@@ -34,20 +41,7 @@ def get_column_type(value):
34
41
elif isinstance (value , float ):
35
42
return "Float64"
36
43
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"
51
45
elif value is None :
52
46
return None
53
47
else :
@@ -101,8 +95,7 @@ def create_clickhouse_table(table_name, column_names, column_types, values):
101
95
102
96
columns = list (
103
97
map (
104
- lambda x , y : f"{ escape_column_name (x )} {
105
- y } " ,
98
+ lambda x , y : f"{ escape_column_name (x )} { y } " ,
106
99
column_names ,
107
100
column_types ,
108
101
)
0 commit comments