Search in sources :

Example 1 with CollectionTupleSource

use of org.teiid.query.processor.CollectionTupleSource in project teiid by teiid.

the class TestSTree method testSearch.

@Test
public void testSearch() throws TeiidComponentException, TeiidProcessingException {
    // due to buffering changes we need to hold this in memory directly rather than serialize it out as that will lead to GC overhead errors
    BufferManagerImpl bm = BufferManagerFactory.getTestBufferManager(Integer.MAX_VALUE, 1);
    ElementSymbol e1 = new ElementSymbol("x");
    e1.setType(Integer.class);
    ElementSymbol e2 = new ElementSymbol("x");
    e2.setType(Integer.class);
    List elements = Arrays.asList(e1, e2);
    STree map = bm.createSTree(elements, "1", 2);
    int size = 1 << 16;
    for (int i = 0; i < size; i++) {
        assertNull(map.insert(Arrays.asList(i, i), InsertMode.NEW, -1));
        assertEquals(i + 1, map.getRowCount());
    }
    map.compact();
    for (int i = 0; i < size; i++) {
        TupleBrowser tb = new TupleBrowser(map, new CollectionTupleSource(Collections.singletonList(Arrays.asList(i)).iterator()), true);
        assertNotNull(tb.nextTuple());
        assertNull(tb.nextTuple());
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) BufferManagerImpl(org.teiid.common.buffer.impl.BufferManagerImpl) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) List(java.util.List) Test(org.junit.Test)

Example 2 with CollectionTupleSource

use of org.teiid.query.processor.CollectionTupleSource in project teiid by teiid.

the class TestSTree method testSearchWithRepeated.

@Test
public void testSearchWithRepeated() throws TeiidComponentException, TeiidProcessingException {
    // due to buffering changes we need to hold this in memory directly rather than serialize it out as that will lead to GC overhead errors
    BufferManagerImpl bm = BufferManagerFactory.getTestBufferManager(Integer.MAX_VALUE, 1);
    ElementSymbol e1 = new ElementSymbol("x");
    e1.setType(Integer.class);
    ElementSymbol e2 = new ElementSymbol("x");
    e2.setType(Integer.class);
    List<ElementSymbol> elements = Arrays.asList(e1, e2);
    STree map = bm.createSTree(elements, "1", 2);
    int size = 1 << 16;
    for (int i = 0; i < size; i++) {
        assertNull(map.insert(Arrays.asList(i, i * 2), InsertMode.NEW, -1));
        assertNull(map.insert(Arrays.asList(i, i * 2 + 1), InsertMode.NEW, -1));
        assertEquals((i + 1) * 2, map.getRowCount());
    }
    map.compact();
    for (int i = 0; i < size; i++) {
        TupleBrowser tb = new TupleBrowser(map, new CollectionTupleSource(Collections.singletonList(Arrays.asList(i)).iterator()), true);
        for (int j = 0; j < 2; j++) {
            assertNotNull(tb.nextTuple());
        }
        assertNull(tb.nextTuple());
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) BufferManagerImpl(org.teiid.common.buffer.impl.BufferManagerImpl) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) Test(org.junit.Test)

Example 3 with CollectionTupleSource

use of org.teiid.query.processor.CollectionTupleSource in project teiid by teiid.

the class TestAsyncTupleSource method testTupleSource.

@Test
public void testTupleSource() throws TeiidComponentException, TeiidProcessingException {
    AsyncTupleSource ats = new AsyncTupleSource(new Callable<TupleSource>() {

        @Override
        public TupleSource call() throws Exception {
            return new CollectionTupleSource(Arrays.asList(Arrays.asList(1), Arrays.asList(2)).iterator());
        }
    }, new CommandContext());
    for (int i = 0; i < 20; i++) {
        try {
            assertEquals(Arrays.asList(1), ats.nextTuple());
            break;
        } catch (BlockedException e) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
            }
        }
    }
    assertEquals(Arrays.asList(2), ats.nextTuple());
    assertNull(ats.nextTuple());
}
Also used : CommandContext(org.teiid.query.util.CommandContext) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) BlockedException(org.teiid.common.buffer.BlockedException) BlockedException(org.teiid.common.buffer.BlockedException) TeiidComponentException(org.teiid.core.TeiidComponentException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) Test(org.junit.Test)

