Skip to content

Commit

Permalink
[CALCITE-6241] Enable a few existing functions to Spark library
Browse files Browse the repository at this point in the history
Functions List:
DECODE, NVL, IFNULL, LEN, LENGTH, LPAD, RPAD, LTRIM, RTRIM, ENDSWITH, STARTSWITH, GREATEST, LEAST,
TRANSLATE, BOOL_AND, BOOL_OR, DATE_FROM_UNIX_DATE, UNIX_DATE, LEFT, REPEAT, RIGHT, SPACE
TIMESTAMP_SECONDS, TIMESTAMP_MILLIS, TIMESTAMP_MICROS, UNIX_SECONDS, UNIX_MILLIS, UNIX_MICROS, MD5, SHA1, POW
  • Loading branch information
YiwenWu authored and macroguo-ghy committed Feb 7, 2024
1 parent cf91b78 commit b5091fe
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private static SqlCall transformConvert(SqlValidator validator, SqlCall call) {
};

/** The "DECODE(v, v1, result1, [v2, result2, ...], resultN)" function. */
@LibraryOperator(libraries = {ORACLE})
@LibraryOperator(libraries = {ORACLE, SPARK})
public static final SqlFunction DECODE =
SqlBasicFunction.create(SqlKind.DECODE, DECODE_RETURN_TYPE,
OperandTypes.VARIADIC);
Expand Down Expand Up @@ -263,24 +263,24 @@ private static SqlCall transformConvert(SqlValidator validator, SqlCall call) {
}

/** The "NVL(value, value)" function. */
@LibraryOperator(libraries = {ORACLE})
@LibraryOperator(libraries = {ORACLE, SPARK})
public static final SqlBasicFunction NVL =
SqlBasicFunction.create(SqlKind.NVL,
ReturnTypes.LEAST_RESTRICTIVE
.andThen(SqlTypeTransforms.TO_NULLABLE_ALL),
OperandTypes.SAME_SAME);

/** The "IFNULL(value, value)" function. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction IFNULL = NVL.withName("IFNULL");

/** The "LEN(string)" function. */
@LibraryOperator(libraries = {SNOWFLAKE})
@LibraryOperator(libraries = {SNOWFLAKE, SPARK})
public static final SqlFunction LEN =
SqlStdOperatorTable.CHAR_LENGTH.withName("LEN");

/** The "LENGTH(string)" function. */
@LibraryOperator(libraries = {BIG_QUERY, SNOWFLAKE})
@LibraryOperator(libraries = {BIG_QUERY, SNOWFLAKE, SPARK})
public static final SqlFunction LENGTH =
SqlStdOperatorTable.CHAR_LENGTH.withName("LENGTH");

Expand All @@ -291,7 +291,7 @@ private static RelDataType deriveTypePad(SqlOperatorBinding binding, RelDataType
}

/** The "LPAD(original_value, return_length[, pattern])" function. */
@LibraryOperator(libraries = {BIG_QUERY, ORACLE})
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, SPARK})
public static final SqlFunction LPAD =
SqlBasicFunction.create(
"LPAD",
Expand All @@ -300,7 +300,7 @@ private static RelDataType deriveTypePad(SqlOperatorBinding binding, RelDataType
SqlFunctionCategory.STRING);

/** The "RPAD(original_value, return_length[, pattern])" function. */
@LibraryOperator(libraries = {BIG_QUERY, ORACLE})
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, SPARK})
public static final SqlFunction RPAD =
SqlBasicFunction.create(
"RPAD",
Expand All @@ -309,7 +309,7 @@ private static RelDataType deriveTypePad(SqlOperatorBinding binding, RelDataType
SqlFunctionCategory.STRING);

/** The "LTRIM(string)" function. */
@LibraryOperator(libraries = {BIG_QUERY, ORACLE})
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, SPARK})
public static final SqlFunction LTRIM =
SqlBasicFunction.create(SqlKind.LTRIM,
ReturnTypes.ARG0.andThen(SqlTypeTransforms.TO_NULLABLE)
Expand All @@ -318,7 +318,7 @@ private static RelDataType deriveTypePad(SqlOperatorBinding binding, RelDataType
.withFunctionType(SqlFunctionCategory.STRING);

/** The "RTRIM(string)" function. */
@LibraryOperator(libraries = {BIG_QUERY, ORACLE})
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, SPARK})
public static final SqlFunction RTRIM =
SqlBasicFunction.create(SqlKind.RTRIM,
ReturnTypes.ARG0.andThen(SqlTypeTransforms.TO_NULLABLE)
Expand Down Expand Up @@ -373,7 +373,7 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
OperandTypes.STRING_SAME_SAME);

