Skip to content

Commit

Permalink
[CALCITE-6766] Move DATEADD and DATEDIFF from PostgreSQL to Redshift …
Browse files Browse the repository at this point in the history
…library
  • Loading branch information
ILuffZhe authored and mihaibudiu committed Jan 9, 2025
1 parent 274cc2d commit bb25643
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ class BabelParserTest extends SqlParserTest {
sql("select date(x) from t").ok(expected);
}

/** In Redshift, PostgreSQL the DATEADD, DATEDIFF and DATE_PART functions have
* ordinary function syntax except that its first argument is a time unit
/** The DATEADD, DATEDIFF (in Redshift, Snowflake) and DATE_PART (in PostgreSQL)
* functions have ordinary function syntax except that its first argument is a time unit
* (e.g. DAY). We must not parse that first argument as an identifier. */
@Test void testRedshiftFunctionsWithDateParts() {
final String sql = "SELECT DATEADD(day, 1, t),\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private SqlLibraryOperators() {

/** The "DATEADD(timeUnit, numeric, datetime)" function
* (Microsoft SQL Server, Redshift, Snowflake). */
@LibraryOperator(libraries = {MSSQL, POSTGRESQL})
@LibraryOperator(libraries = {MSSQL, REDSHIFT, SNOWFLAKE})
public static final SqlFunction DATEADD =
new SqlTimestampAddFunction("DATEADD");

Expand Down Expand Up @@ -162,7 +162,7 @@ private SqlLibraryOperators() {
* <p>MySQL has "DATEDIFF(date, date2)" and "TIMEDIFF(time, time2)" functions
* but Calcite does not implement these because they have no "timeUnit"
* argument. */
@LibraryOperator(libraries = {MSSQL, POSTGRESQL})
@LibraryOperator(libraries = {MSSQL, REDSHIFT, SNOWFLAKE})
public static final SqlFunction DATEDIFF =
new SqlTimestampDiffFunction("DATEDIFF",
OperandTypes.family(SqlTypeFamily.ANY, SqlTypeFamily.DATE,
Expand Down
8 changes: 2 additions & 6 deletions site/_docs/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -2851,8 +2851,8 @@ In the following:
| b | DATE(timestampLtz, timeZone) | Extracts the DATE from *timestampLtz* (an instant; BigQuery's TIMESTAMP type) in *timeZone*
| b | DATE(string) | Equivalent to `CAST(string AS DATE)`
| b | DATE(year, month, day) | Returns a DATE value for *year*, *month*, and *day* (all of type INTEGER)
| p q r | DATEADD(timeUnit, integer, datetime) | Equivalent to `TIMESTAMPADD(timeUnit, integer, datetime)`
| p q r | DATEDIFF(timeUnit, datetime, datetime2) | Equivalent to `TIMESTAMPDIFF(timeUnit, datetime, datetime2)`
| q r f | DATEADD(timeUnit, integer, datetime) | Equivalent to `TIMESTAMPADD(timeUnit, integer, datetime)`
| q r f | DATEDIFF(timeUnit, datetime, datetime2) | Equivalent to `TIMESTAMPDIFF(timeUnit, datetime, datetime2)`
| q | DATEPART(timeUnit, datetime) | Equivalent to `EXTRACT(timeUnit FROM datetime)`
| b | DATETIME(date, time) | Converts *date* and *time* to a TIMESTAMP
| b | DATETIME(date) | Converts *date* to a TIMESTAMP value (at midnight)
Expand Down Expand Up @@ -3035,10 +3035,6 @@ In the following:

Note:

* Calcite has no Redshift library, so the Postgres library
is used instead. The functions `DATEADD`, `DATEDIFF` are
implemented in Redshift and not Postgres but nevertheless
appear in Calcite's Postgres library
* Functions `DATEADD`, `DATEDIFF`, `DATE_PART` require the Babel parser
* `JSON_TYPE` / `JSON_DEPTH` / `JSON_PRETTY` / `JSON_STORAGE_SIZE` return null if the argument is null
* `JSON_LENGTH` / `JSON_KEYS` / `JSON_REMOVE` return null if the first argument is null
Expand Down

0 comments on commit bb25643

Please sign in to comment.