Skip to content

Commit 5005482

Browse files
committed
Add performance warning for MAP_FILTER
1 parent bc7c50b commit 5005482

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

presto-main-base/src/main/java/com/facebook/presto/sql/analyzer/ExpressionAnalyzer.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.facebook.presto.common.type.MapType;
3131
import com.facebook.presto.common.type.RowType;
3232
import com.facebook.presto.common.type.Type;
33+
import com.facebook.presto.common.type.TypeSignature;
3334
import com.facebook.presto.common.type.TypeSignatureParameter;
3435
import com.facebook.presto.common.type.TypeUtils;
3536
import com.facebook.presto.common.type.TypeWithName;
@@ -154,6 +155,7 @@
154155
import static com.facebook.presto.common.type.VarcharType.VARCHAR;
155156
import static com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.JAVA_BUILTIN_NAMESPACE;
156157
import static com.facebook.presto.spi.StandardErrorCode.OPERATOR_NOT_FOUND;
158+
import static com.facebook.presto.spi.StandardWarningCode.PERFORMANCE_WARNING;
157159
import static com.facebook.presto.spi.StandardWarningCode.SEMANTIC_WARNING;
158160
import static com.facebook.presto.sql.NodeUtils.getSortItemsFromOrderBy;
159161
import static com.facebook.presto.sql.analyzer.Analyzer.verifyNoAggregateWindowOrGroupingFunctions;
@@ -1118,6 +1120,17 @@ else if (frame.getType() == GROUPS) {
11181120
}
11191121
}
11201122
}
1123+
List<TypeSignature> arguments = functionMetadata.getArgumentTypes();
1124+
1125+
if (!argumentTypes.isEmpty() && "map".equals(arguments.get(0).getBase())) {
1126+
arguments.stream()
1127+
.skip(1).map(arg -> arg.getBase())
1128+
.filter(base -> "function".equals(base) || "lambda".equals(base))
1129+
.forEach(base -> {
1130+
String warningMessage = createWarningMessage(node, "Lambdas on large maps are expensive, consider using map_subset");
1131+
warningCollector.add(new PrestoWarning(PERFORMANCE_WARNING, warningMessage));
1132+
});
1133+
}
11211134

11221135
if (node.isIgnoreNulls() && node.getWindow().isPresent()) {
11231136
if (!functionResolution.isWindowValueFunction(function)) {

presto-main-base/src/test/java/com/facebook/presto/sql/analyzer/TestAnalyzer.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,35 @@ void testNoORWarning()
152152
assertNoWarning(analyzeWithWarnings("SELECT * FROM t1 JOIN t2 ON t1.a = t2.a \n" + "AND (t1.b = t2.b OR t1.b > t2.b)"));
153153
}
154154

155+
@Test
156+
public void testMapFilterWarnings()
157+
{
158+
assertHasWarning(
159+
analyzeWithWarnings("SELECT map_filter(x, (k, v) -> v > 1) FROM (VALUES (map(ARRAY[1,2], ARRAY[2,3]))) AS t(x)"),
160+
PERFORMANCE_WARNING,
161+
"Lambdas on large maps are expensive, consider using map_subset");
162+
163+
assertHasWarning(
164+
analyzeWithWarnings("SELECT map_filter(x, (k, v) -> k = 2) FROM (VALUES (map(ARRAY[1,2,3], ARRAY[10,20,30]))) AS t(x)"),
165+
PERFORMANCE_WARNING,
166+
"Lambdas on large maps are expensive, consider using map_subset");
167+
168+
assertHasWarning(
169+
analyzeWithWarnings("SELECT map_filter(x, (k, v) -> k IN (1, 3)) FROM (VALUES (map(ARRAY[1,2,3], ARRAY[10,20,30]))) AS t(x)"),
170+
PERFORMANCE_WARNING,
171+
"Lambdas on large maps are expensive, consider using map_subset");
172+
173+
assertHasWarning(
174+
analyzeWithWarnings("SELECT map_filter(x, (k, v) -> v IN (20, 30)) FROM (VALUES (map(ARRAY[1,2,3], ARRAY[10,20,30]))) AS t(x)"),
175+
PERFORMANCE_WARNING,
176+
"Lambdas on large maps are expensive, consider using map_subset");
177+
178+
assertHasWarning(
179+
analyzeWithWarnings("SELECT map_filter(x, (k, v) -> k + v > 25) FROM (VALUES (map(ARRAY[1,2,3], ARRAY[10,20,30]))) AS t(x)"),
180+
PERFORMANCE_WARNING,
181+
"Lambdas on large maps are expensive, consider using map_subset");
182+
}
183+
155184
@Test
156185
public void testIgnoreNullWarning()
157186
{

0 commit comments

Comments
 (0)