Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@
import org.apache.calcite.schema.SchemaPlus;
import org.apache.calcite.schema.Table;
import org.apache.calcite.schema.impl.AbstractTableQueryable;
import org.apache.calcite.schema.impl.MaterializedViewTable;
import org.apache.calcite.server.CalciteServerStatement;
import org.apache.calcite.sql.SqlJdbcFunctionCall;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlOperatorTable;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParser;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.tools.FrameworkConfig;
Expand Down Expand Up @@ -502,6 +507,70 @@ public Enumerable<MetaColumn> columns(final MetaTable table_) {
"TABLE_TYPE");
}

@Override public MetaResultSet getFunctions(ConnectionHandle ch,
String catalog,
Pat schemaPattern,
Pat functionNamePattern) {
final Predicate1<MetaSchema> schemaMatcher = namedMatcher(schemaPattern);
return createResultSet(schemas(catalog)
.where(schemaMatcher)
.selectMany(schema -> functions(schema, catalog, matcher(functionNamePattern)))
.orderBy(x ->
(Comparable) FlatLists.of(
x.functionCat, x.functionSchem, x.functionName, x.specificName
)),
MetaFunction.class,
"FUNCTION_CAT",
"FUNCTION_SCHEM",
"FUNCTION_NAME",
"REMARKS",
"FUNCTION_TYPE",
"SPECIFIC_NAME");
}

Enumerable<MetaFunction> functions(final MetaSchema schema_, final String catalog) {
final CalciteMetaSchema schema = (CalciteMetaSchema) schema_;
Enumerable<MetaFunction> opTableFunctions = Linq4j.emptyEnumerable();
if (schema.calciteSchema.schema.equals(MetadataSchema.INSTANCE)) {
SqlOperatorTable opTable = getConnection().config()
.fun(SqlOperatorTable.class, SqlStdOperatorTable.instance());
List<SqlOperator> q = opTable.getOperatorList();
opTableFunctions = Linq4j.asEnumerable(q)
.where(op -> SqlKind.FUNCTION.contains(op.getKind()))
.select(op ->
new MetaFunction(
catalog,
schema.getName(),
op.getName(),
(short) DatabaseMetaData.functionResultUnknown,
op.getName()
)
);
}
return Linq4j.asEnumerable(schema.calciteSchema.getFunctionNames())
.selectMany(name ->
Linq4j.asEnumerable(schema.calciteSchema.getFunctions(name, true))
//exclude materialized views from the result set
.where(fn -> !(fn instanceof MaterializedViewTable.MaterializedViewTableMacro))
.select(fnx ->
new MetaFunction(
catalog,
schema.getName(),
name,
(short) DatabaseMetaData.functionResultUnknown,
name
)
)
)
.concat(opTableFunctions);
}

Enumerable<MetaFunction> functions(final MetaSchema schema, final String catalog,
final Predicate1<String> functionNameMatcher) {
return functions(schema, catalog)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add some tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reviewing, i've added a test and sorting according to the JDBC specification.

FYI, there is also a discussion in JIRA on how to handle built-in functions.

.where(v1 -> functionNameMatcher.apply(v1.functionName));
}

@Override public Iterable<Object> createIterable(StatementHandle handle, QueryState state,
Signature signature, @Nullable List<TypedValue> parameterValues, @Nullable Frame firstFrame) {
// Drop QueryState
Expand Down
42 changes: 42 additions & 0 deletions core/src/test/java/org/apache/calcite/test/JdbcAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.calcite.config.Lex;
import org.apache.calcite.test.CalciteAssert.AssertThat;
import org.apache.calcite.test.CalciteAssert.DatabaseInstance;
import org.apache.calcite.util.Smalls;
import org.apache.calcite.util.TestUtil;

import org.hsqldb.jdbcDriver;
Expand Down Expand Up @@ -760,6 +761,47 @@ class JdbcAdapterTest {
});
}

@Test void testMetadataFunctions() {
final String model = ""
+ "{\n"
+ " version: '1.0',\n"
+ " schemas: [\n"
+ " {\n"
+ " name: 'adhoc',\n"
+ " functions: [\n"
+ " {\n"
+ " name: 'MY_STR',\n"
+ " className: '" + Smalls.MyToStringFunction.class.getName() + "'\n"
+ " },\n"
+ " {\n"
+ " name: 'FIBONACCI_TABLE',\n"
+ " className: '" + Smalls.class.getName() + "',\n"
+ " methodName: 'fibonacciTable'\n"
+ " }\n"
+ " ],\n"
+ " materializations: [\n"
+ " {\n"
+ " table: 'TEST_VIEW',\n"
+ " sql: 'SELECT 1'\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "}";
CalciteAssert.model(model)
.withDefaultSchema("adhoc")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

.metaData(connection -> {
try {
return connection.getMetaData().getFunctions(null, "adhoc", "%");
} catch (SQLException e) {
throw TestUtil.rethrow(e);
}
})
.returns(""
+ "FUNCTION_CAT=null; FUNCTION_SCHEM=adhoc; FUNCTION_NAME=FIBONACCI_TABLE; REMARKS=null; FUNCTION_TYPE=0; SPECIFIC_NAME=FIBONACCI_TABLE\n"
+ "FUNCTION_CAT=null; FUNCTION_SCHEM=adhoc; FUNCTION_NAME=MY_STR; REMARKS=null; FUNCTION_TYPE=0; SPECIFIC_NAME=MY_STR\n");
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-666">[CALCITE-666]
* Anti-semi-joins against JDBC adapter give wrong results</a>. */
Expand Down