-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
detach buildType logic into a service
- Loading branch information
1 parent
665dbb8
commit 5d5df77
Showing
6 changed files
with
73 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...ka/connector/internal/streaming/schemaevolution/iceberg/IcebergColumnTreeTypeBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.snowflake.kafka.connector.internal.streaming.schemaevolution.iceberg; | ||
|
||
public class IcebergColumnTreeTypeBuilder { | ||
|
||
/** Returns data type of the column */ | ||
String buildType(IcebergColumnTree columnTree) { | ||
StringBuilder sb = new StringBuilder(); | ||
IcebergFieldNode rootNode = columnTree.getRootNode(); | ||
return buildType(sb, rootNode, ROOT_NODE_TYPE).toString(); | ||
} | ||
|
||
/** | ||
* Generate Snow SQL type for the column. | ||
* | ||
* @param sb StringBuilder | ||
* @param parentType Snowflake Iceberg table compatible type. ROOT_NODE_TYPE is a special case, | ||
* here we never generate column name for it. | ||
* @return field name + data type | ||
*/ | ||
private StringBuilder buildType(StringBuilder sb, IcebergFieldNode fieldNode, String parentType) { | ||
if (parentType.equals("ARRAY") | ||
|| parentType.equals("MAP") | ||
|| parentType.equals(ROOT_NODE_TYPE)) { | ||
sb.append(fieldNode.snowflakeIcebergType); | ||
} else { | ||
appendNameAndType(sb, fieldNode); | ||
} | ||
if (!fieldNode.children.isEmpty()) { | ||
sb.append("("); | ||
appendChildren(sb, fieldNode); | ||
sb.append(")"); | ||
} | ||
return sb; | ||
} | ||
|
||
private void appendNameAndType(StringBuilder sb, IcebergFieldNode fieldNode) { | ||
sb.append(fieldNode.name); | ||
sb.append(" "); | ||
sb.append(fieldNode.snowflakeIcebergType); | ||
} | ||
|
||
private void appendChildren(StringBuilder sb, IcebergFieldNode parentNode) { | ||
String parentType = parentNode.snowflakeIcebergType; | ||
parentNode.children.forEach( | ||
(name, childNode) -> { | ||
buildType(sb, childNode, parentType); | ||
sb.append(", "); | ||
}); | ||
removeLastSeparator(sb); | ||
} | ||
|
||
private void removeLastSeparator(StringBuilder sb) { | ||
sb.deleteCharAt(sb.length() - 1); | ||
sb.deleteCharAt(sb.length() - 1); | ||
} | ||
|
||
private static final String ROOT_NODE_TYPE = "ROOT_NODE"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters