Skip to content

Reestablish warning on duplicate column in query. #1825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1680-duplicate-column-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1680-duplicate-column-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1680-duplicate-column-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1680-duplicate-column-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataRetrievalFailureException;
import org.springframework.data.jdbc.core.convert.RowDocumentExtractorSupport.AggregateContext;
import org.springframework.data.jdbc.core.convert.RowDocumentExtractorSupport.RowDocumentSink;
Expand All @@ -43,6 +45,9 @@
*/
class RowDocumentResultSetExtractor {

private static final Logger log = LoggerFactory.getLogger(RowDocumentResultSetExtractor.class);
public static final String DUPLICATE_COLUMN_WARNING = "ResultSet contains column \"{}\" multiple times. Later column index is {}";

private final RelationalMappingContext context;
private final PathToColumnMapping propertyToColumn;

Expand All @@ -66,9 +71,13 @@ static RowDocument toRowDocument(ResultSet resultSet) throws SQLException {
RowDocument document = new RowDocument(columnCount);

for (int i = 0; i < columnCount; i++) {

Object rsv = JdbcUtils.getResultSetValue(resultSet, i + 1);
String columnName = md.getColumnLabel(i + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We should use JdbcUtils.lookupColumnName(…) here.

document.put(columnName, rsv instanceof Array a ? a.getArray() : rsv);
Object old = document.put(columnName, rsv instanceof Array a ? a.getArray() : rsv);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering why we're not using putIfAbsent semantics. Obtaining a value from the ResultSet typically uses the first occurrence of a column while we should be agnostic to the fact whether a column occurs multiple times in the result.

For reference, none of Spring JDBC's mappers care about duplicate columns, they just accept the fact that ResultSet either returns different values when iterating over the column count or use the value by name lookup.

Furthermore, when an external query declares the same column names, why is it us to validate this concern?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, the JDBC spec says when you have duplicate column names, the first one should be returned.

Copy link
Contributor Author

@schauder schauder Jun 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it us to validate this concern?

Because we are nice people and we notice something is weird.

if (old != null) {
log.warn(DUPLICATE_COLUMN_WARNING, columnName, i);
}
}

return document;
Expand Down Expand Up @@ -107,7 +116,12 @@ public Map<String, Integer> getColumnMap(ResultSet result) {
Map<String, Integer> columns = new LinkedCaseInsensitiveMap<>(metaData.getColumnCount());

for (int i = 0; i < metaData.getColumnCount(); i++) {
columns.put(metaData.getColumnLabel(i + 1), i + 1);

String columnLabel = metaData.getColumnLabel(i + 1);
Object old = columns.put(columnLabel, i + 1);
if (old != null) {
log.warn(DUPLICATE_COLUMN_WARNING, columnLabel, i);
}
}
return columns;
} catch (SQLException e) {
Expand Down
4 changes: 2 additions & 2 deletions spring-data-r2dbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-r2dbc</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1680-duplicate-column-SNAPSHOT</version>

<name>Spring Data R2DBC</name>
<description>Spring Data module for R2DBC</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1680-duplicate-column-SNAPSHOT</version>
</parent>

<properties>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1680-duplicate-column-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-1680-duplicate-column-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Loading