dawidwys commented on a change in pull request #7967: [FLINK-11449][table]
Uncouple the Expression class from RexNodes
URL: https://github.com/apache/flink/pull/7967#discussion_r264742116
##########
File path:
flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/validate/FunctionCatalog.scala
##########
@@ -63,252 +112,16 @@ class FunctionCatalog {
)
/**
- * Lookup and create an expression if we find a match.
+ * Lookup a function by name and operands and return the
[[FunctionDefinition]].
*/
- def lookupFunction(name: String, children: Seq[Expression]): Expression = {
- val funcClass = functionBuilders
- .getOrElse(name.toLowerCase, throw new ValidationException(s"Undefined
function: $name"))
-
- // Instantiate a function using the provided `children`
- funcClass match {
-
- // user-defined scalar function call
- case sf if classOf[ScalarFunction].isAssignableFrom(sf) =>
- val scalarSqlFunction = sqlFunctions
- .find(f => f.getName.equalsIgnoreCase(name) &&
f.isInstanceOf[ScalarSqlFunction])
- .getOrElse(throw new ValidationException(s"Undefined scalar
function: $name"))
- .asInstanceOf[ScalarSqlFunction]
- ScalarFunctionCall(scalarSqlFunction.getScalarFunction, children)
-
- // user-defined table function call
- case tf if classOf[TableFunction[_]].isAssignableFrom(tf) =>
- val tableSqlFunction = sqlFunctions
- .find(f => f.getName.equalsIgnoreCase(name) &&
f.isInstanceOf[TableSqlFunction])
- .getOrElse(throw new ValidationException(s"Undefined table function:
$name"))
- .asInstanceOf[TableSqlFunction]
- val typeInfo = tableSqlFunction.getRowTypeInfo
- val function = tableSqlFunction.getTableFunction
- TableFunctionCall(name, function, children, typeInfo)
-
- // user-defined aggregate function call
- case af if classOf[AggregateFunction[_, _]].isAssignableFrom(af) =>
- val aggregateFunction = sqlFunctions
- .find(f => f.getName.equalsIgnoreCase(name) &&
f.isInstanceOf[AggSqlFunction])
- .getOrElse(throw new ValidationException(s"Undefined table function:
$name"))
- .asInstanceOf[AggSqlFunction]
- val function = aggregateFunction.getFunction
- val returnType = aggregateFunction.returnType
- val accType = aggregateFunction.accType
- AggFunctionCall(function, returnType, accType, children)
-
- // general expression call
- case expression if classOf[Expression].isAssignableFrom(expression) =>
- // try to find a constructor accepts `Seq[Expression]`
- Try(funcClass.getDeclaredConstructor(classOf[Seq[_]])) match {
- case Success(seqCtor) =>
- Try(seqCtor.newInstance(children).asInstanceOf[Expression]) match {
- case Success(expr) => expr
- case Failure(e) => throw new ValidationException(e.getMessage)
- }
- case Failure(_) =>
- Try(funcClass.getDeclaredConstructor(classOf[Expression],
classOf[Seq[_]])) match {
- case Success(ctor) =>
- Try(ctor.newInstance(children.head,
children.tail).asInstanceOf[Expression]) match {
- case Success(expr) => expr
- case Failure(e) => throw new
ValidationException(e.getMessage)
- }
- case Failure(_) =>
- val childrenClass =
Seq.fill(children.length)(classOf[Expression])
- // try to find a constructor matching the exact number of
children
- Try(funcClass.getDeclaredConstructor(childrenClass: _*)) match
{
- case Success(ctor) =>
- Try(ctor.newInstance(children:
_*).asInstanceOf[Expression]) match {
- case Success(expr) => expr
- case Failure(exception) => throw new
ValidationException(exception.getMessage)
- }
- case Failure(_) =>
- throw new ValidationException(
- s"Invalid number of arguments for function $funcClass")
- }
- }
- }
- case _ =>
- throw new ValidationException("Unsupported function.")
- }
+ def lookupFunction(name: String, children: Seq[Expression]):
FunctionDefinition = {
Review comment:
I would remove the `children`. It is not used for the lookup and complicates
other places.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services