/** The "ENDSWITH(value1, value2)" function (Snowflake). */
@LibraryOperator(libraries = {SNOWFLAKE})
@LibraryOperator(libraries = {SNOWFLAKE, SPARK})
public static final SqlFunction ENDSWITH = ENDS_WITH.withName("ENDSWITH");

/** The "STARTS_WITH(value1, value2)" function (BigQuery, PostgreSQL). */
Expand All @@ -383,7 +383,7 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
OperandTypes.STRING_SAME_SAME);

/** The "STARTSWITH(value1, value2)" function (Snowflake). */
@LibraryOperator(libraries = {SNOWFLAKE})
@LibraryOperator(libraries = {SNOWFLAKE, SPARK})
public static final SqlFunction STARTSWITH = STARTS_WITH.withName("STARTSWITH");

/** BigQuery's "SUBSTR(string, position [, substringLength ])" function. */
Expand Down Expand Up @@ -436,14 +436,14 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
SqlFunctionCategory.STRING);

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

/** The "LEAST(value, value)" function. */
@LibraryOperator(libraries = {BIG_QUERY, ORACLE})
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, SPARK})
public static final SqlFunction LEAST =
SqlBasicFunction.create(SqlKind.LEAST,
ReturnTypes.LEAST_RESTRICTIVE.andThen(SqlTypeTransforms.TO_NULLABLE),
Expand Down Expand Up @@ -472,7 +472,7 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
* <p>It is not defined in the SQL standard, but occurs in Oracle and
* PostgreSQL.
*/
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, POSTGRESQL})
@LibraryOperator(libraries = {BIG_QUERY, ORACLE, POSTGRESQL, SPARK})
public static final SqlFunction TRANSLATE3 = new SqlTranslate3Function();

@LibraryOperator(libraries = {MYSQL})
Expand Down Expand Up @@ -602,13 +602,13 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,

/** The "BOOL_AND(condition)" aggregate function, PostgreSQL and Redshift's
* equivalent to {@link SqlStdOperatorTable#EVERY}. */
@LibraryOperator(libraries = {POSTGRESQL})
@LibraryOperator(libraries = {POSTGRESQL, SPARK})
public static final SqlAggFunction BOOL_AND =
new SqlMinMaxAggFunction("BOOL_AND", SqlKind.MIN, OperandTypes.BOOLEAN);

/** The "BOOL_OR(condition)" aggregate function, PostgreSQL and Redshift's
* equivalent to {@link SqlStdOperatorTable#SOME}. */
@LibraryOperator(libraries = {POSTGRESQL})
@LibraryOperator(libraries = {POSTGRESQL, SPARK})
public static final SqlAggFunction BOOL_OR =
new SqlMinMaxAggFunction("BOOL_OR", SqlKind.MAX, OperandTypes.BOOLEAN);

Expand Down Expand Up @@ -857,15 +857,15 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,

