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-11010] [TABLE] Flink SQL timestamp is inconsistent with currentProcessingTime() #7180

Closed
wants to merge 7 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1449,6 +1449,7 @@ abstract class CodeGenerator(
val resultCode =
s"""
|long $resultTerm = $contextTerm.timerService().currentProcessingTime();
|$resultTerm = $resultTerm + java.util.TimeZone.getDefault().getOffset($resultTerm);
lamberken marked this conversation as resolved.
Show resolved Hide resolved
|""".stripMargin
GeneratedExpression(resultTerm, NEVER_NULL, resultCode, SqlTimeTypeInfo.TIMESTAMP)
}
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.api.java.StreamTableEnvironment;
Expand All @@ -33,9 +34,14 @@
import org.apache.flink.test.util.AbstractTestBase;
import org.apache.flink.types.Row;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.Assert;
import org.junit.Test;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
Expand Down Expand Up @@ -164,4 +170,40 @@ public void testUnion() throws Exception {

StreamITCase.compareWithList(expected);
}

@Test
public void testProctime() throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
StreamTableEnvironment tableEnv = TableEnvironment.getTableEnvironment(env);

DataStream<Tuple3<Integer, Long, String>> ds = JavaStreamTestData.getSmall3TupleDataSet(env);
tableEnv.registerDataStream("MyTable", ds, "a, b, c, proctime.proctime");

String sqlQuery = "select proctime from MyTable";

Table result = tableEnv.sqlQuery(sqlQuery);

tableEnv
.toAppendStream(result, TypeInformation.of(Row.class))
.addSink(new SinkFunction<Row>() {
@Override
public void invoke(Row value, Context context) throws Exception {

Calendar calendar = Calendar.getInstance();

Timestamp procTimestamp = (Timestamp) value.getField(0);
calendar.setTimeInMillis(procTimestamp.getTime());
int procHour = calendar.get(Calendar.HOUR_OF_DAY);

calendar.setTimeInMillis(System.currentTimeMillis());
int sysHour = calendar.get(Calendar.HOUR_OF_DAY);

// just need to validate hour
Assert.assertTrue(procHour == sysHour);

}
});

env.execute();
}
}