You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@paimon.apache.org by "JackeyLee007 (via GitHub)" <gi...@apache.org> on 2025/02/22 11:52:40 UTC

[PR] Cdc audit time [paimon]

JackeyLee007 opened a new pull request, #5138:
URL: https://github.com/apache/paimon/pull/5138

   <!-- Please specify the module before the PR name: [core] ... or [flink] ... -->
   
   ### Purpose
   
   <!-- Linking this pull request to the issue -->
   Linked issue: close #5068
   
   <!-- What is the purpose of the change -->
   Support special audition computed columns, especially for cdc sync database
   
   ### Tests
   
   <!-- List UT and IT cases to verify this change -->
   org.apache.paimon.flink.action.cdc.kafka.KafkaCanalSyncDatabaseActionITCase#testAuditTime
   ### API and Format
   
   <!-- Does this change affect API or storage format -->
   
   ### Documentation
   
   <!-- Does this change introduce a new feature -->
   


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [cdc] Support computed expression of now [paimon]

Posted by "yuzelin (via GitHub)" <gi...@apache.org>.
yuzelin commented on code in PR #5138:
URL: https://github.com/apache/paimon/pull/5138#discussion_r1996635187


##########
paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/Expression.java:
##########
@@ -608,4 +609,22 @@ public String eval(String input) {
             return value;
         }
     }
