tkalkirill commented on code in PR #5318:
URL: https://github.com/apache/calcite/pull/5318#discussion_r4510257879


##########
core/src/main/java/org/apache/calcite/sql/SqlUtil.java:
##########
@@ -588,6 +593,80 @@ private static Iterator<SqlOperator> 
filterOperatorRoutinesByKind(
   /**
    * Looks up all subject routines matching the given name and argument types.
    *
+   * <p>For user-defined scalar and table functions with
+   * {@link SqlSyntax#FUNCTION} syntax and fixed parameter metadata supplied by
+   * {@link SqlOperandMetadata}, Calcite resolves overloads using the following
+   * rules:
+   *
+   * <ol>
+   * <li>Collect candidates by function name, category and syntax using the
+   * operator table and {@code nameMatcher}. Keep only candidates whose operand
+   * count range accepts the number of supplied arguments.</li>
+   * <li>Match arguments to formal parameters by position, or by parameter name
+   * when {@code argNames} is supplied. Parameter names are matched
+   * case-insensitively. Reject candidates with an unknown parameter name or an
+   * incompatible argument type. Compatibility is determined by
+   * {@link SqlTypeUtil#canCastFrom(RelDataType, RelDataType, boolean)}, with 
the
+   * formal parameter as the target type and {@code coerce} controlling the
+   * permitted conversions.</li>
+   * <li>If {@code coerce} is true, return the compatible candidates without
+   * ranking them by type precedence. If fewer than two candidates remain,
+   * return them immediately as well.</li>
+   * <li>Otherwise, consider the supplied arguments from left to right, in call
+   * order. For each argument, obtain its
+   * {@link RelDataType#getPrecedenceList() type precedence list} and examine 
the
+   * corresponding parameter types of the remaining candidates. If any of those
+   * types belong to the list, keep only candidates with a parameter type of 
the
+   * highest precedence according to
+   * {@link RelDataTypePrecedenceList#compareTypePrecedence}. Discard parameter
+   * types outside the list in this case. If none belong to the list, that
+   * argument does not eliminate any candidates. Candidates eliminated for an
+   * earlier argument are not reconsidered for a later argument.</li>
+   * <li>After precedence filtering, retain candidates whose
+   * {@link SqlKind#getFunctionKind() function kind} matches the requested
+   * kind.</li>
+   * </ol>
+   *
+   * <p>Type compatibility and type precedence are separate checks. A parameter
+   * of type {@link SqlTypeName#ANY ANY} can accept a concrete argument even
+   * though it is outside that argument's precedence list. With Calcite's
+   * built-in SQL types, a compatible concrete parameter in that list therefore
+   * takes precedence over an {@code ANY} parameter. If no compatible concrete
+   * overload exists, an {@code ANY} overload can still be selected. 
Conversely,
+   * an argument of type {@code ANY} prefers an {@code ANY} parameter when one
+   * remains among the candidates. If no remaining parameter belongs to the
+   * argument's precedence list, the candidates remain tied at that position.
+   *
+   * <p>An untyped {@link SqlTypeName#NULL NULL} argument supplies no 
precedence
+   * preference between non-{@code NULL} parameter types. Other arguments may
+   * still distinguish the candidates. A typed null, such as
+   * {@code CAST(NULL AS TIMESTAMP)}, uses the precedence list of its declared
+   * type.
+   *
+   * <p>For example, given overloads {@code F(VARCHAR)}, {@code F(TIMESTAMP)} 
and
+   * {@code F(ANY)}, lookup with {@code coerce = false} resolves as follows,
+   * independently of candidate order:
+   *
+   * <pre>{@code
+   * F('text')                           -> F(VARCHAR)
+   * F(TIMESTAMP '2024-01-02 03:04:05')   -> F(TIMESTAMP)
+   * F(TRUE)                             -> F(ANY)
+   * F(CAST(NULL AS TIMESTAMP))           -> F(TIMESTAMP)
+   * }</pre>
+   *
+   * <p>For {@code F(NULL)}, all three overloads remain candidates. Filtering
+   * preserves the order supplied by the operator table, and
+   * {@link #lookupRoutine} returns the first surviving candidate. Thus 
candidate
+   * order can affect calls that remain tied, but not calls with a unique best
+   * match.
+   *
+   * <p>Resolution uses the argument types supplied to this method, including
+   * types derived for nested calls and other SQL expressions. It does not

Review Comment:
   I tried to fix the Javadoc.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to