Search in sources :

Example 1 with AbstractInterval

use of org.joda.time.base.AbstractInterval in project druid by apache.

the class DruidSqlParserUtils method validateQueryAndConvertToIntervals.

/**
 * This method validates and converts a {@link SqlNode} representing a query into an optmizied list of intervals to
 * be used in creating an ingestion spec. If the sqlNode is an SqlLiteral of {@link #ALL}, returns a singleton list of
 * "ALL". Otherwise, it converts and optimizes the query using {@link MoveTimeFiltersToIntervals} into a list of
 * intervals which contain all valid values of time as per the query.
 *
 * The following validations are performed
 * 1. Only __time column and timestamp literals are present in the query
 * 2. The interval after optimization is not empty
 * 3. The operands in the expression are supported
 * 4. The intervals after adjusting for timezone are aligned with the granularity parameter
 *
 * @param replaceTimeQuery Sql node representing the query
 * @param granularity granularity of the query for validation
 * @param dateTimeZone timezone
 * @return List of string representation of intervals
 * @throws ValidationException if the SqlNode cannot be converted to a list of intervals
 */
public static List<String> validateQueryAndConvertToIntervals(SqlNode replaceTimeQuery, Granularity granularity, DateTimeZone dateTimeZone) throws ValidationException {
    if (replaceTimeQuery instanceof SqlLiteral && ALL.equalsIgnoreCase(((SqlLiteral) replaceTimeQuery).toValue())) {
        return ImmutableList.of(ALL);
    }
    DimFilter dimFilter = convertQueryToDimFilter(replaceTimeQuery, dateTimeZone);
    Filtration filtration = Filtration.create(dimFilter);
    filtration = MoveTimeFiltersToIntervals.instance().apply(filtration);
    List<Interval> intervals = filtration.getIntervals();
    if (filtration.getDimFilter() != null) {
        throw new ValidationException("Only " + ColumnHolder.TIME_COLUMN_NAME + " column is supported in OVERWRITE WHERE clause");
    }
    if (intervals.isEmpty()) {
        throw new ValidationException("Intervals for replace are empty");
    }
    for (Interval interval : intervals) {
        DateTime intervalStart = interval.getStart();
        DateTime intervalEnd = interval.getEnd();
        if (!granularity.bucketStart(intervalStart).equals(intervalStart) || !granularity.bucketStart(intervalEnd).equals(intervalEnd)) {
            throw new ValidationException("OVERWRITE WHERE clause contains an interval " + intervals + " which is not aligned with PARTITIONED BY granularity " + granularity);
        }
    }
    return intervals.stream().map(AbstractInterval::toString).collect(Collectors.toList());
}
Also used : Filtration(org.apache.druid.sql.calcite.filtration.Filtration) ValidationException(org.apache.calcite.tools.ValidationException) SqlLiteral(org.apache.calcite.sql.SqlLiteral) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) NotDimFilter(org.apache.druid.query.filter.NotDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) AbstractInterval(org.joda.time.base.AbstractInterval) Interval(org.joda.time.Interval)

Example 2 with AbstractInterval

use of org.joda.time.base.AbstractInterval in project druid by druid-io.

the class DruidSqlParserUtils method validateQueryAndConvertToIntervals.

/**
 * This method validates and converts a {@link SqlNode} representing a query into an optmizied list of intervals to
 * be used in creating an ingestion spec. If the sqlNode is an SqlLiteral of {@link #ALL}, returns a singleton list of
 * "ALL". Otherwise, it converts and optimizes the query using {@link MoveTimeFiltersToIntervals} into a list of
 * intervals which contain all valid values of time as per the query.
 *
 * The following validations are performed
 * 1. Only __time column and timestamp literals are present in the query
 * 2. The interval after optimization is not empty
 * 3. The operands in the expression are supported
 * 4. The intervals after adjusting for timezone are aligned with the granularity parameter
 *
 * @param replaceTimeQuery Sql node representing the query
 * @param granularity granularity of the query for validation
 * @param dateTimeZone timezone
 * @return List of string representation of intervals
 * @throws ValidationException if the SqlNode cannot be converted to a list of intervals
 */
public static List<String> validateQueryAndConvertToIntervals(SqlNode replaceTimeQuery, Granularity granularity, DateTimeZone dateTimeZone) throws ValidationException {
    if (replaceTimeQuery instanceof SqlLiteral && ALL.equalsIgnoreCase(((SqlLiteral) replaceTimeQuery).toValue())) {
        return ImmutableList.of(ALL);
    }
    DimFilter dimFilter = convertQueryToDimFilter(replaceTimeQuery, dateTimeZone);
    Filtration filtration = Filtration.create(dimFilter);
    filtration = MoveTimeFiltersToIntervals.instance().apply(filtration);
    List<Interval> intervals = filtration.getIntervals();
    if (filtration.getDimFilter() != null) {
        throw new ValidationException("Only " + ColumnHolder.TIME_COLUMN_NAME + " column is supported in OVERWRITE WHERE clause");
    }
    if (intervals.isEmpty()) {
        throw new ValidationException("Intervals for replace are empty");
    }
    for (Interval interval : intervals) {
        DateTime intervalStart = interval.getStart();
        DateTime intervalEnd = interval.getEnd();
        if (!granularity.bucketStart(intervalStart).equals(intervalStart) || !granularity.bucketStart(intervalEnd).equals(intervalEnd)) {
            throw new ValidationException("OVERWRITE WHERE clause contains an interval " + intervals + " which is not aligned with PARTITIONED BY granularity " + granularity);
        }
    }
    return intervals.stream().map(AbstractInterval::toString).collect(Collectors.toList());
}
Also used : Filtration(org.apache.druid.sql.calcite.filtration.Filtration) ValidationException(org.apache.calcite.tools.ValidationException) SqlLiteral(org.apache.calcite.sql.SqlLiteral) BoundDimFilter(org.apache.druid.query.filter.BoundDimFilter) AndDimFilter(org.apache.druid.query.filter.AndDimFilter) NotDimFilter(org.apache.druid.query.filter.NotDimFilter) DimFilter(org.apache.druid.query.filter.DimFilter) OrDimFilter(org.apache.druid.query.filter.OrDimFilter) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) AbstractInterval(org.joda.time.base.AbstractInterval) Interval(org.joda.time.Interval)

Aggregations

ZonedDateTime (java.time.ZonedDateTime)2 SqlLiteral (org.apache.calcite.sql.SqlLiteral)2 ValidationException (org.apache.calcite.tools.ValidationException)2 AndDimFilter (org.apache.druid.query.filter.AndDimFilter)2 BoundDimFilter (org.apache.druid.query.filter.BoundDimFilter)2 DimFilter (org.apache.druid.query.filter.DimFilter)2 NotDimFilter (org.apache.druid.query.filter.NotDimFilter)2 OrDimFilter (org.apache.druid.query.filter.OrDimFilter)2 Filtration (org.apache.druid.sql.calcite.filtration.Filtration)2 DateTime (org.joda.time.DateTime)2 Interval (org.joda.time.Interval)2 AbstractInterval (org.joda.time.base.AbstractInterval)2