Apache Calcite - 自定义标量函数

news/2024/10/18 5:59:30/

前言

上一篇文章中我们介绍了calcite中内置函数的使用。实际需求中会遇到一些场景标准内置函数无法满足需求,这时候就需要用到自定义函数。在 Apache Calcite 中添加自定义函数,以便在 SQL 查询中使用自定义的逻辑。这对于执行特定的数据处理或分析任务非常有用。

相关依赖

通过前面的学习,我们知道表、函数等信息维护在schema中,因此我们要做的事情就是在schema中新增自定义的函数。

以下演示基于1.36版包实现。

    <dependency><groupId>org.apache.calcite</groupId><artifactId>calcite-core</artifactId><version>1.36.0</version></dependency>

为了新增函数我们需要重写AbstractSchema中的 getFunctionMultimap方法,方法定义如下:

  protected Multimap<String, Function> getFunctionMultimap() {return ImmutableMultimap.of();}

Function有多种实现,由于我们要扩展的是标量函数,所以关注ScalarFunction即可。
在这里插入图片描述
这里我们用到ScalarFunctionImpl.create方法来创建标量函数,方法中的参数1是所建函数所在的类,第二个是对应的方法名。

  /*** Creates {@link org.apache.calcite.schema.ScalarFunction} from given class.** <p>If a method of the given name is not found, or it does not suit,* returns {@code null}.** @param clazz class that is used to implement the function* @param methodName Method name (typically "eval")* @return created {@link ScalarFunction} or null*/public static @Nullable ScalarFunction create(Class<?> clazz, String methodName) {final Method method = findMethod(clazz, methodName);if (method == null) {return null;}return create(method);}

实现自定义标量函数

为了方便演示,我们实现一个无用的简单方法,对整数减1

public class CustomFunctions {public static Integer minus1(int num1) {return num1 - 1;}
}

接着扩展前文中用到的schema,实现getFunctionMultimap方法,并增加添加函数的方法

public class ListSchema extends AbstractSchema {Map<String, Table> tableMap = new HashMap<>();Multimap<String, Function> functionMap = LinkedListMultimap.create();public void addTable(String name, Table table) {tableMap.put(name, table);}public void addFunction(String name,Function function) { functionMap.put(name, function);}public ListSchema() {}@Overrideprotected Map<String, Table> getTableMap() {return tableMap;}@Overrideprotected Multimap<String, Function> getFunctionMultimap() {return functionMap;}
}

最后我们在schema中注册函数

ListSchema listSchema = new ListSchema();
listSchema.addFunction("minus1", ScalarFunctionImpl.create(CustomFunctions.class,"minus1"));

完成上述工作后,在sql中使用自定义的函数

ResultSet countResult = statement.executeQuery("select age,listSchema.minus1(age) from listSchema.MyTable");

打印结果,可以看到自定义方法的计算结果

95 94 
21 20 
47 46 

异常处理

在完成上述工作时也遇到了几个异常,分别如下:

No match found for function signature minus1(< NUMERIC>)

  • 详细的异常提示为:java.sql.SQLException: Error while executing SQL “select age,minus1(age) from listSchema.MyTable”: From line 1, column 12 to line 1, column 22: No match found for function signature minus1()
  • 异常原因
    1.注册的方法名与使用方法名不一致。最开始注册时,将方法名首字母写成了大写。但实际使用时却使用的小写名 Minus1 - > minus1 listSchema.addFunction(“Minus1”, ScalarFunctionImpl.create(CustomFunctions.class,“minus1”));
    2.另外使用方法名时没指定schema名,导致了错误
    上述两个问题修改完毕程序正常运行

总结

实现自定义标量函数,注册到schema最后使用,schema是核心,维护了各类元信息,并提供了扩展接口来实现自定义的能力


http://www.ppmy.cn/news/1466245.html

相关文章

动态库(DLL)和静态库(LIB)的区别

链接时间&#xff1a; 静态库&#xff08;LIB&#xff09;在编译链接时整合到程序中。动态库&#xff08;DLL&#xff09;在程序运行时动态加载。 内存共享&#xff1a; 静态库导致每个程序副本都包含库代码。动态库允许多个程序共享同一份代码。 更新维护&#xff1a; DLL更新…

Rejected the attempt to advance SCN问题的分析处理

一、故障描述 5月8日下午12点30分左右&#xff0c;应用厂家反馈&#xff0c;IP是130.XXXXX(jyfx)的数据库无法连接&#xff0c;检查数据库告警日志&#xff0c;提示内容如下&#xff1a; Rejected the attempt to advance SCN over limit by 124166 hours worth to 0x15cb.a9a2…

【AD21】原理图PDF文件的输出

原理图PDF文件可以共享给团队成员&#xff0c;用于设计审核、讨论和协同工作。 菜单栏中点击文件->智能PDF。 在弹出的界面点击Next&#xff0c;勾选当前项目&#xff0c;修改文件名&#xff0c;避免与制造装备图PDF文件重名将其覆盖&#xff0c;点击Next。 只输出原理图…

Layui2.5.6树形表格TreeTable使用

1、问题概述? Layui2.5.6的树形表格-TreeTable终于用明白了,步骤详细,提供源码下载。 如果你使用的是Layui2.8+版本,那么点个赞,赶紧去官网看吧,官网更行了。 更新地址:树表组件 treeTable - Layui 文档 最近在项目中需要使用到树形表格,用来显示菜单的层级关系,当…

深入解析Spring与MyBatis框架注解及其实例应用

在现代Java开发中&#xff0c;Spring与MyBatis框架已经成为了不可或缺的利器。它们提供了丰富的注解&#xff0c;用于简化开发流程、提高代码可读性和可维护性。让我们深入探讨这些注解&#xff0c;并结合实际场景进行详细分析。 1. Spring框架注解 1.1 组件注解 Component&…

Go 语言字符串及 strings 和 strconv 包

在 Go 语言编程中&#xff0c;字符串是最基本、最常用的数据类型之一。无论是处理用户输入、读取文件内容&#xff0c;还是生成输出&#xff0c;字符串操作无处不在。为了方便开发者对字符串进行各种操作&#xff0c;Go 语言提供了强大的 strings 包和 strconv 包。strings 包包…

JDBC入门基础

目录 JDBC的基本概念 快速入门&#xff08;基本步骤&#xff09; 创建数据库 注册驱动&#xff08;可以省略不写&#xff09; 获取连接对象 获取执行SQL语句的对象 编写SQL语句&#xff0c;并执行&#xff0c;以及接收返回的结果 处理结果&#xff0c;遍历结果集和 释放资源&…

java —— 匿名内部类与 Lambda 表达式

一、匿名内部类 匿名内部类是一种没有名称的类&#xff0c;多用于只使用一次的情况&#xff0c;本质上就是其所继承的父类或接口的一个子类。 &#xff08;一&#xff09;继承普通类的情况 public class Test{public void method(){System.out.println("通用方法"…