+
+    /** Get current timestamp. */
+    final class NowExpression implements Expression {
+        @Override
+        public String fieldReference() {
+            return null;
+        }
+
+        @Override
+        public DataType outputType() {
+            return DataTypes.TIMESTAMP();

Review Comment:
   TIMESTAMP(3)



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cdc audit time [paimon]

Posted by "JackeyLee007 (via GitHub)" <gi...@apache.org>.
JackeyLee007 closed pull request #5138: Cdc audit time
URL: https://github.com/apache/paimon/pull/5138


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cdc audit time [paimon]

Posted by "JackeyLee007 (via GitHub)" <gi...@apache.org>.
JackeyLee007 closed pull request #5138: Cdc audit time
URL: https://github.com/apache/paimon/pull/5138


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cdc audit time [paimon]

Posted by "JingsongLi (via GitHub)" <gi...@apache.org>.
JingsongLi commented on code in PR #5138:
URL: https://github.com/apache/paimon/pull/5138#discussion_r1976816501


##########
paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/Expression.java:
##########
@@ -608,4 +618,37 @@ public String eval(String input) {
             return value;
         }
     }
+
+    /** Get current timestamp. */
+    final class AuditTimeExpression implements Expression {
+
+        private final boolean insertOnly;
+        private final String fieldReference;
+
+        public AuditTimeExpression(boolean insertOnly) {
+            this.insertOnly = insertOnly;
+            this.fieldReference = SpecialFields.VALUE_KIND.name();
+        }
+
+        @Override
+        public String fieldReference() {
+            return this.fieldReference;
+        }
+
+        @Override
+        public DataType outputType() {
+            return DataTypes.TIMESTAMP();
+        }
+
+        @Override
+        public String eval(String input) {
+            RowKind rowKind = RowKind.fromShortString(input);
+            String now = java.sql.Timestamp.valueOf(LocalDateTime.now()).toString();
+            if (rowKind == RowKind.INSERT) {
+                return now;
+            }
+
+            return insertOnly ? null : now;

Review Comment:
   Copy from https://github.com/apache/paimon/pull/5086 :
   So if a new input update the record, the create_time will become null?



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [cdc] Support computed expression of now [paimon]

Posted by "yuzelin (via GitHub)" <gi...@apache.org>.
yuzelin merged PR #5138:
URL: https://github.com/apache/paimon/pull/5138


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [cdc] Support computed expression of now [paimon]

Posted by "yuzelin (via GitHub)" <gi...@apache.org>.
yuzelin commented on code in PR #5138:
URL: https://github.com/apache/paimon/pull/5138#discussion_r1996636360


##########
paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/SyncDatabaseActionBase.java:
##########
@@ -261,4 +270,11 @@ protected void buildSink(
                                 excludingTables))
                 .build();
     }
+
+    @Override
+    protected void beforeBuildingSourceSink() throws Exception {
+        computedColumns =
+                buildComputedColumns(
+                        computedColumnArgs, Collections.singletonList(SpecialFields.VALUE_KIND));

Review Comment:
   Move to `withComputedColumnArgs`



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/CdcActionITCaseBase.java:
##########
@@ -165,15 +182,40 @@ protected void waitForResult(
                             rowType);
             List<String> sortedActual = new ArrayList<>(result);
             Collections.sort(sortedActual);
-            if (sortedExpected.equals(sortedActual)) {
-                break;
-            }
+
             LOG.info("actual: " + sortedActual);
             LOG.info("expected: " + sortedExpected);
+
+            if (withRegx) {
+                if (isRegxMatchList(sortedActual, sortedExpected)) {
+                    break;
+                }
+            } else if (sortedExpected.equals(sortedActual)) {
+                break;
+            }
+            LOG.warn("actual: " + sortedActual);
+            LOG.warn("expected: " + sortedExpected);

Review Comment:
   Delete this.



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/kafka/KafkaCanalSyncDatabaseActionITCase.java:
##########
@@ -48,6 +52,8 @@
 
 /** IT cases for {@link KafkaSyncDatabaseAction}. */
 public class KafkaCanalSyncDatabaseActionITCase extends KafkaActionITCaseBase {
+    private static final Logger LOG =

Review Comment:
   Seems this is not used.



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/kafka/KafkaCanalSyncDatabaseActionITCase.java:
##########
@@ -643,6 +650,133 @@ public void testSpecifyKeys() throws Exception {
                 Collections.singletonList("k"));
     }
 
+    @Test
+    @Timeout(120)
+    public void testAuditTime() throws Exception {

Review Comment:
   testExpressionNow



##########
paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/Expression.java:
##########
@@ -608,4 +609,22 @@ public String eval(String input) {
             return value;
         }
     }
+
+    /** Get current timestamp. */
+    final class NowExpression implements Expression {
+        @Override
+        public String fieldReference() {
+            return null;
+        }
+
+        @Override
+        public DataType outputType() {
+            return DataTypes.TIMESTAMP();
+        }
+
+        @Override
+        public String eval(String input) {
+            return java.sql.Timestamp.valueOf(LocalDateTime.now()).toString();

Review Comment:
   Use `DateTimeUtils.formatLocalDateTime`



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/format/aliyun/AliyunJsonRecordParserTest.java:
##########
@@ -106,13 +122,31 @@ public void extractInsertRecord() throws Exception {
 
             MessageQueueCdcTimestampExtractor extractor = new MessageQueueCdcTimestampExtractor();
             Assert.assertTrue(extractor.extractTimestamp(cdcRecord) > 0);
+
+            Map<String, String> data = records.get(0).toRichCdcRecord().toCdcRecord().data();
+            String createTime = data.get("etl_create_time");
+            String updateTime = data.get("etl_update_time");
+
+            // Mock the real timestamp string which retrieved from store and convert through paimon
+            // Timestamp
+            createTime =
+                    BinaryStringUtils.toTimestamp(BinaryString.fromString(createTime), 6)
+                            .toString();
+            updateTime =
+                    BinaryStringUtils.toTimestamp(BinaryString.fromString(updateTime), 6)
+                            .toString();
+
+            Assert.assertTrue(createTime.matches(dateTimeRegex));
+            Assert.assertTrue(updateTime.matches(dateTimeRegex));
+
+            log.info("createTime: {}, updateTime: {}", createTime, updateTime);
         }
     }
 
     @Test
     public void extractUpdateRecord() throws Exception {
         AliyunRecordParser parser =
-                new AliyunRecordParser(TypeMapping.defaultMapping(), Collections.emptyList());
+                new AliyunRecordParser(TypeMapping.defaultMapping(), computedColumns);
         for (String json : updateList) {
             // 将json解析为JsonNode对象

Review Comment:
   Use english.



##########
paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/action/cdc/format/AbstractJsonRecordParser.java:
##########
@@ -103,6 +103,7 @@ protected Map<String, String> extractRowData(JsonNode record, RowType.Builder ro
                                             }
                                             return Objects.toString(entry.getValue());
                                         }));
+

Review Comment:
   Delete this.



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/kafka/KafkaCanalSyncDatabaseActionITCase.java:
##########
@@ -643,6 +650,133 @@ public void testSpecifyKeys() throws Exception {
                 Collections.singletonList("k"));
     }
 
+    @Test
+    @Timeout(120)
+    public void testAuditTime() throws Exception {
+        final String topic = "specify-keys";
+        createTestTopic(topic, 1, 1);
+        writeRecordsToKafka(topic, "kafka/canal/database/audit-time/canal-data-1.txt");
+
+        Map<String, String> kafkaConfig = getBasicKafkaConfig();
+        kafkaConfig.put(VALUE_FORMAT.key(), "canal-json");
+        kafkaConfig.put(TOPIC.key(), topic);
+
+        KafkaSyncDatabaseAction action =
+                syncDatabaseActionBuilder(kafkaConfig)
+                        .withTableConfig(getBasicTableConfig())
+                        .withPrimaryKeys("k")
+                        .withComputedColumnArgs(
+                                Arrays.asList("etl_create_time=now()", "etl_update_time=now()"))
+                        .build();
+        runActionWithDefaultEnv(action);
+
+        waitingTables("t1");
+
+        FileStoreTable table1 = getFileStoreTable("t1");
+        assertThat(table1.primaryKeys()).containsExactly("k");
+
+        RowType rowType1 =
+                RowType.of(
+                        new DataType[] {
+                            DataTypes.INT().notNull(),
+                            DataTypes.VARCHAR(10),
+                            DataTypes.TIMESTAMP(),

Review Comment:
   Timestamp(3)



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/format/aliyun/AliyunJsonRecordParserTest.java:
##########
@@ -106,13 +122,31 @@ public void extractInsertRecord() throws Exception {
 
             MessageQueueCdcTimestampExtractor extractor = new MessageQueueCdcTimestampExtractor();
             Assert.assertTrue(extractor.extractTimestamp(cdcRecord) > 0);
+
+            Map<String, String> data = records.get(0).toRichCdcRecord().toCdcRecord().data();
+            String createTime = data.get("etl_create_time");
+            String updateTime = data.get("etl_update_time");
+
+            // Mock the real timestamp string which retrieved from store and convert through paimon
+            // Timestamp
+            createTime =
+                    BinaryStringUtils.toTimestamp(BinaryString.fromString(createTime), 6)
+                            .toString();
+            updateTime =
+                    BinaryStringUtils.toTimestamp(BinaryString.fromString(updateTime), 6)
+                            .toString();
+
+            Assert.assertTrue(createTime.matches(dateTimeRegex));
+            Assert.assertTrue(updateTime.matches(dateTimeRegex));
+
+            log.info("createTime: {}, updateTime: {}", createTime, updateTime);

Review Comment:
   Delete this.



##########
paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/action/ActionITCaseBase.java:
##########
@@ -130,6 +134,27 @@ protected FileStoreTable getFileStoreTable(String tableName) throws Exception {
         return (FileStoreTable) catalog.getTable(identifier);
     }
 
+    protected AuditLogTable getAuditLogTable(String tableName) throws Exception {
+        Identifier identifier = Identifier.create(database, tableName + "$audit_log");
+        return (AuditLogTable) catalog.getTable(identifier);
+    }
+
+    protected List<InternalRow> getAuditLogData(String tableName) throws Exception {

Review Comment:
   We don't need to do this test.



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/CdcActionITCaseBase.java:
##########
@@ -143,6 +153,13 @@ protected void waitForResult(
                     if (sameName && sameType) {
                         cnt++;
                     }
+
+                    LOG.info("actual: field: " + field.name() + " " + field.type());

Review Comment:
   Delete this



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/format/aliyun/AliyunJsonRecordParserTest.java:
##########
@@ -134,13 +168,24 @@ public void extractUpdateRecord() throws Exception {
 
             MessageQueueCdcTimestampExtractor extractor = new MessageQueueCdcTimestampExtractor();
             Assert.assertTrue(extractor.extractTimestamp(cdcRecord) > 0);
+
+            Map<String, String> data = records.get(0).toRichCdcRecord().toCdcRecord().data();
+            String createTime = data.get("etl_create_time");
+            String updateTime = data.get("etl_update_time");
+            Assert.assertNotNull(createTime);
+
+            updateTime =
+                    BinaryStringUtils.toTimestamp(BinaryString.fromString(updateTime), 6)
+                            .toString();
+
+            Assert.assertTrue(updateTime.matches(dateTimeRegex));
         }
     }
 
     @Test
     public void extractDeleteRecord() throws Exception {
         AliyunRecordParser parser =
-                new AliyunRecordParser(TypeMapping.defaultMapping(), Collections.emptyList());
+                new AliyunRecordParser(TypeMapping.defaultMapping(), computedColumns);
         for (String json : deleteList) {
             // 将json解析为JsonNode对象

Review Comment:
   Use english.



##########
paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/action/cdc/format/aliyun/AliyunJsonRecordParserTest.java:
##########
@@ -76,15 +87,20 @@ public void setup() {
                     .filter(this::isRecordLine)
                     .forEach(e -> deleteList.add(e));
 
+            computedColumns =
+                    ComputedColumnUtils.buildComputedColumns(
+                            Arrays.asList(computedColumnArgs), Collections.emptyList());
+
         } catch (Exception e) {
             log.error("Fail to init aliyun-json cases", e);
         }
     }
 
     @Test
     public void extractInsertRecord() throws Exception {
+
         AliyunRecordParser parser =
-                new AliyunRecordParser(TypeMapping.defaultMapping(), Collections.emptyList());
+                new AliyunRecordParser(TypeMapping.defaultMapping(), computedColumns);
         for (String json : insertList) {
             // 将json解析为JsonNode对象

Review Comment:
   Please use english.



-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [cdc] Support computed expression of now [paimon]

Posted by "yuzelin (via GitHub)" <gi...@apache.org>.
yuzelin commented on PR #5138:
URL: https://github.com/apache/paimon/pull/5138#issuecomment-2726457329

   +1


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Cdc audit time [paimon]

Posted by "JackeyLee007 (via GitHub)" <gi...@apache.org>.
JackeyLee007 commented on PR #5138:
URL: https://github.com/apache/paimon/pull/5138#issuecomment-2677253864

   @JingsongLi Have a code review again, please. I adpated the code according to your comments.
   1. Add the rowkind parameter to extractRowData method directly, instead of moving evalComputedColumns out.
   2. Refine the UT case, to test UPDATE and check the etl_create_time value which is not updated bu NULL value. 


-- 
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: issues-unsubscribe@paimon.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org