Java Code Examples for com.google.common.collect.Ordering#nullsFirst()

The following examples show how to use com.google.common.collect.Ordering#nullsFirst() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OrderingByPosition.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private OrderingByPosition (int position, boolean reverse, @Nullable Boolean nullFirst) {
    this.position = position;

    // note, that we are reverse for the queue so this conditional is by intent
    Ordering<Comparable> ordering;
    nullFirst = nullFirst != null ? !nullFirst : null; // swap because queue is reverse
    if (reverse) {
        ordering = Ordering.natural();
        if (nullFirst == null || !nullFirst) {
            ordering = ordering.nullsLast();
        } else {
            ordering = ordering.nullsFirst();
        }
    } else {
        ordering = Ordering.natural().reverse();
        if (nullFirst == null || nullFirst) {
            ordering = ordering.nullsFirst();
        } else {
            ordering = ordering.nullsLast();
        }
    }
    this.ordering = ordering;
}
 
Example 2
Source File: OrderedResultIterator.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a comparator from the list of columns in ORDER BY clause.
 * @param orderByExpressions the columns in ORDER BY clause.
 * @return the comparator built from the list of columns in ORDER BY clause.
 */
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
    Ordering<ResultEntry> ordering = null;
    int pos = 0;
    for (OrderByExpression col : orderByExpressions) {
        Expression e = col.getExpression();
        Comparator<ImmutableBytesWritable> comparator = 
                e.getSortOrder() == SortOrder.DESC && !e.getDataType().isFixedWidth() 
                ? buildDescVarLengthComparator() 
                : new ImmutableBytesWritable.Comparator();
        Ordering<ImmutableBytesWritable> o = Ordering.from(comparator);
        if(!col.isAscending()) o = o.reverse();
        o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
        Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
        ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
    }
    return ordering;
}
 
Example 3
Source File: OrderedResultIterator.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a comparator from the list of columns in ORDER BY clause.
 * @param orderByExpressions the columns in ORDER BY clause.
 * @return the comparator built from the list of columns in ORDER BY clause.
 */
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
    Ordering<ResultEntry> ordering = null;
    int pos = 0;
    for (OrderByExpression col : orderByExpressions) {
        Ordering<ImmutableBytesWritable> o = Ordering.from(new ImmutableBytesWritable.Comparator());
        if(!col.isAscending()) o = o.reverse();
        o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
        Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
        ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
    }
    return ordering;
}
 
Example 4
Source File: ProductMetricsIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void helpTestOrderByNullColumns(boolean nullsFirst) throws Exception {
    long ts = nextTimestamp();
    String tenantId = getOrganizationId();
    String query = "SELECT region " +
                   "FROM PRODUCT_METRICS " + 
                   "WHERE organization_id=? " +
                   "GROUP BY region " +
                   "ORDER BY region nulls " + (nullsFirst ? "first" : "last");

    String url = getUrl() + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + (ts + 5); // Run query at timestamp 5
    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    try {
        initTableValues(tenantId, getSplits(tenantId), ts);
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, tenantId);
        ResultSet rs = statement.executeQuery();
        
        List<String> expected = Lists.newArrayList(null, R1, R2);
        Ordering<String> regionOrdering = Ordering.natural();
        regionOrdering = nullsFirst ? regionOrdering.nullsFirst() : regionOrdering.nullsLast();
        Collections.sort(expected, regionOrdering);
        
        for (String region : expected) {
            assertTrue(rs.next());
            assertEquals(region, rs.getString(1));
        }
        assertFalse(rs.next());
    } finally {
        conn.close();
    }
}
 
Example 5
Source File: ProductMetricsIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
private void helpTestOrderByNullColumns(boolean nullsFirst) throws Exception {
    String tablename=generateUniqueName();
    String tenantId = getOrganizationId();
    String query = "SELECT region " +
            "FROM " +tablename+
            " WHERE organization_id=? " +
            "GROUP BY region " +
            "ORDER BY region nulls " + (nullsFirst ? "first" : "last");

    Properties props = PropertiesUtil.deepCopy(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(getUrl(), props);
    try {
        initTableValues(tablename, tenantId, getSplits(tenantId));
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, tenantId);
        ResultSet rs = statement.executeQuery();

        List<String> expected = Lists.newArrayList(null, R1, R2);
        Ordering<String> regionOrdering = Ordering.natural();
        regionOrdering = nullsFirst ? regionOrdering.nullsFirst() : regionOrdering.nullsLast();
        Collections.sort(expected, regionOrdering);

        for (String region : expected) {
            assertTrue(rs.next());
            assertEquals(region, rs.getString(1));
        }
        assertFalse(rs.next());
    } finally {
        conn.close();
    }
}
 
