Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.type.SqlTypeFamily;
import org.apache.calcite.avatica.util.TimeUnit;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.SqlIntervalQualifier;
import org.apache.druid.java.util.common.DateTimes;
import org.apache.druid.java.util.common.ISE;
import org.apache.druid.java.util.common.StringUtils;
Expand Down Expand Up @@ -371,7 +373,14 @@ public static DruidLiteral calciteLiteralToDruidLiteral(
retVal = new DruidLiteral(ExpressionType.DOUBLE, number == null ? null : number.doubleValue());
} else if (SqlTypeFamily.INTERVAL_DAY_TIME == sqlTypeName.getFamily()) {
// Calcite represents DAY-TIME intervals in milliseconds.
final long milliseconds = ((Number) RexLiteral.value(rexNode)).longValue();
long milliseconds = ((Number) RexLiteral.value(rexNode)).longValue();
// Calcite has a known quirk where WEEK interval literals are stored as 1 hour in the
// INTERVAL_DAY_TIME family (see https://github.com/apache/druid/issues/18665).
// Detect the WEEK qualifier and convert the value to 7 days.
final SqlIntervalQualifier intervalQualifier = rexNode.getType().getIntervalQualifier();
if (intervalQualifier != null && intervalQualifier.getStartUnit() == TimeUnit.WEEK) {
milliseconds = milliseconds * 7 * 24;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Avoid rescaling already-correct WEEK literals

This branch uses Calcite 1.41, which already converts WEEK intervals to millisecond values. A narrow probe produced 604800000 for INTERVAL '1' WEEK; multiplying by 7 * 24 changes it to 101606400000 ms, making existing quoted and unquoted WEEK expressions 168 times too large. Apply the workaround only to the pre-1.38 representation or remove it.

}
retVal = new DruidLiteral(ExpressionType.LONG, milliseconds);
} else if (SqlTypeFamily.INTERVAL_YEAR_MONTH == sqlTypeName.getFamily()) {
// Calcite represents YEAR-MONTH intervals in months.
Expand Down
Loading