Hi Calcite Devs,

I am able to query(basic query = "select * from tableFromJsonArr") JSON
array as a table,
But RelDataType is fixed in this scenario, but I am looking for dynamic
RelDataType, which works for all kinds of JsonArray (JSON Array of
books/employee or anything etc).

I will post if I get a solution, so that, any other developers also
benefit from that.

I updated my ScannableTable structure and it worked.

public class ListTable extends AbstractTable implements ScannableTable {
  private final RelDataType rowType;
  private final List<List<JsonElement>> data;
  ListTable(RelDataType rowType, List<List<JsonElement>> data) {
    this.rowType = rowType;
    this.data = data;
  }

  @Override
  public Enumerable scan(final DataContext root) {
    return Linq4j.asEnumerable(data);
  }
  @Override public RelDataType getRowType(final RelDataTypeFactory
typeFactory) {
    return rowType;
  }
}


Regards,
Kartik

On Mon, Oct 3, 2022 at 2:33 PM Kartik Kudada <[email protected]>
wrote:

> Hi Calcite Community Developers,
>
> I have JSON array, which I  like to use as a table for query like "select
> * from jsonTable".
> I followed as given tutorials (youtube
> <https://www.youtube.com/watch?v=p1O3E33FIs8>) but got a
> ClassCastException [ com.google.gson.JsonObject cannot be cast to
> [Ljava.lang.Object]
> I checked implementations of org.apache.calcite.schema.ScannableTable
> which can support for JsonArray. but didn't find any.
>
> Any help would be appreciated.
>
> // My code snippet .
>
> JsonAray jsonArray = // JsonArray consists of employee details
>
> RelDataTypeFactory typeFactory = new JavaTypeFactoryImpl();
>
> RelDataType tableType = typeFactory.builder()
>     .add("name", SqlTypeName.VARCHAR)
>     .add("dob",SqlTypeName.TIMESTAMP)
>     .add("age",SqlTypeName.INTEGER)
>     .build();
> ListTable jsonTable = new ListTable(tableType,jsonArray);
> getConnection().getRootSchema().add("jsonTable",jsonTable);
>
>
> ListTable class :
>
> public class ListTable extends AbstractTable implements ScannableTable {
>   private final RelDataType rowType;
>   private final JsonArray data;
>
> @Override
>
> public Enumerable scan(final DataContext root) { return 
> Linq4j.asEnumerable(data);}
> @Override
>
> public RelDataType getRowType(final RelDataTypeFactory typeFactory) { return 
> rowType;}
>
> }
>
>
> Reference for tutorials : youtube 
> <https://www.youtube.com/watch?v=p1O3E33FIs8> and github 
> <https://github.com/zabetak/calcite/blob/demo-january-2021/core/src/test/java/org/apache/calcite/examples/foodmart/java/EndToEndExampleBindable.java>
>
>
> Regards,
>
> Kartik
>

Reply via email to