1

I'm new to Calcite. The functionality it provides look fabulous!
While doing a research, I'm trying to figure out how to do some basic SQL queries with example ElasticSearch adapter.
In the AbstractElasticsearchTable.getRowType, it maps rows to a MAP.

The issue is:

  1. Query:

    select * from zips where \"city\" = 'BROOKLYN'
    

    returns:

    city=BROOKLYN; longitude=-73.956985; latitude=40.646694; pop=111396; state=NY; id=11226
    
  2. Query:

    select \"pop\" from zips where \"city\" = 'BROOKLYN'
    

    returns:

    pop={pop=111396}
    

My goal is to sum up all the 'pop' values.
So when I construct query like this:

select sum(\"pop\") from zips where \"city\" = 'BROOKLYN'

The error is:

   Caused by: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to java.lang.Integer
   at Baz$2.apply(Unknown Source)
   at Baz$2.apply(Unknown Source)
   at org.apache.calcite.linq4j.EnumerableDefaults.aggregate(EnumerableDefaults.java:117)
   at org.apache.calcite.linq4j.DefaultEnumerable.aggregate(DefaultEnumerable.java:107)
   at Baz.bind(Unknown Source)
   at org.apache.calcite.jdbc.CalcitePrepare$CalciteSignature.enumerable(CalcitePrepare.java:356)

Can somebody point me to the right direction to figure out how to do aggregations with such mapping like in example?

To execute this query I added a test into ElasticSearchAdapterTest.java.

@Test
public void select() {
  CalciteAssert.that().with(newConnectionFactory())
    .query("select sum(\"pop\") from zips where \"city\" = 'BROOKLYN'").returns("");
}
zx485
  • 28,498
  • 28
  • 50
  • 59

1 Answers1

0

Implementation of RowType as StructType resolved my issue. Here it is:

  public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    try {
      Map<String, String> mapping = getMapping();
      List<RelDataType> types = new ArrayList<RelDataType>();
      List<String> names = new ArrayList<>();
      for(Map.Entry<String, String> e : mapping.entrySet()) {
        names.add(e.getKey());
        types.add(translateEsType(e.getValue(), typeFactory));
      }
      return typeFactory.createStructType(types, names);
    } catch(IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    }

  }
  • There is another solution to the issue described above: remove "cast" from a view create script. E.g.: remove cast from " cast(_MAP['pop'] AS integer) AS \"pop\", " – Ilya Anisimov Oct 30 '18 at 00:26