-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
-Support for Sink Codecs #3081
Closed
Closed
-Support for Sink Codecs #3081
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7d4019c
-Support for Sink Codecs
omkarmmore95 dcac7fa
Merge branch 'opensearch-project:main' into tabularschema
omkarmmore95 c2553cb
Merge branch 'opensearch-project:main' into tabularschema
omkarmmore95 d82ad55
Merge branch 'opensearch-project:main' into tabularschema
omkarmmore95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
194 changes: 194 additions & 0 deletions
194
...java/org/opensearch/dataprepper/plugins/codec/avro/AvroSchemaParserFromTabularFormat.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,194 @@ | ||
package org.opensearch.dataprepper.plugins.codec.avro; | ||
|
||
import org.apache.avro.Schema; | ||
|
||
import java.io.IOException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class AvroSchemaParserFromTabularFormat { | ||
|
||
private static final String END_SCHEMA_STRING = "]}"; | ||
private static final Pattern pattern = Pattern.compile("\\(([^()]*|)*\\)"); | ||
|
||
static Schema generateSchemaFromTabularString(final String inputString) throws IOException { | ||
String recordSchemaOutputString = inputString; | ||
recordSchemaOutputString = recordSchemaOutputString.trim(); | ||
|
||
final String tableName = extractTableName(recordSchemaOutputString.split("\\s+")); | ||
recordSchemaOutputString = getStringFromParanthesis(recordSchemaOutputString); | ||
recordSchemaOutputString = removeSpaceAndReplaceParanthesis(recordSchemaOutputString); | ||
|
||
final StringBuilder mainSchemaBuilder = new StringBuilder(); | ||
final String baseSchemaStr = "{\"type\":\"record\",\"name\":\"" + tableName + "\",\"fields\":["; | ||
mainSchemaBuilder.append(baseSchemaStr); | ||
|
||
iterateRecursively(mainSchemaBuilder, recordSchemaOutputString, false, tableName, 0); | ||
|
||
mainSchemaBuilder.append(END_SCHEMA_STRING); | ||
|
||
return new org.apache.avro.Schema.Parser().parse(mainSchemaBuilder.toString()); | ||
} | ||
|
||
private static String removeSpaceAndReplaceParanthesis(String recordSchemaOutputString) { | ||
recordSchemaOutputString = recordSchemaOutputString.replaceAll("\\(", ""); | ||
recordSchemaOutputString = recordSchemaOutputString.replaceAll("\\)", ""); | ||
|
||
recordSchemaOutputString = recordSchemaOutputString.trim(); | ||
|
||
recordSchemaOutputString = recordSchemaOutputString.replaceAll("\\n", ""); | ||
recordSchemaOutputString = recordSchemaOutputString.replaceAll(",\\s+", ","); | ||
recordSchemaOutputString = recordSchemaOutputString.replaceAll(">\\s+", ">"); | ||
recordSchemaOutputString = recordSchemaOutputString.replaceAll("<\\s+", "<"); | ||
return recordSchemaOutputString; | ||
} | ||
|
||
private static String getStringFromParanthesis(String recordSchemaOutputString) { | ||
final Matcher matcher = pattern.matcher(recordSchemaOutputString); | ||
if (matcher.find()) { | ||
recordSchemaOutputString = matcher.group(0); | ||
} | ||
return recordSchemaOutputString; | ||
} | ||
|
||
private static String extractTableName(final String[] words) throws IOException { | ||
String tableName = null; | ||
if (words.length >= 2) { | ||
tableName = words[1]; | ||
} else { | ||
throw new IOException("Invalid schema string."); | ||
} | ||
return tableName; | ||
} | ||
|
||
private static String buildBaseSchemaString(final String part) { | ||
return "{\"type\":\"record\",\"name\":\"" + part + "\",\"fields\":["; | ||
} | ||
|
||
private static String buildSchemaStringForArr() { | ||
return "{\"type\":\"array\", \"items\":\"string\"}"; | ||
} | ||
|
||
public static void iterateRecursively(final StringBuilder mainSchemaBuilder, final String recordSchema, | ||
final boolean isStructString, final String tableName, int innerRecCounter) { | ||
boolean isNameStringFormed = false; | ||
StringBuilder fieldNameBuilder = new StringBuilder(); | ||
StringBuilder fieldTypeBuilder = new StringBuilder(); | ||
boolean isFirstRecordForName = true; | ||
|
||
final char[] schemaStrCharArr = recordSchema.toCharArray(); | ||
int curPosInSchemaStrCharArr = 0; | ||
|
||
while (curPosInSchemaStrCharArr < schemaStrCharArr.length) { | ||
final char currentCharFromArr = schemaStrCharArr[curPosInSchemaStrCharArr]; | ||
curPosInSchemaStrCharArr++; | ||
|
||
if (!isNameStringFormed) { | ||
if (isStructString && currentCharFromArr == ':') { | ||
if (isFirstRecordForName) { | ||
mainSchemaBuilder.append("{\"name\":\"" + fieldNameBuilder.toString() + "\",\"type\":\""); | ||
} else { | ||
mainSchemaBuilder.append(",{\"name\":\"" + fieldNameBuilder.toString() + "\",\"type\":\""); | ||
} | ||
isNameStringFormed = true; | ||
fieldNameBuilder = new StringBuilder(); | ||
isFirstRecordForName = false; | ||
continue; | ||
} else if (currentCharFromArr == ' ') { | ||
if (isFirstRecordForName) { | ||
mainSchemaBuilder.append("{\"name\":\"" + fieldNameBuilder.toString() + "\",\"type\":\""); | ||
} else { | ||
mainSchemaBuilder.append(",{\"name\":\"" + fieldNameBuilder.toString() + "\",\"type\":\""); | ||
} | ||
isNameStringFormed = true; | ||
fieldNameBuilder = new StringBuilder(); | ||
isFirstRecordForName = false; | ||
continue; | ||
} | ||
fieldNameBuilder.append(currentCharFromArr); | ||
} | ||
|
||
if (isNameStringFormed) { | ||
|
||
if (currentCharFromArr == ',' || curPosInSchemaStrCharArr == schemaStrCharArr.length) { | ||
if (curPosInSchemaStrCharArr == schemaStrCharArr.length) { | ||
fieldTypeBuilder.append(currentCharFromArr); | ||
} | ||
final String type = fieldTypeBuilder.toString().trim() + "\"}"; | ||
|
||
mainSchemaBuilder.append(type); | ||
isNameStringFormed = false; | ||
fieldTypeBuilder = new StringBuilder(); | ||
continue; | ||
} | ||
|
||
fieldTypeBuilder.append(currentCharFromArr); | ||
if ("struct".equals(fieldTypeBuilder.toString())) { | ||
mainSchemaBuilder.deleteCharAt(mainSchemaBuilder.length() - 1); | ||
mainSchemaBuilder.append(buildBaseSchemaString(tableName + "_" + innerRecCounter)); | ||
final String structSchemaStr = recordSchema.substring(curPosInSchemaStrCharArr); | ||
final StringBuilder structString = new StringBuilder(); | ||
int openClosedCounter = 0; | ||
int structSchemaStrEndBracketPos = 0; | ||
for (final char innerChar : structSchemaStr.toCharArray()) { | ||
structSchemaStrEndBracketPos++; | ||
if (innerChar == '<') { | ||
openClosedCounter++; | ||
} else if (innerChar == '>') { | ||
openClosedCounter--; | ||
} | ||
structString.append(innerChar); | ||
if (openClosedCounter == 0) { | ||
break; | ||
} | ||
} | ||
|
||
final String innerRecord = structString.toString().substring(1, structSchemaStrEndBracketPos - 1); | ||
iterateRecursively(mainSchemaBuilder, innerRecord, true, | ||
tableName, innerRecCounter + 1); | ||
mainSchemaBuilder.append("}"); | ||
curPosInSchemaStrCharArr = curPosInSchemaStrCharArr + structSchemaStrEndBracketPos; | ||
if (curPosInSchemaStrCharArr < schemaStrCharArr.length) { | ||
// Skip one comma after the close struct close | ||
curPosInSchemaStrCharArr++; | ||
} | ||
isNameStringFormed = false; | ||
fieldTypeBuilder = new StringBuilder(); | ||
} else if ("array".equals(fieldTypeBuilder.toString())) { | ||
mainSchemaBuilder.deleteCharAt(mainSchemaBuilder.length() - 1); | ||
mainSchemaBuilder.append(buildSchemaStringForArr()); | ||
final String structSchemaStr = recordSchema.substring(curPosInSchemaStrCharArr); | ||
int openClosedCounter = 0; | ||
int structSchemaStrEndBracketPos = 0; | ||
|
||
for (final char innerChar : structSchemaStr.toCharArray()) { | ||
structSchemaStrEndBracketPos++; | ||
if (innerChar == '<') { | ||
openClosedCounter++; | ||
} else if (innerChar == '>') { | ||
openClosedCounter--; | ||
} | ||
if (openClosedCounter == 0) { | ||
break; | ||
} | ||
} | ||
|
||
mainSchemaBuilder.append("}"); | ||
curPosInSchemaStrCharArr = curPosInSchemaStrCharArr + structSchemaStrEndBracketPos; | ||
if (curPosInSchemaStrCharArr < schemaStrCharArr.length) { | ||
// Skip one comma after the close struct close | ||
curPosInSchemaStrCharArr++; | ||
} | ||
|
||
isNameStringFormed = false; | ||
fieldTypeBuilder = new StringBuilder(); | ||
} | ||
} | ||
} | ||
|
||
if (isStructString) { | ||
mainSchemaBuilder.append(END_SCHEMA_STRING); | ||
} | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code appears to be taking a SQL-like schema and then converting it into an Avro schema. Right?
Why not just accept the Avro schema in the pipeline configuration?
This appears to be introducing an undefined language and there may be many different edge cases that we are not accounting for.
If this is using a well defined language, can we perform a model mapping to make sure it is valid? Say for example, it is a Postgresql DDL, can we parse the DDL using a Postgresql library into a Java model? Then we can perform a more accurate mapping.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @dlvenable , This is Raj's suggestion to support glue like schema and converting it onto avro schema, We are already accepting Avro schema in pipeline YAML itself,
As of now there is no library to do mapping between glue schema like structure to Avro/Parquet schema as there are nested and logical types in Avro/Parquet also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the syntax of this table structure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dlvenable , Shared over mail, as I cant paste here