Skip to content

Conversation

stringintech
Copy link
Contributor

@stringintech stringintech commented Nov 10, 2024

This PR resolves an ID conflict in Hibernate's CteInsertHandler batch insert implementation when using sequence-based ID generation with the PooledOptimizer. The issue arises from a misalignment between the batch insert ID generation strategy and the PooledOptimizer strategy, which can lead to ID conflicts.

The fix ensures that CteInsertHandler correctly integrates with the PooledOptimizer's ID allocation strategy by properly handling the next database-generated sequence value as the "hi" value, thereby maintaining consistent ID allocation across all insert operations.

A unit test (CteInsertStrategyWithPooledOptimizerTest) is included to validate the fix.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.


https://hibernate.atlassian.net/browse/HHH-18818

@beikov
Copy link
Member

beikov commented Dec 27, 2024

Thanks a lot for the test and fix. I just now realized that other optimizer strategies might also run into this problem.
I think it would be best to extend Optimizer to allow generating the AST for the low/base value, since e.g. the HiLoOptimizer has a completely different approach that isn't as simple as conditionally subtracting the increment.

Would you be willing to implement that and also add tests for the other optimizers?

Expression createLowValueExpression(Expression databaseValue, SessionFactoryImplementor sessionFactory);

@stringintech
Copy link
Contributor Author

@beikov Thanks for the feedback. I'd be happy to do it. I'll go over the different optimizer strategies, and I'll reach out if I have any questions during implementation.

@stringintech stringintech changed the title HHH-18818 Fix ID conflicts between CTE batch inserts and PooledOptimizer sequence allocation HHH-18818 Fix ID conflicts between CTE batch inserts and optimizer strategies Jan 1, 2025
@stringintech
Copy link
Contributor Author

@beikov I've implemented the requested createLowValueExpression() method in the Optimizer interface, along with corresponding tests for proper ID allocation behavior across optimizer strategies. I'd appreciate your review and am ready to make adjustments based on your feedback.

@yrodiere
Copy link
Member

Hey @beikov, it seems this is waiting for your feedback?

@beikov
Copy link
Member

beikov commented Jul 29, 2025

Thanks a lot for the work @stringintech and sorry it took so long for me to work on this. I created #10650 now which is a rebased version of your PR with some small cosmetic fixes. Going to close this PR in favor of the other and push that through the finish line.

@beikov beikov closed this Jul 29, 2025
@stringintech
Copy link
Contributor Author

You're welcome @beikov.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants