def read_db_properties_format(raw_db_properties):
    """Helper to read non-standard db properties format.

    Note:
        Spark/Hive doesn't provide a way to read separate key/values for database properties.
        They provide a custom format like: ((key_a,value_a), (key_b,value_b))
        Neither keys nor values are escaped.
        Here we try our best to parse this format by tracking balanced parentheses.
        We assume property names don't contain comma.

    Return:
        list[list[str]] - the list of key-value pairs.
    """
    def _unpack_parentheses(string):
        bits = []
        last_bit = ''
        checksum = 0

        for c in string:
            if c == '(':
                if checksum == 0:
                    last_bit = ''
                else:
                    last_bit += c
                checksum += 1
            elif c == ')':
                checksum -= 1
                if checksum == 0:
                    bits.append(last_bit)
                else:
                    last_bit += c
            else:
                last_bit += c

            if checksum < 0:
                raise ValueError('Parentheses are not balanced')

        if checksum != 0:
            raise ValueError('Parentheses are not balanced')

        return bits

    properties = _unpack_parentheses(raw_db_properties)
    if properties:
        return [x.split(',', 1) for x in _unpack_parentheses(properties[0])]
    else:
        return []