Skip to content

Commit d6b2cda

Browse files
authored
[AMORO-4193][ams] Fix MysqlDataTruncation caused by negative finishTime in TableProcessMeta (#4192)
* Fix MysqlDataTruncation caused by negative finishTime in TableProcessMeta When creating a new TableProcessMeta via createProcessMeta(), finishTime was initialized to -1. Long2TsConverter only converted 0 to null, so Timestamp(-1) was passed to MySQL, which is outside the valid TIMESTAMP range (1970-01-01 00:00:01 UTC ~), causing MysqlDataTruncation. - Long2TsConverter: convert parameter <= 0 to null instead of only 0 - TableProcessMeta: initialize finishTime to 0 in createProcessMeta() for consistency with the of() factory method Signed-off-by: Jiwon Park <jpark92@outlook.kr> * Add unit tests for Long2TsConverter Signed-off-by: Jiwon Park <jpark92@outlook.kr> --------- Signed-off-by: Jiwon Park <jpark92@outlook.kr>
1 parent 93cde39 commit d6b2cda

3 files changed

Lines changed: 61 additions & 2 deletions

File tree

amoro-ams/src/main/java/org/apache/amoro/server/persistence/converter/Long2TsConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class Long2TsConverter extends LongTypeHandler {
3535
@Override
3636
public void setNonNullParameter(PreparedStatement ps, int i, Long parameter, JdbcType jdbcType)
3737
throws SQLException {
38-
if (parameter == 0) {
38+
if (parameter <= 0) {
3939
ps.setTimestamp(i, null);
4040
} else {
4141
ps.setTimestamp(i, new Timestamp(parameter));

amoro-ams/src/main/java/org/apache/amoro/server/process/TableProcessMeta.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public static TableProcessMeta createProcessMeta(TableProcess process) {
202202
tableProcessMeta.setExecutionEngine(process.getExecutionEngine());
203203
tableProcessMeta.setRetryNumber(0);
204204
tableProcessMeta.setCreateTime(System.currentTimeMillis());
205-
tableProcessMeta.setFinishTime(-1);
205+
tableProcessMeta.setFinishTime(0);
206206
tableProcessMeta.setFailMessage("");
207207
tableProcessMeta.setProcessParameters(process.getProcessParameters());
208208
tableProcessMeta.setSummary(process.getSummary());
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.amoro.server.persistence.converter;
20+
21+
import org.apache.ibatis.type.JdbcType;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.Test;
24+
import org.mockito.Mockito;
25+
26+
import java.sql.PreparedStatement;
27+
import java.sql.SQLException;
28+
import java.sql.Timestamp;
29+
30+
public class TestLong2TsConverter {
31+
32+
private Long2TsConverter converter;
33+
private PreparedStatement ps;
34+
35+
@BeforeEach
36+
void setUp() {
37+
converter = new Long2TsConverter();
38+
ps = Mockito.mock(PreparedStatement.class);
39+
}
40+
41+
@Test
42+
void testSetNullWhenZero() throws SQLException {
43+
converter.setNonNullParameter(ps, 1, 0L, JdbcType.TIMESTAMP);
44+
Mockito.verify(ps).setTimestamp(1, null);
45+
}
46+
47+
@Test
48+
void testSetNullWhenNegative() throws SQLException {
49+
converter.setNonNullParameter(ps, 1, -1L, JdbcType.TIMESTAMP);
50+
Mockito.verify(ps).setTimestamp(1, null);
51+
}
52+
53+
@Test
54+
void testSetTimestampWhenPositive() throws SQLException {
55+
long now = System.currentTimeMillis();
56+
converter.setNonNullParameter(ps, 1, now, JdbcType.TIMESTAMP);
57+
Mockito.verify(ps).setTimestamp(1, new Timestamp(now));
58+
}
59+
}

0 commit comments

Comments
 (0)