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 @@ -21,10 +21,12 @@
import com.facebook.presto.common.type.DecimalParseResult;
import com.facebook.presto.common.type.Decimals;
import com.facebook.presto.common.type.FunctionType;
import com.facebook.presto.common.type.MapType;
import com.facebook.presto.common.type.RowType;
import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.TypeSignatureParameter;
import com.facebook.presto.common.type.TypeUtils;
import com.facebook.presto.common.type.TypeWithName;
import com.facebook.presto.common.type.VarcharType;
import com.facebook.presto.metadata.FunctionAndTypeManager;
Expand All @@ -35,7 +37,6 @@
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.PrestoWarning;
import com.facebook.presto.spi.StandardErrorCode;
import com.facebook.presto.spi.StandardWarningCode;
import com.facebook.presto.spi.WarningCollector;
import com.facebook.presto.spi.function.FunctionHandle;
import com.facebook.presto.spi.function.FunctionMetadata;
Expand Down Expand Up @@ -142,6 +143,7 @@
import static com.facebook.presto.metadata.BuiltInTypeAndFunctionNamespaceManager.DEFAULT_NAMESPACE;
import static com.facebook.presto.metadata.CastType.CAST;
import static com.facebook.presto.metadata.FunctionAndTypeManager.qualifyObjectName;
import static com.facebook.presto.spi.StandardWarningCode.SEMANTIC_WARNING;
import static com.facebook.presto.sql.NodeUtils.getSortItemsFromOrderBy;
import static com.facebook.presto.sql.analyzer.Analyzer.verifyNoAggregateWindowOrGroupingFunctions;
import static com.facebook.presto.sql.analyzer.Analyzer.verifyNoExternalFunctions;
Expand Down Expand Up @@ -979,10 +981,30 @@ protected Type visitFunctionCall(FunctionCall node, StackableAstVisitorContext<C
throw new SemanticException(INVALID_PROCEDURE_ARGUMENTS, initialValueArg, "REDUCE_AGG only supports non-NULL literal as the initial value", initialValueArg);
}
}

Type type = functionAndTypeManager.getType(functionMetadata.getReturnType());

if (type instanceof MapType) {
Type keyType = ((MapType) type).getKeyType();
if (TypeUtils.isApproximateNumericType(keyType)) {
String warningMessage = createWarningMessage(node, "Map keys with real/double type can be non-deterministic. Please use decimal type instead");
warningCollector.add(new PrestoWarning(SEMANTIC_WARNING, warningMessage));
}
}

return setExpressionType(node, type);
}

private String createWarningMessage(Node node, String message)
{
if (node.getLocation().isPresent()) {
return format("%s Expression:%s line %s:%s", message, node, node.getLocation().get().getLineNumber(), node.getLocation().get().getColumnNumber());
}
else {
return format("%s Expression:%s", message, node);
}
}

@Override
protected Type visitAtTimeZone(AtTimeZone node, StackableAstVisitorContext<Context> context)
{
Expand Down Expand Up @@ -1416,7 +1438,7 @@ private void addOrReplaceExpressionCoercion(Expression expression, Type type, Ty
{
if (sqlFunctionProperties.isLegacyTypeCoercionWarningEnabled()) {
if ((type.getTypeSignature().getBase().equals(StandardTypes.DATE) || type.getTypeSignature().getBase().equals(StandardTypes.TIMESTAMP)) && superType.getTypeSignature().getBase().equals(StandardTypes.VARCHAR)) {
warningCollector.add(new PrestoWarning(StandardWarningCode.SEMANTIC_WARNING, format("This query relies on legacy semantic behavior that coerces date/timestamp to varchar. Expression: %s", expression)));
warningCollector.add(new PrestoWarning(SEMANTIC_WARNING, format("This query relies on legacy semantic behavior that coerces date/timestamp to varchar. Expression: %s", expression)));
}
}
NodeRef<Expression> ref = NodeRef.of(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static com.facebook.presto.spi.StandardWarningCode.MULTIPLE_ORDER_BY;
import static com.facebook.presto.spi.StandardWarningCode.PARSER_WARNING;
import static com.facebook.presto.spi.StandardWarningCode.PERFORMANCE_WARNING;
import static com.facebook.presto.spi.StandardWarningCode.SEMANTIC_WARNING;
import static com.facebook.presto.spi.StandardWarningCode.TOO_MANY_STAGES;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Sets.difference;
Expand Down Expand Up @@ -150,6 +151,57 @@ public void testMultipleOrderByWarnings()
queryRunner, TEST_SESSION, query, ImmutableSet.of(MULTIPLE_ORDER_BY.toWarningCode()));
}

@Test
public void testMapWithDoubleKeysProducesWarnings()
{
String query = "select map_from_entries(map_entries(MAP(ARRAY[12E2, 2.3, 3.4], ARRAY['x', 'y', 'z'])))";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));

query = "select CAST(MAP(ARRAY[7E2,5.2,3.3,1.1], ARRAY[8,6,4,2]) AS JSON)";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));

query = "select CARDINALITY(MAP(ARRAY [12E-2], ARRAY [2.2]))";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));

query = "select element_at(MAP(ARRAY [123e3], ARRAY [1e0]), 2)";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));

query = "select MAP(ARRAY [134E-2, 3.12], ARRAY [2.0E0, 4.0E0])[1.34]";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));

query = "select MAP_CONCAT(MAP(ARRAY [11E1], ARRAY [2.2]), MAP(ARRAY [5.1], ARRAY [33.2]))";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));

query = "select multimap_from_entries(ARRAY[(12E-2, 'x'), (2.3, 'y'), (1.2, 'a'), (3.4, 'b'), (2.3, 'c'), (3.4, null)])";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));

query = "select transform_keys(map(ARRAY [25.5E0, 26.5E0, 27.5E0], ARRAY [25.5E0, 26.5E0, 27.5E0]), (k, v) -> k + v)";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));

query = "SELECT histogram(RETAILPRICE) FROM tpch.tiny.part";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of(SEMANTIC_WARNING.toWarningCode()));
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.

love the test cases!

}

@Test
public void testMapWithNonDoubleKeyProducesNoWarnings()
{
String query = "select map_from_entries(map_entries(MAP(ARRAY[1, 2, 3], ARRAY['x', 'y', 'z'])))";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of());

query = "SELECT histogram(TYPE) FROM tpch.tiny.part";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of());
}

@Test
public void testMapWithDecimalKeyProducesNoWarnings()
{
String query = "select map_from_entries(map_entries(MAP(ARRAY[1.2, 2.3, 3.4], ARRAY['x', 'y', 'z'])))";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of());

query = "select CAST(MAP(ARRAY[7.2,5.2,3.3,1.1], ARRAY[8,6,4,2]) AS JSON)";
assertWarnings(queryRunner, TEST_SESSION, query, ImmutableSet.of());
}

private static void assertWarnings(QueryRunner queryRunner, Session session, @Language("SQL") String sql, Set<WarningCode> expectedWarnings)
{
Set<WarningCode> warnings = queryRunner.execute(session, sql).getWarnings().stream()
Expand Down