**How to reproduce**
Clickhouse version 21.2-21.3

```
SELECT x
FROM
(
    SELECT 1 AS x
    UNION DISTINCT
    SELECT 1
)
LIMIT 1

Query id: b82bbb8d-dea9-4f1c-9a2f-5b0e9b6c2422


0 rows in set. Elapsed: 0.002 sec.

Received exception from server (version 21.2.3):
Code: 49. DB::Exception: Received from localhost:9000. DB::Exception: Bad cast from type DB::ASTSelectWithUnionQuery to DB::ASTSelectQuery.
```


const Context & context,
    const StorageID & table_id,
    const StorageMetadataPtr & table_metadata,
    const Strings & required_columns,
    const TreeRewriterResult & syntax_analyzer_result)
{
    if (!syntax_analyzer_result.has_explicit_columns && table_metadata && !table_metadata->getColumns().empty())
    {
        
        
        
        
        
        auto access = context.getAccess();
        for (const auto & column : table_metadata->getColumns())
        {
            if (access->isGranted(AccessType::SELECT, table_id.database_name, table_id.table_name, column.name))
                return;
        }
        throw Exception(context.getUserName() + ": Not enough privileges. "
                        "To execute this query it's necessary to have grant SELECT for at least one column on " + table_id.getFullTableName(),
                        ErrorCodes::ACCESS_DENIED);
    }
    
    context.checkAccess(AccessType::SELECT, table_id, required_columns);
}
static bool shouldIgnoreQuotaAndLimits(const StorageID & table_id)