Skip to content

Commit d49b877

Browse files
authored
chore(query): delete no useful function epoch(Int64Type)->IntervalType (#17730)
* chore(query): delete no useful function epoch(Int64Type)->IntervalType * add some function alias * truncate function add alias trunc
1 parent 0b3841f commit d49b877

File tree

10 files changed

+179
-26
lines changed

10 files changed

+179
-26
lines changed

src/query/ast/src/parser/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
917917
);
918918
let date_part = map(
919919
rule! {
920-
DATE_PART ~ "(" ~ ^#interval_kind ~ "," ~ ^#subexpr(0) ~ ^")"
920+
(DATE_PART | DATEPART) ~ "(" ~ ^#interval_kind ~ "," ~ ^#subexpr(0) ~ ^")"
921921
},
922922
|(_, _, field, _, expr, _)| ExprElement::DatePart {
923923
field,
@@ -1243,7 +1243,7 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
12431243

12441244
let date_diff = map(
12451245
rule! {
1246-
DATE_DIFF ~ "(" ~ #interval_kind ~ "," ~ #subexpr(0) ~ "," ~ #subexpr(0) ~ ")"
1246+
(DATE_DIFF | DATEDIFF) ~ "(" ~ #interval_kind ~ "," ~ #subexpr(0) ~ "," ~ #subexpr(0) ~ ")"
12471247
},
12481248
|(_, _, unit, _, date_start, _, date_end, _)| ExprElement::DateDiff {
12491249
unit,
@@ -1254,7 +1254,7 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
12541254

12551255
let date_sub = map(
12561256
rule! {
1257-
DATE_SUB ~ "(" ~ #interval_kind ~ "," ~ #subexpr(0) ~ "," ~ #subexpr(0) ~ ")"
1257+
(DATE_SUB | DATESUB) ~ "(" ~ #interval_kind ~ "," ~ #subexpr(0) ~ "," ~ #subexpr(0) ~ ")"
12581258
},
12591259
|(_, _, unit, _, interval, _, date, _)| ExprElement::DateSub {
12601260
unit,

src/query/ast/src/parser/token.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,16 @@ pub enum TokenKind {
499499
DATE_ADD,
500500
#[token("DATE_DIFF", ignore(ascii_case))]
501501
DATE_DIFF,
502+
#[token("DATEDIFF", ignore(ascii_case))]
503+
DATEDIFF,
502504
#[token("DATE_PART", ignore(ascii_case))]
503505
DATE_PART,
506+
#[token("DATEPART", ignore(ascii_case))]
507+
DATEPART,
504508
#[token("DATE_SUB", ignore(ascii_case))]
505509
DATE_SUB,
510+
#[token("DATESUB", ignore(ascii_case))]
511+
DATESUB,
506512
#[token("DATE_TRUNC", ignore(ascii_case))]
507513
DATE_TRUNC,
508514
#[token("DATETIME", ignore(ascii_case))]
@@ -1534,6 +1540,9 @@ impl TokenKind {
15341540
| TokenKind::EXISTS
15351541
| TokenKind::EXTRACT
15361542
| TokenKind::DATE_PART
1543+
| TokenKind::DATEPART
1544+
| TokenKind::DATEDIFF
1545+
| TokenKind::DATESUB
15371546
| TokenKind::FALSE
15381547
| TokenKind::FLOAT
15391548
// | TokenKind::FOREIGN

src/query/ast/tests/it/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,9 @@ fn test_expr() {
12321232
r#"timestamp()"#, // issue #16628
12331233
r#"extract(year from d)"#,
12341234
r#"date_part(year, d)"#,
1235+
r#"datepart(year, d)"#,
1236+
r#"DATEDIFF(SECOND, to_timestamp('2024-01-01 21:01:35.423179'), to_timestamp('2023-12-31 09:38:18.165575'))"#,
1237+
r#"datesub(QUARTER, 1, to_date('2018-01-02'))"#,
12351238
r#"position('a' in str)"#,
12361239
r#"substring(a from b for c)"#,
12371240
r#"substring(a, b, c)"#,

src/query/ast/tests/it/testdata/expr-error.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ error:
5252
--> SQL:1:10
5353
|
5454
1 | CAST(col1)
55-
| ---- ^ unexpected `)`, expecting `AS`, `,`, `(`, `IS`, `NOT`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATE_ADD`, or 36 more ...
55+
| ---- ^ unexpected `)`, expecting `AS`, `,`, `(`, `IS`, `NOT`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATE_ADD`, or 39 more ...
5656
| |
5757
| while parsing `CAST(... AS ...)`
5858
| while parsing expression
@@ -81,7 +81,7 @@ error:
8181
1 | $ abc + 3
8282
| ^
8383
| |
84-
| unexpected `$`, expecting `IS`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATE_ADD`, `DATE_DIFF`, `DATE_SUB`, `DATE_TRUNC`, or 34 more ...
84+
| unexpected `$`, expecting `IS`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATE_ADD`, `DATE_DIFF`, `DATEDIFF`, `DATE_SUB`, or 37 more ...
8585
| while parsing expression
8686

8787

src/query/ast/tests/it/testdata/expr.txt

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,162 @@ DatePart {
18101810
}
18111811

18121812

1813+
---------- Input ----------
1814+
datepart(year, d)
1815+
---------- Output ---------
1816+
DATE_PART(YEAR, d)
1817+
---------- AST ------------
1818+
DatePart {
1819+
span: Some(
1820+
0..17,
1821+
),
1822+
kind: Year,
1823+
expr: ColumnRef {
1824+
span: Some(
1825+
15..16,
1826+
),
1827+
column: ColumnRef {
1828+
database: None,
1829+
table: None,
1830+
column: Name(
1831+
Identifier {
1832+
span: Some(
1833+
15..16,
1834+
),
1835+
name: "d",
1836+
quote: None,
1837+
ident_type: None,
1838+
},
1839+
),
1840+
},
1841+
},
1842+
}
1843+
1844+
1845+
---------- Input ----------
1846+
DATEDIFF(SECOND, to_timestamp('2024-01-01 21:01:35.423179'), to_timestamp('2023-12-31 09:38:18.165575'))
1847+
---------- Output ---------
1848+
DATE_DIFF(SECOND, to_timestamp('2024-01-01 21:01:35.423179'), to_timestamp('2023-12-31 09:38:18.165575'))
1849+
---------- AST ------------
1850+
DateDiff {
1851+
span: Some(
1852+
0..104,
1853+
),
1854+
unit: Second,
1855+
date_start: FunctionCall {
1856+
span: Some(
1857+
17..59,
1858+
),
1859+
func: FunctionCall {
1860+
distinct: false,
1861+
name: Identifier {
1862+
span: Some(
1863+
17..29,
1864+
),
1865+
name: "to_timestamp",
1866+
quote: None,
1867+
ident_type: None,
1868+
},
1869+
args: [
1870+
Literal {
1871+
span: Some(
1872+
30..58,
1873+
),
1874+
value: String(
1875+
"2024-01-01 21:01:35.423179",
1876+
),
1877+
},
1878+
],
1879+
params: [],
1880+
order_by: [],
1881+
window: None,
1882+
lambda: None,
1883+
},
1884+
},
1885+
date_end: FunctionCall {
1886+
span: Some(
1887+
61..103,
1888+
),
1889+
func: FunctionCall {
1890+
distinct: false,
1891+
name: Identifier {
1892+
span: Some(
1893+
61..73,
1894+
),
1895+
name: "to_timestamp",
1896+
quote: None,
1897+
ident_type: None,
1898+
},
1899+
args: [
1900+
Literal {
1901+
span: Some(
1902+
74..102,
1903+
),
1904+
value: String(
1905+
"2023-12-31 09:38:18.165575",
1906+
),
1907+
},
1908+
],
1909+
params: [],
1910+
order_by: [],
1911+
window: None,
1912+
lambda: None,
1913+
},
1914+
},
1915+
}
1916+
1917+
1918+
---------- Input ----------
1919+
datesub(QUARTER, 1, to_date('2018-01-02'))
1920+
---------- Output ---------
1921+
DATE_SUB(QUARTER, 1, to_date('2018-01-02'))
1922+
---------- AST ------------
1923+
DateSub {
1924+
span: Some(
1925+
0..42,
1926+
),
1927+
unit: Quarter,
1928+
interval: Literal {
1929+
span: Some(
1930+
17..18,
1931+
),
1932+
value: UInt64(
1933+
1,
1934+
),
1935+
},
1936+
date: FunctionCall {
1937+
span: Some(
1938+
20..41,
1939+
),
1940+
func: FunctionCall {
1941+
distinct: false,
1942+
name: Identifier {
1943+
span: Some(
1944+
20..27,
1945+
),
1946+
name: "to_date",
1947+
quote: None,
1948+
ident_type: None,
1949+
},
1950+
args: [
1951+
Literal {
1952+
span: Some(
1953+
28..40,
1954+
),
1955+
value: String(
1956+
"2018-01-02",
1957+
),
1958+
},
1959+
],
1960+
params: [],
1961+
order_by: [],
1962+
window: None,
1963+
lambda: None,
1964+
},
1965+
},
1966+
}
1967+
1968+
18131969
---------- Input ----------
18141970
position('a' in str)
18151971
---------- Output ---------

src/query/ast/tests/it/testdata/stmt-error.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ error:
513513
--> SQL:1:41
514514
|
515515
1 | SELECT * FROM t GROUP BY GROUPING SETS ()
516-
| ------ ^ unexpected `)`, expecting `(`, `IS`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATE_ADD`, `DATE_DIFF`, `DATE_SUB`, or 34 more ...
516+
| ------ ^ unexpected `)`, expecting `(`, `IS`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, `POSITION`, `IdentVariable`, `DATE_ADD`, `DATE_DIFF`, `DATEDIFF`, or 37 more ...
517517
| |
518518
| while parsing `SELECT ...`
519519

@@ -935,7 +935,7 @@ error:
935935
--> SQL:1:65
936936
|
937937
1 | CREATE FUNCTION IF NOT EXISTS isnotempty AS(p) -> not(is_null(p)
938-
| ------ -- ---- ^ unexpected end of input, expecting `)`, `IGNORE`, `RESPECT`, `OVER`, `WITHIN`, `(`, `IS`, `NOT`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, or 40 more ...
938+
| ------ -- ---- ^ unexpected end of input, expecting `)`, `IGNORE`, `RESPECT`, `OVER`, `WITHIN`, `(`, `IS`, `NOT`, `IN`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `LIKE`, `REGEXP`, `RLIKE`, `SOUNDS`, <BitWiseOr>, <BitWiseAnd>, <BitWiseXor>, <ShiftLeft>, <ShiftRight>, `->`, `->>`, `#>`, `#>>`, `?`, `?|`, `?&`, `@>`, `<@`, `@?`, `@@`, `#-`, <Factorial>, <SquareRoot>, <BitWiseNot>, <CubeRoot>, <Abs>, `CAST`, `TRY_CAST`, `::`, or 43 more ...
939939
| | | | |
940940
| | | | while parsing `(<expr> [, ...])`
941941
| | | while parsing expression

src/query/functions/src/scalars/mathematics/src/math.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ pub fn register(registry: &mut FunctionRegistry) {
204204
}
205205

206206
registry.register_aliases("ceil", &["ceiling"]);
207+
registry.register_aliases("truncate", &["trunc"]);
207208

208209
registry.register_1_arg::<StringType, NumberType<u32>, _, _>(
209210
"crc32",

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,6 @@ fn register_interval_add_sub_mul(registry: &mut FunctionRegistry) {
206206
}
207207

208208
fn register_number_to_interval(registry: &mut FunctionRegistry) {
209-
registry.register_passthrough_nullable_1_arg::<Int64Type, IntervalType, _, _>(
210-
"epoch",
211-
|_, _| FunctionDomain::MayThrow,
212-
vectorize_with_builder_1_arg::<Int64Type, IntervalType>(|val, output, _| {
213-
let res = months_days_micros::new(0, 0, val * 1_000_000);
214-
output.push(res);
215-
}),
216-
);
217-
218209
registry.register_passthrough_nullable_1_arg::<Int64Type, IntervalType, _, _>(
219210
"to_centuries",
220211
|_, _| FunctionDomain::MayThrow,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ subtract -> minus
6666
to_datetime -> to_timestamp
6767
to_text -> to_string
6868
to_varchar -> to_string
69+
trunc -> truncate
6970
try_ipv4_num_to_string -> try_inet_ntoa
7071
try_ipv4_string_to_num -> try_inet_aton
7172
try_to_datetime -> try_to_timestamp
@@ -1673,10 +1674,8 @@ Functions overloads:
16731674
0 divnull(Float64 NULL, Float64 NULL) :: Float64 NULL
16741675
0 epoch(Timestamp) :: Float64
16751676
1 epoch(Timestamp NULL) :: Float64 NULL
1676-
2 epoch(Int64) :: Interval
1677-
3 epoch(Int64 NULL) :: Interval NULL
1678-
4 epoch(Interval) :: Float64
1679-
5 epoch(Interval NULL) :: Float64 NULL
1677+
2 epoch(Interval) :: Float64
1678+
3 epoch(Interval NULL) :: Float64 NULL
16801679
0 eq(Variant, Variant) :: Boolean
16811680
1 eq(Variant NULL, Variant NULL) :: Boolean NULL
16821681
2 eq(String, String) :: Boolean

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ select to_interval('1 hours') = to_interval('3600 seconds') as eq, to_interval('
8080
----
8181
1 1
8282

83-
onlyif http
84-
query T
85-
SELECT epoch(10);
86-
----
87-
0:00:10
88-
8983
onlyif http
9084
query T
9185
SELECT to_centuries(2);

0 commit comments

Comments
 (0)