io.prestosql.spi.predicate.Range Java Examples

The following examples show how to use io.prestosql.spi.predicate.Range. 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: TestDomainTranslator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testConjunctExpression()
{
    Expression expression = and(
            comparison(GREATER_THAN, C_DOUBLE.toSymbolReference(), doubleLiteral(0)),
            comparison(GREATER_THAN, C_BIGINT.toSymbolReference(), bigintLiteral(0)));
    assertPredicateTranslates(
            expression,
            withColumnDomains(ImmutableMap.of(
                    C_DOUBLE, Domain.create(ValueSet.ofRanges(Range.greaterThan(DOUBLE, .0)), false),
                    C_BIGINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(BIGINT, 0L)), false))));

    assertEquals(
            toPredicate(fromPredicate(expression).getTupleDomain()),
            and(
                    comparison(GREATER_THAN, C_DOUBLE.toSymbolReference(), doubleLiteral(0)),
                    comparison(GREATER_THAN, C_BIGINT.toSymbolReference(), bigintLiteral(0))));
}
 
Example #2
Source File: TestJdbcQueryBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildSqlWithFloat()
        throws SQLException
{
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
            columns.get(10), Domain.create(SortedRangeSet.copyOf(REAL,
                    ImmutableList.of(
                            Range.equal(REAL, (long) floatToRawIntBits(100.0f + 0)),
                            Range.equal(REAL, (long) floatToRawIntBits(100.008f + 0)),
                            Range.equal(REAL, (long) floatToRawIntBits(100.0f + 14)))),
                    false)));

    Connection connection = database.getConnection();
    try (PreparedStatement preparedStatement = new QueryBuilder("\"").buildSql(jdbcClient, SESSION, connection, "", "", "test_table", Optional.empty(), columns, tupleDomain, Optional.empty(), identity());
            ResultSet resultSet = preparedStatement.executeQuery()) {
        ImmutableSet.Builder<Long> longBuilder = ImmutableSet.builder();
        ImmutableSet.Builder<Float> floatBuilder = ImmutableSet.builder();
        while (resultSet.next()) {
            longBuilder.add((Long) resultSet.getObject("col_0"));
            floatBuilder.add((Float) resultSet.getObject("col_10"));
        }
        assertEquals(longBuilder.build(), ImmutableSet.of(0L, 14L));
        assertEquals(floatBuilder.build(), ImmutableSet.of(100.0f, 114.0f));
    }
}
 
Example #3
Source File: TupleDomainOrcPredicate.java    From presto with Apache License 2.0 6 votes vote down vote up
@VisibleForTesting
public static Optional<Collection<Object>> extractDiscreteValues(ValueSet valueSet)
{
    return valueSet.getValuesProcessor().transform(
            ranges -> {
                ImmutableList.Builder<Object> discreteValues = ImmutableList.builder();
                for (Range range : ranges.getOrderedRanges()) {
                    if (!range.isSingleValue()) {
                        return Optional.empty();
                    }
                    discreteValues.add(range.getSingleValue());
                }
                return Optional.of(discreteValues.build());
            },
            discreteValues -> Optional.of(discreteValues.getValues()),
            allOrNone -> allOrNone.isAll() ? Optional.empty() : Optional.of(ImmutableList.of()));
}
 
Example #4
Source File: TestJdbcQueryBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildSqlWithVarchar()
        throws SQLException
{
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
            columns.get(3), Domain.create(SortedRangeSet.copyOf(VARCHAR,
                    ImmutableList.of(
                            Range.range(VARCHAR, utf8Slice("test_str_700"), true, utf8Slice("test_str_702"), false),
                            Range.equal(VARCHAR, utf8Slice("test_str_180")),
                            Range.equal(VARCHAR, utf8Slice("test_str_196")))),
                    false)));

    Connection connection = database.getConnection();
    try (PreparedStatement preparedStatement = new QueryBuilder("\"").buildSql(jdbcClient, SESSION, connection, "", "", "test_table", Optional.empty(), columns, tupleDomain, Optional.empty(), identity());
            ResultSet resultSet = preparedStatement.executeQuery()) {
        ImmutableSet.Builder<String> builder = ImmutableSet.builder();
        while (resultSet.next()) {
            builder.add((String) resultSet.getObject("col_3"));
        }
        assertEquals(builder.build(), ImmutableSet.of("test_str_700", "test_str_701", "test_str_180", "test_str_196"));

        assertContains(preparedStatement.toString(), "\"col_3\" >= ?");
        assertContains(preparedStatement.toString(), "\"col_3\" < ?");
        assertContains(preparedStatement.toString(), "\"col_3\" IN (?,?)");
    }
}
 
