hive uses Java simpledateformat. As per Java guide,
- y (lowercase) is year
- Y (uppercase) is 'week-based-year'
This difference will cause your code to work perfectly fine, except for when dealing with dates at the very end of some years.
So, when you use ('2021-12-27','YYYYMMdd'), yyyy will output 2021 but YYYY will output 2022. Because the week that the 27th of December falls in the first week of 2022.
date_format('2021-12-25','YYYYMMdd')
is working because 25th Dec is not the first week of 2022.
Refer to below screenshot, you can see lowercase y is giving you correct result and uppercase is picking up year from first week of year which is 2022.

Please always use yyyy
as per hive docs says.