adarshsanjeev commented on code in PR #12386:
URL: https://github.com/apache/druid/pull/12386#discussion_r862680464
##########
sql/src/main/java/org/apache/druid/sql/calcite/parser/DruidSqlParserUtils.java:
##########
@@ -167,4 +189,89 @@ public static Granularity
convertSqlNodeToGranularity(SqlNode sqlNode) throws Pa
TimeFloorOperatorConversion.SQL_FUNCTION_NAME
));
}
+
+ 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);
+ if
(!ImmutableSet.of(ColumnHolder.TIME_COLUMN_NAME).equals(dimFilter.getRequiredColumns()))
{
+ throw new ValidationException("Only " + ColumnHolder.TIME_COLUMN_NAME +
" column is supported in OVERWRITE WHERE clause");
+ }
+
+ Filtration filtration = Filtration.create(dimFilter);
+ filtration = MoveTimeFiltersToIntervals.instance().apply(filtration);
+ List<Interval> intervals = filtration.getIntervals();
+
+ 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 which is not aligned with PARTITIONED BY granularity");
+ }
+ }
+ return intervals
+ .stream()
+ .map(AbstractInterval::toString)
+ .collect(Collectors.toList());
+ }
+
+ public static DimFilter convertQueryToDimFilter(SqlNode replaceTimeQuery,
DateTimeZone dateTimeZone)
+ throws ValidationException
+ {
+ if (replaceTimeQuery instanceof SqlBasicCall) {
+ SqlBasicCall sqlBasicCall = (SqlBasicCall) replaceTimeQuery;
+ Pair<String, String> columnValuePair;
+ switch (sqlBasicCall.getOperator().getKind()) {
+ case AND:
+ List<DimFilter> dimFilters = new ArrayList<>();
+ for (SqlNode sqlNode : sqlBasicCall.getOperandList()) {
+ dimFilters.add(convertQueryToDimFilter(sqlNode, dateTimeZone));
+ }
+ return new AndDimFilter(dimFilters);
+ case OR:
+ dimFilters = new ArrayList<>();
+ for (SqlNode sqlNode : sqlBasicCall.getOperandList()) {
+ dimFilters.add(convertQueryToDimFilter(sqlNode, dateTimeZone));
+ }
+ return new OrDimFilter(dimFilters);
+ case NOT:
+ return new
NotDimFilter(convertQueryToDimFilter(sqlBasicCall.getOperandList().get(0),
dateTimeZone));
+ case GREATER_THAN_OR_EQUAL:
+ columnValuePair =
createColumnValuePair(sqlBasicCall.getOperandList(), dateTimeZone);
+ return new BoundDimFilter(columnValuePair.left,
columnValuePair.right, null, false, null, null, null,
StringComparators.NUMERIC);
+ case LESS_THAN_OR_EQUAL:
+ columnValuePair =
createColumnValuePair(sqlBasicCall.getOperandList(), dateTimeZone);
+ return new BoundDimFilter(columnValuePair.left, null,
columnValuePair.right, null, false, null, null, StringComparators.NUMERIC);
+ case GREATER_THAN:
+ columnValuePair =
createColumnValuePair(sqlBasicCall.getOperandList(), dateTimeZone);
+ return new BoundDimFilter(columnValuePair.left,
columnValuePair.right, null, true, null, null, null, StringComparators.NUMERIC);
+ case LESS_THAN:
+ columnValuePair =
createColumnValuePair(sqlBasicCall.getOperandList(), dateTimeZone);
+ return new BoundDimFilter(columnValuePair.left, null,
columnValuePair.right, null, true, null, null, StringComparators.NUMERIC);
+ default:
Review Comment:
Added support. One thing to note is that BETWEEN in sql is inclusive for the
interval. This might lead to some strange queries in most use cases, like
`__time BETWEEN TIMESTAMP '2000-01-01' AND TIMESTAMP '2000-01-31
23:59:59.999'`. Should we stick with convention or make the end time exclusive?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]