Skip to content

Commit

Permalink
feat: update error message
Browse files Browse the repository at this point in the history
  • Loading branch information
evenyag committed Sep 26, 2024
1 parent a5c2e55 commit 73bfcd1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 28 deletions.
34 changes: 26 additions & 8 deletions src/query/src/range_select/plan_rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use catalog::table_source::DfTableSourceProvider;
use chrono::Utc;
use common_time::interval::{MS_PER_DAY, NANOS_PER_MILLI};
use common_time::timestamp::TimeUnit;
use common_time::{IntervalDayTime, IntervalMonthDayNano, Timestamp, Timezone};
use common_time::{IntervalDayTime, IntervalMonthDayNano, IntervalYearMonth, Timestamp, Timezone};
use datafusion::datasource::DefaultTableSource;
use datafusion::prelude::Column;
use datafusion::scalar::ScalarValue;
Expand Down Expand Up @@ -160,18 +160,36 @@ fn evaluate_expr_to_millisecond(args: &[Expr], i: usize, interval_only: bool) ->
Expr::Literal(ScalarValue::TimestampSecond(ts_secs, _))
| Expr::Literal(ScalarValue::DurationSecond(ts_secs)) => ts_secs.map(|v| v * 1_000),
// We don't support interval with months as days in a month is unclear.
Expr::Literal(ScalarValue::IntervalYearMonth(interval)) => interval
.map(|v| {
let interval = IntervalYearMonth::from_i32(v);
if interval.months != 0 {
return Err(DataFusionError::Plan(format!(
"Year or month interval is not allowed in range query: {}",
expr.display_name().unwrap_or_default()
)));
}

Ok(0)
})
.transpose()?,
Expr::Literal(ScalarValue::IntervalDayTime(interval)) => interval.map(|v| {
let interval = IntervalDayTime::from_i64(v);
interval.as_millis()
}),
Expr::Literal(ScalarValue::IntervalMonthDayNano(interval)) => interval.and_then(|v| {
let interval = IntervalMonthDayNano::from_i128(v);
if interval.months != 0 {
return None;
}
Expr::Literal(ScalarValue::IntervalMonthDayNano(interval)) => interval
.map(|v| {
let interval = IntervalMonthDayNano::from_i128(v);
if interval.months != 0 {
return Err(DataFusionError::Plan(format!(
"Year or month interval is not allowed in range query: {}",
expr.display_name().unwrap_or_default()
)));
}

Some(interval.days as i64 * MS_PER_DAY + interval.nanoseconds / NANOS_PER_MILLI)
}),
Ok(interval.days as i64 * MS_PER_DAY + interval.nanoseconds / NANOS_PER_MILLI)
})
.transpose()?,
_ => None,
}
.ok_or_else(|| {
Expand Down
31 changes: 12 additions & 19 deletions tests/cases/standalone/common/range/interval.result
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,18 @@ Affected Rows: 8

SELECT ts, host, min(val) RANGE (INTERVAL '1 year') FROM host ALIGN (INTERVAL '1 year') ORDER BY host, ts;

+---------------------+-------+----------------------------------------------------------------------------+
| ts | host | MIN(host.val) RANGE IntervalMonthDayNano("950737950171172051122527404032") |
+---------------------+-------+----------------------------------------------------------------------------+
| 1970-01-01T00:00:00 | host1 | 0 |
| 1970-12-27T00:00:00 | host1 | 2 |
| 1970-01-01T00:00:00 | host2 | 4 |
| 1970-12-27T00:00:00 | host2 | 6 |
+---------------------+-------+----------------------------------------------------------------------------+

SELECT ts, host, min(val) RANGE (INTERVAL '1' year) FROM host ALIGN (INTERVAL '1' year) ORDER BY host, ts;

+---------------------+-------+----------------------------------------------------------------------------+
| ts | host | MIN(host.val) RANGE IntervalMonthDayNano("950737950171172051122527404032") |
+---------------------+-------+----------------------------------------------------------------------------+
| 1970-01-01T00:00:00 | host1 | 0 |
| 1970-12-27T00:00:00 | host1 | 2 |
| 1970-01-01T00:00:00 | host2 | 4 |
| 1970-12-27T00:00:00 | host2 | 6 |
+---------------------+-------+----------------------------------------------------------------------------+
Error: 3000(PlanQuery), DataFusion error: Error during planning: Year or month interval is not allowed in range query: IntervalMonthDayNano("950737950171172051122527404032")

SELECT ts, host, min(val) RANGE (INTERVAL '1' day) FROM host ALIGN (INTERVAL '1' day) ORDER BY host, ts;

+---------------------+-------+------------------------------------------------------------------+
| ts | host | MIN(host.val) RANGE IntervalMonthDayNano("18446744073709551616") |
+---------------------+-------+------------------------------------------------------------------+
| 1970-01-01T00:00:00 | host1 | 0 |
| 1971-01-02T00:00:00 | host1 | 2 |
| 1970-01-01T00:00:00 | host2 | 4 |
| 1971-01-02T00:00:00 | host2 | 6 |
+---------------------+-------+------------------------------------------------------------------+

DROP TABLE host;

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/standalone/common/range/interval.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ INSERT INTO TABLE host VALUES

SELECT ts, host, min(val) RANGE (INTERVAL '1 year') FROM host ALIGN (INTERVAL '1 year') ORDER BY host, ts;

SELECT ts, host, min(val) RANGE (INTERVAL '1' year) FROM host ALIGN (INTERVAL '1' year) ORDER BY host, ts;
SELECT ts, host, min(val) RANGE (INTERVAL '1' day) FROM host ALIGN (INTERVAL '1' day) ORDER BY host, ts;

DROP TABLE host;

0 comments on commit 73bfcd1

Please sign in to comment.