apache calcite,不使用jdbc api查询

kos*_*ios 6 apache-calcite

我想在不使用 jdbc 连接的情况下使用 apache calcite api raw。我可以很好地使用 jdbc api,但是在尝试使用 api 时出现空 ptr 异常。到目前为止我所做的是:

package calcite.examples

import java.util.Properties

import calcite.demo.DemoSchema
import org.apache.calcite.DataContext
import org.apache.calcite.config.CalciteConnectionConfigImpl
import org.apache.calcite.jdbc.CalcitePrepare.Query
import org.apache.calcite.jdbc.{CalcitePrepare, CalciteSchema, JavaTypeFactoryImpl}
import org.apache.calcite.prepare.CalcitePrepareImpl

import scala.collection.JavaConverters._

object TryIt extends App
{
    val ctx = new AdapterContext
    val sql = Query.of[Any]("SELECT * FROM dep")
    //  assert(sql.rel != null)

    val elementType = classOf[Array[Object]]
    val prepared = new CalcitePrepareImpl().prepareSql(ctx, sql, elementType, -1)
    val enumerable = prepared.enumerable(new MyDataContext)
}

class AdapterContext extends CalcitePrepare.Context
{
    private val properties = new Properties
    private val rootSchema = CalciteSchema.createRootSchema(true)
    rootSchema.add("default", new DemoSchema)

    // default schema names
    override def getDefaultSchemaPath = List("default").asJava

    override def spark() = {
        val enable = config.spark
        CalcitePrepare.Dummy.getSparkHandler(enable)
    }

    override val config = new CalciteConnectionConfigImpl(properties)

    override val getTypeFactory = new JavaTypeFactoryImpl

    override def getRootSchema = rootSchema

    override def getDataContext = new MyDataContext
}

class MyDataContext extends DataContext
{
    override def get(name: String) = {
        println(s"MyDataContext name=$name")
        null
    }

    override def getTypeFactory = ???

    override def getQueryProvider = ???

    override def getRootSchema = ???
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行这个时,我得到

MyDataContext 名称=v0stashed

Exception in thread "main" java.lang.NullPointerException
at org.apache.calcite.interpreter.Interpreter.<init>(Interpreter.java:71)
at Baz.bind(Unknown Source)
at org.apache.calcite.jdbc.CalcitePrepare$CalciteSignature.enumerable(CalcitePrepare.java:327)
at calcite.examples.TryIt$.delayedEndpoint$calcite$examples$TryIt$1(TryIt.scala:26)
at calcite.examples.TryIt$delayedInit$body.apply(TryIt.scala:18)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at calcite.examples.TryIt$.main(TryIt.scala:18)
at calcite.examples.TryIt.main(TryIt.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Run Code Online (Sandbox Code Playgroud)

请注意,它试图从上下文中获取 v0stashed,我不知道它应该是什么。我看到的唯一 CalcitePrepare.Context impl 是使用 CalciteConnection 的,我试图避免使用它。我还从方解石文档中获取了代码片段,但它在方解石 1.7 中已经过时

想法?

小智 0

这应该几乎可以工作,我也有同样的事情,但使用JdbcSchema. 我的预感是您正在尝试使用 Spark 模式,并且仅根据 Calcite 中的版本号来判断,我认为它维护得不是很好。尝试一种更简单的模式,例如反射式 HR 模式或 JDBC 模式,它应该可以工作,只需对您所拥有的进行一些小的更改。