Search in sources :

Example 61 with UserException

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);
        }
    }
}
Also used : UserException(org.apache.drill.common.exceptions.UserException) QueryEvent(org.apache.drill.test.BufferingQueryEventListener.QueryEvent)

Example 62 with UserException

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);
        }
    }
}
Also used : QueryDataBatch(org.apache.drill.exec.rpc.user.QueryDataBatch) SQLException(java.sql.SQLException) DrillStatement(org.apache.drill.jdbc.DrillStatement) UserException(org.apache.drill.common.exceptions.UserException)

Example 63 with UserException

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());
    }
}
Also used : Project(org.apache.drill.exec.physical.config.Project) RowSet(org.apache.drill.exec.physical.rowSet.RowSet) MockRecordBatch(org.apache.drill.exec.physical.impl.MockRecordBatch) UserException(org.apache.drill.common.exceptions.UserException) OperatorTest(org.apache.drill.categories.OperatorTest) Test(org.junit.Test)

Example 64 with UserException

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);
}
Also used : ScanFixture(org.apache.drill.exec.physical.impl.scan.ScanTestUtils.ScanFixture) UserException(org.apache.drill.common.exceptions.UserException) Test(org.junit.Test)

Example 65 with UserException

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);
}
Also used : ScanFixture(org.apache.drill.exec.physical.impl.scan.ScanTestUtils.ScanFixture) UserException(org.apache.drill.common.exceptions.UserException) Test(org.junit.Test)

Aggregations

UserException (org.apache.drill.common.exceptions.UserException)102 Test (org.junit.Test)76 EvfTest (org.apache.drill.categories.EvfTest)39 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)30 SubOperatorTest (org.apache.drill.test.SubOperatorTest)30 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)28 RowBatchReader (org.apache.drill.exec.physical.impl.scan.RowBatchReader)12 ManagedReader (org.apache.drill.exec.physical.impl.scan.v3.ManagedReader)12 ScanLifecycleBuilder (org.apache.drill.exec.physical.impl.scan.v3.ScanLifecycleBuilder)11 SchemaNegotiator (org.apache.drill.exec.physical.impl.scan.v3.SchemaNegotiator)11 ScanOperatorExec (org.apache.drill.exec.physical.impl.scan.ScanOperatorExec)9 ScanFixture (org.apache.drill.exec.physical.impl.scan.ScanTestUtils.ScanFixture)9 SchemaPath (org.apache.drill.common.expression.SchemaPath)8 ResultSetOptions (org.apache.drill.exec.physical.resultSet.impl.ResultSetLoaderImpl.ResultSetOptions)8 RowSet (org.apache.drill.exec.physical.rowSet.RowSet)8 MockRecordBatch (org.apache.drill.exec.physical.impl.MockRecordBatch)6 ArrayList (java.util.ArrayList)5 OperatorTest (org.apache.drill.categories.OperatorTest)5 DrillException (org.apache.drill.common.exceptions.DrillException)5 BaseTest (org.apache.drill.test.BaseTest)5