Repository: apache/flink
File: flink-python/pyflink/table/tests/test_expression.py (Python)

Code before:

        from pyflink.table.expressions import UNBOUNDED_ROW, UNBOUNDED_RANGE, CURRENT_ROW, \
            CURRENT_RANGE
        self.assertEqual('UNBOUNDED_ROW', str(UNBOUNDED_ROW))
        self.assertEqual('UNBOUNDED_RANGE', str(UNBOUNDED_RANGE))
        self.assertEqual('CURRENT_ROW', str(CURRENT_ROW))
        self.assertEqual('CURRENT_RANGE', str(CURRENT_RANGE))
        self.assertEqual('currentDatabase()', str(current_database()))

        self.assertEqual('currentDate()', str(current_date()))
        self.assertEqual('currentTime()', str(current_time()))
        self.assertEqual('currentTimestamp()', str(current_timestamp()))
        self.assertEqual('localTime()', str(local_time()))
        self.assertEqual('localTimestamp()', str(local_timestamp()))
        self.assertEqual("toDate('2018-03-18')", str(to_date('2018-03-18')))
        self.assertEqual("toDate('2018-03-18', 'yyyy-MM-dd')",
                         str(to_date('2018-03-18', 'yyyy-MM-dd')))
        self.assertEqual('TO_TIMESTAMP_LTZ(100)', str(to_timestamp_ltz(100)))
        self.assertEqual("TO_TIMESTAMP_LTZ('2023-01-01 00:00:00')",
                         str(to_timestamp_ltz('2023-01-01 00:00:00')))
        self.assertEqual("TO_TIMESTAMP_LTZ('01/01/2023 00:00:00', 'MM/dd/yyyy HH:mm:ss')",
                         str(to_timestamp_ltz("01/01/2023 00:00:00", "MM/dd/yyyy HH:mm:ss")))
        self.assertEqual("TO_TIMESTAMP_LTZ('2023-01-01 00:00:00', 'yyyy-MM-dd HH:mm:ss', 'UTC')",
                         str(to_timestamp_ltz("2023-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss", "UTC")))
        self.assertEqual("TO_TIMESTAMP_LTZ(123, 0)", str(to_timestamp_ltz(123, 0)))
        self.assertEqual('TO_TIMESTAMP_LTZ(a)', str(to_timestamp_ltz(expr1))
        self.assertEqual('TO_TIMESTAMP_LTZ(a, 0)', str(to_timestamp_ltz(expr1, 0))
        self.assertEqual("TO_TIMESTAMP_LTZ(a, 'MM/dd/yyyy HH:mm:ss')",
                         str(to_timestamp_ltz(expr1, "MM/dd/yyyy HH:mm:ss"))
        self.assertEqual("TO_TIMESTAMP_LTZ(a, 'MM/dd/yyyy HH:mm:ss', 'UTC')",
                         str(to_timestamp_ltz(expr1, "MM/dd/yyyy HH:mm:ss", "UTC"))
        self.assertEqual("toTimestamp('1970-01-01 08:01:40')",
                         str(to_timestamp('1970-01-01 08:01:40')))
        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')))
        self.assertEqual("temporalOverlaps(cast('2:55:00', TIME(0)), 3600000, "
                         "cast('3:30:00', TIME(0)), 7200000)",
                         str(temporal_overlaps(
                             lit("2:55:00").to_time,
                             lit(1).hours,
                             lit("3:30:00").to_time,
                             lit(2).hours)))
        self.assertEqual("dateFormat(time, '%Y, %d %M')",
                         str(date_format(col("time"), "%Y, %d %M")))
        self.assertEqual("timestampDiff(DAY, cast('2016-06-15', DATE), cast('2016-06-18', DATE))",
                         str(timestamp_diff(
                             TimePointUnit.DAY,
                             lit("2016-06-15").to_date,
                             lit("2016-06-18").to_date)))
        self.assertEqual("convertTz('2018-03-14 11:00:00', 'UTC', 'Asia/Shanghai')",
                         str(convert_tz("2018-03-14 11:00:00", "UTC", "Asia/Shanghai")))

Review comment (bug):
I am curious, would these added tests would have failed before the fix? As the Lit function gets string input - which I would think it could cope with. By all means leave in these tests, but I think we should have more tests; I suggest including a test of the reported issue with col() which fails prior to the fix. 

I suggest adding some tests where the function is not successful.  
  

Code after:

        from pyflink.table.expressions import UNBOUNDED_ROW, UNBOUNDED_RANGE, CURRENT_ROW, \
            CURRENT_RANGE
        self.assertEqual('UNBOUNDED_ROW', str(UNBOUNDED_ROW))
        self.assertEqual('UNBOUNDED_RANGE', str(UNBOUNDED_RANGE))
        self.assertEqual('CURRENT_ROW', str(CURRENT_ROW))
        self.assertEqual('CURRENT_RANGE', str(CURRENT_RANGE))
        self.assertEqual('currentDatabase()', str(current_database()))

        self.assertEqual('currentDate()', str(current_date()))
        self.assertEqual('currentTime()', str(current_time()))
        self.assertEqual('currentTimestamp()', str(current_timestamp()))
        self.assertEqual('localTime()', str(local_time()))
        self.assertEqual('localTimestamp()', str(local_timestamp()))
        self.assertEqual("toDate('2018-03-18')", str(to_date('2018-03-18')))
        self.assertEqual("toDate('2018-03-18', 'yyyy-MM-dd')",
                         str(to_date('2018-03-18', 'yyyy-MM-dd')))
        self.assertEqual('TO_TIMESTAMP_LTZ(100)', str(to_timestamp_ltz(100)))
        self.assertEqual("TO_TIMESTAMP_LTZ('2023-01-01 00:00:00')",
                         str(to_timestamp_ltz('2023-01-01 00:00:00')))
        self.assertEqual("TO_TIMESTAMP_LTZ('01/01/2023 00:00:00', 'MM/dd/yyyy HH:mm:ss')",
                         str(to_timestamp_ltz("01/01/2023 00:00:00", "MM/dd/yyyy HH:mm:ss")))
        self.assertEqual("TO_TIMESTAMP_LTZ('2023-01-01 00:00:00', 'yyyy-MM-dd HH:mm:ss', 'UTC')",
                         str(to_timestamp_ltz("2023-01-01 00:00:00", "yyyy-MM-dd HH:mm:ss", "UTC")))
        self.assertEqual("TO_TIMESTAMP_LTZ(123, 0)", str(to_timestamp_ltz(123, 0)))
        self.assertEqual("TO_TIMESTAMP_LTZ(a)", str(to_timestamp_ltz(expr1)))
        self.assertEqual("TO_TIMESTAMP_LTZ(a, 0)", str(to_timestamp_ltz(expr1, 0)))
        self.assertEqual("TO_TIMESTAMP_LTZ(a, 'MM/dd/yyyy HH:mm:ss')",
                         str(to_timestamp_ltz(expr1, "MM/dd/yyyy HH:mm:ss")))
        self.assertEqual("TO_TIMESTAMP_LTZ(a, 'MM/dd/yyyy HH:mm:ss', 'UTC')",
                         str(to_timestamp_ltz(expr1, "MM/dd/yyyy HH:mm:ss", "UTC")))
        self.assertEqual("toTimestamp('1970-01-01 08:01:40')",
                         str(to_timestamp('1970-01-01 08:01:40')))
        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')))
        self.assertEqual("temporalOverlaps(cast('2:55:00', TIME(0)), 3600000, "
                         "cast('3:30:00', TIME(0)), 7200000)",
                         str(temporal_overlaps(
                             lit("2:55:00").to_time,
                             lit(1).hours,
                             lit("3:30:00").to_time,
                             lit(2).hours)))
        self.assertEqual("dateFormat(time, '%Y, %d %M')",
                         str(date_format(col("time"), "%Y, %d %M")))
        self.assertEqual("timestampDiff(DAY, cast('2016-06-15', DATE), cast('2016-06-18', DATE))",
                         str(timestamp_diff(
                             TimePointUnit.DAY,
                             lit("2016-06-15").to_date,
                             lit("2016-06-18").to_date)))
        self.assertEqual("convertTz('2018-03-14 11:00:00', 'UTC', 'Asia/Shanghai')",
                         str(convert_tz("2018-03-14 11:00:00", "UTC", "Asia/Shanghai")))