Java Code Examples for io.prestosql.spi.predicate.Range

The following examples show how to use io.prestosql.spi.predicate.Range. These examples are extracted from open source projects. 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 Project: presto   Source File: TestPrometheusSplit.java    License: 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 2
Source Project: presto   Source File: TestJdbcQueryBuilder.java    License: 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 Project: presto   Source File: TestJdbcQueryBuilder.java    License: 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 4
Source Project: presto   Source File: TupleDomainOrcPredicate.java    License: 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 5
Source Project: presto   Source File: TupleDomainOrcPredicate.java    License: 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 Project: presto   Source File: TestTupleDomainOrcPredicate.java    License: 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 7
Source Project: presto   Source File: TestDomainTranslator.java    License: 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 8
Source Project: presto   Source File: TestDomainTranslator.java    License: 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 9
Source Project: presto   Source File: TestDomainTranslator.java    License: 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 10
Source Project: presto   Source File: TestDomainTranslator.java    License: 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 11
Source Project: presto   Source File: TestDomainTranslator.java    License: 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 12
Source Project: presto   Source File: TestDomainTranslator.java    License: 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 13
Source Project: presto   Source File: TestDomainTranslator.java    License: 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 14
Source Project: presto   Source File: TestIonSqlQueryBuilder.java    License: 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 15
Source Project: presto   Source File: TestIonSqlQueryBuilder.java    License: 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 16
Source Project: presto   Source File: TestIonSqlQueryBuilder.java    License: 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 17
Source Project: presto   Source File: AtopSplitManager.java    License: 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 18
Source Project: presto   Source File: TupleDomainParquetPredicate.java    License: 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 19
Source Project: presto   Source File: TestDynamicTable.java    License: 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 20
Source Project: presto   Source File: TestPrometheusSplit.java    License: 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 21
Source Project: presto   Source File: TestPrometheusSplit.java    License: 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 22
Source Project: presto   Source File: TestPrometheusSplit.java    License: 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 23
Source Project: presto   Source File: TestCoordinatorDynamicFiltering.java    License: 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 24
Source Project: presto   Source File: TestJdbcQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildSqlWithChar()
        throws SQLException
{
    CharType charType = CharType.createCharType(0);
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
            columns.get(11), Domain.create(SortedRangeSet.copyOf(charType,
                    ImmutableList.of(
                            Range.range(charType, utf8Slice("test_str_700"), true, utf8Slice("test_str_702"), false),
                            Range.equal(charType, utf8Slice("test_str_180")),
                            Range.equal(charType, 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_11"));
        }
        assertEquals(builder.build(), ImmutableSet.of("test_str_700", "test_str_701", "test_str_180", "test_str_196"));

        assertContains(preparedStatement.toString(), "\"col_11\" >= ?");
        assertContains(preparedStatement.toString(), "\"col_11\" < ?");
        assertContains(preparedStatement.toString(), "\"col_11\" IN (?,?)");
    }
}
 
Example 25
Source Project: presto   Source File: TestJdbcQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildSqlWithDateTime()
        throws SQLException
{
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
            columns.get(4), Domain.create(SortedRangeSet.copyOf(DATE,
                    ImmutableList.of(
                            Range.range(DATE, toDays(2016, 6, 7), true, toDays(2016, 6, 17), false),
                            Range.equal(DATE, toDays(2016, 6, 3)),
                            Range.equal(DATE, toDays(2016, 10, 21)))),
                    false),
            columns.get(5), Domain.create(SortedRangeSet.copyOf(TIME,
                    ImmutableList.of(
                            Range.range(TIME, toTimeRepresentation(SESSION, 6, 12, 23), false, toTimeRepresentation(SESSION, 8, 23, 37), true),
                            Range.equal(TIME, toTimeRepresentation(SESSION, 2, 3, 4)),
                            Range.equal(TIME, toTimeRepresentation(SESSION, 20, 23, 37)))),
                    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<Date> dateBuilder = ImmutableSet.builder();
        ImmutableSet.Builder<Time> timeBuilder = ImmutableSet.builder();
        while (resultSet.next()) {
            dateBuilder.add((Date) resultSet.getObject("col_4"));
            timeBuilder.add((Time) resultSet.getObject("col_5"));
        }
        assertEquals(dateBuilder.build(), ImmutableSet.of(toDate(2016, 6, 7), toDate(2016, 6, 13), toDate(2016, 10, 21)));
        assertEquals(timeBuilder.build(), ImmutableSet.of(toTime(8, 23, 37), toTime(20, 23, 37)));

        assertContains(preparedStatement.toString(), "\"col_4\" >= ?");
        assertContains(preparedStatement.toString(), "\"col_4\" < ?");
        assertContains(preparedStatement.toString(), "\"col_4\" IN (?,?)");
        assertContains(preparedStatement.toString(), "\"col_5\" > ?");
        assertContains(preparedStatement.toString(), "\"col_5\" <= ?");
        assertContains(preparedStatement.toString(), "\"col_5\" IN (?,?)");
    }
}
 
Example 26
Source Project: presto   Source File: TestJdbcQueryBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildSqlWithTimestamp()
        throws SQLException
{
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(
            columns.get(6), Domain.create(SortedRangeSet.copyOf(TIMESTAMP,
                    ImmutableList.of(
                            Range.equal(TIMESTAMP, toPrestoTimestamp(2016, 6, 3, 0, 23, 37)),
                            Range.equal(TIMESTAMP, toPrestoTimestamp(2016, 10, 19, 16, 23, 37)),
                            Range.range(TIMESTAMP, toPrestoTimestamp(2016, 6, 7, 8, 23, 37), false, toPrestoTimestamp(2016, 6, 9, 12, 23, 37), true))),
                    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<Timestamp> builder = ImmutableSet.builder();
        while (resultSet.next()) {
            builder.add((Timestamp) resultSet.getObject("col_6"));
        }
        assertEquals(builder.build(), ImmutableSet.of(
                toTimestamp(2016, 6, 3, 0, 23, 37),
                toTimestamp(2016, 6, 8, 10, 23, 37),
                toTimestamp(2016, 6, 9, 12, 23, 37),
                toTimestamp(2016, 10, 19, 16, 23, 37)));

        assertContains(preparedStatement.toString(), "\"col_6\" > ?");
        assertContains(preparedStatement.toString(), "\"col_6\" <= ?");
        assertContains(preparedStatement.toString(), "\"col_6\" IN (?,?)");
    }
}
 
Example 27
Source Project: presto   Source File: TestPrestoThriftRangeValueSet.java    License: 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 28
Source Project: presto   Source File: InformationSchemaMetadata.java    License: Apache License 2.0 5 votes vote down vote up
private <T> Optional<Set<String>> filterString(TupleDomain<T> constraint, T column)
{
    if (constraint.isNone()) {
        return Optional.of(ImmutableSet.of());
    }

    Domain domain = constraint.getDomains().get().get(column);
    if (domain == null) {
        return Optional.empty();
    }

    if (domain.isSingleValue()) {
        return Optional.of(ImmutableSet.of(((Slice) domain.getSingleValue()).toStringUtf8()));
    }
    if (domain.getValues() instanceof EquatableValueSet) {
        Collection<Object> values = ((EquatableValueSet) domain.getValues()).getValues();
        return Optional.of(values.stream()
                .map(Slice.class::cast)
                .map(Slice::toStringUtf8)
                .collect(toImmutableSet()));
    }
    if (domain.getValues() instanceof SortedRangeSet) {
        ImmutableSet.Builder<String> result = ImmutableSet.builder();
        for (Range range : domain.getValues().getRanges().getOrderedRanges()) {
            checkState(!range.isAll()); // Already checked
            if (!range.isSingleValue()) {
                return Optional.empty();
            }

            result.add(((Slice) range.getSingleValue()).toStringUtf8());
        }

        return Optional.of(result.build());
    }
    return Optional.empty();
}
 
Example 29
Source Project: presto   Source File: WindowFilterPushDown.java    License: 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 30
Source Project: presto   Source File: DomainTranslator.java    License: Apache License 2.0 5 votes vote down vote up
private Expression combineRangeWithExcludedPoints(Type type, SymbolReference reference, Range range, List<Expression> excludedPoints)
{
    if (excludedPoints.isEmpty()) {
        return processRange(type, range, reference);
    }

    Expression excludedPointsExpression = new NotExpression(new InPredicate(reference, new InListExpression(excludedPoints)));
    if (excludedPoints.size() == 1) {
        excludedPointsExpression = new ComparisonExpression(NOT_EQUAL, reference, getOnlyElement(excludedPoints));
    }

    return combineConjuncts(metadata, processRange(type, range, reference), excludedPointsExpression);
}