4

My list:

pylist = ['A', 'B', 'C', 'D']

My spark.sql() line:

query = spark.sql(
    """
    SELECT col1, col2, col3
    FROM database.table
    WHERE col3 IN ('A', 'B', 'C', 'D')
    """
)

I want to replace the list of elements in the spark.sql() statement with the Python list so that the last line in the SQL is:

...
AND col3 IN pylist

I am aware of {} and str.format but I don't understand if that's the correct option and how that works.

4 Answers 4

4

I think the solution is .format(tuple(pylist)):

pylist = ['A', 'B', 'C', 'D']

s = """
    SELECT col1, col2, col3
    FROM database.table
    WHERE col3 IN {}
    """.format(tuple(pylist))

query = spark.sql(s)
Sign up to request clarification or add additional context in comments.

1 Comment

This won't work if len(pylist)==1.
0

Convert to tuple to get "()" instead of "[]" while converting to string

sql_str="SELECT col1, col2, col3 FROM database.table WHERE col3 IN " + str(tuple(pylist))

query = spark.sql(sql_str)

Comments

0

You can also use an f-string with {tuple(pylist)} as shown:

The statement now is:

pylist = ['A', 'B', 'C', 'D']

s = f"""
    SELECT col1, col2, col3
    FROM database.table
    WHERE col3 IN {tuple(pylist)}
    """

query = spark.sql(s)

1 Comment

This won't work if len(pylist)==1.
0

If it's one element in the list use a conditional statement to fix the DELETE query's WHERE clause:

if len(filesToDelete) > 1:
    s = """
    DELETE from table_name WHERE s3_file_name in {}
    """.format(tuple(filesToDelete))
    print(s)
elif len(filesToDelete) == 1:
    s = """ DELETE from table_name WHERE s3_file_name = '{}'
    """.format(filesToDelete[0])
    print(s)
else:
    print("Log No Files to Delete")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.