[SPARK-3395] [SQL] DSL sometimes incorrectly reuses attribute ids, breaking queries

This resolves https://issues.apache.org/jira/browse/SPARK-3395