Example #5
Source File: TupleDomainOrcPredicate.java    From presto with Apache License 2.0 6 votes vote down vote up
private static <F, T extends Comparable<T>> Domain createDomain(Type type, boolean hasNullValue, RangeStatistics<F> rangeStatistics, Function<F, T> function)
{
    F min = rangeStatistics.getMin();
    F max = rangeStatistics.getMax();

    if (min != null && max != null) {
        return Domain.create(ValueSet.ofRanges(Range.range(type, function.apply(min), true, function.apply(max), true)), hasNullValue);
    }
    if (max != null) {
        return Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(type, function.apply(max))), hasNullValue);
    }
    if (min != null) {
        return Domain.create(ValueSet.ofRanges(Range.greaterThanOrEqual(type, function.apply(min))), hasNullValue);
    }
    return Domain.create(ValueSet.all(type), hasNullValue);
}
 
Example #6
Source File: TestPrometheusSplit.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testPredicatePushDownSetsLowerBoundOnly()
        throws Exception
{
    long predicateLowValue = 1568638171999L - 600000L;
    Range lowRange = Range.greaterThanOrEqual(TimestampType.TIMESTAMP, predicateLowValue);
    ValueSet valueSet = ValueSet.ofRanges(lowRange);
    Domain testDomain = Domain.create(valueSet, false);
    TupleDomain<ColumnHandle> testTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>of(
            (ColumnHandle) new PrometheusColumnHandle("timestamp", TimestampType.TIMESTAMP, 2), testDomain));
    prometheusTableHandle.setPredicate(Optional.of(testTupleDomain));
    io.airlift.units.Duration maxQueryRangeDuration = new io.airlift.units.Duration(120, TimeUnit.SECONDS);
    io.airlift.units.Duration queryChunkSizeDuration = new io.airlift.units.Duration(30, TimeUnit.SECONDS);
    LocalDateTime now = ofInstant(Instant.ofEpochMilli(1568638171999L), ZoneId.systemDefault());
    PrometheusTimeMachine.useFixedClockAt(now);
    TemporalAmount maxQueryAsTime = java.time.Duration.ofMillis(maxQueryRangeDuration.toMillis());
    List<String> splitTimes = PrometheusSplitManager.generateTimesForSplits(now, maxQueryRangeDuration, queryChunkSizeDuration, prometheusTableHandle);

    String earliestSplit = splitTimes.get(0);
    ZonedDateTime earliestSplitAsTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(longFromDecimalSecondString(earliestSplit)), ZoneId.systemDefault());
    TemporalAmount queryChunkAsTime = java.time.Duration.ofMillis(queryChunkSizeDuration.toMillis());
    ZonedDateTime startOfQuery = earliestSplitAsTime.minus(queryChunkAsTime);
    assertNotEquals(startOfQuery.toLocalDateTime(), now.minus(maxQueryAsTime).minus(java.time.Duration.ofMillis((splitTimes.size() - 1) * OFFSET_MILLIS)));
    assertEquals(startOfQuery.toInstant().toEpochMilli(), Instant.ofEpochMilli(predicateLowValue).toEpochMilli() - ((splitTimes.size() - 1) * OFFSET_MILLIS));
}
 
Example #7
Source File: TestTupleDomainOrcPredicate.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testBoolean()
{
    assertEquals(getDomain(BOOLEAN, 0, null), none(BOOLEAN));
    assertEquals(getDomain(BOOLEAN, 10, null), all(BOOLEAN));

    assertEquals(getDomain(BOOLEAN, 0, booleanColumnStats(null, null)), none(BOOLEAN));
    assertEquals(getDomain(BOOLEAN, 0, booleanColumnStats(0L, null)), none(BOOLEAN));
    assertEquals(getDomain(BOOLEAN, 0, booleanColumnStats(0L, 0L)), none(BOOLEAN));

    assertEquals(getDomain(BOOLEAN, 10, booleanColumnStats(0L, 0L)), onlyNull(BOOLEAN));
    assertEquals(getDomain(BOOLEAN, 10, booleanColumnStats(10L, null)), notNull(BOOLEAN));

    assertEquals(getDomain(BOOLEAN, 10, booleanColumnStats(10L, 10L)), singleValue(BOOLEAN, true));
    assertEquals(getDomain(BOOLEAN, 10, booleanColumnStats(10L, 0L)), singleValue(BOOLEAN, false));

    assertEquals(getDomain(BOOLEAN, 20, booleanColumnStats(10L, 5L)), all(BOOLEAN));

    assertEquals(getDomain(BOOLEAN, 20, booleanColumnStats(10L, 10L)), create(ValueSet.ofRanges(Range.equal(BOOLEAN, true)), true));
    assertEquals(getDomain(BOOLEAN, 20, booleanColumnStats(10L, 0L)), create(ValueSet.ofRanges(Range.equal(BOOLEAN, false)), true));
}
 
