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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ call
;

expressionCapture
: identifier ':' type
: identifier (':' type)?
;

type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.trino.spi.expression.ConnectorExpression;

import java.util.Objects;
import java.util.Optional;

import static io.trino.matching.Capture.newCapture;
import static io.trino.plugin.base.expression.ConnectorExpressionPatterns.type;
Expand All @@ -29,17 +30,30 @@ public class ExpressionCapture
extends ExpressionPattern
{
private final String name;
private final TypePattern type;
private final Optional<TypePattern> type;

private final Capture<ConnectorExpression> capture = newCapture();
private final Pattern<ConnectorExpression> pattern;

public ExpressionCapture(String name)
{
this(name, Optional.empty());
}

public ExpressionCapture(String name, TypePattern type)
{
this(name, Optional.of(type));
}

public ExpressionCapture(String name, Optional<TypePattern> type)
{
this.name = requireNonNull(name, "name is null");
this.type = requireNonNull(type, "type is null");
this.pattern = Pattern.typeOf(ConnectorExpression.class).capturedAs(capture)
.with(type().matching(type.getPattern()));
Pattern<ConnectorExpression> pattern = Pattern.typeOf(ConnectorExpression.class).capturedAs(capture);
if (type.isPresent()) {
pattern = pattern.with(type().matching(type.get().getPattern()));
}
this.pattern = pattern;
}

@Override
Expand All @@ -52,7 +66,7 @@ public Pattern<ConnectorExpression> getPattern()
public void resolve(Captures captures, MatchContext matchContext)
{
matchContext.record(name, captures.get(capture));
type.resolve(captures, matchContext);
type.ifPresent(type -> type.resolve(captures, matchContext));
}

@Override
Expand All @@ -78,6 +92,9 @@ public int hashCode()
@Override
public String toString()
{
return format("%s: %s", name, type);
if (type.isEmpty()) {
return name;
}
return format("%s: %s", name, type.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ExpressionPattern visitExpressionCapture(ConnectorExpressionPatternParser
{
return new ExpressionCapture(
visit(context.identifier(), String.class),
visit(context.type(), TypePattern.class));
visitIfPresent(context.type(), TypePattern.class));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class TestExpressionMappingParser
@Test
public void testCapture()
{
assertExpressionPattern("b", new ExpressionCapture("b"));

assertExpressionPattern(
"b: bigint",
new ExpressionCapture(
Expand All @@ -52,6 +54,13 @@ public void testParameterizedType()
@Test
public void testCallPattern()
{
assertExpressionPattern(
"is_null(a)",
new CallPattern(
"is_null",
List.of(new ExpressionCapture("a")),
Optional.empty()));

assertExpressionPattern(
"$like_pattern(a: varchar(n), b: varchar(m))",
new CallPattern(
Expand Down