Skip to content

Commit 98975a3

Browse files
committed
feat: snowflake all data type
1 parent f0bfc4a commit 98975a3

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

connectors/snowflake-connector/src/main/java/io/tapdata/connector/snowflake/SnowflakeConnector.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.tapdata.entity.schema.TapIndex;
1717
import io.tapdata.entity.schema.TapIndexField;
1818
import io.tapdata.entity.schema.TapTable;
19+
import io.tapdata.entity.schema.value.*;
1920
import io.tapdata.entity.simplify.TapSimplify;
2021
import io.tapdata.entity.simplify.pretty.BiClassHandlers;
2122
import io.tapdata.entity.utils.DataMap;
@@ -28,9 +29,13 @@
2829
import io.tapdata.pdk.apis.entity.WriteListResult;
2930
import io.tapdata.pdk.apis.functions.ConnectorFunctions;
3031

31-
import java.sql.Connection;
32-
import java.sql.SQLException;
32+
import java.sql.*;
33+
import java.time.Instant;
34+
import java.time.ZoneId;
35+
import java.time.ZoneOffset;
3336
import java.util.List;
37+
import java.util.Map;
38+
import java.util.TimeZone;
3439
import java.util.concurrent.atomic.AtomicInteger;
3540
import java.util.function.Consumer;
3641
import java.util.stream.Collectors;
@@ -143,6 +148,21 @@ public void registerCapabilities(ConnectorFunctions connectorFunctions, TapCodec
143148
connectorFunctions.supportDropFieldFunction(this::fieldDDLHandler);
144149
connectorFunctions.supportGetTableNamesFunction(this::getTableNames);
145150
connectorFunctions.supportExecuteCommandFunction((a, b, c) -> SqlExecuteCommandFunction.executeCommand(a, b, () -> snowflakeJdbcContext.getConnection(), this::isAlive, c));
151+
152+
codecRegistry.registerFromTapValue(TapRawValue.class, "TEXT", tapRawValue -> {
153+
if (tapRawValue != null && tapRawValue.getValue() != null) return toJson(tapRawValue.getValue());
154+
return "null";
155+
});
156+
codecRegistry.registerFromTapValue(TapTimeValue.class, tapTimeValue -> tapTimeValue.getValue().toTimeStr());
157+
codecRegistry.registerFromTapValue(TapDateTimeValue.class, tapDateTimeValue -> {
158+
if (EmptyKit.isNotNull(tapDateTimeValue.getValue().getTimeZone())) {
159+
return tapDateTimeValue.getValue().toTimestamp();
160+
} else {
161+
return formatTapDateTime(tapDateTimeValue.getValue(), "yyyy-MM-dd HH:mm:ss.SSSSSS");
162+
}
163+
});
164+
codecRegistry.registerFromTapValue(TapDateValue.class, tapDateValue -> tapDateValue.getValue().toSqlDate());
165+
codecRegistry.registerFromTapValue(TapYearValue.class, "TEXT(4)", TapValue::getOriginValue);
146166
}
147167

148168
@Override
@@ -188,5 +208,23 @@ private void writeRecord(TapConnectorContext connectorContext, List<TapRecordEve
188208
.setTapLogger(tapLogger)
189209
.write(tapRecordEvents, writeListResultConsumer, this::isAlive);
190210
}
211+
212+
@Override
213+
protected void processDataMap(DataMap dataMap, TapTable tapTable) {
214+
for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
215+
Object value = entry.getValue();
216+
if (value instanceof Timestamp) {
217+
if (!tapTable.getNameFieldMap().get(entry.getKey()).getDataType().startsWith("TIMESTAMP_TZ")) {
218+
entry.setValue(((Timestamp) value).toLocalDateTime().minusHours(snowflakeConfig.getZoneOffsetHour()));
219+
} else {
220+
entry.setValue(((Timestamp) value).toLocalDateTime().minusHours(TimeZone.getDefault().getRawOffset() / 3600000).atZone(ZoneOffset.UTC));
221+
}
222+
} else if (value instanceof Date) {
223+
entry.setValue(Instant.ofEpochMilli(((Date) value).getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime());
224+
} else if (value instanceof Time) {
225+
entry.setValue(Instant.ofEpochMilli(((Time) value).getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime().minusHours(snowflakeConfig.getZoneOffsetHour()));
226+
}
227+
}
228+
}
191229
}
192230

connectors/snowflake-connector/src/main/resources/spec_snowflake.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@
219219
}
220220
},
221221
"dataTypes": {
222+
"BIGINT": {
223+
"to": "TapNumber",
224+
"bit": 64,
225+
"precision": 19,
226+
"value": [
227+
-9223372036854775808,
228+
9223372036854775807
229+
]
230+
},
222231
"NUMBER[($precision,$scale)]": {
223232
"precision": [
224233
1,
@@ -229,8 +238,10 @@
229238
37
230239
],
231240
"fixed": true,
232-
"preferPrecision": 38,
233-
"preferScale": 0,
241+
"preferPrecision": 12,
242+
"preferScale": 4,
243+
"defaultPrecision": 38,
244+
"defaultScale": 0,
234245
"priority": 1,
235246
"to": "TapNumber"
236247
},

0 commit comments

Comments
 (0)