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.optimizations;
 
 
 import java.util.List;
 import java.util.Map;
 
 import static com.facebook.presto.sql.planner.optimizations.LocalProperties.extractLeadingConstants;
 import static com.facebook.presto.sql.planner.optimizations.LocalProperties.stripLeadingConstants;
 import static org.testng.Assert.assertEquals;
 
 public class TestLocalProperties
 {
     @Test
     public void testConstantProcessing()
             throws Exception
     {
         Assert.assertEquals(stripLeadingConstants(ImmutableList.of()), ImmutableList.of());
         Assert.assertEquals(extractLeadingConstants(ImmutableList.of()), ImmutableSet.of());
 
         List<LocalProperty<String>> input = ImmutableList.of(grouped("a"));
         Assert.assertEquals(stripLeadingConstants(input), ImmutableList.of(grouped("a")));
         Assert.assertEquals(extractLeadingConstants(input), ImmutableSet.of());
 
         input = ImmutableList.of(constant("b"), grouped("a"));
         Assert.assertEquals(stripLeadingConstants(input), ImmutableList.of(grouped("a")));
         Assert.assertEquals(extractLeadingConstants(input), ImmutableSet.of("b"));
 
         input = ImmutableList.of(constant("a"), grouped("a"));
         Assert.assertEquals(stripLeadingConstants(input), ImmutableList.of(grouped("a")));
         Assert.assertEquals(extractLeadingConstants(input), ImmutableSet.of("a"));
 
         input = ImmutableList.of(grouped("a"), constant("b"));
         Assert.assertEquals(stripLeadingConstants(input), input);
         Assert.assertEquals(extractLeadingConstants(input), ImmutableSet.of());
 
         input = ImmutableList.of(constant("a"));
         Assert.assertEquals(stripLeadingConstants(input), ImmutableList.of());
         Assert.assertEquals(extractLeadingConstants(input), ImmutableSet.of("a"));
 
         input = ImmutableList.of(constant("a"), constant("b"));
         Assert.assertEquals(stripLeadingConstants(input), ImmutableList.of());
         Assert.assertEquals(extractLeadingConstants(input), ImmutableSet.of("a""b"));
     }
 
     @Test
     public void testTranslate()
             throws Exception
     {
         Map<StringStringmap = ImmutableMap.of();
         List<LocalProperty<String>> input = ImmutableList.of();
         Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of());
 
         map = ImmutableMap.of();
         input = ImmutableList.of(grouped("a"));
         Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of());
 
         map = ImmutableMap.of("a""a1");
         input = ImmutableList.of(grouped("a"));
         Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(grouped("a1")));
 
         map = ImmutableMap.of();
         input = ImmutableList.of(constant("a"));
         Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of());
 
         map = ImmutableMap.of();
         input = ImmutableList.of(constant("a"), grouped("b"));
         Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of());
 
         map = ImmutableMap.of("b""b1");
         input = ImmutableList.of(constant("a"), grouped("b"));
         Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(grouped("b1")));
 
        map = ImmutableMap.of("a""a1""b""b1");
        input = ImmutableList.of(constant("a"), grouped("b"));
        Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(constant("a1"), grouped("b1")));
        map = ImmutableMap.of("a""a1""b""b1");
        input = ImmutableList.of(grouped("a""b"));
        Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(grouped("a1""b1")));
        map = ImmutableMap.of("a""a1""c""c1");
        input = ImmutableList.of(constant("a"), grouped("b"), grouped("c"));
        Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(constant("a1")));
        map = ImmutableMap.of("a""a1""c""c1");
        input = ImmutableList.of(grouped("a""b"), grouped("c"));
        Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of());
        map = ImmutableMap.of("a""a1""c""c1");
        input = ImmutableList.of(grouped("a"), grouped("b"), grouped("c"));
        Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(grouped("a1")));
        map = ImmutableMap.of("a""a1""c""c1");
        input = ImmutableList.of(constant("b"), grouped("a""b"), grouped("c")); // Because b is constant, we can rewrite (a, b)
        Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(grouped("a1"), grouped("c1")));
        map = ImmutableMap.of("a""a1""c""c1");
        input = ImmutableList.of(grouped("a"), constant("b"), grouped("c")); // Don't fail c translation due to a failed constant translation
        Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(grouped("a1"), grouped("c1")));
        map = ImmutableMap.of("a""a1""b""b1""c""c1");
        input = ImmutableList.of(grouped("a"), constant("b"), grouped("c"));
        Assert.assertEquals(LocalProperties.translate(inputtranslateWithMap(map)), ImmutableList.of(grouped("a1"), constant("b1"), grouped("c1")));
    }
    private static <X, Y> Function<X, Optional<Y>> translateWithMap(Map<X, Y> translateMap)
    {
        return input -> Optional.ofNullable(translateMap.get(input));
    }
    @Test
    public void testNormalizeEmpty()
            throws Exception
    {
        List<LocalProperty<String>> localProperties = builder().build();
        assertNormalize(localProperties);
        assertNormalizeAndFlatten(localProperties);
    }
    @Test
    public void testNormalizeSingleSmbolGroup()
            throws Exception
    {
        List<LocalProperty<String>> localProperties = builder().grouped("a").build();
        assertNormalize(localProperties, Optional.of(grouped("a")));
        assertNormalizeAndFlatten(localPropertiesgrouped("a"));
    }
    @Test
    public void testNormalizeOverlappingSymbol()
            throws Exception
    {
        List<LocalProperty<String>> localProperties = builder()
                .grouped("a")
                .sorted("a".)
                .constant("a")
                .build();
        assertNormalize(
                localProperties,
                Optional.of(grouped("a")),
                Optional.empty(),
                Optional.empty());
                localProperties,
                grouped("a"));
    }
    @Test
            throws Exception
    {
        List<LocalProperty<String>> localProperties = builder()
                .constant("a")
                .grouped("a""b")
                .grouped("b""c")
                .sorted("c".)
                .build();
        assertNormalize(
                localProperties,
                Optional.of(constant("a")),
                Optional.of(grouped("b")),
                Optional.of(grouped("c")),
                Optional.empty());
                localProperties,
                constant("a"),
                grouped("b"),
                grouped("c"));
    }
    @Test
            throws Exception
    {
        List<LocalProperty<String>> localProperties = builder()
                .sorted("a".)
                .grouped("a""b")
                .grouped("c")
                .constant("a")
                .build();
        assertNormalize(
                localProperties,
                Optional.of(sorted("a".)),
                Optional.of(grouped("b")),
                Optional.of(grouped("c")),
                Optional.empty());
                localProperties,
                sorted("a".),
                grouped("b"),
                grouped("c"));
    }
    @Test
    public void testNormalizeDifferentSorts()
            throws Exception
    {
        List<LocalProperty<String>> localProperties = builder()
                .sorted("a".)
                .sorted("a".)
                .build();
        assertNormalize(
                localProperties,
                Optional.of(sorted("a".)),
                Optional.empty());
                localProperties,
                sorted("a".));
    }
    @Test
    public void testMatchedGroupHierarchy()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .grouped("a")
                .grouped("b")
                .grouped("c")
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c""d").build(),
                Optional.of(grouped("d")));
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("b").build(),
                Optional.of(grouped("b")));
        assertMatch(
                actual,
                builder().grouped("b""c").build(),
                Optional.of(grouped("b""c")));
        assertMatch(
                actual,
                builder().grouped("a""c").build(),
                Optional.of(grouped("c")));
        assertMatch(
                actual,
                builder().grouped("c").build(),
                Optional.of(grouped("c")));
        assertMatch(
                actual,
                builder()
                        .grouped("a")
                        .grouped("a")
                        .grouped("a")
                        .grouped("a")
                        .grouped("b")
                        .build(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty(),
                Optional.empty());
    }
    @Test
    public void testGroupedTuple()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .grouped("a""b""c")
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c""d").build(),
                Optional.of(grouped("d")));
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.of(grouped("a""b")));
        assertMatch(
                actual,
                builder().grouped("a").build(),
                Optional.of(grouped("a")));
        assertMatch(
                actual,
                builder().grouped("a").grouped("b").build(),
                Optional.of(grouped("a")),
                Optional.of(grouped("b")));
    }
    @Test
    public void testGroupedDoubleThenSingle()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .grouped("a""b")
                .grouped("c")
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c""d").build(),
                Optional.of(grouped("d")));
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""c").build(),
                Optional.of(grouped("a""c")));
        assertMatch(
                actual,
                builder().grouped("a").build(),
                Optional.of(grouped("a")));
        assertMatch(
                actual,
                builder().grouped("c").build(),
                Optional.of(grouped("c")));
    }
    @Test
    public void testGroupedDoubleThenDouble()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .grouped("a""b")
                .grouped("c""a")
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c""d").build(),
                Optional.of(grouped("d")));
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("b""c").build(),
                Optional.of(grouped("b""c")));
        assertMatch(
                actual,
                builder().grouped("a").build(),
                Optional.of(grouped("a")));
        assertMatch(
                actual,
                builder().grouped("c").build(),
                Optional.of(grouped("c")));
    }
    @Test
    public void testSortProperties()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .sorted("a".)
                .sorted("b".)
                .sorted("c".)
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c""d").build(),
                Optional.of(grouped("d")));
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("b""c").build(),
                Optional.of(grouped("b""c")));
        assertMatch(
                actual,
                builder().grouped("b").build(),
                Optional.of(grouped("b")));
        assertMatch(
                actual,
                builder()
                        .sorted("a".)
                        .sorted("c".)
                        .build(),
                Optional.empty(),
                Optional.of(sorted("c".)));
    }
    @Test
    public void testSortGroupSort()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .sorted("a".)
                .grouped("b""c")
                .sorted("d".)
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c""d""e").build(),
                Optional.of(grouped("e")));
        assertMatch(
                actual,
                builder().grouped("a""b""c""d").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.of(grouped("b")));
        assertMatch(
                actual,
                builder().grouped("a").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("b").build(),
                Optional.of(grouped("b")));
        assertMatch(
                actual,
                builder().grouped("d").build(),
                Optional.of(grouped("d")));
        assertMatch(
                actual,
                builder()
                        .sorted("a".)
                        .sorted("b".)
                        .build(),
                Optional.empty(),
                Optional.of(sorted("b".)));
        assertMatch(
                actual,
                builder()
                        .sorted("a".)
                        .grouped("b""c""d")
                        .grouped("e""a")
                        .build(),
                Optional.empty(),
                Optional.empty(),
                Optional.of(grouped("e")));
    }
    @Test
    public void testPartialConstantGroup()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .constant("a")
                .grouped("a""b")
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.of(grouped("c")));
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("b").build(),
                Optional.empty());
    }
    @Test
    public void testNonoverlappingConstantGroup()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .constant("a")
                .grouped("b")
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.of(grouped("c")));
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder()
                        .grouped("b")
                        .grouped("a")
                        .build(),
                Optional.empty(),
                Optional.empty());
    }
    @Test
    public void testConstantWithMultiGroup()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .constant("a")
                .grouped("a""b")
                .grouped("a""c")
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""c""d").build(),
                Optional.of(grouped("d")));
        assertMatch(
                actual,
                builder().grouped("a""b""c").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("a""c").build(),
                Optional.of(grouped("c")));
        assertMatch(
                actual,
                builder().grouped("b").build(),
                Optional.empty());
        assertMatch(
                actual,
                builder().grouped("b""c").build(),
                Optional.empty());
    }
    @Test
    public void testConstantWithSort()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .constant("b")
                .sorted("a".)
                .sorted("b".)
                .sorted("c".)
                .build();
        assertMatch(
                actual,
                builder().grouped("a""b""d").build(),
                Optional.of(grouped("d")));
        assertMatch(
                actual,
                builder().grouped("a""c").build(),
                Optional.empty());
    }
    @Test
    public void testMoreRequiredGroupsThanActual()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .constant("b")
                .grouped("a")
                .grouped("d")
                .build();
        assertMatch(
                actual,
                builder()
                        .grouped("a")
                        .grouped("b")
                        .grouped("c")
                        .grouped("d")
                        .build(),
                Optional.empty(),
                Optional.empty(),
                Optional.of(grouped("c")),
                Optional.of(grouped("d")));
    }
    @Test
    public void testDifferentSortOrders()
            throws Exception
    {
        List<LocalProperty<String>> actual = builder()
                .sorted("a".)
                .build();
        assertMatch(
                actual,
                builder()
                        .sorted("a".)
                        .build(),
                Optional.of(sorted("a".)));
    }
    private static <T> void assertMatch(List<LocalProperty<T>> actualList<LocalProperty<T>> wantedOptional<LocalProperty<T>>... match)
    {
        assertEquals(LocalProperties.match(actualwanted), Arrays.asList(match));
    }
    private static <T> void assertNormalize(List<LocalProperty<T>> localPropertiesOptional<LocalProperty<T>>... normalized)
    {
        assertEquals(LocalProperties.normalize(localProperties), Arrays.asList(normalized));
    }
    private static <T> void assertNormalizeAndFlatten(List<LocalProperty<T>> localPropertiesLocalProperty<T>... normalized)
    {
        assertEquals(LocalProperties.normalizeAndPrune(localProperties), Arrays.asList(normalized));
    }
    private static ConstantProperty<Stringconstant(String column)
    {
        return new ConstantProperty<>(column);
    }
    private static GroupingProperty<Stringgrouped(String... columns)
    {
        return new GroupingProperty<>(Arrays.asList(columns));
    }
    private static SortingProperty<Stringsorted(String columnSortOrder order)
    {
        return new SortingProperty<>(columnorder);
    }
    private static Builder builder()
    {
        return new Builder();
    }
    private static class Builder
    {
        private final List<LocalProperty<String>> properties = new ArrayList<>();
        public Builder grouped(String... columns)
        {
            .add(new GroupingProperty<>(Arrays.asList(columns)));
            return this;
        }
        public Builder sorted(String columnSortOrder order)
        {
            .add(new SortingProperty<>(columnorder));
            return this;
        }
        public Builder constant(String column)
        {
            .add(new ConstantProperty<>(column));
            return this;
        }
        public List<LocalProperty<String>> build()
        {
            return new ArrayList<>();
        }
    }
New to GrepCode? Check out our FAQ X