Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FLINK-13284] [table-planner-blink] Correct some builtin functions' r… #9146

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ public void lookupOperatorOverloads(
public static final SqlFunction DATE_FORMAT = new SqlFunction(
"DATE_FORMAT",
SqlKind.OTHER_FUNCTION,
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.VARCHAR), SqlTypeTransforms.TO_NULLABLE),
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.VARCHAR), SqlTypeTransforms.FORCE_NULLABLE),
InferTypes.RETURN_TYPE,
OperandTypes.or(
OperandTypes.sequence("'(TIMESTAMP, FORMAT)'",
Expand Down Expand Up @@ -792,7 +792,7 @@ public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
public static final SqlFunction TO_TIMESTAMP = new SqlFunction(
"TO_TIMESTAMP",
SqlKind.OTHER_FUNCTION,
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.TIMESTAMP), SqlTypeTransforms.TO_NULLABLE),
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.TIMESTAMP), SqlTypeTransforms.FORCE_NULLABLE),
null,
OperandTypes.or(
OperandTypes.family(SqlTypeFamily.NUMERIC),
Expand All @@ -811,7 +811,7 @@ public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
public static final SqlFunction TO_DATE = new SqlFunction(
"TO_DATE",
SqlKind.OTHER_FUNCTION,
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.DATE), SqlTypeTransforms.TO_NULLABLE),
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.DATE), SqlTypeTransforms.FORCE_NULLABLE),
null,
OperandTypes.or(
OperandTypes.family(SqlTypeFamily.NUMERIC),
Expand All @@ -822,7 +822,7 @@ public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
public static final SqlFunction TO_TIMESTAMP_TZ = new SqlFunction(
"TO_TIMESTAMP_TZ",
SqlKind.OTHER_FUNCTION,
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.TIMESTAMP), SqlTypeTransforms.TO_NULLABLE),
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.TIMESTAMP), SqlTypeTransforms.FORCE_NULLABLE),
null,
OperandTypes.or(
OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.STRING),
Expand All @@ -842,7 +842,7 @@ public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) {
public static final SqlFunction CONVERT_TZ = new SqlFunction(
"CONVERT_TZ",
SqlKind.OTHER_FUNCTION,
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.VARCHAR), SqlTypeTransforms.TO_NULLABLE),
ReturnTypes.cascade(ReturnTypes.explicit(SqlTypeName.VARCHAR), SqlTypeTransforms.FORCE_NULLABLE),
null,
OperandTypes.or(
OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.STRING, SqlTypeFamily.STRING),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2556,6 +2556,7 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {

@Test
def testToTimestamp(): Unit = {
testSqlApi("to_timestamp('abc')", "null")
testSqlApi("to_timestamp(1513135677000)", "2017-12-13 03:27:57.000")
testSqlApi("to_timestamp('2017-09-15 00:00:00')", "2017-09-15 00:00:00.000")
testSqlApi("to_timestamp('20170915000000', 'yyyyMMddHHmmss')", "2017-09-15 00:00:00.000")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,16 +642,53 @@ class TemporalTypesTest extends ExpressionTestBase {

testSqlApi("FROM_UNIXTIME(cast(NUll as bigInt))", nullable)

testSqlApi("TO_DATE(cast(NUll as varchar))", nullable)

testSqlApi("TO_TIMESTAMP_TZ(cast(NUll as varchar), 'Asia/Shanghai')", nullable)

testSqlApi(
"DATE_FORMAT_TZ(cast(NUll as timestamp), 'yyyy/MM/dd HH:mm:ss', 'Asia/Shanghai')",
nullable)
}

@Test
def testInvalidInputCase(): Unit = {
val invalidStr = "invalid value"
testSqlApi(s"DATE_FORMAT('$invalidStr', 'yyyy/MM/dd HH:mm:ss')", nullable)
testSqlApi(s"TO_TIMESTAMP('$invalidStr', 'yyyy-mm-dd')", nullable)
testSqlApi(s"TO_DATE('$invalidStr')", nullable)
testSqlApi(s"TO_TIMESTAMP_TZ('$invalidStr', 'Asia/Shanghai')", nullable)
testSqlApi(
s"CONVERT_TZ('$invalidStr', 'yyyy-MM-dd HH:mm:ss', 'UTC', 'Asia/Shanghai')",
nullable)
}

@Test
def testTypeInferenceWithInvalidInput(): Unit = {
// fix https://issues.apache.org/jira/browse/FLINK-13284
val invalidStr = "invalid value"
val cases = Seq(
s"DATE_FORMAT('$invalidStr', 'yyyy/MM/dd HH:mm:ss')",
s"TO_TIMESTAMP('$invalidStr', 'yyyy-mm-dd')",
s"TO_DATE('$invalidStr')",
s"TO_TIMESTAMP_TZ('$invalidStr', 'Asia/Shanghai')",
s"CONVERT_TZ('$invalidStr', 'yyyy-MM-dd HH:mm:ss', 'UTC', 'Asia/Shanghai')")

cases.foreach {
caseExpr =>
testSqlApi(
s"CASE WHEN ($caseExpr) is null THEN '$nullable' ELSE '$notNullable' END", nullable)
}
}

@Test
def testTimeZoneFunction(): Unit = {
testSqlApi("TO_TIMESTAMP_TZ('2018-03-14 11:00:00', 'Asia/Shanghai')", "2018-03-14 03:00:00.000")
testSqlApi("TO_TIMESTAMP_TZ('2018-03-14 11:00:00', 'yyyy-MM-dd HH:mm:ss', 'Asia/Shanghai')",
"2018-03-14 03:00:00.000")
"2018-03-14 03:00:00.000")

testSqlApi("CONVERT_TZ('2018-03-14 11:00:00', 'yyyy-MM-dd HH:mm:ss', 'UTC', 'Asia/Shanghai')",
"2018-03-14 19:00:00")
"2018-03-14 19:00:00")

testSqlApi("TO_TIMESTAMP_TZ(f14, 'UTC')", "null")

Expand All @@ -661,7 +698,6 @@ class TemporalTypesTest extends ExpressionTestBase {
testSqlApi("TO_TIMESTAMP_TZ('2018-03-14 11:00:00', 'invalid_tz')", "2018-03-14 11:00:00.000")
}


// ----------------------------------------------------------------------------------------------

override def testData: Row = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,11 @@ public static String dateFormatTz(long ts, String tzStr) {
* @param tzTo the target time zone
*/
public static String convertTz(String dateStr, String format, String tzFrom, String tzTo) {
return dateFormatTz(toTimestampTz(dateStr, format, tzFrom), tzTo);
Long ts = toTimestampTz(dateStr, format, tzFrom);
if (null != ts) { // avoid NPE
return dateFormatTz(ts, tzTo);
}
return null;
}

public static String convertTz(String dateStr, String tzFrom, String tzTo) {
Expand Down