def generate_unique_result_value_name(columns, result_value_name, variables):
    """
    If result_value_name is inside variables, generate unique
    result_value_name. If not, return result_value_name directly.

    Args:
        columns (list[str]): the column names in the source table.
        result_value_name (str): the original result value name to be
            optimized.
        variables (list[str]): the variable names to be optimized.

    Returns:
        A unique result_value_name that is not duplicated with any name in
        columns and variables.
    """
    variables = [v.lower() for v in variables]
    columns_lower = [c.lower() for c in columns]
    for v in variables:
        assert v in columns_lower, "cannot find variable %s column" % v

    assert len(set(variables)) == len(variables), \
        "duplicate variables are not allowed"

    if len(variables) > 1 or result_value_name.lower() != variables[0]:
        assert result_value_name.lower() not in columns_lower, \
            "result value name should not be duplicated with the column name"
        return result_value_name

    result_value_name += "_value"
    if result_value_name.lower() not in columns_lower:
        return result_value_name

    i = 0
    while True:
        new_name = result_value_name + ("_%d" % i)
        i += 1
        if new_name.lower() not in columns_lower:
            return new_name