Skip to content

[FIX] PreparedStatement.executeBatch() When the Insert Sql Column Name Case Mismatch, Throws BatchUpdateException "Unable to retrieve column metadata." #2589

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

wooln
Copy link

@wooln wooln commented Jan 21, 2025

fix #2588 [BUG] PreparedStatement.executeBatch() When the Insert Sql Column Name Case Mismatch, Throws BatchUpdateException "Unable to retrieve column metadata."

@wooln
Copy link
Author

wooln commented Jan 21, 2025

@microsoft-github-policy-service agree

@lilgreenbird
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Contributor

@lilgreenbird lilgreenbird left a comment

Choose a reason for hiding this comment

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

fix imports and test cleanup

@wooln wooln force-pushed the bugfix-execute-batch-case-sensitive branch from 719687f to 902ac88 Compare January 23, 2025 04:56
@lilgreenbird
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Collaborator

@David-Engel David-Engel left a comment

Choose a reason for hiding this comment

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

Something to consider:

Does your database use a case-sensitive (like SQL_Latin1_General_CP1_CS_AS) or case-insensitive collation (like SQL_Latin1_General_CP1_CI_AS)?

What happens if you are using a case-sensitive collation and a table like this?

create table [Test1] ([C1] int, [c1] varchar)

@lilgreenbird
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@wooln
Copy link
Author

wooln commented Jan 31, 2025

Something to consider:

Does your database use a case-sensitive (like SQL_Latin1_General_CP1_CS_AS) or case-insensitive collation (like SQL_Latin1_General_CP1_CI_AS)?

What happens if you are using a case-sensitive collation and a table like this?

create table [Test1] ([C1] int, [c1] varchar)

Good idea. I covered the unit tests that use both columns c1 and C1, and an exception occurred, indicating that no one has ever covered CS before. I'll try to fix it by the way.

com.microsoft.sqlserver.jdbc.SQLServerException: Duplicate column names are not allowed.

	at com.microsoft.sqlserver.jdbc.SQLServerBulkRecord.checkDuplicateColumnName(SQLServerBulkRecord.java:157)
	at com.microsoft.sqlserver.jdbc.SQLServerBulkBatchInsertRecord.addColumnMetadataInternal(SQLServerBulkBatchInsertRecord.java:323)
	at com.microsoft.sqlserver.jdbc.SQLServerBulkRecord.addColumnMetadata(SQLServerBulkRecord.java:83)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeBatch(SQLServerPreparedStatement.java:2232)
	at com.microsoft.sqlserver.jdbc.unit.statement.BatchExecutionTest.testExecuteBatchColumnCaseMismatch_CS(BatchExecutionTest.java:632)

@Jeffery-Wasty Jeffery-Wasty added this to the 12.11.0 milestone Feb 20, 2025
@wooln wooln requested a review from mmimica February 27, 2025 01:50
@wooln
Copy link
Author

wooln commented Mar 14, 2025

@lilgreenbird trigger /azp run, pls.

Copy link

Commenter does not have sufficient privileges for PR 2589 in repo microsoft/mssql-jdbc

@wooln
Copy link
Author

wooln commented Mar 21, 2025

@Jeffery-Wasty , who can continue?

@lilgreenbird trigger /azp run, pls.

@Jeffery-Wasty
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@wooln
Copy link
Author

wooln commented Mar 24, 2025

@Jeffery-Wasty ,pipelines error. is it necessary to rebase main? the pipeline does in the code repository.

@azure-pipelines azure-pipelines Build log #L14
An unexpected error occurred while trying to download the package. Exit code(1) and error({"@t":"2025-03-21T16:08:27.5659272Z","@m":"An error occurred on the service. VS800075: The project with id 'vstfs:///Classification/TeamProject/436dae6e-dac6-40f4-8c2d-d1fb7a8f09f7' does not exist, or you do not have permission to access it.","@i":"daf054c7","@l":"Error","SourceContext":"ArtifactTool.Program","UtcTimestamp":"2025-03-21 16:08:27.565Z"})

@Jeffery-Wasty
Copy link
Contributor

@wooln This needs to be resolved on our end.

@machavan
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@wooln
Copy link
Author

wooln commented Mar 27, 2025

@wooln This needs to be resolved on our end.

@Jeffery-Wasty ,Oops, CI is still in error.

@machavan
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@wooln
Copy link
Author

wooln commented Mar 27, 2025

@Jeffery-Wasty @machavan , CI is stilllll in error.

wooln added 10 commits April 24, 2025 11:21
- Add test case for matching column case in batch execution
- Add test case for mismatched column case in batch execution, expected SQLServerException
- Update column index search to be case-insensitive in SQLServerPreparedStatement
- Remove failing test case in BatchExecutionTest that was checking for specific exception
- Add getIsCaseSensitive() method to SQLCollation class
- Update SQLServerPreparedStatement to use case sensitivity information
- Improve performance by avoiding unnecessary case-insensitive comparisons
- Add support for case-sensitive column names in SQLServerBulkBatchInsertRecord
- Update SQLServerBulkRecord to handle case sensitivity in column metadata
- Modify SQLServerPreparedStatement to pass case sensitivity information to batch records
- Add tests for case-sensitive and case-insensitive column name handling in batch execution
…abase in unit test.
@wooln wooln force-pushed the bugfix-execute-batch-case-sensitive branch from 6408cc4 to 0a77577 Compare April 24, 2025 03:22
@wooln
Copy link
Author

wooln commented Apr 24, 2025

@machavan, Needs review.

I've rebased main. What else do I need to do?

@machavan
Copy link
Contributor

Hi @wooln, We will review and get back on this PR.

Thanks

@@ -2193,7 +2193,7 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
}

SQLServerBulkBatchInsertRecord batchRecord = new SQLServerBulkBatchInsertRecord(
batchParamValues, bcOperationColumnList, bcOperationValueList, null);
batchParamValues, bcOperationColumnList, bcOperationValueList, null, connection.getDatabaseCollation().getIsCaseSensitive());
Copy link
Contributor

Choose a reason for hiding this comment

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

is databaseCollation always non-null and populated in Connection object ?( without involving an explicit round-trip)

Copy link
Author

Choose a reason for hiding this comment

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

At least in this place, the connection has opened and the collation has obtained.

To be prudent, the database in the connection string and the table operated on in the Statement may not necessarily be the same database (because the table name in SQL can have a database name prefix). I don't know how to deal with it.

* @return
*/
boolean getIsCaseSensitive() {
return sortOrderIndex.get(this.sortId).name.contains("_CS_");
Copy link
Contributor

Choose a reason for hiding this comment

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

is this a reliable way to check case sensitivity?

Copy link
Author

Choose a reason for hiding this comment

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

I couldn't find any other methods from Microsoft's documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Under Peer Review
6 participants