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
6 changes: 6 additions & 0 deletions presto-docs/src/main/sphinx/functions/map.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ Map Functions
MAP(ARRAY['a', 'b', 'c'], ARRAY[1, 2, 3]),
(k, v1, v2) -> k || CAST(v1/v2 AS VARCHAR));

.. function:: no_keys_match(x(K,V), function(K, boolean)) -> boolean

Returns whether no keys of a map match the given predicate. Returns true if none of the keys match the predicate (a special case is when the map is empty); false if one or more keys match; NULL if the predicate function returns NULL for one or more keys and false for all other keys. ::

SELECT no_keys_match(map(array['a', 'b', 'c'], array[1, 2, 3]), x -> x = 'd'); -- true

.. function:: transform_keys(map(K1,V), function(K1,V,K2)) -> map(K2,V)

Returns a map that applies ``function`` to each entry of ``map`` and transforms the keys::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,15 @@ public static String anyKeysMatch()
{
return "RETURN ANY_MATCH(MAP_KEYS(input), f)";
}

@SqlInvokedScalarFunction(value = "no_keys_match", deterministic = true, calledOnNullInput = true)
@Description("Returns whether no keys of a map match the given predicate.")
@TypeParameter("K")
@TypeParameter("V")
@SqlParameters({@SqlParameter(name = "input", type = "map(K, V)"), @SqlParameter(name = "f", type = "function(K, boolean)")})
@SqlType("boolean")
public static String noKeysMatch()
{
return "RETURN NONE_MATCH(MAP_KEYS(input), f)";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.operator.scalar.sql;

import com.facebook.presto.operator.scalar.AbstractTestFunctions;
import com.facebook.presto.sql.analyzer.SemanticErrorCode;
import org.testng.annotations.Test;

import static com.facebook.presto.common.type.BooleanType.BOOLEAN;

public class TestNoKeysMatchFunction
extends AbstractTestFunctions
{
@Test
public void testBasic()
{
assertFunction(
"NO_KEYS_MATCH(MAP(ARRAY[1, 2, 3], ARRAY[4, 5, 6]), (x) -> x > 3)",
BOOLEAN,
true);
assertFunction(
"NO_KEYS_MATCH(MAP(ARRAY[-1, -2, -3], ARRAY[4, 5, 6]), (x) -> x < -2)",
BOOLEAN,
false);
assertFunction(
"NO_KEYS_MATCH(MAP(ARRAY['ab', 'bc', 'cd'], ARRAY['x', 'y', 'z']), (x) -> x IS NULL)",
BOOLEAN,
true);
assertFunction(
"NO_KEYS_MATCH(MAP(ARRAY[123.0, 99.5, 1000.99], ARRAY['x', 'y', 'z']), (x) -> x > 1.0)",
BOOLEAN,
false);
}

@Test
public void testEmpty()
{
assertFunction("NO_KEYS_MATCH(MAP(ARRAY[], ARRAY[]), (x) -> x > 0)", BOOLEAN, true);
assertFunction("NO_KEYS_MATCH(MAP(ARRAY[], ARRAY[]), (x) -> x IS NULL)", BOOLEAN, true);
assertFunction("NO_KEYS_MATCH(MAP(), (x) -> FALSE)", BOOLEAN, true);
}

@Test
public void testNull()
{
assertFunction("NO_KEYS_MATCH(NULL, (x) -> x LIKE '%ab%')", BOOLEAN, null);
assertFunction("NO_KEYS_MATCH(MAP(ARRAY['x', 'y'], ARRAY[1, 2]), (x) -> CAST(NULL AS BOOLEAN))", BOOLEAN, null);
assertFunction("NO_KEYS_MATCH(MAP(ARRAY['x', 'y'], ARRAY[1, 2]), (x) -> IF(x = 'x', false, CAST(NULL AS BOOLEAN)))", BOOLEAN, null);
assertFunction("NO_KEYS_MATCH(MAP(ARRAY['x', 'y', 'z'], ARRAY[1, 2, 3]), (x) -> IF(x IN ('x', 'y'), false, CAST(NULL AS BOOLEAN)))", BOOLEAN, null);
assertFunction("NO_KEYS_MATCH(MAP(ARRAY['x', 'y', 'z'], ARRAY[1, 2, 3]), (x) -> IF(x = 'x', false, IF(x = 'y', true, CAST(NULL AS BOOLEAN))))", BOOLEAN, false);
}

@Test
public void testComplexKeys()
{
assertFunction(
"NO_KEYS_MATCH(MAP(ARRAY[ROW('x', 1), ROW('y', 2)], ARRAY[1, 2]), (x) -> x[1] = 'x')",
BOOLEAN,
false);
assertFunction(
"NO_KEYS_MATCH(MAP(ARRAY[ROW('x', 1), ROW('x', -2)], ARRAY[2, 1]), (x) -> x[2] >= 2)",
BOOLEAN,
true);
assertFunction(
"NO_KEYS_MATCH(MAP(ARRAY[ROW('x', 1), ROW('x', -2), ROW('y', 1)], ARRAY[100, 200, null]), (x) -> x[1] = 'ab')",
BOOLEAN,
true);
}

@Test
public void testError()
{
assertInvalidFunction(
"NO_KEYS_MATCH(MAP(ARRAY[ROW('x', 1), ROW('y', 2)], ARRAY[1, 2]), (x) -> x[2] LIKE '%ab%')",
SemanticErrorCode.TYPE_MISMATCH);
assertInvalidFunction(
"NO_KEYS_MATCH(MAP(ARRAY[1, 2, 3], ARRAY[4, 5, 6]))",
SemanticErrorCode.FUNCTION_NOT_FOUND);
assertInvalidFunction(
"NO_KEYS_MATCH(MAP(ARRAY['a', 'b', 'c'], ARRAY[4, 5, 6]), 1)",
SemanticErrorCode.FUNCTION_NOT_FOUND);
}
}