Example 4 with CollectionTupleSource

use of org.teiid.query.processor.CollectionTupleSource in project teiid by teiid.

the class DataTierManagerImpl method processSystemQuery.

/**
 * @param command
 * @param workItem
 * @return
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 */
private TupleSource processSystemQuery(CommandContext context, Command command, DQPWorkContext workContext) throws TeiidComponentException, TeiidProcessingException {
    String vdbName = workContext.getVdbName();
    String vdbVersion = workContext.getVdbVersion();
    final VDBMetaData vdb = workContext.getVDB();
    TransformationMetadata indexMetadata = vdb.getAttachment(TransformationMetadata.class);
    CompositeMetadataStore metadata = indexMetadata.getMetadataStore();
    if (command instanceof Query) {
        Query query = (Query) command;
        UnaryFromClause ufc = (UnaryFromClause) query.getFrom().getClauses().get(0);
        GroupSymbol group = ufc.getGroup();
        if (StringUtil.startsWithIgnoreCase(group.getNonCorrelationName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
            final SystemAdminTables sysTable = SystemAdminTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
            BaseExtractionTable<?> et = systemAdminTables.get(sysTable);
            return et.processQuery(query, vdb, indexMetadata, context);
        }
        final SystemTables sysTable = SystemTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
        BaseExtractionTable<?> et = systemTables.get(sysTable);
        return et.processQuery(query, vdb, indexMetadata, context);
    }
    Collection<List<?>> rows = new ArrayList<List<?>>();
    StoredProcedure proc = (StoredProcedure) command;
    if (StringUtil.startsWithIgnoreCase(proc.getProcedureCallableName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
        final SystemAdminProcs sysProc = SystemAdminProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
        switch(sysProc) {
            case LOGMSG:
            case ISLOGGABLE:
                String level = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                String logContext = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
                Object message = null;
                if (sysProc == SystemAdminProcs.LOGMSG) {
                    message = ((Constant) proc.getParameter(4).getExpression()).getValue();
                }
                int msgLevel = getLevel(level);
                boolean logged = false;
                if (LogManager.isMessageToBeRecorded(logContext, msgLevel)) {
                    if (message != null) {
                        LogManager.log(msgLevel, logContext, message);
                    }
                    logged = true;
                }
                if (proc.returnParameters()) {
                    rows.add(Arrays.asList(logged));
                }
                return new CollectionTupleSource(rows.iterator());
            case SETPROPERTY:
                try {
                    String uuid = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                    String key = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
                    Clob value = (Clob) ((Constant) proc.getParameter(4).getExpression()).getValue();
                    key = MetadataFactory.resolvePropertyKey(null, key);
                    String strVal = null;
                    String result = null;
                    if (value != null) {
                        if (value.length() > MAX_VALUE_LENGTH) {
                            throw new TeiidProcessingException(QueryPlugin.Event.TEIID30548, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30548, MAX_VALUE_LENGTH));
                        }
                        strVal = ObjectConverterUtil.convertToString(value.getCharacterStream());
                    }
                    final AbstractMetadataRecord target = getByUuid(metadata, uuid);
                    if (target == null) {
                        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30549, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30549, uuid));
                    }
                    AbstractMetadataRecord schema = target;
                    while (!(schema instanceof Schema) && schema.getParent() != null) {
                        schema = schema.getParent();
                    }
                    if (schema instanceof Schema && vdb.getImportedModels().contains(((Schema) schema).getName())) {
                        // $NON-NLS-1$
                        throw new TeiidProcessingException(QueryPlugin.Event.TEIID31098, QueryPlugin.Util.getString("ValidationVisitor.invalid_alter", uuid));
                    }
                    if (getMetadataRepository(target, vdb) != null) {
                        getMetadataRepository(target, vdb).setProperty(vdbName, vdbVersion, target, key, strVal);
                    }
                    result = DdlPlan.setProperty(vdb, target, key, strVal);
                    if (eventDistributor != null) {
                        eventDistributor.setProperty(vdbName, vdbVersion, uuid, key, strVal);
                    }
                    // materialization depends upon the property values
                    // $NON-NLS-1$
                    indexMetadata.addToMetadataCache(target, "transformation/matview", null);
                    if (proc.returnParameters()) {
                        if (result == null) {
                            rows.add(Arrays.asList((Clob) null));
                        } else {
                            rows.add(Arrays.asList(new ClobType(new ClobImpl(result))));
                        }
                    }
                    return new CollectionTupleSource(rows.iterator());
                } catch (SQLException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30550, e);
                } catch (IOException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30551, e);
                }
        }
        final Table table = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
        switch(sysProc) {
            case SETCOLUMNSTATS:
                final String columnName = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                Column c = null;
                for (Column col : table.getColumns()) {
                    if (col.getName().equalsIgnoreCase(columnName)) {
                        c = col;
                        break;
                    }
                }
                if (c == null) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30552, columnName + TransformationMetadata.NOT_EXISTS_MESSAGE);
                }
                Number distinctVals = (Number) ((Constant) proc.getParameter(3).getExpression()).getValue();
                Number nullVals = (Number) ((Constant) proc.getParameter(4).getExpression()).getValue();
                String max = (String) ((Constant) proc.getParameter(5).getExpression()).getValue();
                String min = (String) ((Constant) proc.getParameter(6).getExpression()).getValue();
                final ColumnStats columnStats = new ColumnStats();
                columnStats.setDistinctValues(distinctVals);
                columnStats.setNullValues(nullVals);
                columnStats.setMaximumValue(max);
                columnStats.setMinimumValue(min);
                if (getMetadataRepository(table, vdb) != null) {
                    getMetadataRepository(table, vdb).setColumnStats(vdbName, vdbVersion, c, columnStats);
                }
                DdlPlan.setColumnStats(vdb, c, columnStats);
                if (eventDistributor != null) {
                    eventDistributor.setColumnStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), columnName, columnStats);
                }
                break;
            case SETTABLESTATS:
                Constant val = (Constant) proc.getParameter(2).getExpression();
                final Number cardinality = (Number) val.getValue();
                TableStats tableStats = new TableStats();
                tableStats.setCardinality(cardinality);
                if (getMetadataRepository(table, vdb) != null) {
                    getMetadataRepository(table, vdb).setTableStats(vdbName, vdbVersion, table, tableStats);
                }
                DdlPlan.setTableStats(vdb, table, tableStats);
                if (eventDistributor != null) {
                    eventDistributor.setTableStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), tableStats);
                }
                break;
        }
        return new CollectionTupleSource(rows.iterator());
    }
    final SystemProcs sysTable = SystemProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
    switch(sysTable) {
        case GETXMLSCHEMAS:
            try {
                Object groupID = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
                List<SQLXMLImpl> schemas = indexMetadata.getXMLSchemas(groupID);
                for (SQLXMLImpl schema : schemas) {
                    rows.add(Arrays.asList(new XMLType(schema)));
                }
            } catch (QueryMetadataException e) {
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30553, e);
            }
            break;
        case ARRAYITERATE:
            Object array = ((Constant) proc.getParameter(1).getExpression()).getValue();
            if (array != null) {
                final Object[] vals;
                if (array instanceof Object[]) {
                    vals = (Object[]) array;
                } else {
                    ArrayImpl arrayImpl = (ArrayImpl) array;
                    vals = arrayImpl.getValues();
                }
                return new CollectionTupleSource(new Iterator<List<?>>() {

                    int index = 0;

                    @Override
                    public boolean hasNext() {
                        return index < vals.length;
                    }

                    @Override
                    public List<?> next() {
                        if (!hasNext()) {
                            throw new NoSuchElementException();
                        }
                        return Arrays.asList(vals[index++]);
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                });
            }
    }
    return new CollectionTupleSource(rows.iterator());
}
Also used : Query(org.teiid.query.sql.lang.Query) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) SQLException(java.sql.SQLException) Constant(org.teiid.query.sql.symbol.Constant) ArrayImpl(org.teiid.core.types.ArrayImpl) ArrayList(java.util.ArrayList) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ArrayList(java.util.ArrayList) List(java.util.List) ClobImpl(org.teiid.core.types.ClobImpl) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) IOException(java.io.IOException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) CompositeMetadataStore(org.teiid.query.metadata.CompositeMetadataStore) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Clob(java.sql.Clob) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with CollectionTupleSource

