use of org.apache.drill.common.exceptions.UserException in project drill by apache.
the class QueryBatchIterator method next.
@Override
public boolean next() {
retainData = false;
if (state == State.EOF) {
return false;
}
while (true) {
QueryEvent event = listener.get();
queryState = event.state;
switch(event.type) {
case BATCH:
// Skip over null batches
if (loadBatch(event)) {
return true;
}
break;
case EOF:
state = State.EOF;
return false;
case ERROR:
state = State.EOF;
if (event.error instanceof UserException) {
throw (UserException) event.error;
} else {
throw new RuntimeException(event.error);
}
case QUERY_ID:
queryId = event.queryId;
break;
default:
throw new IllegalStateException("Unexpected event: " + event.type);
}
}
}
use of org.apache.drill.common.exceptions.UserException in project drill by apache.
the class DrillCursor method nextRowInternally.
/**
* ...
* <p>
* Is to be called (once) from {@link #loadInitialSchema} for
* {@link DrillResultSetImpl#execute()}, and then (repeatedly) from
* {@link #next()} for {@link org.apache.calcite.avatica.AvaticaResultSet#next()}.
* </p>
*
* @return whether cursor is positioned at a row (false when after end of
* results)
*/
private boolean nextRowInternally() throws SQLException {
if (currentRecordNumber + 1 < currentBatchHolder.getRecordCount()) {
// Have next row in current batch--just advance index and report "at a row."
currentRecordNumber++;
return true;
} else {
try {
QueryDataBatch qrb = resultsListener.getNext();
// the (initial) schema but no rows)).
if (afterFirstBatch) {
while (qrb != null && (qrb.getHeader().getRowCount() == 0 && qrb.getData() == null)) {
// Empty message--dispose of and try to get another.
logger.warn("Spurious batch read: {}", qrb);
qrb.release();
qrb = resultsListener.getNext();
}
}
afterFirstBatch = true;
if (qrb == null) {
// End of batches--clean up, set state to done, report after last row.
// (We load it so we clear it.)
currentBatchHolder.clear();
afterLastRow = true;
return false;
} else {
// Got next (or first) batch--reset record offset to beginning;
// assimilate schema if changed; set up return value for first call
// to next().
currentRecordNumber = 0;
if (qrb.getHeader().hasAffectedRowsCount()) {
int updateCount = qrb.getHeader().getAffectedRowsCount();
int currentUpdateCount = statement.getUpdateCount() == -1 ? 0 : statement.getUpdateCount();
((DrillStatement) statement).setUpdateCount(updateCount + currentUpdateCount);
((DrillStatement) statement).setResultSet(null);
}
final boolean schemaChanged;
try {
schemaChanged = currentBatchHolder.load(qrb.getHeader().getDef(), qrb.getData());
} finally {
qrb.release();
}
schema = currentBatchHolder.getSchema();
if (schemaChanged) {
updateColumns();
}
if (returnTrueForNextCallToNext && currentBatchHolder.getRecordCount() == 0) {
returnTrueForNextCallToNext = false;
}
return true;
}
} catch (UserException e) {
// error type is accessible, of course. :-()
throw new SQLException(e.getMessage(), e);
} catch (InterruptedException e) {
// but JDBC client certainly could.
throw new SQLException("Interrupted.", e);
} catch (RuntimeException e) {
throw new SQLException("Unexpected RuntimeException: " + e.toString(), e);
}
}
}
use of org.apache.drill.common.exceptions.UserException in project drill by apache.
the class TestProjectEmitOutcome method testProjectWithComplexWritersAndEmitOutcome_NonEmptyFirstBatch.
/**
* Test to show that in cases with functions in project list whose output is complex types, if Project sees an EMIT
* outcome then it fails. This scenario can happen when complex functions are used in subquery between LATERAL and
* UNNEST. In which case guidance is to use those functions in project list of outermost query.
* Below test passes first batch as non-empty with OK_NEW_SCHEMA during which complex writers are cached for
* projected columns and later when an empty batch arrives with EMIT outcome the exception is thrown.
* @throws Throwable
*/
@Test
public void testProjectWithComplexWritersAndEmitOutcome_NonEmptyFirstBatch() throws Throwable {
final RowSet.SingleRowSet nonEmptyInputRowSet2 = operatorFixture.rowSetBuilder(inputSchema).addRow(2, 20, "{ \"a\" : 1 }").build();
inputContainer.add(nonEmptyInputRowSet2.container());
inputContainer.add(emptyInputRowSet.container());
inputOutcomes.add(RecordBatch.IterOutcome.OK_NEW_SCHEMA);
inputOutcomes.add(RecordBatch.IterOutcome.EMIT);
final MockRecordBatch mockInputBatch = new MockRecordBatch(operatorFixture.getFragmentContext(), opContext, inputContainer, inputOutcomes, emptyInputRowSet.container().getSchema());
final Project projectConf = new Project(parseExprs("convert_fromJSON(name_left)", "name_columns"), null);
try (final ProjectRecordBatch projectBatch = new ProjectRecordBatch(projectConf, mockInputBatch, operatorFixture.getFragmentContext())) {
assertEquals(RecordBatch.IterOutcome.OK_NEW_SCHEMA, projectBatch.next());
// Fails
projectBatch.next();
fail();
} catch (UserException e) {
// exception is expected because of complex writers case
assertEquals(ErrorType.UNSUPPORTED_OPERATION, e.getErrorType());
}
}
use of org.apache.drill.common.exceptions.UserException in project drill by apache.
the class TestScanOperExecBasics method testExceptionOnSecondNext.
/**
* Test throwing an exception after the first batch, but while
* "reading" the second. Note that the first batch returns data
* and is spread over two next() calls, so the error is on the
* third call to the scan operator next().
*/
@Test
public void testExceptionOnSecondNext() {
MockEarlySchemaReader reader = new MockEarlySchemaReader() {
@Override
public boolean next() {
if (batchCount == 1) {
// Load some data
super.next();
throw new IllegalStateException(ERROR_MSG);
}
return super.next();
}
};
reader.batchLimit = 2;
ScanFixture scanFixture = simpleFixture(reader);
ScanOperatorExec scan = scanFixture.scanOp;
// Schema
assertTrue(scan.buildSchema());
// First batch
assertTrue(scan.next());
scan.batchAccessor().release();
try {
scan.next();
fail();
} catch (UserException e) {
assertTrue(e.getMessage().contains(ERROR_MSG));
assertTrue(e.getCause() instanceof IllegalStateException);
}
scanFixture.close();
assertTrue(reader.closeCalled);
}
use of org.apache.drill.common.exceptions.UserException in project drill by apache.
the class TestScanOperExecBasics method testUserExceptionOnFirstNext.
@Test
public void testUserExceptionOnFirstNext() {
MockEarlySchemaReader reader = new MockEarlySchemaReader() {
@Override
public boolean next() {
// Load some data
super.next();
throw UserException.dataReadError().addContext(ERROR_MSG).build(logger);
}
};
reader.batchLimit = 2;
ScanFixture scanFixture = simpleFixture(reader);
ScanOperatorExec scan = scanFixture.scanOp;
assertTrue(scan.buildSchema());
try {
scan.next();
fail();
} catch (UserException e) {
assertTrue(e.getMessage().contains(ERROR_MSG));
assertNull(e.getCause());
}
assertTrue(reader.openCalled);
assertEquals(0, scan.batchAccessor().rowCount());
scanFixture.close();
assertTrue(reader.closeCalled);
}
Aggregations