Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.metadata;
 
 
 import java.util.List;
 
 import static com.facebook.presto.metadata.FunctionRegistry.getMagicLiteralFunctionSignature;
 import static com.facebook.presto.metadata.FunctionRegistry.mangleOperatorName;
 import static com.facebook.presto.metadata.FunctionRegistry.unmangleOperator;
 import static com.facebook.presto.spi.type.HyperLogLogType.HYPER_LOG_LOG;
 import static com.facebook.presto.spi.type.TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE;
 import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
 import static com.facebook.presto.type.TypeUtils.resolveTypes;
 import static com.facebook.presto.util.ImmutableCollectors.toImmutableList;
 import static com.google.common.collect.Lists.transform;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
 public class TestFunctionRegistry
 {
     @Test
     public void testIdentityCast()
     {
         TypeRegistry typeManager = new TypeRegistry();
         FunctionRegistry registry = new FunctionRegistry(typeManagernew BlockEncodingManager(typeManager), true);
         FunctionInfo exactOperator = registry.getCoercion();
         assertEquals(exactOperator.getSignature().getName(), mangleOperatorName(..name()));
         assertEquals(transform(exactOperator.getArgumentTypes(), Functions.toStringFunction()), ImmutableList.of(.));
         assertEquals(exactOperator.getReturnType().getBase(), .);
     }
 
     @Test
     public void testExactMatchBeforeCoercion()
     {
         TypeRegistry typeManager = new TypeRegistry();
         FunctionRegistry registry = new FunctionRegistry(typeManagernew BlockEncodingManager(typeManager), true);
         boolean foundOperator = false;
         for (ParametricFunction function : registry.listOperators()) {
             OperatorType operatorType = unmangleOperator(function.getSignature().getName());
             if (operatorType == .) {
                 continue;
             }
             if (function.isUnbound()) {
                 continue;
             }
             FunctionInfo exactOperator = registry.resolveOperator(operatorTyperesolveTypes(function.getSignature().getArgumentTypes(), typeManager));
             assertEquals(exactOperator.getSignature(), function.getSignature());
             foundOperator = true;
         }
         assertTrue(foundOperator);
     }
 
     @Test
     public void testMagicLiteralFunction()
     {
         assertEquals(signature.getName(), "$literal$timestamp with time zone");
         assertEquals(signature.getArgumentTypes(), ImmutableList.of(parseTypeSignature(.)));
 
         TypeRegistry typeManager = new TypeRegistry();
         FunctionRegistry registry = new FunctionRegistry(typeManagernew BlockEncodingManager(typeManager), true);
         FunctionInfo function = registry.resolveFunction(QualifiedName.of(signature.getName()), signature.getArgumentTypes(), false);
         assertEquals(function.getArgumentTypes(), ImmutableList.of(parseTypeSignature(.)));
     }
 
     @Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "\\QFunction already registered: custom_add(bigint,bigint):bigint\\E")
     public void testDuplicateFunctions()
     {
         List<ParametricFunctionfunctions = new FunctionListBuilder(new TypeRegistry())
                 .scalar(CustomFunctions.class)
                 .getFunctions()
                 .stream()
                 .filter(input -> input.getSignature().getName().equals("custom_add"))
                 .collect(toImmutableList());
        TypeRegistry typeManager = new TypeRegistry();
        FunctionRegistry registry = new FunctionRegistry(typeManagernew BlockEncodingManager(typeManager), true);
        registry.addFunctions(functions);
        registry.addFunctions(functions);
    }
    @Test(expectedExceptions = IllegalStateException.class, expectedExceptionsMessageRegExp = "'sum' is both an aggregation and a scalar function")
    public void testConflictingScalarAggregation()
            throws Exception
    {
        List<ParametricFunctionfunctions = new FunctionListBuilder(new TypeRegistry())
                .scalar(ScalarSum.class)
                .getFunctions();
        TypeRegistry typeManager = new TypeRegistry();
        FunctionRegistry registry = new FunctionRegistry(typeManagernew BlockEncodingManager(typeManager), true);
        registry.addFunctions(functions);
    }
    @Test
    public void testListingHiddenFunctions()
            throws Exception
    {
        TypeRegistry typeManager = new TypeRegistry();
        FunctionRegistry registry = new FunctionRegistry(typeManagernew BlockEncodingManager(typeManager), true);
        List<ParametricFunctionfunctions = registry.list();
        List<Stringnames = transform(functionsinput -> input.getSignature().getName());
        assertTrue(names.contains("length"), "Expected function names " + names + " to contain 'length'");
        assertTrue(names.contains("stddev"), "Expected function names " + names + " to contain 'stddev'");
        assertTrue(names.contains("rank"), "Expected function names " + names + " to contain 'rank'");
        assertFalse(names.contains("like"), "Expected function names " + names + " not to contain 'like'");
    }
    public static final class ScalarSum
    {
        private ScalarSum() {}
        @ScalarFunction
        @SqlType(.)
        public static long sum(@SqlType(.long a, @SqlType(.long b)
        {
            return a + b;
        }
    }
New to GrepCode? Check out our FAQ X