Example #8
Source File: TestDynamicTable.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilterWithPushdownConstraint()
{
    String tableName = realtimeOnlyTable.getTableName();
    String query = format("select FlightNum from %s limit 60", tableName.toLowerCase(ENGLISH));
    DynamicTable dynamicTable = buildFromPql(pinotMetadata, new SchemaTableName("default", query));
    PinotColumnHandle columnHandle = new PinotColumnHandle("OriginCityName", VARCHAR);
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>builder()
            .put(columnHandle,
                    Domain.create(ValueSet.ofRanges(Range.equal(VARCHAR, Slices.utf8Slice("Catfish Paradise"))), false))
            .build());
    String expectedPql = "select FlightNum from realtimeonly where (OriginCityName = 'Catfish Paradise') limit 60";
    assertEquals(extractPql(dynamicTable, tupleDomain, ImmutableList.<PinotColumnHandle>builder()
            .add(columnHandle)
            .build()), expectedPql);
}
 
Example #9
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testRoundTrip()
{
    TupleDomain<Symbol> tupleDomain = withColumnDomains(ImmutableMap.<Symbol, Domain>builder()
            .put(C_BIGINT, Domain.singleValue(BIGINT, 1L))
            .put(C_DOUBLE, Domain.onlyNull(DOUBLE))
            .put(C_VARCHAR, Domain.notNull(VARCHAR))
            .put(C_BOOLEAN, Domain.singleValue(BOOLEAN, true))
            .put(C_BIGINT_1, Domain.singleValue(BIGINT, 2L))
            .put(C_DOUBLE_1, Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(DOUBLE, 1.1), Range.equal(DOUBLE, 2.0), Range.range(DOUBLE, 3.0, false, 3.5, true)), true))
            .put(C_VARCHAR_1, Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(VARCHAR, utf8Slice("2013-01-01")), Range.greaterThan(VARCHAR, utf8Slice("2013-10-01"))), false))
            .put(C_TIMESTAMP, Domain.singleValue(TIMESTAMP, TIMESTAMP_VALUE))
            .put(C_DATE, Domain.singleValue(DATE, DATE_VALUE))
            .put(C_COLOR, Domain.singleValue(COLOR, COLOR_VALUE_1))
            .put(C_HYPER_LOG_LOG, Domain.notNull(HYPER_LOG_LOG))
            .build());

    assertPredicateTranslates(toPredicate(tupleDomain), tupleDomain);
}
 
Example #10
Source File: TupleDomainParquetPredicate.java    From presto with Apache License 2.0 6 votes vote down vote up
private static <F, T> Domain createDomain(Type type, boolean hasNullValue, ParquetRangeStatistics<F> rangeStatistics, Function<F, T> function)
{
    F min = rangeStatistics.getMin();
    F max = rangeStatistics.getMax();

    if (min != null && max != null) {
        return Domain.create(ValueSet.ofRanges(Range.range(type, function.apply(min), true, function.apply(max), true)), hasNullValue);
    }
    if (max != null) {
        return Domain.create(ValueSet.ofRanges(Range.lessThanOrEqual(type, function.apply(max))), hasNullValue);
    }
    if (min != null) {
        return Domain.create(ValueSet.ofRanges(Range.greaterThanOrEqual(type, function.apply(min))), hasNullValue);
    }
    return Domain.create(ValueSet.all(type), hasNullValue);
}
 
