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.sql.planner;
 
 
 
 import java.util.Map;
 
 import static com.facebook.presto.SessionTestUtils.TEST_SESSION;
 import static com.facebook.presto.operator.scalar.FunctionAssertions.createExpression;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static org.testng.Assert.assertEquals;
 
 {
     private static final SqlParser SQL_PARSER = new SqlParser();
     private static final Metadata METADATA = new MetadataManager();
 
     @Test
     public void testBooleanExpression()
     {
         assertProjection("true"true);
         assertProjection("false"false);
         assertProjection("1 = 1"true);
         assertProjection("1 = 0"false);
         assertProjection("true and false"false);
     }
 
     @Test
     public void testArithmeticExpression()
     {
         assertProjection("42 + 87", 42L + 87L);
         assertProjection("42 + 22.2", 42L + 22.2);
         assertProjection("11.1 + 22.2", 11.1 + 22.2);
 
         assertProjection("42 - 87", 42L - 87L);
         assertProjection("42 - 22.2", 42L - 22.2);
         assertProjection("11.1 - 22.2", 11.1 - 22.2);
 
         assertProjection("42 * 87", 42L * 87L);
         assertProjection("42 * 22.2", 42L * 22.2);
         assertProjection("11.1 * 22.2", 11.1 * 22.2);
 
         assertProjection("42 / 87", 42L / 87L);
         assertProjection("42 / 22.2", 42L / 22.2);
         assertProjection("11.1 / 22.2", 11.1 / 22.2);
 
         assertProjection("42 % 87", 42L % 87L);
         assertProjection("42 % 22.2", 42L % 22.2);
         assertProjection("11.1 % 22.2", 11.1 % 22.2);
     }
 
     @Test
     public void testArithmeticExpressionWithNulls()
     {
         for (ArithmeticBinaryExpression.Type type : ArithmeticBinaryExpression.Type.values()) {
             assertProjection("NULL " + type.getValue() + " NULL"null);
 
             assertProjection("42 " + type.getValue() + " NULL"null);
             assertProjection("NULL " + type.getValue() + " 42"null);
 
             assertProjection("11.1 " + type.getValue() + " NULL"null);
             assertProjection("NULL " + type.getValue() + " 11.1"null);
         }
     }
 
     @Test
     public void testCoalesceExpression()
     {
         assertProjection("COALESCE(42, 87, 100)", 42L);
         assertProjection("COALESCE(NULL, 87, 100)", 87L);
         assertProjection("COALESCE(42, NULL, 100)", 42L);
        assertProjection("COALESCE(NULL, NULL, 100)", 100L);
        assertProjection("COALESCE(42.2, 87.2, 100.2)", 42.2);
        assertProjection("COALESCE(NULL, 87.2, 100.2)", 87.2);
        assertProjection("COALESCE(42.2, NULL, 100.2)", 42.2);
        assertProjection("COALESCE(NULL, NULL, 100.2)", 100.2);
        assertProjection("COALESCE('foo', 'bar', 'zah')""foo");
        assertProjection("COALESCE(NULL, 'bar', 'zah')""bar");
        assertProjection("COALESCE('foo', NULL, 'zah')""foo");
        assertProjection("COALESCE(NULL, NULL, 'zah')""zah");
        assertProjection("COALESCE(NULL, NULL, NULL)"null);
    }
    @Test
    public void testNullIf()
    {
        assertProjection("NULLIF(42, 42)"null);
        assertProjection("NULLIF(42, 42.0)"null);
        assertProjection("NULLIF(42.42, 42.42)"null);
        assertProjection("NULLIF('foo', 'foo')"null);
        assertProjection("NULLIF(42, 87)", 42L);
        assertProjection("NULLIF(42, 22.2)", 42L);
        assertProjection("NULLIF(42.42, 22.2)", 42.42);
        assertProjection("NULLIF('foo', 'bar')""foo");
        assertProjection("NULLIF(NULL, NULL)"null);
        assertProjection("NULLIF(42, NULL)", 42L);
        assertProjection("NULLIF(NULL, 42)"null);
        assertProjection("NULLIF(11.1, NULL)", 11.1);
        assertProjection("NULLIF(NULL, 11.1)"null);
    }
    @Test
    public void testSymbolReference()
    {
        Symbol symbol = new Symbol("symbol");
        ImmutableMap<SymbolIntegersymbolToInputMappings = ImmutableMap.of(symbol, 0);
        assertProjection("symbol"truesymbolToInputMappings, ImmutableMap.<SymbolType>of(symbol), 0, createBlock(true));
        assertProjection("symbol"nullsymbolToInputMappings, ImmutableMap.<SymbolType>of(symbol), 0, createBlock(null));
        assertProjection("symbol", 42L, symbolToInputMappings, ImmutableMap.<SymbolType>of(symbol), 0, createBlock(, 42));
        assertProjection("symbol"nullsymbolToInputMappings, ImmutableMap.<SymbolType>of(symbol), 0, createBlock(null));
        assertProjection("symbol", 11.1, symbolToInputMappings, ImmutableMap.<SymbolType>of(symbol), 0, createBlock(, 11.1));
        assertProjection("symbol"nullsymbolToInputMappings, ImmutableMap.<SymbolType>of(symbol), 0, createBlock(null));
        assertProjection("symbol""foo"symbolToInputMappings, ImmutableMap.<SymbolType>of(symbol), 0, createBlock("foo"));
        assertProjection("symbol"nullsymbolToInputMappings, ImmutableMap.<SymbolType>of(symbol), 0, createBlock(null));
    }
    public static void assertProjection(String expression, @Nullable Object expectedValue)
    {
        assertProjection(
                expression,
                expectedValue,
                ImmutableMap.<SymbolInteger>of(),
                ImmutableMap.<SymbolType>of(),
                0);
    }
    private static void assertProjection(
            String expression,
            @Nullable Object expectedValue,
            Map<SymbolIntegersymbolToInputMappings,
            Map<SymbolTypesymbolTypes,
            int position,
            Block... blocks)
    {
        InterpretedProjectionFunction projectionFunction = new InterpretedProjectionFunction(
                createExpression(expressionsymbolTypes),
                symbolTypes,
                symbolToInputMappings,
                ,
                ,
                
        );
        // create output
        Type type = projectionFunction.getType();
        BlockBuilder builder = type.createBlockBuilder(new BlockBuilderStatus(), 1);
        // project
        projectionFunction.project(positionblocksbuilder);
        // extract single value
        Object actualValue = BlockAssertions.getOnlyValue(typebuilder.build());
        assertEquals(actualValueexpectedValue);
    }
    private static Block createBlock(Type typeObject value)
    {
        BlockBuilder blockBuilder = type.createBlockBuilder(new BlockBuilderStatus(), 1);
        BlockUtils.appendObject(typeblockBuildervalue);
        return blockBuilder.build();
    }
New to GrepCode? Check out our FAQ X