Skip to content

Commit 8842a0b

Browse files
authored
Fix Avro ts mapping in case of avro-confluent format in AvroTableSchemaFactory (#1661)
1 parent 4e0ea0d commit 8842a0b

File tree

2 files changed

+116
-3
lines changed

2 files changed

+116
-3
lines changed

sqrl-planner/src/main/java/com/datasqrl/io/schema/avro/AvroTableSchemaFactory.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,33 @@ public Set<String> getExtensions() {
6464
}
6565

6666
/**
67-
* Extracts the legacy timestamp mapping setting from table properties, checking both direct and
68-
* value-prefixed property keys. Returns false if the keys are null or undefined.
67+
* Extracts the legacy timestamp mapping setting from table properties.
68+
*
69+
* <p>Checks for the property in the following order:
70+
*
71+
* <ol>
72+
* <li>Direct property key
73+
* <li>Value-prefixed property key
74+
* <li>If not found and any property key contains "avro-confluent", returns true
75+
* <li>Otherwise returns false
76+
* </ol>
77+
*
78+
* @param tableProps the table properties to extract the setting from
79+
* @return true if legacy timestamp mapping should be used, false otherwise
6980
*/
70-
private boolean getLegacyTimestampMapping(Map<String, String> tableProps) {
81+
boolean getLegacyTimestampMapping(Map<String, String> tableProps) {
7182
var legacyTimestampMappingStr = tableProps.get(LEGACY_TIMESTAMP_MAPPING_KEY);
7283

7384
if (legacyTimestampMappingStr == null) {
7485
legacyTimestampMappingStr = tableProps.get("value." + LEGACY_TIMESTAMP_MAPPING_KEY);
7586
}
7687

88+
// avro-confluent uses legacy no matter what
89+
if (legacyTimestampMappingStr == null
90+
&& tableProps.keySet().stream().anyMatch(key -> key.contains("avro-confluent"))) {
91+
return true;
92+
}
93+
7794
return Boolean.parseBoolean(legacyTimestampMappingStr);
7895
}
7996
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright © 2021 DataSQRL ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datasqrl.io.schema.avro;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.util.HashMap;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
class AvroTableSchemaFactoryTest {
25+
26+
private AvroTableSchemaFactory factory;
27+
28+
@BeforeEach
29+
void setUp() {
30+
factory = new AvroTableSchemaFactory();
31+
}
32+
33+
@Test
34+
void givenDirectPropertyKeySetToTrue_whenGetLegacyTimestampMapping_thenReturnsTrue() {
35+
// Given
36+
var tableProps = new HashMap<String, String>();
37+
tableProps.put("avro.timestamp_mapping.legacy", "true");
38+
39+
// When
40+
var result = factory.getLegacyTimestampMapping(tableProps);
41+
42+
// Then
43+
assertThat(result).isTrue();
44+
}
45+
46+
@Test
47+
void givenDirectPropertyKeySetToFalse_whenGetLegacyTimestampMapping_thenReturnsFalse() {
48+
// Given
49+
var tableProps = new HashMap<String, String>();
50+
tableProps.put("avro.timestamp_mapping.legacy", "false");
51+
52+
// When
53+
var result = factory.getLegacyTimestampMapping(tableProps);
54+
55+
// Then
56+
assertThat(result).isFalse();
57+
}
58+
59+
@Test
60+
void givenValuePrefixedPropertyKeySetToTrue_whenGetLegacyTimestampMapping_thenReturnsTrue() {
61+
// Given
62+
var tableProps = new HashMap<String, String>();
63+
tableProps.put("value.avro.timestamp_mapping.legacy", "true");
64+
65+
// When
66+
var result = factory.getLegacyTimestampMapping(tableProps);
67+
68+
// Then
69+
assertThat(result).isTrue();
70+
}
71+
72+
@Test
73+
void givenNoPropertiesButAvroConfluentKey_whenGetLegacyTimestampMapping_thenReturnsTrue() {
74+
// Given
75+
var tableProps = new HashMap<String, String>();
76+
tableProps.put("avro-confluent.url", "registry");
77+
78+
// When
79+
var result = factory.getLegacyTimestampMapping(tableProps);
80+
81+
// Then
82+
assertThat(result).isTrue();
83+
}
84+
85+
@Test
86+
void givenEmptyTableProps_whenGetLegacyTimestampMapping_thenReturnsFalse() {
87+
// Given
88+
var tableProps = new HashMap<String, String>();
89+
90+
// When
91+
var result = factory.getLegacyTimestampMapping(tableProps);
92+
93+
// Then
94+
assertThat(result).isFalse();
95+
}
96+
}

0 commit comments

Comments
 (0)