Example #11
Source File: AtopSplitManager.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectorSplitSource getSplits(ConnectorTransactionHandle transactionHandle, ConnectorSession session, ConnectorTableHandle table, SplitSchedulingStrategy splitSchedulingStrategy)
{
    AtopTableHandle tableHandle = (AtopTableHandle) table;

    List<ConnectorSplit> splits = new ArrayList<>();
    ZonedDateTime end = ZonedDateTime.now(timeZone);
    for (Node node : nodeManager.getWorkerNodes()) {
        ZonedDateTime start = end.minusDays(maxHistoryDays - 1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        while (start.isBefore(end)) {
            ZonedDateTime splitEnd = start.withHour(23).withMinute(59).withSecond(59).withNano(0);
            Domain splitDomain = Domain.create(ValueSet.ofRanges(Range.range(TIMESTAMP_WITH_TIME_ZONE, 1000 * start.toEpochSecond(), true, 1000 * splitEnd.toEpochSecond(), true)), false);
            if (tableHandle.getStartTimeConstraint().overlaps(splitDomain) && tableHandle.getEndTimeConstraint().overlaps(splitDomain)) {
                splits.add(new AtopSplit(node.getHostAndPort(), start.toEpochSecond(), start.getZone()));
            }
            start = start.plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        }
    }

    return new FixedSplitSource(splits);
}
 
Example #12
Source File: TestIonSqlQueryBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotPushDoublePredicates()
{
    IonSqlQueryBuilder queryBuilder = new IonSqlQueryBuilder(typeManager);
    List<HiveColumnHandle> columns = ImmutableList.of(
            createBaseColumn("quantity", 0, HIVE_INT, INTEGER, REGULAR, Optional.empty()),
            createBaseColumn("extendedprice", 1, HIVE_DOUBLE, DOUBLE, REGULAR, Optional.empty()),
            createBaseColumn("discount", 2, HIVE_DOUBLE, DOUBLE, REGULAR, Optional.empty()));
    TupleDomain<HiveColumnHandle> tupleDomain = withColumnDomains(
            ImmutableMap.of(
                    columns.get(0), Domain.create(ofRanges(Range.lessThan(BIGINT, 50L)), false),
                    columns.get(1), Domain.create(ofRanges(Range.equal(DOUBLE, 0.05)), false),
                    columns.get(2), Domain.create(ofRanges(Range.range(DOUBLE, 0.0, true, 0.02, true)), false)));
    assertEquals("SELECT s._1, s._2, s._3 FROM S3Object s WHERE ((case s._1 when '' then null else CAST(s._1 AS INT) end < 50))",
            queryBuilder.buildSql(columns, tupleDomain));
}
 
Example #13
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromAndPredicate()
{
    Expression originalPredicate = and(
            and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)),
            and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)));
    ExtractionResult result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), and(unprocessableExpression1(C_BIGINT), unprocessableExpression2(C_BIGINT)));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, false, 5L, false)), false))));

    // Test complements
    assertUnsupportedPredicate(not(and(
            and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT)),
            and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));

    originalPredicate = not(and(
            not(and(greaterThan(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT))),
            not(and(lessThan(C_BIGINT, bigintLiteral(5L)), unprocessableExpression2(C_BIGINT)))));
    result = fromPredicate(originalPredicate);
    assertEquals(result.getRemainingExpression(), originalPredicate);
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.notNull(BIGINT))));
}
 
Example #14
Source File: TestIonSqlQueryBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecimalColumns()
{
    TypeManager typeManager = this.typeManager;
    IonSqlQueryBuilder queryBuilder = new IonSqlQueryBuilder(typeManager);
    List<HiveColumnHandle> columns = ImmutableList.of(
            createBaseColumn("quantity", 0, HiveType.valueOf("decimal(20,0)"), DecimalType.createDecimalType(), REGULAR, Optional.empty()),
            createBaseColumn("extendedprice", 1, HiveType.valueOf("decimal(20,2)"), DecimalType.createDecimalType(), REGULAR, Optional.empty()),
            createBaseColumn("discount", 2, HiveType.valueOf("decimal(10,2)"), DecimalType.createDecimalType(), REGULAR, Optional.empty()));
    DecimalType decimalType = DecimalType.createDecimalType(10, 2);
    TupleDomain<HiveColumnHandle> tupleDomain = withColumnDomains(
            ImmutableMap.of(
                    columns.get(0), Domain.create(ofRanges(Range.lessThan(DecimalType.createDecimalType(20, 0), longDecimal("50"))), false),
                    columns.get(1), Domain.create(ofRanges(Range.equal(HiveType.valueOf("decimal(20,2)").getType(typeManager), longDecimal("0.05"))), false),
                    columns.get(2), Domain.create(ofRanges(Range.range(decimalType, shortDecimal("0.0"), true, shortDecimal("0.02"), true)), false)));
    assertEquals("SELECT s._1, s._2, s._3 FROM S3Object s WHERE ((case s._1 when '' then null else CAST(s._1 AS DECIMAL(20,0)) end < 50)) AND " +
                    "(case s._2 when '' then null else CAST(s._2 AS DECIMAL(20,2)) end = 0.05) AND ((case s._3 when '' then null else CAST(s._3 AS DECIMAL(10,2)) " +
                    "end >= 0.00 AND case s._3 when '' then null else CAST(s._3 AS DECIMAL(10,2)) end <= 0.02))",
            queryBuilder.buildSql(columns, tupleDomain));
}
 
