Search in sources :

Example 6 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class SizeUtility method getSize.

/**
 * Get size of object
 * @return Size in bytes
 */
public static long getSize(Object obj, boolean accountForValueCache) {
    if (obj == null) {
        return 0;
    }
    Class<? extends Object> clazz = obj.getClass();
    if (clazz == DataTypeManager.DefaultDataClasses.STRING) {
        int length = ((String) obj).length();
        if (length > 0) {
            return alignMemory(40 + (2 * length));
        }
        return 40;
    } else if (clazz == DataTypeManager.DefaultDataClasses.VARBINARY) {
        int length = ((BinaryType) obj).getLength();
        if (length > 0) {
            return alignMemory(16 + length);
        }
        return 16;
    } else if (clazz == DataTypeManager.DefaultDataClasses.BIG_DECIMAL) {
        int bitLength = ((BigDecimal) obj).unscaledValue().bitLength();
        // TODO: this does not account for the possibility of a cached string
        long result = 88 + alignMemory(4 + (bitLength >> 3));
        return result;
    } else if (clazz == DataTypeManager.DefaultDataClasses.BIG_INTEGER) {
        int bitLength = ((BigInteger) obj).bitLength();
        long result = 40 + alignMemory(4 + (bitLength >> 3));
        return result;
    } else if (obj instanceof Iterable<?>) {
        Iterable<?> i = (Iterable<?>) obj;
        long total = 16;
        for (Object object : i) {
            total += getSize(object, false) + REFERENCE_SIZE;
        }
        return total;
    } else if (clazz.isArray() || obj instanceof ArrayImpl) {
        int overhead = 0;
        if (obj instanceof ArrayImpl) {
            obj = ((ArrayImpl) obj).getValues();
            clazz = obj.getClass();
            overhead += 2 * REFERENCE_SIZE;
        }
        Class<?> componentType = clazz.getComponentType();
        if (!componentType.isPrimitive()) {
            Object[] rows = (Object[]) obj;
            // Array overhead
            long total = overhead + 16 + alignMemory(rows.length * REFERENCE_SIZE);
            for (int i = 0; i < rows.length; i++) {
                total += getSize(rows[i], false);
            }
            return total;
        }
        int length = Array.getLength(obj);
        int primitiveSize = 8;
        if (componentType == boolean.class) {
            primitiveSize = 4;
        } else if (componentType == byte.class) {
            primitiveSize = 1;
        } else if (componentType == short.class) {
            primitiveSize = 2;
        } else if (componentType == int.class || componentType == float.class) {
            primitiveSize = 4;
        }
        return overhead + alignMemory(length * primitiveSize) + 16;
    } else if (obj instanceof Streamable<?>) {
        try {
            Streamable<?> s = (Streamable) obj;
            Object o = s.getReference();
            if (o instanceof BaseLob) {
                InputStreamFactory isf = ((BaseLob) o).getStreamFactory();
                if (isf.getStorageMode() == StorageMode.MEMORY) {
                    long length = isf.getLength();
                    if (length >= 0) {
                        return 40 + alignMemory(length);
                    }
                } else if (isf.getStorageMode() == StorageMode.PERSISTENT) {
                    long length = isf.getLength();
                    return 40 + alignMemory(Math.min(DataTypeManager.MAX_LOB_MEMORY_BYTES, length));
                }
            }
        } catch (Exception e) {
        }
    } else {
        if (SIZE_ESTIMATES.containsKey(clazz)) {
            return getSize(accountForValueCache, clazz);
        }
        // assume we can get a plausable estimate from the serialized size
        if (obj instanceof Serializable) {
            // we're ignoring classloader differences here
            ClassStats stats = objectEstimates.get(clazz.getName());
            if (stats == null) {
                stats = new ClassStats();
                objectEstimates.put(clazz.getName(), stats);
            }
            int samples = stats.samples.getAndIncrement();
            if (samples < 1000 || (samples & 1023) == 1023) {
                try {
                    DummyOutputStream os = new DummyOutputStream();
                    ObjectOutputStream oos = new ObjectOutputStream(os) {

                        @Override
                        protected void writeClassDescriptor(ObjectStreamClass desc) throws IOException {
                        }

                        @Override
                        protected void writeStreamHeader() throws IOException {
                        }
                    };
                    oos.writeObject(obj);
                    oos.close();
                    int result = (int) alignMemory(os.getBytes() * 3);
                    if (result > stats.averageSize) {
                        stats.averageSize = (stats.averageSize + result * 2) / 3;
                    } else {
                        stats.averageSize = (stats.averageSize + result) / 2;
                    }
                    return result;
                } catch (Exception e) {
                }
            }
            return stats.averageSize;
        }
    }
    return getSize(accountForValueCache, clazz);
}
Also used : Serializable(java.io.Serializable) ArrayImpl(org.teiid.core.types.ArrayImpl) BaseLob(org.teiid.core.types.BaseLob) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ObjectOutputStream(java.io.ObjectOutputStream) IOException(java.io.IOException) ObjectStreamClass(java.io.ObjectStreamClass) Streamable(org.teiid.core.types.Streamable) ObjectStreamClass(java.io.ObjectStreamClass)

