将具有聚合函数的SQL查询转换为Apache Calcite中的关系代数表达式-未找到函数签名的匹配项

时间:2019-03-27 00:13:37

标签: sql relational-algebra apache-calcite sql-parser

我正在尝试使用Apache Calcite SqlToRelConverter将SQL查询转换为关系代数表达式。

此查询工作正常(引号用于确保小写):

queryToRelationalAlgebraRoot("SELECT \"country\" FROM \"mytable\"")

但是此查询失败:

queryToRelationalAlgebraRoot("SELECT \"country\", SUM(\"salary\") FROM \"mytable\" GROUP BY \"country\"")

出现此错误:

org.apache.calcite.sql.validate.SqlValidatorException: No match found for function signature SUM(<NUMERIC>)

似乎SQL验证程序不具备注册总和或计数之类的聚合功能。

case class Income(id: Int, salary: Double, country: String)

class SparkDataFrameTable(df: DataFrame) extends AbstractTable {

  def getRowType(typeFactory: RelDataTypeFactory): RelDataType = {
    val typeList = df.schema.fields.map {
      field => field.dataType match {
        case t: StringType => typeFactory.createSqlType(SqlTypeName.VARCHAR)
        case t: IntegerType => typeFactory.createSqlType(SqlTypeName.INTEGER)
        case t: DoubleType => typeFactory.createSqlType(SqlTypeName.DOUBLE)
      }
    }.toList.asJava
    val fieldNameList = df.schema.fieldNames.toList.asJava
    typeFactory.createStructType(typeList, fieldNameList)
  }

}

object RelationalAlgebra {

  def queryToRelationalAlgebraRoot(query: String): RelRoot = {
    val sqlParser = SqlParser.create(query)
    val sqlParseTree = sqlParser.parseQuery()

    val frameworkConfig = Frameworks.newConfigBuilder().build()
    val planner = new PlannerImpl(frameworkConfig)

    val rootSchema = CalciteSchema.createRootSchema(true, true)

    // some sample data for testing
    val inc1 = new Income(1, 100000, "USA")
    val inc2 = new Income(2, 110000, "USA")
    val inc3 = new Income(3, 80000, "Canada")
    val spark = SparkSession.builder().master("local").getOrCreate()
    import spark.implicits._
    val df = Seq(inc1, inc2, inc3).toDF()
    rootSchema.add("mytable", new SparkDataFrameTable(df))

    val defaultSchema = List[String]().asJava
    val calciteConnectionConfigProperties = new Properties()
    val calciteConnectionConfigImpl = new CalciteConnectionConfigImpl(calciteConnectionConfigProperties)
    val sqlTypeFactoryImpl = new SqlTypeFactoryImpl(RelDataTypeSystem.DEFAULT)
    val calciteCatelogReader = new CalciteCatalogReader(rootSchema, defaultSchema, sqlTypeFactoryImpl, calciteConnectionConfigImpl)
    val defaultValidator = SqlValidatorUtil.newValidator(new SqlStdOperatorTable(), calciteCatelogReader, sqlTypeFactoryImpl, SqlConformanceEnum.LENIENT)

    val relExpressionOptimizationCluster = RelOptCluster.create(new VolcanoPlanner(), new RexBuilder(sqlTypeFactoryImpl))

    val sqlToRelConfig = SqlToRelConverter.configBuilder().build()

    val sqlToRelConverter = new SqlToRelConverter(planner, defaultValidator, calciteCatelogReader, relExpressionOptimizationCluster, StandardConvertletTable.INSTANCE, sqlToRelConfig)

    sqlToRelConverter.convertQuery(sqlParseTree, true, true)
  }

}

2 个答案:

答案 0 :(得分:1)

该代码的问题是new SqlStdOperatorTable()创建了一个未初始化的验证器。使用SqlStdOperatorTable的正确方法是使用SqlStdOperatorTable.instance()

在通过电子邮件发送dev@calcite.apache.org邮件列表之后,我找到了解决方案。非常感谢Chen Yuzhao Chen调查我遇到的问题并指出我的代码存在的问题。

答案 1 :(得分:0)

我不熟悉api,但是您的SQL需要按国家/地区分组。而且,如果要使用此工具来输出并使用它,则可能还需要使用别名来命名该列。