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 @@ -85,20 +85,25 @@ else if (standardFunctionResolution.isBetweenFunction(node.getFunctionHandle()))
}
else if (standardFunctionResolution.isLikeFunction(node.getFunctionHandle())) {
RowExpression value = node.getArguments().get(0);
CallExpression patternCallExpression = (CallExpression) node.getArguments().get(1);

// second LIKE argument is:
// CAST(pattern as LikePattern), if escape is not present
// LIKE_PATTERN(pattern, escape), if escape is present
if (standardFunctionResolution.isCastFunction(patternCallExpression.getFunctionHandle())) {
RowExpression pattern = patternCallExpression.getArguments().get(0);
return String.format("%s LIKE %s", formatRowExpression(session, value), formatRowExpression(session, pattern));
}
else {
RowExpression pattern = patternCallExpression.getArguments().get(0);
RowExpression escape = patternCallExpression.getArguments().get(1);
return String.format("%s LIKE %s ESCAPE %s", formatRowExpression(session, value), formatRowExpression(session, pattern), formatRowExpression(session, escape));
RowExpression patternRowExpression = node.getArguments().get(1);

if (patternRowExpression instanceof CallExpression) {
CallExpression patternCallExpression = (CallExpression) patternRowExpression;
// second LIKE argument is:
// CAST(pattern as LikePattern), if escape is not present
// LIKE_PATTERN(pattern, escape), if escape is present
if (standardFunctionResolution.isCastFunction(patternCallExpression.getFunctionHandle())) {
RowExpression pattern = patternCallExpression.getArguments().get(0);
return String.format("%s LIKE %s", formatRowExpression(session, value), formatRowExpression(session, pattern));
}
else if (standardFunctionResolution.isLikePatternFunction(patternCallExpression.getFunctionHandle())) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this belong inside the if? Looks like it's unrelated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those are created here.

RowExpression pattern = patternCallExpression.getArguments().get(0);
RowExpression escape = patternCallExpression.getArguments().get(1);
return String.format("%s LIKE %s ESCAPE %s", formatRowExpression(session, value), formatRowExpression(session, pattern), formatRowExpression(session, escape));
}
}

return String.format("%s LIKE %s", formatRowExpression(session, value), formatRowExpression(session, patternRowExpression));
}
FunctionMetadata metadata = functionMetadataManager.getFunctionMetadata(node.getFunctionHandle());
return node.getDisplayName() + (metadata.getVersion().hasVersion() ? ":" + metadata.getVersion() : "") + "(" + String.join(", ", formatRowExpressions(session, node.getArguments())) + ")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,24 @@ public FunctionHandle likeCharFunction(Type valueType)
return functionAndTypeManager.lookupFunction("LIKE", fromTypes(valueType, LIKE_PATTERN));
}

@Override
public boolean isLikeFunction(FunctionHandle functionHandle)
{
return functionAndTypeManager.getFunctionMetadata(functionHandle).getName().equals(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "LIKE"));
}

@Override
public FunctionHandle likePatternFunction()
{
return functionAndTypeManager.lookupFunction("LIKE_PATTERN", fromTypes(VARCHAR, VARCHAR));
}

@Override
public boolean isLikePatternFunction(FunctionHandle functionHandle)
{
return functionAndTypeManager.getFunctionMetadata(functionHandle).getName().equals(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "LIKE_PATTERN"));
}

@Override
public boolean isCastFunction(FunctionHandle functionHandle)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import com.facebook.presto.common.type.Type;
import com.facebook.presto.metadata.CastType;
import com.facebook.presto.metadata.FunctionAndTypeManager;
import com.facebook.presto.metadata.MetadataManager;
import com.facebook.presto.spi.relation.CallExpression;
import com.facebook.presto.spi.relation.RowExpression;
import com.facebook.presto.spi.relation.SpecialFormExpression;
import com.facebook.presto.spi.relation.VariableReferenceExpression;
import com.facebook.presto.sql.planner.planPrinter.RowExpressionFormatter;
import com.facebook.presto.sql.relational.FunctionResolution;
import com.facebook.presto.sql.relational.RowExpressionOptimizer;
import com.google.common.collect.ImmutableList;
import com.google.common.io.BaseEncoding;
import io.airlift.slice.Slice;
Expand Down Expand Up @@ -65,20 +67,23 @@
import static com.facebook.presto.common.type.VarbinaryType.VARBINARY;
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
import static com.facebook.presto.metadata.FunctionAndTypeManager.createTestFunctionAndTypeManager;
import static com.facebook.presto.spi.relation.ExpressionOptimizer.Level.OPTIMIZED;
import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.AND;
import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.IS_NULL;
import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.OR;
import static com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes;
import static com.facebook.presto.sql.relational.Expressions.call;
import static com.facebook.presto.sql.relational.Expressions.constant;
import static com.facebook.presto.sql.relational.Expressions.constantNull;
import static com.facebook.presto.testing.TestingConnectorSession.SESSION;
import static com.facebook.presto.type.ColorType.COLOR;
import static com.facebook.presto.type.IntervalDayTimeType.INTERVAL_DAY_TIME;
import static com.facebook.presto.type.IntervalYearMonthType.INTERVAL_YEAR_MONTH;
import static com.facebook.presto.type.LikePatternType.LIKE_PATTERN;
import static io.airlift.slice.Slices.utf8Slice;
import static java.lang.Float.floatToIntBits;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class TestRowExpressionFormatter
{
Expand All @@ -88,6 +93,7 @@ public class TestRowExpressionFormatter
private static final VariableReferenceExpression C_BIGINT = new VariableReferenceExpression("c_bigint", BIGINT);
private static final VariableReferenceExpression C_BIGINT_ARRAY = new VariableReferenceExpression("c_bigint_array", new ArrayType(BIGINT));
private static final VariableReferenceExpression C_VARCHAR = new VariableReferenceExpression("c_varchar", VARCHAR);
private static final RowExpressionOptimizer OPTIMIZER = new RowExpressionOptimizer(MetadataManager.createTestMetadataManager());

@Test
public void testConstants()
Expand Down Expand Up @@ -255,6 +261,9 @@ public void testCalls()
constant(utf8Slice("prefix%"), VARCHAR)));
assertEquals(format(callExpression), "c_varchar LIKE VARCHAR'prefix%'");

callExpression = OPTIMIZER.optimize(callExpression, OPTIMIZED, SESSION);
assertTrue(format(callExpression).startsWith("c_varchar LIKE LIKEPATTERN'io.airlift.joni.Regex@"));

// like escape
callExpression = call(
"LIKE",
Expand All @@ -268,6 +277,9 @@ public void testCalls()
constant(utf8Slice("%escaped$_"), VARCHAR),
constant(utf8Slice("$"), VARCHAR)));
assertEquals(format(callExpression), "c_varchar LIKE VARCHAR'%escaped$_' ESCAPE VARCHAR'$'");

callExpression = OPTIMIZER.optimize(callExpression, OPTIMIZED, SESSION);
assertTrue(format(callExpression).startsWith("c_varchar LIKE LIKEPATTERN'io.airlift.joni.Regex@"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public interface StandardFunctionResolution

boolean isLikeFunction(FunctionHandle functionHandle);

FunctionHandle likePatternFunction();

boolean isLikePatternFunction(FunctionHandle functionHandle);

FunctionHandle arrayConstructor(List<? extends Type> argumentTypes);

FunctionHandle arithmeticFunction(OperatorType operator, Type leftType, Type rightType);
Expand Down