I have made a customed JSONSchema on github : 
https://github.com/guxiaobo/calcite-json-adapter,
I am still debug it, a simple example failed regarding getting the datatype of 
tables:

Map<String, List<JSONObject>> map = new HashMap<String, List<JSONObject>>();
                List<JSONObject> t1 = new ArrayList<JSONObject>();
                JSONObject r1 = new JSONObject();
                t1.add(r1);
                map.put("t1", t1);
                
                r1.put("c1", Long.valueOf(100));
                r1.put("c2", "column2");
                r1.put("c3", Boolean.FALSE);
                r1.put("c4", new BigDecimal("2.1"));
                r1.put("c5", new java.sql.Date(2022, 2, 22));
                r1.put("c6", new java.sql.Time(System.currentTimeMillis()));
                r1.put("c7", new 
java.sql.Timestamp(System.currentTimeMillis()));
                
                String sql1 = "select count(*) from t1";
                String sql2 = "select count(*) from js.t1";
                Schema schema = new JsonSchema(map);

                try {
                        CalciteDatabase db = new CalciteDatabase(schema, "js");
                        Long l1 = db.exeGetLong(sql1);
                        Long l2 = db.exeGetLong(sql2);
                        
                        System.out.println("sql result1 " + l1);
                        System.out.println("sql result2 " + l2);

                } catch (SQLException | ValidationException | SqlParseException 
| RelConversionException e) {
                        e.printStackTrace();
                }




java.sql.SQLException: exception while executing query: null
        at org.apache.calcite.avatica.Helper.createException(Helper.java:56)
        at org.apache.calcite.avatica.Helper.createException(Helper.java:41)
        at 
org.apache.calcite.avatica.AvaticaConnection.executeQueryInternal(AvaticaConnection.java:576)
        at 
org.apache.calcite.avatica.AvaticaPreparedStatement.executeQuery(AvaticaPreparedStatement.java:137)
        at 
com.xsmartware.common.calcite.CalciteDatabase.executeQuery(CalciteDatabase.java:78)
        at 
com.xsmartware.common.calcite.CalciteDatabase.executeQuery(CalciteDatabase.java:72)
        at 
com.xsmartware.common.calcite.CalciteDatabase.exeGetLong(CalciteDatabase.java:95)
        at 
com.xsmartware.javatest.calcite.CalCiteTest.test9(CalCiteTest.java:174)
        at com.xsmartware.javatest.calcite.CalCiteTest.run(CalCiteTest.java:114)
        at 
org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:758)
        at 
org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:748)
        at 
org.springframework.boot.SpringApplication.run(SpringApplication.java:309)
        at 
org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
        at 
org.springframework.boot.SpringApplication.run(SpringApplication.java:1290)
        at 
com.xsmartware.javatest.JavaTestApplication.main(JavaTestApplication.java:9)
Caused by: java.lang.NullPointerException
        at 
org.apache.calcite.adapter.json.JsonEnumerator.<init>(JsonEnumerator.java:49)
        at 
org.apache.calcite.adapter.json.JsonScannableTable$1.enumerator(JsonScannableTable.java:48)
        at 
org.apache.calcite.linq4j.EnumerableDefaults.aggregate(EnumerableDefaults.java:130)
        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:363)
        at 
org.apache.calcite.jdbc.CalciteConnectionImpl.enumerable(CalciteConnectionImpl.java:338)
        at 
org.apache.calcite.jdbc.CalciteMetaImpl._createIterable(CalciteMetaImpl.java:578)
        at 
org.apache.calcite.jdbc.CalciteMetaImpl.createIterable(CalciteMetaImpl.java:569)
        at 
org.apache.calcite.avatica.AvaticaResultSet.execute(AvaticaResultSet.java:184)
        at 
org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:64)
        at 
org.apache.calcite.jdbc.CalciteResultSet.execute(CalciteResultSet.java:43)
        at 
org.apache.calcite.avatica.AvaticaConnection.executeQueryInternal(AvaticaConnection.java:572)
        ... 12 more







------------------ Original ------------------
From:  "Gavin Ray";<ray.gavi...@gmail.com>;
Send time: Monday, Feb 21, 2022 1:51 AM
To: "dev"<dev@calcite.apache.org>; 

Subject:  Re: dynamic reflective schema



Ah, you don't want to use ReflectiveSchema, it's a simple schema type meant
for easily making test schemas

You want to extend from "AbstractSchema" and override the function
"Map<String, Table> getTableMap()"
For the "Table" type, you probably want to use "JsonScannableTable"

The CsvSchema example does exactly this, if you want to see an example
implementation:
https://github.com/apache/calcite/blob/4bc916619fd286b2c0cc4d5c653c96a68801d74e/example/csv/src/main/java/org/apache/calcite/adapter/csv/CsvSchema.java#L69-L106

Hope this helps =)



On Sat, Feb 19, 2022 at 11:03 PM xiaobo <guxiaobo1...@qq.com.invalid> wrote:

> Hi,
> When using  reflectiveSchema we must define a static Java class for the
> schema object, but in our use case the actual data to query is dynamic,
> define JAVA class for each data combination is impossible, we have an idea
> that can we make a JSONSchema which accepts a Map<String,
> List<JSONObject>>, so that each List will be mapped to a table , and each
> JSONObject mapped to a row, JSONObject keys will be column names. Is there
> anything similar with this?
>
>
> Regards

Reply via email to