Skip to content

Commit 34cbaca

Browse files
committed
Fix quarter interval bug in calcite (counte-react)
Signed-off-by: Yuanchun Shen <yuanchu@amazon.com>
1 parent 1772ef9 commit 34cbaca

2 files changed

Lines changed: 12 additions & 8 deletions

File tree

core/src/main/java/org/opensearch/sql/calcite/validate/PplRelToSqlNodeConverter.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414
*
1515
* <p>This converter is used during the validation phase to convert RelNode back to SqlNode for
1616
* validation and type checking using Calcite's SqlValidator.
17-
*
18-
* <p>Note: Interval literal issues are handled by preprocessing with {@link
19-
* IntervalLiteralFixShuttle} before conversion.
2017
*/
21-
public class PplRelToSqlConverter extends RelToSqlConverter {
18+
public class PplRelToSqlNodeConverter extends RelToSqlConverter {
2219
/**
2320
* Creates a RelToSqlConverter for PPL.
2421
*
2522
* @param dialect the SQL dialect to use for conversion
2623
*/
27-
public PplRelToSqlConverter(SqlDialect dialect) {
24+
public PplRelToSqlNodeConverter(SqlDialect dialect) {
2825
super(dialect);
2926
}
3027
}

core/src/main/java/org/opensearch/sql/calcite/validate/PplRelToSqlRelShuttle.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ public RexNode visitLiteral(RexLiteral literal) {
3939
return literal;
4040
}
4141
TimeUnit unit = qualifier.getUnit();
42+
// An ad-hoc fix to a Calcite bug in RexLiteral#intervalString -- quarter type does not
43+
// exist in SqlTypeName, rendering it return number of months instead of number of
44+
// quarters.
45+
BigDecimal forwardMultiplier =
46+
TimeUnit.QUARTER.equals(unit) ? BigDecimal.valueOf(1) : unit.multiplier;
47+
48+
// QUARTER intervals are stored as INTERVAL_MONTH in Calcite's type system
49+
// but the qualifier preserves the actual unit (QUARTER vs MONTH).
50+
// The multiplier for QUARTER is 3 (months), for MONTH is 1.
4251
BigDecimal newValue =
4352
forward
44-
? value.multiply(unit.multiplier)
53+
? value.multiply(forwardMultiplier)
4554
: value.divideToIntegralValue(unit.multiplier);
4655
return rexBuilder.makeIntervalLiteral(newValue, qualifier);
4756
}
@@ -50,9 +59,7 @@ public RexNode visitLiteral(RexLiteral literal) {
5059

5160
@Override
5261
protected RelNode visitChild(RelNode parent, int i, RelNode child) {
53-
// First visit the child recursively
5462
RelNode newChild = super.visitChild(parent, i, child);
55-
// Then apply the RexShuttle to the child's expressions
5663
return newChild.accept(rexShuttle);
5764
}
5865
}

0 commit comments

Comments
 (0)