第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Apache Calcite - 注冊 UDF 以在 RelBuilder 中使用

Apache Calcite - 注冊 UDF 以在 RelBuilder 中使用

回首憶惘然 2023-09-27 17:20:24
OOTB 示例 udf junits (UdfTest.java) 使用虛擬 jdbc 架構,并且不顯示 RelBuilder api 的用法。我正在注冊一個簡單的 UDF,它返回輸入字符串的長度。我已經創(chuàng)建了 SqlFunction 并在 SqlStdOperatorTable 中注冊了相同的內容 -SqlFunction length = new SqlFunction("STRLEN",                SqlKind.OTHER_FUNCTION,                ReturnTypes.INTEGER,                null,                OperandTypes.STRING,                SqlFunctionCategory.USER_DEFINED_FUNCTION);SqlStdOperatorTable sqlStdOperatorTable = SqlStdOperatorTable.instance();sqlStdOperatorTable.register(length);并用它來創(chuàng)建 FrameworkConfig -FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder()                .parserConfig(SqlParser.Config.DEFAULT)                .defaultSchema(connection.getRootSchema().getSubSchema("SYSTEM"))                .programs(Programs.sequence(Programs.ofRules(Programs.RULE_SET), Programs.CALC_PROGRAM))                .operatorTable(sqlStdOperatorTable)                .build();現在我可以使用預定義的 sql 函數substr,例如在 SqlStringLengthFunction 類中定義的函數,其中包含以下部分:RelNode udfRelNode = builder                .scan("EMP")                .project(builder.call(new SqlStringLengthFunction(),builder.literal("SampleString"), builder.literal(3))                .build();PreparedStatement statement = RelRunners.run(udfRelNode);ResultSet resultSet = statement.executeQuery();但是當我在 builder.call 中嘗試使用上述函數“l(fā)ength”時,它會拋出異常 -java.lang.RuntimeException: cannot translate call STRLEN($t3)構建器從類中的私有映射中獲取這些函數的實現RexImpTable。此類中沒有公開/受保護的 API 來向該映射添加值。您能否指導如何使用 Calcite 注冊任何 UDF 并將其與 RelBuilder 一起使用?
查看完整描述

1 回答

?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊


這是因為 SqlStdOperatorTable.instance() 為注冊的函數做了一些初始化工作。因此,在 #register 之后調用它不會按預期工作。正確的方法是使用 ListSqlOperatorTable 并將其與帶有 ChainedSqlOperatorTable 的 StdSqlOperatorTable 鏈接起來,預置代碼可能如下所示:

ListSqlOperatorTable listOpTable = new ListSqlOperatorTable();

listOpTable.add(my_udf);

ChainedSqlOperatorTable chainedOpTable = ChainedSqlOperatorTable.of(listOpTable, SqlStdOperatorTable.instance());

// then use this chainedOpTable


// If you want to use a special dialect operators, you can code like this

SqlOperatorTable optable = SqlLibraryOperatorTableFactory.INSTANCE

? .getOperatorTable(SqlLibrary.STANDARD, SqlLibrary.POSTGRESQL);

我用以下方法解決了我的問題 -


// methods containing the udf logic?

public static class MyUdf1 {

? ? ? ? public Integer eval(String a) {

? ? ? ? ? ? return a.length();

? ? ? ? }

? ? }



@Test

? ? public void test1() throws SQLException, ClassNotFoundException {


? ? ? ? CalciteConnection connection = MyTests.getCalciteConnection();


? ? ? ? final String functionName = "STR_LEN";

? ? ? ? final ScalarFunction udfLengthFunction = ScalarFunctionImpl.create(Types.lookupMethod(MyUdf1.class, "eval", String.class));

? ? ? ? connection.getRootSchema().getSubSchema("SYSTEM").add(functionName, udfLengthFunction);


? ? ? ? FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder()

? ? ? ? ? ? ? ? .parserConfig(SqlParser.Config.DEFAULT)

? ? ? ? ? ? ? ? .defaultSchema(connection.getRootSchema().getSubSchema("SYSTEM"))

? ? ? ? ? ? ? ? .programs(Programs.sequence(Programs.ofRules(Programs.RULE_SET), Programs.CALC_PROGRAM))

? ? ? ? ? ? ? ? .build();


? ? ? ? SqlIdentifier udfLengthIdentifier = new SqlIdentifier(Collections.singletonList(functionName), null, SqlParserPos.ZERO, null);

? ? ? ? final SqlOperator strLenOperator = new SqlUserDefinedFunction(udfLengthIdentifier, ReturnTypes.INTEGER, null, OperandTypes.STRING, null, udfLengthFunction);


? ? ? ? final RelBuilder builder = RelBuilder.create(frameworkConfig);

? ? ? ? RelNode udfRelNode = builder

? ? ? ? ? ? ? ? .scan("EMP")

? ? ? ? ? ? ? ? .project(builder.call(strLenOperator, builder.literal("SampleString")))

? ? ? ? ? ? ? ? .build();


? ? ? ? ResultSet set = RelRunners.run(udfRelNode).executeQuery();

? ? ? ? set.next();

? ? ? ? System.out.println(set.getString(1));

? ? }


查看完整回答
反對 回復 2023-09-27
  • 1 回答
  • 0 關注
  • 174 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號