From cf02306ea168e1e7a4d3579278bb499a104f968b Mon Sep 17 00:00:00 2001 From: Kevin Wilfong Date: Mon, 9 Dec 2024 09:55:52 -0800 Subject: [PATCH] fix: Only register inequality comparisons for orderable types (#11784) Summary: Inequalities only work with types that are orderable pretty much by definition. For maps in particular we allow users to call map1 < map2 but it will always throw because the map type does not support inequalities unless null is treated as a value (which it is not in GenericView's compare function which is what gets invoked). This will address https://github.com/facebookincubator/velox/issues/11549 where inequalities on maps can be generated in fuzzers and result in exceptions. Reviewed By: xiaoxmeng Differential Revision: D66909744 --- .../ComparisonFunctionsRegistration.cpp | 10 ++++++---- .../prestosql/tests/ComparisonsTest.cpp | 18 ------------------ 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/velox/functions/prestosql/registration/ComparisonFunctionsRegistration.cpp b/velox/functions/prestosql/registration/ComparisonFunctionsRegistration.cpp index 2870a3ff787f..f167d0e3e330 100644 --- a/velox/functions/prestosql/registration/ComparisonFunctionsRegistration.cpp +++ b/velox/functions/prestosql/registration/ComparisonFunctionsRegistration.cpp @@ -52,20 +52,22 @@ void registerComparisonFunctions(const std::string& prefix) { registerNonSimdizableScalar({prefix + "lt"}); VELOX_REGISTER_VECTOR_FUNCTION(udf_simd_comparison_lt, prefix + "lt"); - registerFunction, Generic>({prefix + "lt"}); + registerFunction, Orderable>( + {prefix + "lt"}); registerNonSimdizableScalar({prefix + "gt"}); VELOX_REGISTER_VECTOR_FUNCTION(udf_simd_comparison_gt, prefix + "gt"); - registerFunction, Generic>({prefix + "gt"}); + registerFunction, Orderable>( + {prefix + "gt"}); registerNonSimdizableScalar({prefix + "lte"}); VELOX_REGISTER_VECTOR_FUNCTION(udf_simd_comparison_lte, prefix + "lte"); - registerFunction, Generic>( + registerFunction, Orderable>( {prefix + "lte"}); registerNonSimdizableScalar({prefix + "gte"}); VELOX_REGISTER_VECTOR_FUNCTION(udf_simd_comparison_gte, prefix + "gte"); - registerFunction, Generic>( + registerFunction, Orderable>( {prefix + "gte"}); registerFunction, Generic>( diff --git a/velox/functions/prestosql/tests/ComparisonsTest.cpp b/velox/functions/prestosql/tests/ComparisonsTest.cpp index 707a61d108fa..01d5cab90f6e 100644 --- a/velox/functions/prestosql/tests/ComparisonsTest.cpp +++ b/velox/functions/prestosql/tests/ComparisonsTest.cpp @@ -724,24 +724,6 @@ TEST_F(ComparisonsTest, gtLtArray) { testCompareArray({NaN}, {NaN, 3.0}, expectedResults); } -TEST_F(ComparisonsTest, gtLtMap) { - // Comparing maps is not supported for any types. - auto expectedException = "Map is not orderable type"; - - std::optional>>> map1 = - {{{1, 2}, {3, 4}}}; - std::optional>>> map2 = - {{{1, 2}, {3, 4}}}; - testCompareMap(map1, map2, expectedException); - - static const auto NaN = std::numeric_limits::quiet_NaN(); - std::optional>>> map3 = { - {{1.0, 2.0}, {NaN, 4.0}}}; - std::optional>>> map4 = { - {{1.0, 2.0}, {3, 4.0}}}; - testCompareMap(map3, map4, expectedException); -} - TEST_F(ComparisonsTest, distinctFrom) { auto input = makeRowVector({ makeNullableFlatVector({3, 1, 1, std::nullopt, std::nullopt}),