Example 6
Source File: OrderedResultIterator.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Builds a comparator from the list of columns in ORDER BY clause.
 * @param orderByExpressions the columns in ORDER BY clause.
 * @return the comparator built from the list of columns in ORDER BY clause.
 */
// ImmutableBytesWritable.Comparator doesn't implement generics
@SuppressWarnings("unchecked")
private static Comparator<ResultEntry> buildComparator(List<OrderByExpression> orderByExpressions) {
    Ordering<ResultEntry> ordering = null;
    int pos = 0;
    for (OrderByExpression col : orderByExpressions) {
        Ordering<ImmutableBytesWritable> o = Ordering.from(new ImmutableBytesWritable.Comparator());
        if(!col.isAscending()) o = o.reverse();
        o = col.isNullsLast() ? o.nullsLast() : o.nullsFirst();
        Ordering<ResultEntry> entryOrdering = o.onResultOf(new NthKey(pos++));
        ordering = ordering == null ? entryOrdering : ordering.compound(entryOrdering);
    }
    return ordering;
}
 
Example 7
Source File: ProductMetricsTest.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void helpTestOrderByNullColumns(boolean nullsFirst) throws Exception {
    long ts = nextTimestamp();
    String tenantId = getOrganizationId();
    String query = "SELECT region " +
                   "FROM PRODUCT_METRICS " + 
                   "WHERE organization_id=? " +
                   "GROUP BY region " +
                   "ORDER BY region nulls " + (nullsFirst ? "first" : "last");

    String url = PHOENIX_JDBC_URL + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + (ts + 5); // Run query at timestamp 5
    Properties props = new Properties(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    try {
        initTableValues(tenantId, getSplits(tenantId), ts);
        PreparedStatement statement = conn.prepareStatement(query);
        statement.setString(1, tenantId);
        ResultSet rs = statement.executeQuery();
        
        List<String> expected = Lists.newArrayList(null, R1, R2);
        Ordering<String> regionOrdering = Ordering.natural();
        regionOrdering = nullsFirst ? regionOrdering.nullsFirst() : regionOrdering.nullsLast();
        Collections.sort(expected, regionOrdering);
        
        for (String region : expected) {
            assertTrue(rs.next());
            assertEquals(region, rs.getString(1));
        }
        assertFalse(rs.next());
    } finally {
        conn.close();
    }
}
 
Example 8
Source File: TestingHelpers.java    From crate with Apache License 2.0 4 votes vote down vote up
public static <T, K extends Comparable> Matcher<Iterable<? extends T>> isSortedBy(final Function<T, K> extractSortingKeyFunction,
                                                                                  final boolean descending,
                                                                                  @Nullable final Boolean nullsFirst) {
    Ordering<K> ordering = Ordering.natural();
    if (descending) {
        ordering = ordering.reverse();
    }
    if (nullsFirst != null && nullsFirst) {
        ordering = ordering.nullsFirst();
    } else {
        ordering = ordering.nullsLast();
    }
    final Ordering<K> ord = ordering;

    return new TypeSafeDiagnosingMatcher<Iterable<? extends T>>() {
        @Override
        protected boolean matchesSafely(Iterable<? extends T> item, Description mismatchDescription) {
            K previous = null;
            int i = 0;
            for (T elem : item) {
                K current = extractSortingKeyFunction.apply(elem);
                if (previous != null) {
                    if (ord.compare(previous, current) > 0) {
                        mismatchDescription
                            .appendText("element ").appendValue(current)
                            .appendText(" at position ").appendValue(i)
                            .appendText(" is ")
                            .appendText(descending ? "bigger" : "smaller")
                            .appendText(" than previous element ")
                            .appendValue(previous);
                        return false;
                    }
                }
                i++;
                previous = current;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("expected iterable to be sorted ");
            if (descending) {
                description.appendText("in DESCENDING order");
            } else {
                description.appendText("in ASCENDING order");
            }
        }
    };
}