Skip to content

feat: view index on select from #140

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

Merged
merged 5 commits into from
Aug 20, 2024
Merged
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 .github/workflows/ci-hibernate-dialect.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

strategy:
matrix:
java: [ '17' ]
java: [ '17', '21' ]

steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 4 additions & 0 deletions hibernate-dialect/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.5 ##

- Added query hint for view index for "select * from ... where" queries

## 0.9.4 ##

- Fixed the generated bool value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.ydb.hibernate.dialect;

import java.time.LocalDateTime;
import java.util.List;
import org.hibernate.boot.model.FunctionContributions;
import org.hibernate.boot.model.TypeContributions;
import org.hibernate.dialect.Dialect;
Expand Down Expand Up @@ -51,6 +52,7 @@
import org.hibernate.type.StandardBasicTypes;
import tech.ydb.hibernate.dialect.exporter.EmptyExporter;
import tech.ydb.hibernate.dialect.exporter.YdbIndexExporter;
import tech.ydb.hibernate.dialect.hint.IndexQueryHintHandler;
import tech.ydb.hibernate.dialect.translator.YdbSqlAstTranslatorFactory;
import tech.ydb.hibernate.dialect.types.LocalDateTimeJavaType;
import tech.ydb.hibernate.dialect.types.LocalDateTimeJdbcType;
Expand Down Expand Up @@ -118,6 +120,11 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
);
}

@Override
public String getQueryHintString(String query, List<String> hintList) {
return IndexQueryHintHandler.INSTANCE.addQueryHints(query, hintList);
}

@Override
public LimitHandler getLimitHandler() {
return LimitOffsetLimitHandler.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tech.ydb.hibernate.dialect.hint;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author Kirill Kurdyukov
*/
public class IndexQueryHintHandler {
private static final Pattern SELECT_FROM_WHERE_QUERY_PATTERN = Pattern
.compile("^\\s*(select.+?from\\s+\\w+)(.+where.+)$", Pattern.CASE_INSENSITIVE);

public static final String HINT_USE_INDEX = "use_index:";
public static final IndexQueryHintHandler INSTANCE = new IndexQueryHintHandler();

public String addQueryHints(String query, List<String> hints) {
if (hints.isEmpty()) {
return query;
}

var useIndexes = new ArrayList<String>();
hints.forEach(hint -> {
if (hint.startsWith(HINT_USE_INDEX)) {
useIndexes.add(hint.substring(HINT_USE_INDEX.length()));
}
});

if (!useIndexes.isEmpty()) {
Matcher matcher = SELECT_FROM_WHERE_QUERY_PATTERN.matcher(query);
if (matcher.matches() && matcher.groupCount() > 1) {
String startToken = matcher.group(1);
String endToken = matcher.group(2);

return startToken + " view " + String.join(", ", useIndexes) + endToken;
}
}

return query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ void studentsAndCourses_Lazy_ManyToManyTest() {
);
}


@Test
void studentsByGroupName_Lazy_OneToManyTest() {
inTransaction(
Expand All @@ -160,6 +159,20 @@ void studentsByGroupName_Lazy_OneToManyTest() {
);
}

@Test
void groupByGroupName_ViewIndex() {
inTransaction(
session -> {
Group group = session
.createQuery("FROM Group g WHERE g.name = 'M3439'", Group.class)
.addQueryHint("use_index:group_name_index")
.getSingleResult();

assertEquals("M3439", group.getName());
}
);
}

@Test
void studentsByGroupName_Eager_OneToManyTest() {
inTransaction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import jakarta.persistence.NamedQuery;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import java.util.List;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
* @author Kirill Kurdyukov
*/
Expand Down