Search in sources :

Example 11 with BinaryType

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

the class TestGeometry method testEwkb.

@Test(expected = ExpressionEvaluationException.class)
public void testEwkb() throws Exception {
    WKBWriter writer = new WKBWriter(3, true);
    GeometryFactory gf = new GeometryFactory();
    Point point = gf.createPoint(new Coordinate(0, 0, 0));
    point.setSRID(100);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    writer.write(point, new OutputStreamOutStream(baos));
    Expression ex1 = TestFunctionResolving.getExpression("ST_GeomFromBinary(X'" + new BinaryType(baos.toByteArray()) + "', 8307)");
    Evaluator.evaluate(ex1);
}
Also used : WKBWriter(com.vividsolutions.jts.io.WKBWriter) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) BinaryType(org.teiid.core.types.BinaryType) Coordinate(com.vividsolutions.jts.geom.Coordinate) Expression(org.teiid.query.sql.symbol.Expression) Point(com.vividsolutions.jts.geom.Point) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStreamOutStream(com.vividsolutions.jts.io.OutputStreamOutStream) Test(org.junit.Test)

Example 12 with BinaryType

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

the class TestAggregateProcessing method testStringAggBinary.

@Test
public void testStringAggBinary() throws Exception {
    // Create query
    // $NON-NLS-1$
    String sql = "SELECT cast(string_agg(to_bytes(e1, 'UTF-8'), X'AB') as varbinary) from pm1.g1 group by e3";
    // Create expected results
    List[] expected = new List[] { Arrays.asList(new BinaryType(new byte[] { (byte) 0x61, (byte) 0xAB, (byte) 0x62, (byte) 0xAB, (byte) 0x61 })), Arrays.asList(new BinaryType(new byte[] { (byte) 0x61, (byte) 0xAB, (byte) 0x63 })) };
    FakeDataManager dataManager = new FakeDataManager();
    sampleData1(dataManager);
    ProcessorPlan plan = helpGetPlan(sql, RealMetadataFactory.example1Cached(), TestAggregatePushdown.getAggregatesFinder());
    helpProcess(plan, dataManager, expected);
}
Also used : BinaryType(org.teiid.core.types.BinaryType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 13 with BinaryType

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

the class DataTypeTransformer method transform.

static final <T> T transform(Object value, Class<T> targetType, Class<?> runtimeType) throws SQLException {
    if (value == null || targetType.isAssignableFrom(value.getClass())) {
        return targetType.cast(value);
    }
    if (targetType == byte[].class) {
        if (value instanceof Blob) {
            Blob blob = (Blob) value;
            long length = blob.length();
            if (length > Integer.MAX_VALUE) {
                // $NON-NLS-1$
                throw new TeiidSQLException(JDBCPlugin.Util.getString("DataTypeTransformer.blob_too_big"));
            }
            return targetType.cast(blob.getBytes(1, (int) length));
        } else if (value instanceof String) {
            return targetType.cast(((String) value).getBytes());
        } else if (value instanceof BinaryType) {
            return targetType.cast(((BinaryType) value).getBytesDirect());
        }
    } else if (targetType == String.class) {
        if (value instanceof SQLXML) {
            return targetType.cast(((SQLXML) value).getString());
        } else if (value instanceof Clob) {
            Clob c = (Clob) value;
            long length = c.length();
            if (length == 0) {
                // $NON-NLS-1$
                return targetType.cast("");
            }
            return targetType.cast(c.getSubString(1, length > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) length));
        }
    }
    try {
        return (T) DataTypeManager.transformValue(DataTypeManager.convertToRuntimeType(value, true), runtimeType);
    } catch (Exception e) {
        String valueStr = value.toString();
        if (valueStr.length() > 20) {
            // $NON-NLS-1$
            valueStr = valueStr.substring(0, 20) + "...";
        }
        // $NON-NLS-1$
        String msg = JDBCPlugin.Util.getString("DataTypeTransformer.Err_converting", valueStr, targetType.getSimpleName());
        throw TeiidSQLException.create(e, msg);
    }
}
Also used : Blob(java.sql.Blob) SQLXML(java.sql.SQLXML) BinaryType(org.teiid.core.types.BinaryType) NClob(java.sql.NClob) Clob(java.sql.Clob) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException)

Example 14 with BinaryType

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

the class XQueryEvaluator method evaluateXQuery.

