Skip to content

Commit d6a5914

Browse files
committed
Enhance doc and error message handling for bins on time-related fields (opensearch-project#4713)
(cherry picked from commit ef4c51e) Signed-off-by: Kai Huang <ahkcs@amazon.com>
1 parent d994ca9 commit d6a5914

5 files changed

Lines changed: 522 additions & 1 deletion

File tree

core/src/main/java/org/opensearch/sql/calcite/utils/CalciteToolsHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,17 @@ public RelNode visit(TableScan scan) {
360360
final RelRunner runner = connection.unwrap(RelRunner.class);
361361
return runner.prepareStatement(rel);
362362
} catch (SQLException e) {
363+
// Detect if error is due to window functions in unsupported context (bins on time fields)
364+
String errorMsg = e.getMessage();
365+
if (errorMsg != null
366+
&& errorMsg.contains("Error while preparing plan")
367+
&& errorMsg.contains("WIDTH_BUCKET")) {
368+
throw new UnsupportedOperationException(
369+
"The 'bins' parameter on timestamp fields requires: (1) pushdown to be enabled"
370+
+ " (controlled by plugins.calcite.pushdown.enabled, enabled by default), and"
371+
+ " (2) the timestamp field to be used as an aggregation bucket (e.g., 'stats"
372+
+ " count() by @timestamp').");
373+
}
363374
throw Util.throwAsRuntime(e);
364375
}
365376
}

core/src/main/java/org/opensearch/sql/calcite/utils/binning/handlers/CountBinHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ public RexNode createExpression(
2828
CountBin countBin = (CountBin) node;
2929

3030
// Create validated binnable field (validates that field is numeric or time-based)
31-
// Note: bins parameter works with both numeric and time-based fields
31+
// Note: bins parameter on time-based fields requires:
32+
// 1. Pushdown to be enabled (controlled by plugins.calcite.pushdown.enabled, enabled by
33+
// default)
34+
// 2. The time-based field to be used as an aggregation bucket
35+
// (e.g., stats count() by @timestamp)
3236
String fieldName = BinFieldValidator.extractFieldName(node);
3337
BinnableField field = new BinnableField(fieldExpr, fieldExpr.getType(), fieldName);
3438

0 commit comments

Comments
 (0)