Example 7 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class LDAPQueryExecution method getRow.

/**
 * Create a row using the searchResult and add it to the supplied batch.
 * @param batch the supplied batch
 * @param result the search result
 * @throws InvalidNameException
 */
// GHH 20080326 - added fetching of DN of result, for directories that
// do not include it as an attribute
private List<?> getRow(SearchResult result) throws TranslatorException, InvalidNameException {
    Attributes attrs = result.getAttributes();
    ArrayList<Column> attributeList = searchDetails.getElementList();
    final List<Object> row = new ArrayList<Object>(attributeList.size());
    for (int i = 0; i < attributeList.size(); i++) {
        Column col = attributeList.get(i);
        // GHH 20080326 - added resultDN parameter to call
        Object val = getValue(col, result, attrs, i == unwrapPos);
        row.add(val);
    }
    if (unwrapPos > -1) {
        Object toUnwrap = row.get(unwrapPos);
        if (toUnwrap == null) {
            // missing value
            return row;
        }
        if (toUnwrap instanceof ArrayImpl) {
            final Object[] val = ((ArrayImpl) toUnwrap).getValues();
            if (val.length == 0) {
                // empty value
                row.set(unwrapPos, null);
            } else {
                unwrapIterator = new Iterator<List<Object>>() {

                    int i = 0;

                    @Override
                    public boolean hasNext() {
                        return i < val.length;
                    }

                    @Override
                    public List<Object> next() {
                        List<Object> newRow = new ArrayList<Object>(row);
                        newRow.set(unwrapPos, val[i++]);
                        return newRow;
                    }

                    @Override
                    public void remove() {
                    }
                };
                if (unwrapIterator.hasNext()) {
                    return unwrapIterator.next();
                }
            }
        }
    }
    return row;
}
Also used : ArrayImpl(org.teiid.core.types.ArrayImpl) Attributes(javax.naming.directory.Attributes) ArrayList(java.util.ArrayList) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) List(java.util.List)

Example 8 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class TestAggregateProcessing method testArrayAggOrderByPersistence.

@Test
public void testArrayAggOrderByPersistence() throws Exception {
    // Create query
    // $NON-NLS-1$
    String sql = "SELECT array_agg(e2 order by e1) from pm1.g1 group by e3";
    // Create expected results
    List[] expected = new List[] { Arrays.asList(new ArrayImpl(new Integer[] { 1, 0, 0, 2 })), Arrays.asList(new ArrayImpl(new Integer[] { 3, 1 })) };
    // Construct data manager with data
    FakeDataManager dataManager = new FakeDataManager();
    sampleData1(dataManager);
    // Plan query
    ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached());
    CommandContext cc = TestProcessor.createCommandContext();
    BufferManagerImpl impl = BufferManagerFactory.getTestBufferManager(0, 2);
    cc.setBufferManager(impl);
    // Run query
    helpProcess(plan, cc, dataManager, expected);
}
Also used : BigInteger(java.math.BigInteger) CommandContext(org.teiid.query.util.CommandContext) BufferManagerImpl(org.teiid.common.buffer.impl.BufferManagerImpl) ArrayImpl(org.teiid.core.types.ArrayImpl) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 9 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class TestArrayProcessing method testMultiDimensionalArrayRewrite.