public static SaxonXQueryExpression.Result evaluateXQuery(final SaxonXQueryExpression xquery, Object context, Map<String, Object> parameterValues, final RowProcessor processor, CommandContext commandContext) throws TeiidProcessingException, TeiidComponentException {
    DynamicQueryContext dynamicContext = new DynamicQueryContext(xquery.config);
    SaxonXQueryExpression.Result result = new SaxonXQueryExpression.Result();
    try {
        for (Map.Entry<String, Object> entry : parameterValues.entrySet()) {
            try {
                Object value = entry.getValue();
                Sequence s = null;
                if (value instanceof SQLXML) {
                    value = XMLSystemFunctions.convertToSource(value);
                    result.sources.add((Source) value);
                    Source source = wrapStax((Source) value);
                    s = xquery.config.buildDocumentTree(source).getRootNode();
                } else if (value instanceof java.util.Date) {
                    s = XQueryEvaluator.convertToAtomicValue(value);
                } else if (value instanceof BinaryType) {
                    s = new HexBinaryValue(((BinaryType) value).getBytesDirect());
                }
                dynamicContext.setParameter(StructuredQName.fromClarkName(entry.getKey()), s);
            } catch (TransformerException e) {
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30148, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30148, entry.getKey()));
            }
        }
        if (context != null) {
            Source source = XMLSystemFunctions.convertToSource(context);
            result.sources.add(source);
            source = wrapStax(source);
            if (xquery.contextRoot != null) {
                // create our own filter as this logic is not provided in the free saxon
                AugmentedSource sourceInput = AugmentedSource.makeAugmentedSource(source);
                sourceInput.addFilter(new FilterFactory() {

                    @Override
                    public ProxyReceiver makeFilter(Receiver arg0) {
                        return new PathMapFilter(xquery.contextRoot, arg0);
                    }
                });
                source = sourceInput;
                // use streamable processing instead
                if (xquery.streamingPath != null && processor != null) {
                    if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP, MessageLevel.DETAIL)) {
                        // $NON-NLS-1$
                        LogManager.logDetail(LogConstants.CTX_DQP, "Using stream processing for evaluation of", xquery.xQueryString);
                    }
                    // set to non-blocking in case default expression evaluation blocks
                    boolean isNonBlocking = commandContext.isNonBlocking();
                    commandContext.setNonBlocking(true);
                    final StreamingTransform myTransform = new StreamingTransform() {

                        public Nodes transform(Element elem) {
                            processor.processRow(XQueryEvaluator.wrap(elem, xquery.config));
                            return NONE;
                        }
                    };
                    Builder builder = new Builder(new SaxonReader(xquery.config, sourceInput), false, new StreamingPathFilter(xquery.streamingPath, xquery.namespaceMap).createNodeFactory(null, myTransform));
                    try {
                        // the builder is hard wired to parse the source, but the api will throw an exception if the stream is null
                        builder.build(FAKE_IS);
                        return result;
                    } catch (ParsingException e) {
                        if (e.getCause() instanceof TeiidRuntimeException) {
                            RelationalNode.unwrapException((TeiidRuntimeException) e.getCause());
                        }
                        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
                    } catch (IOException e) {
                        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
                    } finally {
                        if (!isNonBlocking) {
                            commandContext.setNonBlocking(false);
                        }
                    }
                }
            }
            TreeInfo doc;
            try {
                doc = xquery.config.buildDocumentTree(source);
            } catch (XPathException e) {
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30151, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30151));
            }
            dynamicContext.setContextItem(doc.getRootNode());
        }
        try {
            result.iter = xquery.xQuery.iterator(dynamicContext);
            return result;
        } catch (TransformerException e) {
            throw new TeiidProcessingException(QueryPlugin.Event.TEIID30152, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30152));
        }
    } finally {
        if (result.iter == null) {
            result.close();
        }
    }
}
Also used : XPathException(net.sf.saxon.trans.XPathException) Element(nu.xom.Element) Builder(nu.xom.Builder) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) AugmentedSource(net.sf.saxon.lib.AugmentedSource) FilterFactory(net.sf.saxon.event.FilterFactory) StreamingPathFilter(nux.xom.xquery.StreamingPathFilter) StreamResult(javax.xml.transform.stream.StreamResult) Result(org.teiid.query.xquery.saxon.SaxonXQueryExpression.Result) QueryResult(net.sf.saxon.query.QueryResult) TeiidProcessingException(org.teiid.core.TeiidProcessingException) DynamicQueryContext(net.sf.saxon.query.DynamicQueryContext) ParsingException(nu.xom.ParsingException) TransformerException(javax.xml.transform.TransformerException) BinaryType(org.teiid.core.types.BinaryType) HexBinaryValue(net.sf.saxon.value.HexBinaryValue) Receiver(net.sf.saxon.event.Receiver) ProxyReceiver(net.sf.saxon.event.ProxyReceiver) Sequence(net.sf.saxon.om.Sequence) IOException(java.io.IOException) SQLXML(java.sql.SQLXML) Result(org.teiid.query.xquery.saxon.SaxonXQueryExpression.Result) StreamingTransform(nux.xom.xquery.StreamingTransform) TreeInfo(net.sf.saxon.om.TreeInfo) Map(java.util.Map) AugmentedSource(net.sf.saxon.lib.AugmentedSource) ProxyReceiver(net.sf.saxon.event.ProxyReceiver)

Example 15 with BinaryType

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

the class TestSizeUtility method testGetSizeByteArray.

@Test
public void testGetSizeByteArray() {
    byte[] bytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    helpTestGetSize(new BinaryType(bytes), 32);
}
Also used : BinaryType(org.teiid.core.types.BinaryType) Test(org.junit.Test)

Aggregations

BinaryType (org.teiid.core.types.BinaryType)21 Test (org.junit.Test)12 Blob (java.sql.Blob)6 List (java.util.List)6 IOException (java.io.IOException)4 BigInteger (java.math.BigInteger)4 SQLException (java.sql.SQLException)4 SQLXML (java.sql.SQLXML)4 InputStream (java.io.InputStream)3 BigDecimal (java.math.BigDecimal)3 Clob (java.sql.Clob)3 ArrayList (java.util.ArrayList)3 BlobImpl (org.teiid.core.types.BlobImpl)3 GeometryType (org.teiid.core.types.GeometryType)3 BasicDBList (com.mongodb.BasicDBList)2 BasicDBObject (com.mongodb.BasicDBObject)2 GridFS (com.mongodb.gridfs.GridFS)2 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)2 Point (com.vividsolutions.jts.geom.Point)2