Skip to content
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
106 changes: 106 additions & 0 deletions core/trino-main/src/main/java/io/trino/likematcher/DFA.java
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)
Comment thread
martint marked this conversation as resolved.
Outdated
{
DFA
{
requireNonNull(start, "start is null");
requireNonNull(failed, "failed is null");
states = ImmutableList.copyOf(states);
Comment thread
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);
}
}
}
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
Comment thread
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;
Comment thread
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];
}
}
Loading