Skip to content

Commit 3b59b1a

Browse files
authored
fix(query): interval type support cast to string (#17395)
* fix(query): interval type support cast to string * fix domain
1 parent 9997961 commit 3b59b1a

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

src/query/expression/src/utils/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl Display for ScalarRef<'_> {
229229
ScalarRef::String(s) => write!(f, "'{s}'"),
230230
ScalarRef::Timestamp(t) => write!(f, "'{}'", timestamp_to_string(*t, &TimeZone::UTC)),
231231
ScalarRef::Date(d) => write!(f, "'{}'", date_to_string(*d as i64, &TimeZone::UTC)),
232-
ScalarRef::Interval(interval) => write!(f, "{}", interval_to_string(interval)),
232+
ScalarRef::Interval(interval) => write!(f, "'{}'", interval_to_string(interval)),
233233
ScalarRef::Array(col) => write!(f, "[{}]", col.iter().join(", ")),
234234
ScalarRef::Map(col) => {
235235
write!(f, "{{")?;

src/query/functions/src/cast_rules.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub const GENERAL_CAST_RULES: AutoCastRules = &[
8787
(DataType::String, DataType::Binary),
8888
(DataType::String, DataType::Timestamp),
8989
(DataType::String, DataType::Date),
90+
(DataType::String, DataType::Interval),
9091
(DataType::String, DataType::Boolean),
9192
(DataType::Date, DataType::Timestamp),
9293
(

src/query/functions/src/scalars/timestamp/src/interval.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::io::Write;
16+
1517
use databend_common_column::types::months_days_micros;
1618
use databend_common_expression::date_helper::EvalMonthsImpl;
1719
use databend_common_expression::error_to_null;
1820
use databend_common_expression::types::interval::interval_to_string;
1921
use databend_common_expression::types::interval::string_to_interval;
2022
use databend_common_expression::types::Int64Type;
2123
use databend_common_expression::types::IntervalType;
22-
use databend_common_expression::types::NullableType;
2324
use databend_common_expression::types::StringType;
2425
use databend_common_expression::types::TimestampType;
2526
use databend_common_expression::vectorize_with_builder_1_arg;
@@ -75,15 +76,13 @@ fn register_string_to_interval(registry: &mut FunctionRegistry) {
7576
}
7677

7778
fn register_interval_to_string(registry: &mut FunctionRegistry) {
78-
registry.register_combine_nullable_1_arg::<IntervalType, StringType, _, _>(
79+
registry.register_passthrough_nullable_1_arg::<IntervalType, StringType, _, _>(
7980
"to_string",
80-
|_, _| FunctionDomain::MayThrow,
81-
vectorize_with_builder_1_arg::<IntervalType, NullableType<StringType>>(
82-
|interval, output, _| {
83-
let res = interval_to_string(&interval).to_string();
84-
output.push(&res);
85-
},
86-
),
81+
|_, _| FunctionDomain::Full,
82+
vectorize_with_builder_1_arg::<IntervalType, StringType>(|interval, output, _| {
83+
write!(output.row_buffer, "{}", interval_to_string(&interval)).unwrap();
84+
output.commit_row();
85+
}),
8786
);
8887
}
8988

src/query/functions/tests/it/scalars/testdata/function_list.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3933,7 +3933,7 @@ Functions overloads:
39333933
34 to_string(Bitmap NULL) :: String NULL
39343934
35 to_string(Geometry) :: String
39353935
36 to_string(Geometry NULL) :: String NULL
3936-
37 to_string(Interval) :: String NULL
3936+
37 to_string(Interval) :: String
39373937
38 to_string(Interval NULL) :: String NULL
39383938
0 to_timestamp(Variant) :: Timestamp
39393939
1 to_timestamp(Variant NULL) :: Timestamp NULL

tests/sqllogictests/suites/query/functions/02_0079_function_interval.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ select * from t where c1 > to_interval('-1 year');
2525
----
2626
-1 month -1:00:00 0:00:00.001
2727

28+
onlyif http
29+
query TT
30+
select * from t where c1 > '-1 year';
31+
----
32+
-1 month -1:00:00 0:00:00.001
33+
2834
onlyif http
2935
statement error 1006
3036
select to_interval('1 month 1 hour ago 1 micros');
@@ -184,3 +190,9 @@ query T
184190
select '2022-01-01'::timestamp-'2021-12-01'::timestamp;
185191
----
186192
2678400000000
193+
194+
onlyif http
195+
query TT
196+
select '1 days'::INTERVAL::String, '11 days'::INTERVAL::String;
197+
----
198+
1 day 11 days

0 commit comments

Comments
 (0)