-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Improve LIKE performance #13479
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
Improve LIKE performance #13479
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
core/trino-main/src/main/java/io/trino/likematcher/DFA.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.likematcher; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkState; | ||
| import static java.lang.String.format; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| record DFA(State start, State failed, List<State> states, Map<Integer, List<Transition>> transitions) | ||
| { | ||
| DFA | ||
| { | ||
| requireNonNull(start, "start is null"); | ||
| requireNonNull(failed, "failed is null"); | ||
| states = ImmutableList.copyOf(states); | ||
|
martint marked this conversation as resolved.
Outdated
|
||
| transitions = ImmutableMap.copyOf(transitions); | ||
| } | ||
|
|
||
| public List<Transition> transitions(State state) | ||
| { | ||
| return transitions.get(state.id); | ||
| } | ||
|
|
||
| record State(int id, String label, boolean accept) | ||
| { | ||
| @Override | ||
| public String toString() | ||
| { | ||
| return "%s:%s%s".formatted( | ||
| id, | ||
| accept ? "*" : "", | ||
| label); | ||
| } | ||
| } | ||
|
|
||
| record Transition(int value, State target) | ||
| { | ||
| @Override | ||
| public String toString() | ||
| { | ||
| return format("-[%s]-> %s", value, target); | ||
| } | ||
| } | ||
|
|
||
| public static class Builder | ||
| { | ||
| private int nextId; | ||
| private State start; | ||
| private State failed; | ||
| private final List<State> states = new ArrayList<>(); | ||
| private final Map<Integer, List<Transition>> transitions = new HashMap<>(); | ||
|
|
||
| public State addState(String label, boolean accept) | ||
| { | ||
| State state = new State(nextId++, label, accept); | ||
| states.add(state); | ||
| return state; | ||
| } | ||
|
|
||
| public State addStartState(String label, boolean accept) | ||
| { | ||
| checkState(start == null, "Start state already set"); | ||
| State state = addState(label, accept); | ||
| start = state; | ||
| return state; | ||
| } | ||
|
|
||
| public State addFailState() | ||
| { | ||
| checkState(failed == null, "Fail state already set"); | ||
| State state = addState("fail", false); | ||
| failed = state; | ||
| return state; | ||
| } | ||
|
|
||
| public void addTransition(State from, int value, State to) | ||
| { | ||
| transitions.computeIfAbsent(from.id(), key -> new ArrayList<>()) | ||
| .add(new Transition(value, to)); | ||
| } | ||
|
|
||
| public DFA build() | ||
| { | ||
| return new DFA(start, failed, states, transitions); | ||
| } | ||
| } | ||
| } | ||
114 changes: 114 additions & 0 deletions
114
core/trino-main/src/main/java/io/trino/likematcher/DenseDfaMatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.likematcher; | ||
|
|
||
| class DenseDfaMatcher | ||
| { | ||
| // The DFA is encoded as a sequence of transitions for each possible byte value for each state. | ||
| // I.e., 256 transitions per state. | ||
| // The content of the transitions array is the base offset into | ||
| // the next state to follow. I.e., the desired state * 256 | ||
|
martint marked this conversation as resolved.
Outdated
|
||
| private final int[] transitions; | ||
|
|
||
| // The starting state | ||
| private final int start; | ||
|
|
||
| // For each state, whether it's an accepting state | ||
| private final boolean[] accept; | ||
|
|
||
| // Artificial state to sink all invalid matches | ||
| private final int fail; | ||
|
|
||
| private final boolean exact; | ||
|
|
||
| /** | ||
| * @param exact whether to match to the end of the input | ||
| */ | ||
| public static DenseDfaMatcher newInstance(DFA dfa, boolean exact) | ||
| { | ||
| int[] transitions = new int[dfa.states().size() * 256]; | ||
| boolean[] accept = new boolean[dfa.states().size()]; | ||
|
|
||
| for (DFA.State state : dfa.states()) { | ||
| for (DFA.Transition transition : dfa.transitions(state)) { | ||
| transitions[state.id() * 256 + transition.value()] = transition.target().id() * 256; | ||
| } | ||
|
|
||
| if (state.accept()) { | ||
| accept[state.id()] = true; | ||
| } | ||
| } | ||
|
|
||
| return new DenseDfaMatcher(transitions, dfa.start().id(), accept, 0, exact); | ||
| } | ||
|
|
||
| private DenseDfaMatcher(int[] transitions, int start, boolean[] accept, int fail, boolean exact) | ||
| { | ||
| this.transitions = transitions; | ||
|
martint marked this conversation as resolved.
Outdated
|
||
| this.start = start; | ||
| this.accept = accept; | ||
| this.fail = fail; | ||
| this.exact = exact; | ||
| } | ||
|
|
||
| public boolean match(byte[] input, int offset, int length) | ||
| { | ||
| if (exact) { | ||
| return exactMatch(input, offset, length); | ||
| } | ||
|
|
||
| return prefixMatch(input, offset, length); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a positive match when the final state after all input has been consumed is an accepting state | ||
| */ | ||
| private boolean exactMatch(byte[] input, int offset, int length) | ||
| { | ||
| int state = start << 8; | ||
| for (int i = offset; i < offset + length; i++) { | ||
| byte inputByte = input[i]; | ||
| state = transitions[state | (inputByte & 0xFF)]; | ||
|
|
||
| if (state == fail) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return accept[state >>> 8]; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a positive match as soon as the DFA reaches an accepting state, regardless of whether | ||
| * the whole input has been consumed | ||
| */ | ||
| private boolean prefixMatch(byte[] input, int offset, int length) | ||
| { | ||
| int state = start << 8; | ||
| for (int i = offset; i < offset + length; i++) { | ||
| byte inputByte = input[i]; | ||
| state = transitions[state | (inputByte & 0xFF)]; | ||
|
|
||
| if (state == fail) { | ||
| return false; | ||
| } | ||
|
|
||
| if (accept[state >>> 8]) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return accept[state >>> 8]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.