I’m adding a new column to my Athena table, and I want to perform my new query on the new column only when it exists, otherwise use the currently implemented query on existing columns, in order to retain the logic in case the code were updated first and my other Athena tables were not updated with the new column.
I want to execute both the check column and the new query in a single SQL string in a single API call, instead of sending another dedicated request just for the check, to avoid potential network and server errors along the way.
My idea of implementing the query:
-- check for column exists
WITH has_column AS (
SELECT count(*) AS c
FROM information_schema.columns
WHERE table_name = 'my_table'
AND column_name = 'new_column'
)
-- query
SELECT count(tb.id) AS s,
check_col.c AS has_col
FROM "my_table" AS tb,
"has_column" AS check_col
WHERE
CASE
has_col
WHEN 1 THEN (
new_column = 'value' -- new logic with new column if exists
) ELSE (
current_column = 'value' -- current logic with existing column
)
END
As I ran the query on Athena editor, it always reports COLUMN_NOT_FOUND
error on the new column (not yet existed) instead of bypassing the case and going on executing the current logic as I thought.
How can I implement this effectively ?
5