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

Use java-string-similarity and JaroWinkler for fuzzy matching #25345

Merged
merged 1 commit into from
Mar 18, 2025
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
18 changes: 12 additions & 6 deletions core/trino-main/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@
<artifactId>failsafe</artifactId>
</dependency>

<dependency>
<groupId>info.debatty</groupId>
<artifactId>java-string-similarity</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.airlift</groupId>
<artifactId>aircompressor-v3</artifactId>
Expand Down Expand Up @@ -329,12 +341,6 @@
<artifactId>joda-time</artifactId>
</dependency>

<dependency>
<groupId>me.xdrop</groupId>
<artifactId>fuzzywuzzy</artifactId>
<version>1.4.0</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package io.trino.execution;

import com.google.inject.Inject;
import info.debatty.java.stringsimilarity.JaroWinkler;
import info.debatty.java.stringsimilarity.interfaces.StringSimilarity;
import io.trino.Session;
import io.trino.metadata.SessionPropertyManager;
import io.trino.security.AccessControl;
Expand All @@ -28,7 +30,6 @@
import io.trino.sql.tree.NodeRef;
import io.trino.sql.tree.Parameter;
import io.trino.sql.tree.QualifiedName;
import me.xdrop.fuzzywuzzy.FuzzySearch;

import java.util.List;
import java.util.Map;
Expand All @@ -44,11 +45,13 @@
import static io.trino.spi.StandardErrorCode.INVALID_SESSION_PROPERTY;
import static io.trino.sql.analyzer.SemanticExceptions.semanticException;
import static java.lang.String.format;
import static java.util.Comparator.comparingInt;
import static java.util.Comparator.comparingDouble;
import static java.util.Objects.requireNonNull;

public class SessionPropertyEvaluator
{
private static final StringSimilarity SIMILARITY = new JaroWinkler();

private final PlannerContext plannerContext;
private final AccessControl accessControl;
private final SessionPropertyManager sessionPropertyManager;
Expand Down Expand Up @@ -120,20 +123,20 @@ public static List<PropertyMetadata<?>> findSimilar(String propertyName, Set<Pro
{
return candidates.stream()
.filter(property -> !property.isHidden())
.map(candidate -> new Match(candidate, FuzzySearch.ratio(candidate.getName(), propertyName)))
.filter(match -> match.ratio() > 75)
.sorted(comparingInt(Match::ratio).reversed())
.map(candidate -> new Match(candidate, SIMILARITY.similarity(candidate.getName(), propertyName)))
.filter(match -> match.ratio() > 0.85)
.sorted(comparingDouble(Match::ratio).reversed())
.limit(count)
.map(Match::metadata)
.collect(toImmutableList());
}

private record Match(PropertyMetadata<?> metadata, int ratio)
private record Match(PropertyMetadata<?> metadata, double ratio)
{
public Match
{
requireNonNull(metadata, "metadata is null");
verify(ratio >= 0 && ratio < 100, "ratio must be in the [0, 100) range");
verify(ratio >= 0.0 && ratio <= 1.0, "ratio must be in the [0, 1.0] range");
}
}

Expand Down