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

Add like_regex predicate to JSON path #24616

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import io.trino.json.ir.IrExistsPredicate;
import io.trino.json.ir.IrIsUnknownPredicate;
import io.trino.json.ir.IrJsonPathVisitor;
import io.trino.json.ir.IrLikeRegexPredicate;
import io.trino.json.ir.IrNegationPredicate;
import io.trino.json.ir.IrPathNode;
import io.trino.json.ir.IrPredicate;
import io.trino.json.ir.IrStartsWithPredicate;
import io.trino.json.ir.JsonLiteralConversionException;
import io.trino.json.ir.TypedValue;
import io.trino.json.regex.XQuerySqlRegex;
import io.trino.operator.scalar.StringFunctions;
import io.trino.spi.function.OperatorType;
import io.trino.spi.type.CharType;
Expand Down Expand Up @@ -381,6 +383,42 @@ protected Boolean visitIrIsUnknownPredicate(IrIsUnknownPredicate node, PathEvalu
return predicateResult == null;
}

@Override
protected Boolean visitIrLikeRegexPredicate(IrLikeRegexPredicate node, PathEvaluationContext context)
{
List<Object> valueSequence;
try {
valueSequence = pathVisitor.process(node.value(), context);
}
catch (PathEvaluationException e) {
return null;
}

if (lax) {
valueSequence = unwrapArrays(valueSequence);
}
if (valueSequence.isEmpty()) {
return FALSE;
}

XQuerySqlRegex regex = node.regex();
boolean found = false;
for (Object object : valueSequence) {
Slice value = getText(object);
if (value == null) {
return null;
}
if (regex.match(value)) {
found = true;
if (lax) {
return TRUE;
}
}
}

return found;
}

@Override
protected Boolean visitIrNegationPredicate(IrNegationPredicate node, PathEvaluationContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ protected R visitIrLastIndexVariable(IrLastIndexVariable node, C context)
return visitIrPathNode(node, context);
}

protected R visitIrLikeRegexPredicate(IrLikeRegexPredicate node, C context)
{
return visitIrPredicate(node, context);
}

protected R visitIrLiteral(IrLiteral node, C context)
{
return visitIrPathNode(node, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.json.ir;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.errorprone.annotations.DoNotCall;
import io.trino.json.regex.XQuerySqlRegex;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

public record IrLikeRegexPredicate(IrPathNode value, @JsonIgnore XQuerySqlRegex regex)
implements IrPredicate
{
public IrLikeRegexPredicate
{
requireNonNull(value, "value is null");
requireNonNull(regex, "regex is null");
}

@JsonCreator
@DoNotCall
public static IrLikeRegexPredicate fromJson(
@JsonProperty("value") IrPathNode value,
@JsonProperty("pattern") String pattern,
@JsonProperty("flags") Optional<String> flags)
{
XQuerySqlRegex regex = XQuerySqlRegex.compile(pattern, flags);
return new IrLikeRegexPredicate(value, regex);
}

@Override
public <R, C> R accept(IrJsonPathVisitor<R, C> visitor, C context)
{
return visitor.visitIrLikeRegexPredicate(this, context);
}

@JsonProperty
public IrPathNode getValue()
{
return value;
}

@JsonProperty
public String getPattern()
{
return regex.getPattern();
}

@JsonProperty
public Optional<String> getFlags()
{
return regex.getFlags();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
@JsonSubTypes.Type(value = IrJsonNull.class, name = "jsonnull"),
@JsonSubTypes.Type(value = IrKeyValueMethod.class, name = "keyvalue"),
@JsonSubTypes.Type(value = IrLastIndexVariable.class, name = "last"),
@JsonSubTypes.Type(value = IrLikeRegexPredicate.class, name = "likeregex"),
@JsonSubTypes.Type(value = IrLiteral.class, name = "literal"),
@JsonSubTypes.Type(value = IrMemberAccessor.class, name = "memberaccessor"),
@JsonSubTypes.Type(value = IrNamedJsonVariable.class, name = "namedjsonvariable"),
Expand Down
10 changes: 9 additions & 1 deletion core/trino-main/src/main/java/io/trino/json/ir/IrPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@

public sealed interface IrPredicate
extends IrPathNode
permits IrComparisonPredicate, IrConjunctionPredicate, IrDisjunctionPredicate, IrExistsPredicate, IrIsUnknownPredicate, IrNegationPredicate, IrStartsWithPredicate
permits
IrComparisonPredicate,
IrConjunctionPredicate,
IrDisjunctionPredicate,
IrExistsPredicate,
IrIsUnknownPredicate,
IrLikeRegexPredicate,
IrNegationPredicate,
IrStartsWithPredicate
{
@Override
default <R, C> R accept(IrJsonPathVisitor<R, C> visitor, C context)
Expand Down
28 changes: 28 additions & 0 deletions core/trino-main/src/main/java/io/trino/json/regex/Atom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.json.regex;

sealed interface Atom
extends RegexNode
permits
BackReference,
CharacterClassEscape,
CharacterClassExpression,
EndMetaCharacter,
NormalCharacter,
StartMetaCharacter,
SubExpression,
WildcardEscape
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.json.regex;

import static com.google.common.base.Preconditions.checkArgument;

record BackReference(int subExpressionIndex)
implements Atom
{
public BackReference
{
checkArgument(subExpressionIndex >= 1, "subExpressionIndex must be >= 1");
}

@Override
public <R> R accept(RegexTreeVisitor<R> visitor)
{
return visitor.visitBackReference(this);
}
}
35 changes: 35 additions & 0 deletions core/trino-main/src/main/java/io/trino/json/regex/Branch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.json.regex;

import com.google.common.collect.ImmutableList;

import java.util.List;

import static java.util.Objects.requireNonNull;

record Branch(List<Piece> pieces)
implements RegexNode
{
public Branch
{
pieces = ImmutableList.copyOf(requireNonNull(pieces, "pieces is null"));
}

@Override
public <R> R accept(RegexTreeVisitor<R> visitor)
{
return visitor.visitBranch(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.json.regex;

import static java.util.Objects.requireNonNull;

record CategoryEscape(String characterProperty, boolean complement)
implements CharacterClassEscape
{
public CategoryEscape
{
requireNonNull(characterProperty, "characterProperty is null");
}

@Override
public <R> R accept(RegexTreeVisitor<R> visitor)
{
return visitor.visitCategoryEscape(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.json.regex;

sealed interface CharacterClassEscape
extends Atom, CharacterGroupElement
permits CategoryEscape, MultiCharacterEscape, SingleCharacterEscape
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.json.regex;

import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

record CharacterClassExpression(boolean negative, List<CharacterGroupElement> elements, Optional<CharacterClassExpression> subtrahend)
implements Atom
{
public CharacterClassExpression
{
elements = ImmutableList.copyOf(requireNonNull(elements, "elements is null"));
requireNonNull(subtrahend, "subtrahend is null");
}

@Override
public <R> R accept(RegexTreeVisitor<R> visitor)
{
return visitor.visitCharacterClassExpression(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.json.regex;

sealed interface CharacterGroupElement
extends RegexNode
permits CharacterClassEscape, CharacterRange, NormalCharacter
{
}
Loading
Loading