Fixed parse_array

This commit is contained in:
romamo 2022-12-10 10:05:31 +02:00 committed by GitHub
parent 12e36caa93
commit 9a7c5329d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -88,35 +88,18 @@ def parse_tsv(line):
def parse_array(array_string): def parse_array(array_string):
""" """
Parse an array or tuple string as returned by clickhouse. For example: Parse an array or tuple string as returned by clickhouse. For example:
"['hello', 'world']" ==> ["hello", "world"] "['hello','world']" ==> ["hello", "world"]
"(1,2,3)" ==> [1, 2, 3] "(1,2,3)" ==> [1, 2, 3]
""" """
# Sanity check
if len(array_string) < 2 or array_string[0] not in '[(' or array_string[-1] not in '])': if len(array_string) == 2:
raise ValueError('Invalid array string: "%s"' % array_string) return []
# Drop opening brace
array_string = array_string[1:] if array_string[1] == '\'':
# Go over the string, lopping off each value at the beginning until nothing is left values = array_string[2:-2].split('\',\'')
values = [] else:
while True: values = array_string[1:-1].split(',')
if array_string in '])': return values
# End of array
return values
elif array_string[0] in ', ':
# In between values
array_string = array_string[1:]
elif array_string[0] == "'":
# Start of quoted value, find its end
match = re.search(r"[^\\]'", array_string)
if match is None:
raise ValueError('Missing closing quote: "%s"' % array_string)
values.append(array_string[1 : match.start() + 1])
array_string = array_string[match.end():]
else:
# Start of non-quoted value, find its end
match = re.search(r",|\]", array_string)
values.append(array_string[0 : match.start()])
array_string = array_string[match.end() - 1:]
def import_submodules(package_name): def import_submodules(package_name):