Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(query): interval type support cast to string #17395

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/query/expression/src/utils/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl Display for ScalarRef<'_> {
ScalarRef::String(s) => write!(f, "'{s}'"),
ScalarRef::Timestamp(t) => write!(f, "'{}'", timestamp_to_string(*t, &TimeZone::UTC)),
ScalarRef::Date(d) => write!(f, "'{}'", date_to_string(*d as i64, &TimeZone::UTC)),
ScalarRef::Interval(interval) => write!(f, "{}", interval_to_string(interval)),
ScalarRef::Interval(interval) => write!(f, "'{}'", interval_to_string(interval)),
ScalarRef::Array(col) => write!(f, "[{}]", col.iter().join(", ")),
ScalarRef::Map(col) => {
write!(f, "{{")?;
Expand Down
1 change: 1 addition & 0 deletions src/query/functions/src/cast_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub const GENERAL_CAST_RULES: AutoCastRules = &[
(DataType::String, DataType::Binary),
(DataType::String, DataType::Timestamp),
(DataType::String, DataType::Date),
(DataType::String, DataType::Interval),
(DataType::String, DataType::Boolean),
(DataType::Date, DataType::Timestamp),
(
Expand Down
17 changes: 8 additions & 9 deletions src/query/functions/src/scalars/timestamp/src/interval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io::Write;

use databend_common_column::types::months_days_micros;
use databend_common_expression::date_helper::EvalMonthsImpl;
use databend_common_expression::error_to_null;
use databend_common_expression::types::interval::interval_to_string;
use databend_common_expression::types::interval::string_to_interval;
use databend_common_expression::types::Int64Type;
use databend_common_expression::types::IntervalType;
use databend_common_expression::types::NullableType;
use databend_common_expression::types::StringType;
use databend_common_expression::types::TimestampType;
use databend_common_expression::vectorize_with_builder_1_arg;
Expand Down Expand Up @@ -75,15 +76,13 @@ fn register_string_to_interval(registry: &mut FunctionRegistry) {
}

fn register_interval_to_string(registry: &mut FunctionRegistry) {
registry.register_combine_nullable_1_arg::<IntervalType, StringType, _, _>(
registry.register_passthrough_nullable_1_arg::<IntervalType, StringType, _, _>(
"to_string",
|_, _| FunctionDomain::MayThrow,
vectorize_with_builder_1_arg::<IntervalType, NullableType<StringType>>(
|interval, output, _| {
let res = interval_to_string(&interval).to_string();
output.push(&res);
},
),
|_, _| FunctionDomain::Full,
vectorize_with_builder_1_arg::<IntervalType, StringType>(|interval, output, _| {
write!(output.row_buffer, "{}", interval_to_string(&interval)).unwrap();
output.commit_row();
}),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3931,7 +3931,7 @@ Functions overloads:
34 to_string(Bitmap NULL) :: String NULL
35 to_string(Geometry) :: String
36 to_string(Geometry NULL) :: String NULL
37 to_string(Interval) :: String NULL
37 to_string(Interval) :: String
38 to_string(Interval NULL) :: String NULL
0 to_timestamp(Variant) :: Timestamp
1 to_timestamp(Variant NULL) :: Timestamp NULL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ select * from t where c1 > to_interval('-1 year');
----
-1 month -1:00:00 0:00:00.001

onlyif http
query TT
select * from t where c1 > '-1 year';
----
-1 month -1:00:00 0:00:00.001

onlyif http
statement error 1006
select to_interval('1 month 1 hour ago 1 micros');
Expand Down Expand Up @@ -184,3 +190,9 @@ query T
select '2022-01-01'::timestamp-'2021-12-01'::timestamp;
----
2678400000000

onlyif http
query TT
select '1 days'::INTERVAL::String, '11 days'::INTERVAL::String;
----
1 day 11 days