Example #15
Source File: TestIonSqlQueryBuilder.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildSQL()
{
    IonSqlQueryBuilder queryBuilder = new IonSqlQueryBuilder(typeManager);
    List<HiveColumnHandle> columns = ImmutableList.of(
            createBaseColumn("n_nationkey", 0, HIVE_INT, INTEGER, REGULAR, Optional.empty()),
            createBaseColumn("n_name", 1, HIVE_STRING, VARCHAR, REGULAR, Optional.empty()),
            createBaseColumn("n_regionkey", 2, HIVE_INT, INTEGER, REGULAR, Optional.empty()));

    assertEquals("SELECT s._1, s._2, s._3 FROM S3Object s",
            queryBuilder.buildSql(columns, TupleDomain.all()));
    TupleDomain<HiveColumnHandle> tupleDomain = withColumnDomains(ImmutableMap.of(
            columns.get(2), Domain.create(SortedRangeSet.copyOf(BIGINT, ImmutableList.of(Range.equal(BIGINT, 3L))), false)));
    assertEquals("SELECT s._1, s._2, s._3 FROM S3Object s WHERE (case s._3 when '' then null else CAST(s._3 AS INT) end = 3)",
            queryBuilder.buildSql(columns, tupleDomain));
}
 
Example #16
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testInPredicateWithNull()
{
    assertPredicateTranslates(
            in(C_BIGINT, Arrays.asList(1L, 2L, null)),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));

    assertPredicateIsAlwaysFalse(not(in(C_BIGINT, Arrays.asList(1L, 2L, null))));
    assertPredicateIsAlwaysFalse(in(C_BIGINT, Arrays.asList(new Long[] {null})));
    assertPredicateIsAlwaysFalse(not(in(C_BIGINT, Arrays.asList(new Long[] {null}))));

    assertUnsupportedPredicate(isNull(in(C_BIGINT, Arrays.asList(1L, 2L, null))));
    assertUnsupportedPredicate(isNotNull(in(C_BIGINT, Arrays.asList(1L, 2L, null))));
    assertUnsupportedPredicate(isNull(in(C_BIGINT, Arrays.asList(new Long[] {null}))));
    assertUnsupportedPredicate(isNotNull(in(C_BIGINT, Arrays.asList(new Long[] {null}))));
}
 
Example #17
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromInPredicateWithCastsAndNulls()
{
    assertPredicateIsAlwaysFalse(new InPredicate(
            C_BIGINT.toSymbolReference(),
            new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT)))));

    assertUnsupportedPredicate(not(new InPredicate(
            cast(C_SMALLINT, BIGINT),
            new InListExpression(ImmutableList.of(toExpression(null, BIGINT))))));

    assertPredicateTranslates(
            new InPredicate(
                    C_BIGINT.toSymbolReference(),
                    new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT), toExpression(1L, BIGINT)))),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L)), false))));

    assertPredicateIsAlwaysFalse(not(new InPredicate(
            C_BIGINT.toSymbolReference(),
            new InListExpression(ImmutableList.of(cast(toExpression(null, SMALLINT), BIGINT), toExpression(1L, SMALLINT))))));
}
 
Example #18
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testFromBetweenPredicate()
{
    assertPredicateTranslates(
            between(C_BIGINT, bigintLiteral(1L), bigintLiteral(2L)),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, true, 2L, true)), false))));

    assertPredicateTranslates(
            between(cast(C_INTEGER, DOUBLE), cast(bigintLiteral(1L), DOUBLE), doubleLiteral(2.1)),
            withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.range(INTEGER, 1L, true, 2L, true)), false))));

    assertPredicateIsAlwaysFalse(between(C_BIGINT, bigintLiteral(1L), nullLiteral(BIGINT)));

    // Test complements
    assertPredicateTranslates(
            not(between(C_BIGINT, bigintLiteral(1L), bigintLiteral(2L))),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.greaterThan(BIGINT, 2L)), false))));

    assertPredicateTranslates(
            not(between(cast(C_INTEGER, DOUBLE), cast(bigintLiteral(1L), DOUBLE), doubleLiteral(2.1))),
            withColumnDomains(ImmutableMap.of(C_INTEGER, Domain.create(ValueSet.ofRanges(Range.lessThan(INTEGER, 1L), Range.greaterThan(INTEGER, 2L)), false))));

    assertPredicateTranslates(
            not(between(C_BIGINT, bigintLiteral(1L), nullLiteral(BIGINT))),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L)), false))));
}
 
