private static boolean isBetween(Range range) { return !range.getLow().isLowerUnbounded() && range.getLow().getBound() == Marker.Bound.EXACTLY && !range.getHigh().isUpperUnbounded() && range.getHigh().getBound() == Marker.Bound.EXACTLY; }
private FormattedMarker formatMarker(Marker marker) { if (!marker.getValueBlock().isPresent()) { return new FormattedMarker(Optional.empty(), marker.getBound()); } return new FormattedMarker(Optional.of(getVarcharValue(marker.getType(), marker.getValue())), marker.getBound()); }
@Test public void testTypes() { assertEquals(Marker.lowerUnbounded(BIGINT).getType(), BIGINT); assertEquals(Marker.below(BIGINT, 1L).getType(), BIGINT); assertEquals(Marker.exactly(BIGINT, 1L).getType(), BIGINT); assertEquals(Marker.above(BIGINT, 1L).getType(), BIGINT); assertEquals(Marker.upperUnbounded(BIGINT).getType(), BIGINT); }
public boolean isAll() { return low.isLowerUnbounded() && high.isUpperUnbounded(); }
public static Range range(Type type, Object low, boolean lowInclusive, Object high, boolean highInclusive) { Marker lowMarker = lowInclusive ? Marker.exactly(type, low) : Marker.above(type, low); Marker highMarker = highInclusive ? Marker.exactly(type, high) : Marker.below(type, high); return new Range(lowMarker, highMarker); }
builder.append((range.getLow().getBound() == Marker.Bound.EXACTLY) ? '[' : '('); if (range.getLow().isLowerUnbounded()) { builder.append("<min>"); builder.append(castToVarchar(type, range.getLow().getValue(), functionRegistry, session)); if (range.getHigh().isUpperUnbounded()) { builder.append("<max>"); builder.append(castToVarchar(type, range.getHigh().getValue(), functionRegistry, session)); builder.append((range.getHigh().getBound() == Marker.Bound.EXACTLY) ? ']' : ')');
@Test public void testUnbounded() { assertTrue(Marker.lowerUnbounded(BIGINT).isLowerUnbounded()); assertFalse(Marker.lowerUnbounded(BIGINT).isUpperUnbounded()); assertTrue(Marker.upperUnbounded(BIGINT).isUpperUnbounded()); assertFalse(Marker.upperUnbounded(BIGINT).isLowerUnbounded()); assertFalse(Marker.below(BIGINT, 1L).isLowerUnbounded()); assertFalse(Marker.below(BIGINT, 1L).isUpperUnbounded()); assertFalse(Marker.exactly(BIGINT, 1L).isLowerUnbounded()); assertFalse(Marker.exactly(BIGINT, 1L).isUpperUnbounded()); assertFalse(Marker.above(BIGINT, 1L).isLowerUnbounded()); assertFalse(Marker.above(BIGINT, 1L).isUpperUnbounded()); }
private static OptionalInt extractUpperBound(TupleDomain<Symbol> tupleDomain, Symbol symbol) { if (tupleDomain.isNone()) { return OptionalInt.empty(); } Domain rowNumberDomain = tupleDomain.getDomains().get().get(symbol); if (rowNumberDomain == null) { return OptionalInt.empty(); } ValueSet values = rowNumberDomain.getValues(); if (values.isAll() || values.isNone() || values.getRanges().getRangeCount() <= 0) { return OptionalInt.empty(); } Range span = values.getRanges().getSpan(); if (span.getHigh().isUpperUnbounded()) { return OptionalInt.empty(); } verify(rowNumberDomain.getType().equals(BIGINT)); long upperBound = (Long) span.getHigh().getValue(); if (span.getHigh().getBound() == BELOW) { upperBound--; } if (upperBound > 0 && upperBound <= Integer.MAX_VALUE) { return OptionalInt.of(toIntExact(upperBound)); } return OptionalInt.empty(); }
public String toString(ConnectorSession session) { StringBuilder buffer = new StringBuilder(); if (isSingleValue()) { buffer.append('[').append(low.getPrintableValue(session)).append(']'); } else { buffer.append((low.getBound() == Marker.Bound.EXACTLY) ? '[' : '('); buffer.append(low.isLowerUnbounded() ? "<min>" : low.getPrintableValue(session)); buffer.append(", "); buffer.append(high.isUpperUnbounded() ? "<max>" : high.getPrintableValue(session)); buffer.append((high.getBound() == Marker.Bound.EXACTLY) ? ']' : ')'); } return buffer.toString(); } }
@Override public SortedRangeSet complement() { Builder builder = new Builder(type); if (lowIndexedRanges.isEmpty()) { return builder.add(Range.all(type)).build(); } Iterator<Range> rangeIterator = lowIndexedRanges.values().iterator(); Range firstRange = rangeIterator.next(); if (!firstRange.getLow().isLowerUnbounded()) { builder.add(new Range(Marker.lowerUnbounded(type), firstRange.getLow().lesserAdjacent())); } Range previousRange = firstRange; while (rangeIterator.hasNext()) { Range currentRange = rangeIterator.next(); Marker lowMarker = previousRange.getHigh().greaterAdjacent(); Marker highMarker = currentRange.getLow().lesserAdjacent(); builder.add(new Range(lowMarker, highMarker)); previousRange = currentRange; } Range lastRange = previousRange; if (!lastRange.getHigh().isUpperUnbounded()) { builder.add(new Range(lastRange.getHigh().greaterAdjacent(), Marker.upperUnbounded(type))); } return builder.build(); }
if (!range.getLow().isLowerUnbounded()) { minValue = range.getLow().getValue(); if (!range.getHigh().isUpperUnbounded()) { maxValue = range.getHigh().getValue();
/** * Adjacency is defined by two Markers being infinitesimally close to each other. * This means they must share the same value and have adjacent Bounds. */ public boolean isAdjacent(Marker other) { checkTypeCompatibility(other); if (isUpperUnbounded() || isLowerUnbounded() || other.isUpperUnbounded() || other.isLowerUnbounded()) { return false; } if (type.compareTo(valueBlock.get(), 0, other.valueBlock.get(), 0) != 0) { return false; } return (bound == Bound.EXACTLY && other.bound != Bound.EXACTLY) || (bound != Bound.EXACTLY && other.bound == Bound.EXACTLY); }
public String toString(ConnectorSession session) { StringBuilder buffer = new StringBuilder("{"); buffer.append("type=").append(type); buffer.append(", value="); if (isLowerUnbounded()) { buffer.append("<min>"); } else if (isUpperUnbounded()) { buffer.append("<max>"); } else { buffer.append(getPrintableValue(session)); } buffer.append(", bound=").append(bound); buffer.append("}"); return buffer.toString(); } }
.put(Marker.lowerUnbounded(BIGINT), -1000) .put(Marker.above(BIGINT, 0L), -100) .put(Marker.below(BIGINT, 1L), -1) .put(Marker.exactly(BIGINT, 1L), 0) .put(Marker.above(BIGINT, 1L), 1) .put(Marker.below(BIGINT, 2L), 100) .put(Marker.upperUnbounded(BIGINT), 1000) .build(); boolean adjacent = entry1.getKey().isAdjacent(entry2.getKey()); boolean distanceIsOne = Math.abs(entry1.getValue() - entry2.getValue()) == 1; assertEquals(adjacent, distanceIsOne); assertEquals(Marker.below(BIGINT, 1L).greaterAdjacent(), Marker.exactly(BIGINT, 1L)); assertEquals(Marker.exactly(BIGINT, 1L).greaterAdjacent(), Marker.above(BIGINT, 1L)); assertEquals(Marker.above(BIGINT, 1L).lesserAdjacent(), Marker.exactly(BIGINT, 1L)); assertEquals(Marker.exactly(BIGINT, 1L).lesserAdjacent(), Marker.below(BIGINT, 1L)); Marker.below(BIGINT, 1L).lesserAdjacent(); fail(); Marker.above(BIGINT, 1L).greaterAdjacent(); fail(); Marker.lowerUnbounded(BIGINT).lesserAdjacent(); fail(); Marker.lowerUnbounded(BIGINT).greaterAdjacent(); fail();
public static PrestoThriftMarker fromMarker(Marker marker) { PrestoThriftBlock value = marker.getValueBlock().isPresent() ? fromBlock(marker.getValueBlock().get(), marker.getType()) : null; return new PrestoThriftMarker(value, fromBound(marker.getBound())); } }
@JsonCreator public Range( @JsonProperty("low") Marker low, @JsonProperty("high") Marker high) { requireNonNull(low, "value is null"); requireNonNull(high, "value is null"); if (!low.getType().equals(high.getType())) { throw new IllegalArgumentException(String.format("Marker types do not match: %s vs %s", low.getType(), high.getType())); } if (low.getBound() == Marker.Bound.BELOW) { throw new IllegalArgumentException("low bound must be EXACTLY or ABOVE"); } if (high.getBound() == Marker.Bound.ABOVE) { throw new IllegalArgumentException("high bound must be EXACTLY or BELOW"); } if (low.compareTo(high) > 0) { throw new IllegalArgumentException("low must be less than or equal to high"); } this.low = low; this.high = high; }
public Object getSingleValue() { if (!isSingleValue()) { throw new IllegalStateException("Range does not have just a single value"); } return low.getValue(); }
public Type getType() { return low.getType(); }
@Setup(Level.Iteration) public void init() { ranges = new ArrayList<>(); int factor = 0; for (int i = 0; i < 10000; i++) { long from = ThreadLocalRandom.current().nextLong(100) + factor * 100; long to = ThreadLocalRandom.current().nextLong(100) + (factor + 1) * 100; factor++; ranges.add(new Range(Marker.above(BIGINT, from), Marker.below(BIGINT, to))); } } }