/** The "DATE_FROM_UNIX_DATE(integer)" function; returns a DATE value
* a given number of seconds after 1970-01-01. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction DATE_FROM_UNIX_DATE =
SqlBasicFunction.create("DATE_FROM_UNIX_DATE",
ReturnTypes.DATE_NULLABLE, OperandTypes.INTEGER,
SqlFunctionCategory.TIMEDATE);

/** The "UNIX_DATE(date)" function; returns the number of days since
* 1970-01-01. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction UNIX_DATE =
SqlBasicFunction.create("UNIX_DATE",
ReturnTypes.INTEGER_NULLABLE, OperandTypes.DATE,
Expand Down Expand Up @@ -896,25 +896,25 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
ReturnTypes.VARCHAR_2000, OperandTypes.DATETIME,
SqlFunctionCategory.TIMEDATE);

@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL})
@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL, SPARK})
public static final SqlFunction LEFT =
SqlBasicFunction.create("LEFT",
ReturnTypes.ARG0_NULLABLE_VARYING,
OperandTypes.CBSTRING_INTEGER, SqlFunctionCategory.STRING);

@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL})
@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL, SPARK})
public static final SqlFunction REPEAT =
SqlBasicFunction.create("REPEAT",
ReturnTypes.VARCHAR_NULLABLE,
OperandTypes.STRING_INTEGER,
SqlFunctionCategory.STRING);

@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL})
@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL, SPARK})
public static final SqlFunction RIGHT =
SqlBasicFunction.create("RIGHT", ReturnTypes.ARG0_NULLABLE_VARYING,
OperandTypes.CBSTRING_INTEGER, SqlFunctionCategory.STRING);

@LibraryOperator(libraries = {MYSQL})
@LibraryOperator(libraries = {MYSQL, SPARK})
public static final SqlFunction SPACE =
SqlBasicFunction.create("SPACE",
ReturnTypes.VARCHAR_NULLABLE,
Expand Down Expand Up @@ -1842,46 +1842,46 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding

/** The "TIMESTAMP_SECONDS(bigint)" function; returns a TIMESTAMP value
* a given number of seconds after 1970-01-01 00:00:00. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction TIMESTAMP_SECONDS =
SqlBasicFunction.create("TIMESTAMP_SECONDS",
ReturnTypes.TIMESTAMP_NULLABLE, OperandTypes.INTEGER,
SqlFunctionCategory.TIMEDATE);

/** The "TIMESTAMP_MILLIS(bigint)" function; returns a TIMESTAMP value
* a given number of milliseconds after 1970-01-01 00:00:00. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction TIMESTAMP_MILLIS =
SqlBasicFunction.create("TIMESTAMP_MILLIS",
ReturnTypes.TIMESTAMP_NULLABLE, OperandTypes.INTEGER,
SqlFunctionCategory.TIMEDATE);

/** The "TIMESTAMP_MICROS(bigint)" function; returns a TIMESTAMP value
* a given number of micro-seconds after 1970-01-01 00:00:00. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction TIMESTAMP_MICROS =
SqlBasicFunction.create("TIMESTAMP_MICROS",
ReturnTypes.TIMESTAMP_NULLABLE, OperandTypes.INTEGER,
SqlFunctionCategory.TIMEDATE);

/** The "UNIX_SECONDS(bigint)" function; returns the number of seconds
* since 1970-01-01 00:00:00. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction UNIX_SECONDS =
SqlBasicFunction.create("UNIX_SECONDS", ReturnTypes.BIGINT_NULLABLE,
OperandTypes.TIMESTAMP, SqlFunctionCategory.TIMEDATE);

/** The "UNIX_MILLIS(bigint)" function; returns the number of milliseconds
* since 1970-01-01 00:00:00. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction UNIX_MILLIS =
SqlBasicFunction.create("UNIX_MILLIS",
ReturnTypes.BIGINT_NULLABLE, OperandTypes.TIMESTAMP,
SqlFunctionCategory.TIMEDATE);

/** The "UNIX_MICROS(bigint)" function; returns the number of microseconds
* since 1970-01-01 00:00:00. */
@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction UNIX_MICROS =
SqlBasicFunction.create("UNIX_MICROS",
ReturnTypes.BIGINT_NULLABLE, OperandTypes.TIMESTAMP,
Expand Down Expand Up @@ -2093,14 +2093,14 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
OperandTypes.INTEGER,
SqlFunctionCategory.NUMERIC);

@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL})
@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL, SPARK})
public static final SqlFunction MD5 =
SqlBasicFunction.create("MD5",
ReturnTypes.VARCHAR_NULLABLE,
OperandTypes.STRING.or(OperandTypes.BINARY),
SqlFunctionCategory.STRING);

@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL})
@LibraryOperator(libraries = {BIG_QUERY, MYSQL, POSTGRESQL, SPARK})
public static final SqlFunction SHA1 =
SqlBasicFunction.create("SHA1",
ReturnTypes.VARCHAR_NULLABLE,
Expand Down Expand Up @@ -2149,7 +2149,7 @@ private static RelDataType deriveTypeMapFromEntries(SqlOperatorBinding opBinding
OperandTypes.NUMERIC_OPTIONAL_NUMERIC,
SqlFunctionCategory.NUMERIC);

@LibraryOperator(libraries = {BIG_QUERY})
@LibraryOperator(libraries = {BIG_QUERY, SPARK})
public static final SqlFunction POW =
SqlStdOperatorTable.POWER.withName("POW");

Expand Down
Loading

0 comments on commit b5091fe

Please sign in to comment.