@Test
public void testMultiDimensionalArrayRewrite() throws Exception {
    // $NON-NLS-1$
    String sql = "select (('a', 'b'),('c','d'))";
    QueryResolver.resolveCommand(helpParse(sql), RealMetadataFactory.example1Cached());
    Command command = helpResolve(sql, RealMetadataFactory.example1Cached());
    assertEquals(String[][].class, command.getProjectedSymbols().get(0).getType());
    command = QueryRewriter.rewrite(command, RealMetadataFactory.example1Cached(), null);
    Expression ex = SymbolMap.getExpression(command.getProjectedSymbols().get(0));
    Constant c = (Constant) ex;
    assertTrue(c.getValue() instanceof ArrayImpl);
}
Also used : Command(org.teiid.query.sql.lang.Command) Expression(org.teiid.query.sql.symbol.Expression) Constant(org.teiid.query.sql.symbol.Constant) ArrayImpl(org.teiid.core.types.ArrayImpl) Test(org.junit.Test)

Example 10 with ArrayImpl

use of org.teiid.core.types.ArrayImpl in project teiid by teiid.

the class TestArrayProcessing method testArrayProjection.

@Test
public void testArrayProjection() throws Exception {
    String sql = "SELECT e1, (e2, e3) FROM pm1.g1";
    BasicSourceCapabilities bsc = new BasicSourceCapabilities();
    bsc.setCapabilitySupport(Capability.QUERY_SELECT_EXPRESSION, true);
    bsc.setCapabilitySupport(Capability.ARRAY_TYPE, true);
    ProcessorPlan pp = TestProcessor.helpGetPlan(sql, RealMetadataFactory.example1Cached(), new DefaultCapabilitiesFinder(bsc));
    HardcodedDataManager dataManager = new HardcodedDataManager();
    dataManager.addData("SELECT pm1.g1.e1, pm1.g1.e2, pm1.g1.e3 FROM pm1.g1", Arrays.asList("a", 1, false));
    TestProcessor.helpProcess(pp, dataManager, new List[] { Arrays.asList("a", new ArrayImpl(1, false)) });
    bsc.setCapabilitySupport(Capability.QUERY_SELECT_EXPRESSION_ARRAY_TYPE, true);
    pp = TestProcessor.helpGetPlan(sql, RealMetadataFactory.example1Cached(), new DefaultCapabilitiesFinder(bsc));
    dataManager.addData("SELECT pm1.g1.e1, (pm1.g1.e2, pm1.g1.e3) FROM pm1.g1", Arrays.asList("a", new ArrayImpl(1, false)));
    TestProcessor.helpProcess(pp, dataManager, new List[] { Arrays.asList("a", new ArrayImpl(1, false)) });
}
Also used : BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) ArrayImpl(org.teiid.core.types.ArrayImpl) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Aggregations

ArrayImpl (org.teiid.core.types.ArrayImpl)31 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 List (java.util.List)7 SQLException (java.sql.SQLException)3 Array (org.teiid.query.sql.symbol.Array)3 Constant (org.teiid.query.sql.symbol.Constant)3 IOException (java.io.IOException)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Array (java.sql.Array)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 TransformationException (org.teiid.core.types.TransformationException)2 XMLType (org.teiid.core.types.XMLType)2 ProcedureParameter (org.teiid.metadata.ProcedureParameter)2 LanguageObject (org.teiid.query.sql.LanguageObject)2 Command (org.teiid.query.sql.lang.Command)2 Expression (org.teiid.query.sql.symbol.Expression)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1