Skip to content
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

DRILL-7979: Self-Closing XML Tags Cause Schema Change Exceptions #2283

Merged
merged 8 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Unit test working but attribute not popping off stack
  • Loading branch information
cgivre committed Aug 1, 2021
commit cae08e401e5b2661b7c4deb7c580af15155edee5
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public class XMLReader {
private InputStream fsStream;
private XMLEventReader reader;
private ImplicitColumns metadata;
private boolean isSelfClosingEvent;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider adding something like IGNORED_ELEMENT or SELF_CLOSING_TAG state to the xmlState enum? Would that come out any simpler than the new boolean isSelfClosingEvent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this, however the issue is where the self-closing tag occurs. Adding an additional state might mess with the other functionality.


/**
* This field indicates the various states in which the reader operates. The names should be self explanatory,
* This field indicates the various states in which the reader operates. The names should be self-explanatory,
* but they are used as the reader iterates over the XML tags to know what to do.
*/
private enum xmlState {
Expand All @@ -96,6 +97,7 @@ public XMLReader(InputStream fsStream, int dataLevel, int maxRecords) throws XML
nestedMapCollection = new HashMap<>();
this.dataLevel = dataLevel;
this.maxRecords = maxRecords;
isSelfClosingEvent = false;
}

public void open(RowSetLoader rootRowWriter, CustomErrorContext errorContext ) {
Expand Down Expand Up @@ -155,10 +157,6 @@ private boolean processElements() {
return false;
}

int lineNumber;
int characterOffset;
int columnNumber;

// Iterate over XML events
while (reader.hasNext()) {
// get the current event
Expand All @@ -171,17 +169,9 @@ private boolean processElements() {
continue;
}

lineNumber = nextEvent.getLocation().getLineNumber();
characterOffset = nextEvent.getLocation().getCharacterOffset();
columnNumber = nextEvent.getLocation().getColumnNumber();

logger.debug("Event Info: {} {} {} {}", nextEvent, lineNumber, characterOffset, columnNumber);
if (isSelfClosingEvent(currentEvent, nextEvent)) {
logger.debug("Found self closing event!!");


currentEvent = nextEvent;
continue;
isSelfClosingEvent = true;
}

// Capture the previous and current event
Expand All @@ -201,7 +191,24 @@ private boolean processElements() {
return true;
}

/**
* One of the challenges with XML parsing are self-closing events. The streaming XML parser
* treats self-closing events as two events: a start event and an ending event. The issue is that
* the self-closing events can cause schema issues with Drill specifically, if a self-closing event
* is detected prior to a non-self-closing event, and that populated event contains a map or other nested data
* Drill will throw a schema change exception.
*
* Since Drill uses Java's streaming XML parser, unfortunately, it does not provide a means of identifying
* self-closing tags. This function does that by comparing the event with the previous event and looking for
* a condition where one event is a start and the other is an ending event. Additionally, the column number and
* character offsets must be the same, indicating that the two events are the same.
*
* @param e1 The first XMLEvent
* @param e2 The second XMLEvent
* @return True if the events represent a self-closing event, false if not.
*/
private boolean isSelfClosingEvent(XMLEvent e1, XMLEvent e2) {
// If either event is null return false.
if (e1 == null || e2 == null) {
return false;
}
Expand Down Expand Up @@ -318,6 +325,12 @@ private void processEvent(XMLEvent currentEvent,
case XMLStreamConstants.END_ELEMENT:
currentNestingLevel--;

if (isSelfClosingEvent) {
isSelfClosingEvent = false;
attributePrefix = XMLUtils.removeField(attributePrefix,fieldName);
break;
}

if (currentNestingLevel < dataLevel - 1) {
break;
} else if (currentEvent.asEndElement().getName().toString().compareTo(rootDataFieldName) == 0) {
Expand All @@ -342,7 +355,7 @@ private void processEvent(XMLEvent currentEvent,

attributePrefix = XMLUtils.removeField(attributePrefix,fieldName);

} else if (currentState != xmlState.ROW_ENDED){
} else if (currentState != xmlState.ROW_ENDED) {
writeFieldData(fieldName, fieldValue, currentTupleWriter);
// Clear out field name and value
attributePrefix = XMLUtils.removeField(attributePrefix, fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void testWildcard() throws Exception {
public void testSelfClosingTags() throws Exception {
String sql = "SELECT * FROM cp.`xml/weather.xml`";
RowSet results = client.queryBuilder().sql(sql).rowSet();
results.print();
assertEquals(1, results.rowCount());

TupleMetadata expectedSchema = new SchemaBuilder()
Expand Down Expand Up @@ -548,6 +549,21 @@ public void testLimitPushdown() throws Exception {
public void testMapError() throws Exception {
String sql = "SELECT * FROM cp.`xml/schemaChange.xml`";
RowSet results = client.queryBuilder().sql(sql).rowSet();
results.print();

TupleMetadata expectedSchema = new SchemaBuilder()
.addMap("attributes")
.resumeSchema()
.addMap("parent")
.addNullable("link", MinorType.VARCHAR)
.addNullable("value", MinorType.VARCHAR)
.resumeSchema()
.build();

RowSet expected = client.rowSetBuilder(expectedSchema)
.addRow(mapArray(), mapArray(null, null))
.addRow(mapArray(), strArray("https://dev57595.service-now.com/api/now/table/task/46eaa7c9a9fe198100bbe282da0d4b7d", "46eaa7c9a9fe198100bbe282da0d4b7d"))
.build();

new RowSetComparison(expected).verifyAndClearAll(results);
}
}