Skip to content

Commit

Permalink
[CALCITE-6664] Replace GREATEST, LEAST functions in Spark library wit…
Browse files Browse the repository at this point in the history
…h the implementation of PostgreSQL Library
  • Loading branch information
joeyutong committed Dec 17, 2024
1 parent 041619f commit 75763c6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
30 changes: 30 additions & 0 deletions babel/src/test/resources/sql/spark.iq
Original file line number Diff line number Diff line change
Expand Up @@ -667,4 +667,34 @@ SELECT KEY, "set" IS NULL AS N FROM COMPLEX;

!ok

SELECT greatest(1, null, 3) AS x;
+---+
| X |
+---+
| 3 |
+---+
(1 row)

!ok

SELECT least(1, 2, null, 3) AS x;
+---+
| X |
+---+
| 1 |
+---+
(1 row)

!ok

SELECT greatest(null, null) AS x;
+---+
| X |
+---+
| |
+---+
(1 row)

!ok

# End spark.iq
6 changes: 3 additions & 3 deletions core/src/main/java/org/apache/calcite/sql/SqlKind.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,10 @@ public enum SqlKind {
/** {@code NVL2} function (Oracle, Spark). */
NVL2,

/** {@code GREATEST} function (Oracle, Spark). */
/** {@code GREATEST} function (Oracle). */
GREATEST,

/** {@code GREATEST} function (PostgreSQL). */
/** {@code GREATEST} function (PostgreSQL, Spark). */
GREATEST_PG,

/** The two-argument {@code CONCAT} function (Oracle). */
Expand All @@ -503,7 +503,7 @@ public enum SqlKind {
/** {@code LEAST} function (Oracle). */
LEAST,

/** {@code LEAST} function (PostgreSQL). */
/** {@code LEAST} function (PostgreSQL, Spark). */
LEAST_PG,

/** {@code LOG} function. (Mysql, Spark). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,28 +480,30 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
SqlFunctionCategory.STRING);

/** The "GREATEST(value, value)" function. */
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, SPARK})
@LibraryOperator(libraries = {BIG_QUERY, ORACLE})
public static final SqlFunction GREATEST =
SqlBasicFunction.create(SqlKind.GREATEST,
ReturnTypes.LEAST_RESTRICTIVE.andThen(SqlTypeTransforms.TO_NULLABLE),
OperandTypes.SAME_VARIADIC);

/** The "GREATEST(value, value)" function. */
@LibraryOperator(libraries = {POSTGRESQL})
/** The "GREATEST(value, value)" function. Identical to the standard <code>GREATEST</code>
* function except it skips null values and only returns null if all parameters are nulls. */
@LibraryOperator(libraries = {POSTGRESQL, SPARK})
public static final SqlFunction GREATEST_PG =
SqlBasicFunction.create("GREATEST", SqlKind.GREATEST_PG,
ReturnTypes.LEAST_RESTRICTIVE.andThen(SqlTypeTransforms.TO_NULLABLE),
OperandTypes.SAME_VARIADIC);

/** The "LEAST(value, value)" function. */
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, SPARK})
@LibraryOperator(libraries = {BIG_QUERY, ORACLE})
public static final SqlFunction LEAST =
SqlBasicFunction.create(SqlKind.LEAST,
ReturnTypes.LEAST_RESTRICTIVE.andThen(SqlTypeTransforms.TO_NULLABLE),
OperandTypes.SAME_VARIADIC);

/** The "GREATEST(value, value)" function. */
@LibraryOperator(libraries = {POSTGRESQL})
/** The "LEAST(value, value)" function. Identical to the standard <code>LEAST</code>
* function except it skips null values and only returns null if all parameters are nulls. */
@LibraryOperator(libraries = {POSTGRESQL, SPARK})
public static final SqlFunction LEAST_PG =
SqlBasicFunction.create("LEAST", SqlKind.LEAST_PG,
ReturnTypes.LEAST_RESTRICTIVE.andThen(SqlTypeTransforms.TO_NULLABLE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11664,7 +11664,7 @@ void assertSubFunReturns(boolean binary, String s, int start,
"VARCHAR(5) NOT NULL");
};
final List<SqlLibrary> libraries =
list(SqlLibrary.BIG_QUERY, SqlLibrary.ORACLE, SqlLibrary.SPARK);
list(SqlLibrary.BIG_QUERY, SqlLibrary.ORACLE);
f0.forEachLibrary(libraries, consumer);
}

Expand All @@ -11686,7 +11686,8 @@ void assertSubFunReturns(boolean binary, String s, int start,
f.checkScalar("greatest(CAST(NULL AS INTEGER), CAST(NULL AS INTEGER))", isNullValue(),
"INTEGER");
};
final List<SqlLibrary> libraries = list(SqlLibrary.POSTGRESQL, SqlLibrary.REDSHIFT);
final List<SqlLibrary> libraries =
list(SqlLibrary.POSTGRESQL, SqlLibrary.REDSHIFT, SqlLibrary.SPARK);
f0.forEachLibrary(libraries, consumer);
}

Expand All @@ -11710,7 +11711,7 @@ void assertSubFunReturns(boolean binary, String s, int start,
"VARCHAR(5) NOT NULL");
};
final List<SqlLibrary> libraries =
list(SqlLibrary.BIG_QUERY, SqlLibrary.ORACLE, SqlLibrary.SPARK);
list(SqlLibrary.BIG_QUERY, SqlLibrary.ORACLE);
f0.forEachLibrary(libraries, consumer);
}

Expand All @@ -11732,7 +11733,8 @@ void assertSubFunReturns(boolean binary, String s, int start,
f.checkScalar("least(CAST(NULL AS INTEGER), CAST(NULL AS INTEGER))", isNullValue(),
"INTEGER");
};
final List<SqlLibrary> libraries = list(SqlLibrary.POSTGRESQL, SqlLibrary.REDSHIFT);
final List<SqlLibrary> libraries =
list(SqlLibrary.POSTGRESQL, SqlLibrary.REDSHIFT, SqlLibrary.SPARK);
f0.forEachLibrary(libraries, consumer);
}

Expand Down

0 comments on commit 75763c6

Please sign in to comment.