Example #19
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpressionConstantFolding()
{
    FunctionCall fromHex = new FunctionCallBuilder(metadata)
            .setName(QualifiedName.of("from_hex"))
            .addArgument(VARCHAR, stringLiteral("123456"))
            .build();
    Expression originalExpression = comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), fromHex);
    ExtractionResult result = fromPredicate(originalExpression);
    assertEquals(result.getRemainingExpression(), TRUE_LITERAL);
    Slice value = Slices.wrappedBuffer(BaseEncoding.base16().decode("123456"));
    assertEquals(result.getTupleDomain(), withColumnDomains(ImmutableMap.of(C_VARBINARY, Domain.create(ValueSet.ofRanges(Range.greaterThan(VARBINARY, value)), false))));

    Expression expression = toPredicate(result.getTupleDomain());
    assertEquals(expression, comparison(GREATER_THAN, C_VARBINARY.toSymbolReference(), varbinaryLiteral(value)));
}
 
Example #20
Source File: WindowFilterPushDown.java    From presto with Apache License 2.0 5 votes vote down vote up
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 >= Integer.MIN_VALUE && upperBound <= Integer.MAX_VALUE) {
        return OptionalInt.of(toIntExact(upperBound));
    }
    return OptionalInt.empty();
}
 
Example #21
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromNotPredicate()
{
    assertUnsupportedPredicate(not(and(equal(C_BIGINT, bigintLiteral(1L)), unprocessableExpression1(C_BIGINT))));
    assertUnsupportedPredicate(not(unprocessableExpression1(C_BIGINT)));

    assertPredicateIsAlwaysFalse(not(TRUE_LITERAL));

    assertPredicateTranslates(
            not(equal(C_BIGINT, bigintLiteral(1L))),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.greaterThan(BIGINT, 1L)), false))));
}
 
Example #22
Source File: TestPrestoThriftRangeValueSet.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromValueSetOfRangesUnbounded()
{
    PrestoThriftValueSet thriftValueSet = fromValueSet(ValueSet.ofRanges(Range.greaterThanOrEqual(BIGINT, 0L)));
    assertNotNull(thriftValueSet.getRangeValueSet());
    assertEquals(thriftValueSet.getRangeValueSet().getRanges(), ImmutableList.of(
            new PrestoThriftRange(new PrestoThriftMarker(longValue(0), EXACTLY), new PrestoThriftMarker(null, BELOW))));
}
 
Example #23
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testFromInPredicate()
{
    assertPredicateTranslates(
            in(C_BIGINT, ImmutableList.of(1L)),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.singleValue(BIGINT, 1L))));

    assertPredicateTranslates(
            in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1))),
            withColumnDomains(ImmutableMap.of(C_COLOR, Domain.singleValue(COLOR, COLOR_VALUE_1))));

    assertPredicateTranslates(
            in(C_BIGINT, ImmutableList.of(1L, 2L)),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.equal(BIGINT, 1L), Range.equal(BIGINT, 2L)), false))));

    assertPredicateTranslates(
            in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2))),
            withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2), false))));

    assertPredicateTranslates(
            not(in(C_BIGINT, ImmutableList.of(1L, 2L))),
            withColumnDomains(ImmutableMap.of(C_BIGINT, Domain.create(ValueSet.ofRanges(Range.lessThan(BIGINT, 1L), Range.range(BIGINT, 1L, false, 2L, false), Range.greaterThan(BIGINT, 2L)), false))));

    assertPredicateTranslates(
            not(in(C_COLOR, ImmutableList.of(colorLiteral(COLOR_VALUE_1), colorLiteral(COLOR_VALUE_2)))),
            withColumnDomains(ImmutableMap.of(C_COLOR, Domain.create(ValueSet.of(COLOR, COLOR_VALUE_1, COLOR_VALUE_2).complement(), false))));
}
 
