Skip to content

Commit

Permalink
Add metadata fields to CalciteColumn()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivrlee committed Aug 22, 2023
1 parent cefef06 commit 9492bf6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
31 changes: 26 additions & 5 deletions core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,24 @@ private static ImmutableMap.Builder<DatabaseProperty, Object> addProperty(
"SCOPE_TABLE",
"SOURCE_DATA_TYPE",
"IS_AUTOINCREMENT",
"IS_GENERATEDCOLUMN");
"IS_GENERATEDCOLUMN",
"LOOKER_FIELD_DESCRIPTION",
"LOOKER_FIELD_LABEL",
"LOOKER_FIELD_NAME",
"LOOKER_VIEW_NAME",
"LOOKER_VIEW_LABEL",
"HIDDEN",
"LOOKER_FIELD_GROUP_VARIANT",
"DIMENSION_GROUP",
"LOOKER_FIELD_CATEGORY",
"LOOKER_USE_STRICT_VALUE_FORMAT",
"REQUIRES_REFRESH_ON_SORT",
"SORTABLE",
"VALUE_FORMAT",
"LOOKER_TYPE",
"LOOKER_FIELD_ALIAS",
"TAGS",
"FILTERS");
}

Enumerable<MetaCatalog> catalogs() {
Expand Down Expand Up @@ -390,7 +407,7 @@ Enumerable<MetaTable> tables(final MetaSchema schema_) {
requireNonNull(schema.calciteSchema.getTable(name, true),
() -> "table " + name + " is not found (case sensitive)")
.getTable();
HashMap<String, String> metadataMap = (table.getTableMetadata() != null)
HashMap<String, Object> metadataMap = (table.getTableMetadata() != null)
? table.getTableMetadata() : new HashMap<>();
return new CalciteMetaTable(table,
schema.tableCatalog,
Expand All @@ -404,7 +421,7 @@ Enumerable<MetaTable> tables(final MetaSchema schema_) {
.entrySet())
.select(pair -> {
final Table table = pair.getValue();
HashMap<String, String> metadataMap = (table.getTableMetadata() != null)
HashMap<String, Object> metadataMap = (table.getTableMetadata() != null)
? table.getTableMetadata() : new HashMap<>();
return new CalciteMetaTable(table,
schema.tableCatalog,
Expand Down Expand Up @@ -459,6 +476,9 @@ protected Enumerable<MetaTypeInfo> allTypeInfo() {

public Enumerable<MetaColumn> columns(final MetaTable table_) {
final CalciteMetaTable table = (CalciteMetaTable) table_;
HashMap<String, HashMap<String, Object>> metadataMap =
(table.calciteTable.getFieldMetadata() != null)
? table.calciteTable.getFieldMetadata() : new HashMap<>();
final RelDataType rowType =
table.calciteTable.getRowType(getConnection().typeFactory);
return Linq4j.asEnumerable(rowType.getFieldList())
Expand Down Expand Up @@ -500,7 +520,8 @@ public Enumerable<MetaColumn> columns(final MetaTable table_) {
/*isGeneratedcolumn=*/
field.getType().getSqlTypeName() == SqlTypeName.MEASURE
? "YES"
: "NO");
: "NO",
metadataMap.getOrDefault(field.getName(), new HashMap()));
});
}

Expand Down Expand Up @@ -842,7 +863,7 @@ private static class CalciteMetaTable extends MetaTable {
this.calciteTable = requireNonNull(calciteTable, "calciteTable");
}
CalciteMetaTable(Table calciteTable, String tableCat,
String tableSchem, String tableName, HashMap<String, String> metadataMap) {
String tableSchem, String tableName, HashMap<String, Object> metadataMap) {
super(tableCat, tableSchem, tableName,
calciteTable.getJdbcTableType().jdbcName, metadataMap);
this.calciteTable = requireNonNull(calciteTable, "calciteTable");
Expand Down
9 changes: 7 additions & 2 deletions core/src/main/java/org/apache/calcite/schema/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ boolean rolledUpColumnValidInsideAgg(String column, SqlCall call,
/**
* Gets Looker Explore metadata if available.
*/
default HashMap<String, String> getTableMetadata() {
HashMap<String, String> map = new HashMap<>();
default HashMap<String, Object> getTableMetadata() {
HashMap<String, Object> map = new HashMap<>();
map.put("EXPLORE_LABEL", null);
map.put("EXPLORE_DESCRIPTION", null);
map.put("EXPLORE_TAGS", null);
return map;
}

default HashMap<String, HashMap<String, Object>> getFieldMetadata() {
HashMap<String, HashMap<String, Object>> map = new HashMap<>();
return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,42 @@ protected static Connection getRemoteConnection() throws SQLException {
.returns(CalciteAssert.checkResultContains("COLUMN_NAME=EMPNO"));
}

@Test void testRemoteColumnsMetadata() throws SQLException {
final Connection connection =
DriverManager.getConnection("jdbc:avatica:remote:factory=" + LJS);
final ResultSet resultSet = connection.getMetaData()
.getColumns(null, null, null, null);
final ResultSetMetaData metaData = resultSet.getMetaData();
assertThat(metaData.getColumnCount(), is(41));
assertThat(metaData.getColumnName(25), is("LOOKER_FIELD_DESCRIPTION"));
assertThat(metaData.getColumnName(26), is("LOOKER_FIELD_LABEL"));
assertThat(metaData.getColumnName(27), is("LOOKER_FIELD_NAME"));
assertThat(metaData.getColumnName(28), is("LOOKER_VIEW_NAME"));
assertThat(metaData.getColumnName(29), is("LOOKER_VIEW_LABEL"));
assertThat(metaData.getColumnName(30), is("HIDDEN"));
assertThat(metaData.getColumnName(31), is("LOOKER_FIELD_GROUP_VARIANT"));
assertThat(metaData.getColumnName(32), is("DIMENSION_GROUP"));
assertThat(metaData.getColumnName(33), is("LOOKER_FIELD_CATEGORY"));
assertThat(metaData.getColumnName(34), is("LOOKER_USE_STRICT_VALUE_FORMAT"));
assertThat(metaData.getColumnName(35), is("REQUIRES_REFRESH_ON_SORT"));
assertThat(metaData.getColumnName(36), is("SORTABLE"));
assertThat(metaData.getColumnName(37), is("VALUE_FORMAT"));
assertThat(metaData.getColumnName(38), is("LOOKER_TYPE"));
assertThat(metaData.getColumnName(39), is("LOOKER_FIELD_ALIAS"));
assertThat(metaData.getColumnName(40), is("TAGS"));
assertThat(metaData.getColumnName(41), is("FILTERS"));
resultSet.close();
connection.close();
assertThat(connection.isClosed(), is(true));
}

@Test void testRemoteTables() throws SQLException {
final Connection connection =
DriverManager.getConnection("jdbc:avatica:remote:factory=" + LJS);
assertThat(connection.isClosed(), is(false));
final ResultSet resultSet =
connection.getMetaData().getTables(connection.getCatalog(), null, null, new String[] {"TABLE"});
connection.getMetaData().getTables(
connection.getCatalog(), null, null, new String[] {"TABLE"});
final ResultSetMetaData metaData = resultSet.getMetaData();
assertThat(metaData.getColumnCount(), is(13));
assertThat(metaData.getColumnName(11), is("EXPLORE_LABEL"));
Expand Down

0 comments on commit 9492bf6

Please sign in to comment.