use of org.teiid.query.processor.CollectionTupleSource in project teiid by teiid.

the class TempTable method createTupleSource.

private TupleSource createTupleSource(final List<? extends Expression> projectedCols, final Criteria condition, OrderBy orderBy, IndexInfo ii, boolean agg) throws TeiidComponentException, TeiidProcessingException {
    TupleBrowser browser = ii.createTupleBrowser(bm.getOptions().getDefaultNullOrder(), true);
    TupleSource ts = new QueryTupleSource(browser, columnMap, agg ? getColumns() : projectedCols, condition);
    boolean usingQueryTupleSource = false;
    boolean success = false;
    TupleBuffer tb = null;
    try {
        if (ii.ordering == null && orderBy != null) {
            SortUtility sort = new SortUtility(ts, orderBy.getOrderByItems(), Mode.SORT, bm, sessionID, projectedCols);
            sort.setNonBlocking(true);
            tb = sort.sort();
        } else if (agg) {
            int count = 0;
            while (ts.nextTuple() != null) {
                count++;
            }
            success = true;
            return new CollectionTupleSource(Arrays.asList(Collections.nCopies(projectedCols.size(), count)).iterator());
        } else if (updatable) {
            tb = bm.createTupleBuffer(projectedCols, sessionID, TupleSourceType.PROCESSOR);
            List<?> next = null;
            while ((next = ts.nextTuple()) != null) {
                tb.addTuple(next);
            }
        } else {
            usingQueryTupleSource = true;
            success = true;
            return ts;
        }
        tb.close();
        success = true;
        return tb.createIndexedTupleSource(true);
    } finally {
        if (!success && tb != null) {
            tb.remove();
        }
        if (!usingQueryTupleSource) {
            // ensure the buffers get released
            ts.closeSource();
        }
    }
}
Also used : SortUtility(org.teiid.query.processor.relational.SortUtility) TupleBufferTupleSource(org.teiid.common.buffer.TupleBuffer.TupleBufferTupleSource) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) TupleBuffer(org.teiid.common.buffer.TupleBuffer) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleBrowser(org.teiid.common.buffer.TupleBrowser)

Aggregations

CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)10 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)6 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 TupleSource (org.teiid.common.buffer.TupleSource)3 BufferManagerImpl (org.teiid.common.buffer.impl.BufferManagerImpl)3 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)3 TupleBrowser (org.teiid.common.buffer.TupleBrowser)2 TeiidComponentException (org.teiid.core.TeiidComponentException)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 Constant (org.teiid.query.sql.symbol.Constant)2 Expression (org.teiid.query.sql.symbol.Expression)2 IOException (java.io.IOException)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 NoSuchElementException (java.util.NoSuchElementException)1 VDBMetaData (org.teiid.adminapi.impl.VDBMetaData)1