Example #24
Source File: TestPrometheusSplit.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testPredicatePushDownLowerBoundDirect()
        throws Exception
{
    Range lowRange = Range.greaterThanOrEqual(TimestampType.TIMESTAMP, 1570460709643L);
    ValueSet valueSet = ValueSet.ofRanges(lowRange);
    Domain testDomain = Domain.create(valueSet, false);
    TupleDomain<ColumnHandle> testTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>of(
            (ColumnHandle) new PrometheusColumnHandle("timestamp", TimestampType.TIMESTAMP, 2), testDomain));
    prometheusTableHandle.setPredicate(Optional.of(testTupleDomain));
    Optional<PrometheusPredicateTimeInfo> predicateTimes = PrometheusSplitManager.determinePredicateTimes(testTupleDomain);
    ZonedDateTime expected = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1570460709643L), ZoneId.systemDefault());
    assertEquals(predicateTimes.get().predicateLowerTimeBound.get(), expected);
}
 
Example #25
Source File: TestCoordinatorDynamicFiltering.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testJoinWithNonSelectiveBuildSide()
{
    assertQueryDynamicFilters(
            "SELECT * FROM lineitem JOIN tpch.tiny.supplier ON lineitem.suppkey = supplier.suppkey",
            TupleDomain.withColumnDomains(ImmutableMap.of(
                    suppKeyHandle,
                    Domain.create(ValueSet.ofRanges(Range.range(BIGINT, 1L, true, 100L, true)), false))));
}
 
Example #26
Source File: TestPrometheusSplit.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testPredicatePushDownSetsUpperAndLowerBound()
        throws Exception
{
    long predicateHighValue = 1568638171999L;
    Range highRange = Range.equal(TimestampType.TIMESTAMP, predicateHighValue);
    long predicateLowValue = 1568638171999L - 600000L;
    Range lowRange = Range.equal(TimestampType.TIMESTAMP, predicateLowValue);
    ValueSet valueSet = ValueSet.ofRanges(lowRange, highRange);

    Domain testDomain = Domain.create(valueSet, false);
    TupleDomain<ColumnHandle> testTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>of(
            (ColumnHandle) new PrometheusColumnHandle("timestamp", TimestampType.TIMESTAMP, 2), testDomain));
    prometheusTableHandle.setPredicate(Optional.of(testTupleDomain));
    io.airlift.units.Duration maxQueryRangeDuration = new io.airlift.units.Duration(120, TimeUnit.SECONDS);
    io.airlift.units.Duration queryChunkSizeDuration = new io.airlift.units.Duration(30, TimeUnit.SECONDS);
    LocalDateTime now = ofInstant(Instant.ofEpochMilli(1568638171999L + 1200000L), ZoneId.systemDefault());
    PrometheusTimeMachine.useFixedClockAt(now);

    List<String> splitTimes = PrometheusSplitManager.generateTimesForSplits(now, maxQueryRangeDuration, queryChunkSizeDuration, prometheusTableHandle);

    TemporalAmount expectedMaxQueryAsTime = java.time.Duration.ofMillis(new io.airlift.units.Duration(10, TimeUnit.MINUTES).toMillis() +
            ((splitTimes.size() - 1) * OFFSET_MILLIS));
    String lastSplit = splitTimes.get(splitTimes.size() - 1);
    ZonedDateTime lastSplitAsTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(longFromDecimalSecondString(lastSplit)), ZoneId.systemDefault());
    String earliestSplit = splitTimes.get(0);
    ZonedDateTime earliestSplitAsTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(longFromDecimalSecondString(earliestSplit)), ZoneId.systemDefault());
    TemporalAmount queryChunkAsTime = java.time.Duration.ofMillis(queryChunkSizeDuration.toMillis());
    java.time.Duration actualMaxDuration = Duration.between(earliestSplitAsTime
            .minus(queryChunkAsTime), lastSplitAsTime);

    assertEquals(lastSplitAsTime.toInstant().toEpochMilli(), 1568638171999L);
    assertEquals(actualMaxDuration, expectedMaxQueryAsTime);
}
 
