Skip to content

Commit e45d7ff

Browse files
committed
[FLINK-xxx][1/N] introduce MODEL keyword for model as argument in function
1 parent b8f8d6e commit e45d7ff

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

flink-table/flink-sql-parser/src/main/codegen/data/Parser.tdd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@
666666
# Example: DateFunctionCall().
667667
builtinFunctionCallMethods: [
668668
"TryCastFunctionCall()"
669+
"ExplicitModel()"
669670
]
670671

671672
# List of methods for parsing extensions to "ALTER <scope>" calls.

flink-table/flink-sql-parser/src/main/codegen/includes/parserImpls.ftl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,6 +3089,22 @@ SqlNode TryCastFunctionCall() :
30893089
}
30903090
}
30913091

3092+
/**
3093+
* Parses an explicit Model m reference.
3094+
*/
3095+
SqlNode ExplicitModel() :
3096+
{
3097+
SqlNode modelRef;
3098+
final Span s;
3099+
}
3100+
{
3101+
<MODEL> modelRef = CompoundIdentifier()
3102+
{
3103+
s = span();
3104+
return new SqlExplicitModelOperator(2, null, null, null).createCall(s.pos(), modelRef);
3105+
}
3106+
}
3107+
30923108
/**
30933109
* Parses a partition key/value,
30943110
* e.g. p or p = '10'.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.calcite.sql;
19+
20+
import org.apache.calcite.sql.parser.SqlParserPos;
21+
import org.checkerframework.checker.nullness.qual.Nullable;
22+
23+
import java.util.List;
24+
25+
/** SqlExplicitModelCall is a SQL call that represents an explicit model. */
26+
public class SqlExplicitModelCall extends SqlBasicCall {
27+
28+
public SqlExplicitModelCall(
29+
SqlOperator operator,
30+
List<? extends @Nullable SqlNode> operandList,
31+
SqlParserPos pos,
32+
@Nullable SqlLiteral functionQualifier) {
33+
super(operator, operandList, pos, functionQualifier);
34+
}
35+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.calcite.sql;
19+
20+
import org.apache.calcite.sql.parser.SqlParserPos;
21+
import org.apache.calcite.sql.type.SqlOperandTypeChecker;
22+
import org.apache.calcite.sql.type.SqlOperandTypeInference;
23+
import org.apache.calcite.sql.type.SqlReturnTypeInference;
24+
import org.apache.calcite.util.ImmutableNullableList;
25+
import org.checkerframework.checker.nullness.qual.Nullable;
26+
27+
/** SqlExplicitModelOperator is a SQL operator that represents an explicit model. */
28+
public class SqlExplicitModelOperator extends SqlPrefixOperator {
29+
30+
public SqlExplicitModelOperator(
31+
int prec,
32+
@Nullable SqlReturnTypeInference returnTypeInference,
33+
@Nullable SqlOperandTypeInference operandTypeInference,
34+
@Nullable SqlOperandTypeChecker operandTypeChecker) {
35+
super(
36+
"MODEL",
37+
SqlKind.OTHER_FUNCTION,
38+
prec,
39+
returnTypeInference,
40+
operandTypeInference,
41+
operandTypeChecker);
42+
}
43+
44+
@Override
45+
public SqlCall createCall(
46+
@Nullable SqlLiteral functionQualifier,
47+
SqlParserPos pos,
48+
@Nullable SqlNode... operands) {
49+
pos = pos.plusAll(operands);
50+
return new SqlExplicitModelCall(
51+
this, ImmutableNullableList.copyOf(operands), pos, functionQualifier);
52+
}
53+
}

flink-table/flink-sql-parser/src/test/java/org/apache/flink/sql/parser/FlinkSqlParserImplTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,6 +3360,22 @@ void testCreateModelAsWithOutput() {
33603360
"CREATE MODEL AS SELECT syntax does not support to specify explicit output columns."));
33613361
}
33623362

3363+
@Test
3364+
void testModelInFunction() {
3365+
sql("select * from table(ml_predict(TABLE my_table, MODEL my_model))")
3366+
.ok(
3367+
"SELECT *\n"
3368+
+ "FROM TABLE(`ML_PREDICT`((TABLE `MY_TABLE`), MODEL `MY_MODEL`))");
3369+
}
3370+
3371+
@Test
3372+
void testModelInFunctionWithoutTable() {
3373+
sql("select * from func(TABLE my_table, MODEL cat.db.my_model)")
3374+
.ok(
3375+
"SELECT *\n"
3376+
+ "FROM TABLE(`FUNC`((TABLE `MY_TABLE`), MODEL `CAT`.`DB`.`MY_MODEL`))");
3377+
}
3378+
33633379
/*
33643380
* This test was backported from Calcite 1.38 (CALCITE-6266).
33653381
* Remove it together with upgrade to Calcite 1.38.

0 commit comments

Comments
 (0)