You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by "barrkel (via GitHub)" <gi...@apache.org> on 2023/09/14 12:40:13 UTC

[GitHub] [calcite] barrkel commented on a diff in pull request #3421: [CALCITE-5982] / Allow overloading the created enumerable in CalciteMetaImpl when calling getTables() or getColumns()

barrkel commented on code in PR #3421:
URL: https://github.com/apache/calcite/pull/3421#discussion_r1325833941


##########
core/src/main/java/org/apache/calcite/jdbc/CalciteMetaImpl.java:
##########
@@ -378,6 +389,102 @@ Enumerable<MetaTable> tables(String catalog) {
             tables(schema, Functions.<String>truePredicate1()));
   }
 
+  /** If CalciteMetaImpl was created with a subclass for CalciteMetaTable, tables() will return
+   * instances of the subclass instead of CalciteMetaTable.  */
+  private MetaTable getMetaTables(Table table, String tableCatalog,
+      String tableSchem, String name) {
+    try {
+      Constructor<?> constructor =
+          this.metaTableClass.getDeclaredConstructor(Table.class, String.class,
+              String.class, String.class);
+      return (MetaTable) constructor.newInstance(table, tableCatalog, tableSchem, name);
+    } catch (NoSuchMethodException | InstantiationException | IllegalAccessException
+             | InvocationTargetException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private MetaColumn getMetaColumns(
+      CalciteMetaTable table,
+      RelDataTypeField field,
+      int jdbcOrdinal,
+      int precision) {
+    try {
+      if (this.metaColumnClass == MetaColumn.class) {
+        return new MetaColumn(
+            table.tableCat,
+            table.tableSchem,
+            table.tableName,
+            field.getName(),
+            jdbcOrdinal,
+            field.getType().getFullTypeString(),
+            precision,
+            field.getType().getSqlTypeName().allowsScale()
+                ? field.getType().getScale()
+                : null,
+            10,
+            field.getType().isNullable()
+                ? DatabaseMetaData.columnNullable
+                : DatabaseMetaData.columnNoNulls,
+            precision,
+            field.getIndex() + 1,
+            field.getType().isNullable() ? "YES" : "NO",
+            /*isAutoincrement=*/
+            "NO",
+            /*isGeneratedcolumn=*/
+            field.getType().getSqlTypeName() == SqlTypeName.MEASURE
+                ? "YES"
+                : "NO");
+      }
+      Constructor<?> constructor =
+          this.metaColumnClass.getDeclaredConstructor(

Review Comment:
   You know the parameter list of the constructor you expect. If instead you had a factory class, it could implement an interface that exposes a method with this prototype. This would avoid a reflection lookup and invocation for every meta column instantiation; the meta implementation could instantiate the factory on starting up. The default implementation could simply be `MetaColumn::new`, if the factory interface is functional.



##########
core/src/main/java/org/apache/calcite/schema/Table.java:
##########
@@ -82,4 +85,20 @@ public interface Table {
    */
   boolean rolledUpColumnValidInsideAgg(String column, SqlCall call,
       @Nullable SqlNode parent, @Nullable CalciteConnectionConfig config);
+
+  /**
+   * Overload to provide additional table metadata if available.
+   */
+  default Map<String, Object> getTableMetadata() {
+    Map<String, Object> map = new HashMap<>();
+    return map;
+  }
+
+  /**
+   * Overload to provide additional column metadata if available.
+   */
+  default Map<String, Map<String, Object>> getColumnMetadata() {

Review Comment:
   I think these methods aren't a great idea. They're untyped and unscoped and lack discoverability. The lack of typing means they're error-prone for consumers to deal with. The lack of scoping means there's a risk of collisions in the future, as base classes and descendants independently introduce properties with the same name but different semantics and possibly different types. The lack of discoverability means consumers need to look up documentation or run experiments to discover values, instead of getting code completion in their IDE.
   
   There are better ways to model the idea but they're also more heavyweight to design; here's a sketch:
   
   ```
   interface MetaProperty { String name(); } // if name is necessary, which it isn't necessarily
   interface MetaStringProperty extends MetaProperty { String getValue(Table table); }
   class MySpecificImplementation { public static final MetaStringProperty MY_FOO_PROPERTY }
   ```
   
   This kind of pattern is typed, scoped and discoverable, but it's a bunch more work.
   
   There's also the question of metadata about metadata. Modeling the keys for additional metadata as objects themselves enables automated interrogation and enumeration (often the entire point of metadata) and creates a place to put things like serialization (if necessary) and documentation.
   
   More generally, methods passing around maps of (maps of) object create a temptation for smuggling underdocumented values through a system in ways that become harder to reason about. It's seductively easy to just add another entry to a map and pull out it out somewhere else, creating data dependencies which are hard to trace with tooling, harder to maintain due to no obvious place to hang javadoc off of, and harder to deprecate in a controlled way (where do you put your `@Deprecated`?).



-- 
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: commits-unsubscribe@calcite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org