Example #27
Source File: TestPrometheusSplit.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testPredicatePushDownSetsUpperBoundOnly()
        throws Exception
{
    long predicateHighValue = 1568638171999L;
    Range highRange = Range.lessThanOrEqual(TimestampType.TIMESTAMP, predicateHighValue);
    ValueSet valueSet = ValueSet.ofRanges(highRange);

    Domain testDomain = Domain.create(valueSet, false);
    TupleDomain<ColumnHandle> testTupleDomain = TupleDomain.withColumnDomains(ImmutableMap.<ColumnHandle, Domain>of(
            (ColumnHandle) new PrometheusColumnHandle("timestamp", TimestampType.TIMESTAMP, 2), testDomain));
    prometheusTableHandle.setPredicate(Optional.of(testTupleDomain));
    io.airlift.units.Duration maxQueryRangeDuration = new io.airlift.units.Duration(120, TimeUnit.SECONDS);
    io.airlift.units.Duration queryChunkSizeDuration = new io.airlift.units.Duration(30, TimeUnit.SECONDS);
    LocalDateTime now = ofInstant(Instant.ofEpochMilli(1568638171999L + 600000L), ZoneId.systemDefault());
    PrometheusTimeMachine.useFixedClockAt(now);

    List<String> splitTimes = PrometheusSplitManager.generateTimesForSplits(now, maxQueryRangeDuration, queryChunkSizeDuration, prometheusTableHandle);

    TemporalAmount expectedMaxQueryAsTime = java.time.Duration.ofMillis(maxQueryRangeDuration.toMillis() +
            ((splitTimes.size() - 1) * OFFSET_MILLIS));
    String lastSplit = splitTimes.get(splitTimes.size() - 1);
    ZonedDateTime lastSplitAsTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(longFromDecimalSecondString(lastSplit)), ZoneId.systemDefault());
    String earliestSplit = splitTimes.get(0);
    ZonedDateTime earliestSplitAsTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(longFromDecimalSecondString(earliestSplit)), ZoneId.systemDefault());
    TemporalAmount queryChunkAsTime = java.time.Duration.ofMillis(queryChunkSizeDuration.toMillis());
    java.time.Duration actualMaxDuration = Duration.between(earliestSplitAsTime
            .minus(queryChunkAsTime), lastSplitAsTime);

    assertEquals(lastSplitAsTime.toInstant().toEpochMilli(), 1568638171999L);
    assertEquals(actualMaxDuration, expectedMaxQueryAsTime);
}
 
Example #28
Source File: TestLogicalPlanner.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testLikePredicate()
{
    assertPlan("SELECT type FROM part WHERE type LIKE 'LARGE PLATED %'",
            anyTree(
                    tableScan(
                            tableHandle -> {
                                Map<ColumnHandle, Domain> domains = ((TpchTableHandle) tableHandle).getConstraint().getDomains()
                                        .orElseThrow(() -> new AssertionError("Unexpected none TupleDomain"));

                                Domain domain = domains.entrySet().stream()
                                        .filter(entry -> ((TpchColumnHandle) entry.getKey()).getColumnName().equals("type"))
                                        .map(Entry::getValue)
                                        .collect(toOptional())
                                        .orElseThrow(() -> new AssertionError("No domain for 'type'"));

                                assertEquals(domain, Domain.multipleValues(
                                        createVarcharType(25),
                                        ImmutableList.of("LARGE PLATED BRASS", "LARGE PLATED COPPER", "LARGE PLATED NICKEL", "LARGE PLATED STEEL", "LARGE PLATED TIN").stream()
                                                .map(Slices::utf8Slice)
                                                .collect(toImmutableList())));
                                return true;
                            },
                            TupleDomain.withColumnDomains(ImmutableMap.of(
                                    tableHandle -> ((TpchColumnHandle) tableHandle).getColumnName().equals("type"),
                                    Domain.create(
                                            ValueSet.ofRanges(Range.range(createVarcharType(25), utf8Slice("LARGE PLATED "), true, utf8Slice("LARGE PLATED!"), false)),
                                            false))),
                            ImmutableMap.of())));
}
 
Example #29
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleCoercionsOnSymbolSide()
{
    assertPredicateTranslates(
            comparison(GREATER_THAN, cast(cast(C_SMALLINT, REAL), DOUBLE), doubleLiteral(3.7)),
            withColumnDomains(ImmutableMap.of(C_SMALLINT, Domain.create(ValueSet.ofRanges(Range.greaterThan(SMALLINT, 3L)), false))));
}
 
Example #30
Source File: TestDomainTranslator.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testCharComparedToVarcharExpression()
{
    Type charType = createCharType(10);
    // varchar literal is coerced to column (char) type
    testSimpleComparison(equal(C_CHAR, cast(stringLiteral("abc"), charType)), C_CHAR, Range.equal(charType, Slices.utf8Slice("abc")));

    // both sides got coerced to char(11)
    charType = createCharType(11);
    assertUnsupportedPredicate(equal(cast(C_CHAR, charType), cast(stringLiteral("abc12345678"), charType)));
}