mihaibudiu commented on code in PR #4794:
URL: https://github.com/apache/calcite/pull/4794#discussion_r2805369731
##########
core/src/main/java/org/apache/calcite/prepare/CalciteCatalogReader.java:
##########
@@ -171,14 +172,57 @@ private Collection<org.apache.calcite.schema.Function>
getFunctionsFrom(
SqlValidatorUtil.getSchema(rootSchema,
Iterables.concat(schemaNames, Util.skipLast(names)),
nameMatcher);
if (schema != null) {
- final String name = Util.last(names);
- boolean caseSensitive = nameMatcher.isCaseSensitive();
- functions2.addAll(schema.getFunctions(name, caseSensitive));
+ final String functionName = Util.last(names);
+ if (nameMatcher.isCaseSensitive()) {
+ addFunctions(functions2, schema, names, functionName, true);
+ } else {
+ boolean hasMatchedFunctionName = false;
+ for (String candidateFunctionName : schema.getFunctionNames()) {
+ if (nameMatcher.matches(functionName, candidateFunctionName)) {
+ hasMatchedFunctionName = true;
+ // candidateFunctionName already has canonical case from schema.
+ // Use case-sensitive lookup to bind each function to that exact
name.
+ addFunctions(functions2, schema, names, candidateFunctionName,
true);
+ }
+ }
+ if (!hasMatchedFunctionName) {
+ // Fallback for schemas where getFunctionNames() is incomplete but
+ // getFunctions(name, false) can still resolve functions.
+ addFunctions(functions2, schema, names, functionName, false);
+ }
+ }
}
}
return functions2;
}
+ private static SqlIdentifier resolvedFunctionIdentifier(CalciteSchema schema,
+ List<String> names, String functionName) {
+ final List<String> schemaPath = schema.path(null);
+ // Keep the same qualifier depth as the original call (e.g. schema.func
+ // stays 2-part, catalog.schema.func stays 3-part).
+ final int qualifierCount = names.size() - 1;
+ // Replace only the suffix that corresponds to the resolved schema path.
+ // Any leading qualifiers that are outside this schema path are kept as-is.
+ final int resolvedQualifierCount = Math.min(qualifierCount,
schemaPath.size());
+ final List<String> resolvedNames = new ArrayList<>(names.size());
+ resolvedNames.addAll(names.subList(0, qualifierCount -
resolvedQualifierCount));
+ resolvedNames.addAll(
+ schemaPath.subList(schemaPath.size() - resolvedQualifierCount,
schemaPath.size()));
+ resolvedNames.add(functionName);
+ return new SqlIdentifier(resolvedNames, SqlParserPos.ZERO);
Review Comment:
How about passing the position of the original identifier too?
Positions can be very useful for error reporting.
##########
core/src/test/java/org/apache/calcite/prepare/LookupOperatorOverloadsTest.java:
##########
@@ -133,6 +143,149 @@ private static void check(List<SqlFunctionCategory>
actuals,
checkInternal(false);
}
+ @Test void testLookupQualifiedNameUsesResolvedCase() {
Review Comment:
Could you add a comment for each of these summarizing what is being tested?
E.g., "lookup MyCatalog.MySchema.MyFUNC as myfunc"
##########
core/src/main/java/org/apache/calcite/prepare/CalciteCatalogReader.java:
##########
@@ -171,14 +172,57 @@ private Collection<org.apache.calcite.schema.Function>
getFunctionsFrom(
SqlValidatorUtil.getSchema(rootSchema,
Iterables.concat(schemaNames, Util.skipLast(names)),
nameMatcher);
if (schema != null) {
- final String name = Util.last(names);
- boolean caseSensitive = nameMatcher.isCaseSensitive();
- functions2.addAll(schema.getFunctions(name, caseSensitive));
+ final String functionName = Util.last(names);
+ if (nameMatcher.isCaseSensitive()) {
+ addFunctions(functions2, schema, names, functionName, true);
+ } else {
+ boolean hasMatchedFunctionName = false;
+ for (String candidateFunctionName : schema.getFunctionNames()) {
+ if (nameMatcher.matches(functionName, candidateFunctionName)) {
+ hasMatchedFunctionName = true;
+ // candidateFunctionName already has canonical case from schema.
+ // Use case-sensitive lookup to bind each function to that exact
name.
+ addFunctions(functions2, schema, names, candidateFunctionName,
true);
+ }
+ }
+ if (!hasMatchedFunctionName) {
+ // Fallback for schemas where getFunctionNames() is incomplete but
+ // getFunctions(name, false) can still resolve functions.
+ addFunctions(functions2, schema, names, functionName, false);
+ }
+ }
}
}
return functions2;
}
+ private static SqlIdentifier resolvedFunctionIdentifier(CalciteSchema schema,
Review Comment:
I don't really see what this has anything to do with functions. This
function seems to be very generic, and work for all all identifiers. Could it
be called "createResolvedIdentifier"?
--
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]