Skip to content
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

HHH-19215 Extends Dialect#addQueryHints to support straight_join syntax #9825

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public abstract class Dialect implements ConversionContext, TypeContributor, Fun
private static final Pattern ESCAPE_CLOSING_COMMENT_PATTERN = Pattern.compile( "\\*/" );
private static final Pattern ESCAPE_OPENING_COMMENT_PATTERN = Pattern.compile( "/\\*" );
private static final Pattern QUERY_PATTERN = Pattern.compile(
"^\\s*(select\\b.+?\\bfrom\\b.+?)(\\b(?:natural )?(?:left |right |full )?(?:inner |outer |cross )?join.+?\\b)?(\\bwhere\\b.+?)$");
"^\\s*(select\\b.+?\\bfrom\\b.+?)(\\b(?:(?:natural )?(?:left |right |full )?(?:inner |outer |cross )?join |straight_join).+?\\b)?(\\bwhere\\b.+?)$");
Copy link
Member

Choose a reason for hiding this comment

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

It looks like the pattern might benefit from being more explicit about necessary whitespace:

Suggested change
"^\\s*(select\\b.+?\\bfrom\\b.+?)(\\b(?:(?:natural )?(?:left |right |full )?(?:inner |outer |cross )?join |straight_join).+?\\b)?(\\bwhere\\b.+?)$");
"^\\s*(select\\s.+?\\sfrom\\s.+?)(\\s(?:(?:natural\\s+)?(?:left\\s+|right\\s+|full\\s+)?(?:inner\\s|outer\\s|cross\\s)?join\\s|straight_join\\s).+?\\b)?(\\swhere\\s.+?)$");

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the suggestion.
You're right; the original regex can't handle some queries with new lines.

Based on your suggestion, I've modified the regex as follows:

  1. \\b -> \\s for improved robustness
  2. (?:A\\s|B\\s|C\\s)? -> (?:A|B|C)?\\s* for better readability


private static final CoreMessageLogger LOG = Logger.getMessageLogger( MethodHandles.lookup(), CoreMessageLogger.class, Dialect.class.getName() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ static Stream<Arguments> _addQueryHints() {
builder.add(Arguments.of("Left join query with on : hint",
"select COUNT(*) from TEST t1_0 use index (MY_INDEX) left join TEST2 t2_0 on t1_0.column2 = t2_0.column2 and t1_0.column3 = t2_0.column3 where field = 'value'",
leftJoinQuery, hints));
final String straightJoinQueryUsing = "select COUNT(*) from TEST t1_0 straight_join TEST2 t2_0 using(column2) where field = 'value'";
builder.add(Arguments.of("Straight join query with using : hint",
"select COUNT(*) from TEST t1_0 use index (MY_INDEX) straight_join TEST2 t2_0 using(column2) where field = 'value'",
straightJoinQueryUsing, hints));
final String straightJoinQueryOn = "select COUNT(*) from TEST t1_0 straight_join TEST2 t2_0 on t1_0.column2 = t2_0.column2 where field = 'value'";
builder.add(Arguments.of("Straight join query with on : hint",
"select COUNT(*) from TEST t1_0 use index (MY_INDEX) straight_join TEST2 t2_0 on t1_0.column2 = t2_0.column2 where field = 'value'",
straightJoinQueryOn, hints));

return builder.build();
}
Expand Down