dianfu commented on code in PR #20948:
URL: https://github.com/apache/flink/pull/20948#discussion_r988518067
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -1559,6 +1572,20 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
.outputTypeStrategy(SpecificTypeStrategies.TO_TIMESTAMP_LTZ)
.build();
+ public static final BuiltInFunctionDefinition TO_TIMESTAMP =
+ BuiltInFunctionDefinition.newBuilder()
+ .name("toTimestamp")
+ .kind(SCALAR)
+ .inputTypeStrategy(
+ or(
+ NO_ARGS,
+
sequence(logical(LogicalTypeFamily.CHARACTER_STRING)),
+ sequence(
+
logical(LogicalTypeFamily.CHARACTER_STRING),
+
logical(LogicalTypeFamily.CHARACTER_STRING))))
+ .outputTypeStrategy(nullableIfArgs(explicit(TIMESTAMP())))
Review Comment:
```suggestion
.outputTypeStrategy(nullableIfArgs(explicit(TIMESTAMP(3))))
```
##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/expressions/TemporalTypesTest.scala:
##########
@@ -613,6 +613,12 @@ class TemporalTypesTest extends ExpressionTestBase {
testSqlApi("TIME '12:44:31'", "12:44:31")
testSqlApi("TO_DATE('2018-03-18')", "2018-03-18")
Review Comment:
This line could be removed as it will be covered by
```
testAllApis(toDate("2018-03-18", "yyyy-MM-dd"), "TO_DATE('2018-03-18')",
"2018-03-18")
```
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/Expressions.java:
##########
@@ -321,6 +332,18 @@ public static ApiExpression toTimestampLtz(Object
numericEpochTime, Object preci
return apiCall(BuiltInFunctionDefinitions.TO_TIMESTAMP_LTZ,
numericEpochTime, precision);
}
+ /**
+ * Converts date time string string1 with format string2 (by default:
‘yyyy-MM-dd HH:mm:ss’)
+ * under the ‘UTC+0’ time zone to a timestamp.
+ *
+ * @param dateStr dateStr the date time string.
+ * @param format The format of the string.
+ * @return the return type of this expression is {@link
DataTypes#TIMESTAMP()}.
+ */
+ public static ApiExpression toTimestamp(Object dateStr, Object format) {
Review Comment:
ditto
##########
flink-python/pyflink/table/tests/test_expression.py:
##########
@@ -257,7 +257,11 @@ def test_expressions(self):
self.assertEqual('currentTimestamp()', str(current_timestamp()))
self.assertEqual('localTime()', str(local_time()))
self.assertEqual('localTimestamp()', str(local_timestamp()))
+ self.assertEqual("toDate('2018-03-18', 'yyyy-MM-dd')",
Review Comment:
Also test the following case where the format is None:
```
toDate('2018-03-18')
```
##########
flink-table/flink-table-api-scala/src/main/scala/org/apache/flink/table/api/ImplicitExpressionConversions.scala:
##########
@@ -481,6 +481,14 @@ trait ImplicitExpressionConversions {
Expressions.localTimestamp()
}
+ /**
+ * Converts date time string string1 in format string2 (by default:
yyyy-MM-dd HH:mm:ss if not
+ * specified) to Unix timestamp (in seconds), using the specified timezone
in table config.
+ */
+ def toDate(dateStr: Expression, format: Expression): Expression = {
Review Comment:
Missing
```
def toDate(dateStr: Expression): Expression
```
##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java:
##########
@@ -1559,6 +1572,20 @@ ANY, and(logical(LogicalTypeRoot.BOOLEAN), LITERAL)
.outputTypeStrategy(SpecificTypeStrategies.TO_TIMESTAMP_LTZ)
.build();
+ public static final BuiltInFunctionDefinition TO_TIMESTAMP =
+ BuiltInFunctionDefinition.newBuilder()
+ .name("toTimestamp")
+ .kind(SCALAR)
+ .inputTypeStrategy(
+ or(
+ NO_ARGS,
Review Comment:
Does It also support NO_ARGS?
##########
flink-table/flink-table-api-scala/src/main/scala/org/apache/flink/table/api/ImplicitExpressionConversions.scala:
##########
@@ -492,6 +500,14 @@ trait ImplicitExpressionConversions {
Expressions.toTimestampLtz(numericEpochTime, precision)
}
+ /**
+ * Converts date time string string1 with format string2 (by default:
‘yyyy-MM-dd HH:mm:ss’) under
+ * the ‘UTC+0’ time zone to a timestamp.
+ */
+ def toTimestamp(dateStr: Expression, format: Expression): Expression = {
Review Comment:
ditto
##########
flink-python/pyflink/table/tests/test_expression.py:
##########
@@ -257,7 +257,11 @@ def test_expressions(self):
self.assertEqual('currentTimestamp()', str(current_timestamp()))
self.assertEqual('localTime()', str(local_time()))
self.assertEqual('localTimestamp()', str(local_timestamp()))
+ self.assertEqual("toDate('2018-03-18', 'yyyy-MM-dd')",
+ str(to_date('2018-03-18', 'yyyy-MM-dd')))
self.assertEqual('toTimestampLtz(123, 0)', str(to_timestamp_ltz(123,
0)))
+ self.assertEqual("toTimestamp('1970-01-01 08:01:40', 'yyyy-MM-dd
HH:mm:ss')",
+ str(to_timestamp('1970-01-01 08:01:40', 'yyyy-MM-dd
HH:mm:ss')))
Review Comment:
ditto
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/api/Expressions.java:
##########
@@ -303,6 +303,17 @@ public static ApiExpression localTimestamp() {
return apiCall(BuiltInFunctionDefinitions.LOCAL_TIMESTAMP);
}
+ /**
+ * Converts a date string string1 with format string2 (by default
‘yyyy-MM-dd’) to a date.
+ *
+ * @param dateStr dateStr the date time string.
+ * @param format The format of the string.
+ * @return the return type of this expression is {@link DataTypes#DATE()}.
+ */
+ public static ApiExpression toDate(Object dateStr, Object format) {
Review Comment:
Need also add the following method:
```
public static ApiExpression toDate(Object dateStr)
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]