Search in sources :

Example 1 with ExceptionStream

use of org.apache.solr.client.solrj.io.stream.ExceptionStream in project lucene-solr by apache.

the class SQLHandler method handleRequestBody.

public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
    params = adjustParams(params);
    req.setParams(params);
    String sql = params.get("stmt");
    // Set defaults for parameters
    params.set("numWorkers", params.getInt("numWorkers", 1));
    params.set("workerCollection", params.get("workerCollection", defaultWorkerCollection));
    params.set("workerZkhost", params.get("workerZkhost", defaultZkhost));
    params.set("aggregationMode", params.get("aggregationMode", "facet"));
    TupleStream tupleStream = null;
    try {
        if (!isCloud) {
            throw new IllegalStateException(sqlNonCloudErrorMsg);
        }
        if (sql == null) {
            throw new Exception("stmt parameter cannot be null");
        }
        String url = CalciteSolrDriver.CONNECT_STRING_PREFIX;
        Properties properties = new Properties();
        // Add all query parameters
        Iterator<String> parameterNamesIterator = params.getParameterNamesIterator();
        while (parameterNamesIterator.hasNext()) {
            String param = parameterNamesIterator.next();
            properties.setProperty(param, params.get(param));
        }
        // Set these last to ensure that they are set properly
        properties.setProperty("lex", Lex.MYSQL.toString());
        properties.setProperty("zk", defaultZkhost);
        String driverClass = CalciteSolrDriver.class.getCanonicalName();
        // JDBC driver requires metadata from the SQLHandler. Default to false since this adds a new Metadata stream.
        boolean includeMetadata = params.getBool("includeMetadata", false);
        tupleStream = new SqlHandlerStream(url, sql, null, properties, driverClass, includeMetadata);
        tupleStream = new StreamHandler.TimerStream(new ExceptionStream(tupleStream));
        rsp.add("result-set", tupleStream);
    } catch (Exception e) {
        //Catch the SQL parsing and query transformation exceptions.
        if (tupleStream != null) {
            tupleStream.close();
        }
        SolrException.log(logger, e);
        rsp.add("result-set", new StreamHandler.DummyErrorStream(e));
    }
}
Also used : ExceptionStream(org.apache.solr.client.solrj.io.stream.ExceptionStream) Properties(java.util.Properties) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException) SQLException(java.sql.SQLException) IOException(java.io.IOException) TupleStream(org.apache.solr.client.solrj.io.stream.TupleStream)

Example 2 with ExceptionStream

use of org.apache.solr.client.solrj.io.stream.ExceptionStream in project lucene-solr by apache.

the class TestSQLHandler method testSQLException.

private void testSQLException() throws Exception {
    try {
        CloudJettyRunner jetty = this.cloudJettys.get(0);
        del("*:*");
        commit();
        indexDoc(sdoc("id", "1", "text", "XXXX XXXX", "str_s", "a", "field_i", "7"));
        indexDoc(sdoc("id", "2", "text", "XXXX XXXX", "str_s", "b", "field_i", "8"));
        indexDoc(sdoc("id", "3", "text", "XXXX XXXX", "str_s", "a", "field_i", "20"));
        indexDoc(sdoc("id", "4", "text", "XXXX XXXX", "str_s", "b", "field_i", "11"));
        indexDoc(sdoc("id", "5", "text", "XXXX XXXX", "str_s", "c", "field_i", "30"));
        indexDoc(sdoc("id", "6", "text", "XXXX XXXX", "str_s", "c", "field_i", "40"));
        indexDoc(sdoc("id", "7", "text", "XXXX XXXX", "str_s", "c", "field_i", "50"));
        indexDoc(sdoc("id", "8", "text", "XXXX XXXX", "str_s", "c", "field_i", "60"));
        commit();
        SolrParams sParams = mapParams(CommonParams.QT, "/sql", "aggregationMode", "map_reduce", "stmt", "select id, str_s from collection1 where text='XXXX' order by field_iff desc");
        SolrStream solrStream = new SolrStream(jetty.url, sParams);
        Tuple tuple = getTuple(new ExceptionStream(solrStream));
        assert (tuple.EOF);
        assert (tuple.EXCEPTION);
        assert (tuple.getException().contains("Column 'field_iff' not found in any table"));
        sParams = mapParams(CommonParams.QT, "/sql", "stmt", "select id, field_iff, str_s from collection1 where text='XXXX' order by field_iff desc");
        solrStream = new SolrStream(jetty.url, sParams);
        tuple = getTuple(new ExceptionStream(solrStream));
        assert (tuple.EOF);
        assert (tuple.EXCEPTION);
        assert (tuple.getException().contains("Column 'field_iff' not found in any table"));
        sParams = mapParams(CommonParams.QT, "/sql", "aggregationMode", "map_reduce", "stmt", "select str_s, count(*), sum(field_iff), min(field_i), max(field_i), cast(avg(1.0 * field_i) as float) from collection1 where text='XXXX' group by str_s having ((sum(field_iff) = 19) AND (min(field_i) = 8))");
        solrStream = new SolrStream(jetty.url, sParams);
        tuple = getTuple(new ExceptionStream(solrStream));
        assert (tuple.EOF);
        assert (tuple.EXCEPTION);
        assert (tuple.getException().contains("Column 'field_iff' not found in any table"));
        sParams = mapParams(CommonParams.QT, "/sql", "aggregationMode", "map_reduce", "stmt", "select str_s, count(*), blah(field_i), min(field_i), max(field_i), cast(avg(1.0 * field_i) as float) from collection1 where text='XXXX' group by str_s having ((sum(field_i) = 19) AND (min(field_i) = 8))");
        solrStream = new SolrStream(jetty.url, sParams);
        tuple = getTuple(new ExceptionStream(solrStream));
        assert (tuple.EOF);
        assert (tuple.EXCEPTION);
        assert (tuple.getException().contains("No match found for function signature blah"));
    } finally {
        delete();
    }
}
Also used : ExceptionStream(org.apache.solr.client.solrj.io.stream.ExceptionStream) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrStream(org.apache.solr.client.solrj.io.stream.SolrStream) Tuple(org.apache.solr.client.solrj.io.Tuple)

Aggregations

ExceptionStream (org.apache.solr.client.solrj.io.stream.ExceptionStream)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 Tuple (org.apache.solr.client.solrj.io.Tuple)1 SolrStream (org.apache.solr.client.solrj.io.stream.SolrStream)1 TupleStream (org.apache.solr.client.solrj.io.stream.TupleStream)1 SolrException (org.apache.solr.common.SolrException)1 SolrParams (org.apache.solr.common.params.SolrParams)1