private InternalTable buildColumns(
     Session session, String catalogName, Map<String, NullableValue> filters) {
   InternalTable.Builder table =
       InternalTable.builder(informationSchemaTableColumns(TABLE_COLUMNS));
   for (Entry<QualifiedObjectName, List<ColumnMetadata>> entry :
       getColumnsList(session, catalogName, filters).entrySet()) {
     QualifiedObjectName tableName = entry.getKey();
     int ordinalPosition = 1;
     for (ColumnMetadata column : entry.getValue()) {
       if (column.isHidden()) {
         continue;
       }
       table.add(
           tableName.getCatalogName(),
           tableName.getSchemaName(),
           tableName.getObjectName(),
           column.getName(),
           ordinalPosition,
           null,
           "YES",
           column.getType().getDisplayName(),
           column.getComment());
       ordinalPosition++;
     }
   }
   return table.build();
 }
 private InternalTable buildSchemata(Session session, String catalogName) {
   InternalTable.Builder table =
       InternalTable.builder(informationSchemaTableColumns(TABLE_SCHEMATA));
   for (String schema : metadata.listSchemaNames(session, catalogName)) {
     table.add(catalogName, schema);
   }
   return table.build();
 }
 private InternalTable buildViews(
     Session session, String catalogName, Map<String, NullableValue> filters) {
   InternalTable.Builder table = InternalTable.builder(informationSchemaTableColumns(TABLE_VIEWS));
   for (Entry<QualifiedObjectName, ViewDefinition> entry :
       getViews(session, catalogName, filters).entrySet()) {
     table.add(
         entry.getKey().getCatalogName(),
         entry.getKey().getSchemaName(),
         entry.getKey().getObjectName(),
         entry.getValue().getOriginalSql());
   }
   return table.build();
 }
  private InternalTable buildTables(
      Session session, String catalogName, Map<String, NullableValue> filters) {
    Set<QualifiedObjectName> tables =
        ImmutableSet.copyOf(getTablesList(session, catalogName, filters));
    Set<QualifiedObjectName> views =
        ImmutableSet.copyOf(getViewsList(session, catalogName, filters));

    InternalTable.Builder table =
        InternalTable.builder(informationSchemaTableColumns(TABLE_TABLES));
    for (QualifiedObjectName name : union(tables, views)) {
      // if table and view names overlap, the view wins
      String type = views.contains(name) ? "VIEW" : "BASE TABLE";
      table.add(name.getCatalogName(), name.getSchemaName(), name.getObjectName(), type);
    }
    return table.build();
  }
 private InternalTable buildFunctions() {
   InternalTable.Builder table =
       InternalTable.builder(informationSchemaTableColumns(TABLE_INTERNAL_FUNCTIONS));
   for (ParametricFunction function : metadata.listFunctions()) {
     if (function.isApproximate()) {
       continue;
     }
     table.add(
         function.getSignature().getName(),
         Joiner.on(", ").join(function.getSignature().getArgumentTypes()),
         function.getSignature().getReturnType().toString(),
         getFunctionType(function),
         function.isDeterministic(),
         nullToEmpty(function.getDescription()));
   }
   return table.build();
 }
  private InternalTable buildPartitions(
      Session session, String catalogName, Map<String, NullableValue> filters) {
    QualifiedObjectName tableName = extractQualifiedTableName(catalogName, filters);

    InternalTable.Builder table =
        InternalTable.builder(informationSchemaTableColumns(TABLE_INTERNAL_PARTITIONS));

    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    if (!tableHandle.isPresent()) {
      throw new TableNotFoundException(tableName.asSchemaTableName());
    }

    List<TableLayoutResult> layouts =
        metadata.getLayouts(
            session, tableHandle.get(), Constraint.<ColumnHandle>alwaysTrue(), Optional.empty());

    if (layouts.size() == 1) {
      Map<ColumnHandle, String> columnHandles =
          ImmutableBiMap.copyOf(metadata.getColumnHandles(session, tableHandle.get())).inverse();
      Map<ColumnHandle, MethodHandle> methodHandles = new HashMap<>();
      for (ColumnHandle columnHandle : columnHandles.keySet()) {
        try {
          ColumnMetadata columnMetadata =
              metadata.getColumnMetadata(session, tableHandle.get(), columnHandle);
          Signature operator =
              metadata.getFunctionRegistry().getCoercion(columnMetadata.getType(), VARCHAR);
          MethodHandle methodHandle =
              metadata
                  .getFunctionRegistry()
                  .getScalarFunctionImplementation(operator)
                  .getMethodHandle();
          methodHandles.put(columnHandle, methodHandle);
        } catch (OperatorNotFoundException exception) {
          // Do not put the columnHandle in the map.
        }
      }

      TableLayout layout = Iterables.getOnlyElement(layouts).getLayout();
      layout
          .getDiscretePredicates()
          .ifPresent(
              domains -> {
                int partitionNumber = 1;
                for (TupleDomain<ColumnHandle> domain : domains) {
                  for (Entry<ColumnHandle, NullableValue> entry :
                      TupleDomain.extractFixedValues(domain).get().entrySet()) {
                    ColumnHandle columnHandle = entry.getKey();
                    String columnName = columnHandles.get(columnHandle);
                    String value = null;
                    if (entry.getValue().getValue() != null) {
                      if (methodHandles.containsKey(columnHandle)) {
                        try {
                          value =
                              ((Slice)
                                      methodHandles
                                          .get(columnHandle)
                                          .invokeWithArguments(entry.getValue().getValue()))
                                  .toStringUtf8();
                        } catch (Throwable throwable) {
                          throw Throwables.propagate(throwable);
                        }
                      } else {
                        // OperatorNotFoundException was thrown for this columnHandle
                        value = "<UNREPRESENTABLE VALUE>";
                      }
                    }
                    table.add(
                        catalogName,
                        tableName.getSchemaName(),
                        tableName.getObjectName(),
                        partitionNumber,
                        columnName,
                        value);
                  }
                  partitionNumber++;
                }
              });
    }
    return table.build();
  }
  private InternalTable buildPartitions(
      Session session, String catalogName, Map<String, SerializableNativeValue> filters) {
    QualifiedTableName tableName = extractQualifiedTableName(catalogName, filters);

    InternalTable.Builder table =
        InternalTable.builder(informationSchemaTableColumns(TABLE_INTERNAL_PARTITIONS));

    Optional<TableHandle> tableHandle = metadata.getTableHandle(session, tableName);
    checkArgument(tableHandle.isPresent(), "Table %s does not exist", tableName);
    Map<ColumnHandle, String> columnHandles =
        ImmutableBiMap.copyOf(metadata.getColumnHandles(session, tableHandle.get())).inverse();

    List<TableLayoutResult> layouts =
        metadata.getLayouts(
            session, tableHandle.get(), Constraint.<ColumnHandle>alwaysTrue(), Optional.empty());

    if (layouts.size() == 1) {
      TableLayout layout = Iterables.getOnlyElement(layouts).getLayout();

      layout
          .getDiscretePredicates()
          .ifPresent(
              domains -> {
                int partitionNumber = 1;
                for (TupleDomain<ColumnHandle> domain : domains) {
                  for (Entry<ColumnHandle, SerializableNativeValue> entry :
                      domain.extractNullableFixedValues().entrySet()) {
                    ColumnHandle columnHandle = entry.getKey();
                    String columnName = columnHandles.get(columnHandle);
                    String value = null;
                    if (entry.getValue().getValue() != null) {
                      ColumnMetadata columnMetadata =
                          metadata.getColumnMetadata(session, tableHandle.get(), columnHandle);
                      try {
                        FunctionInfo operator =
                            metadata
                                .getFunctionRegistry()
                                .getCoercion(columnMetadata.getType(), VARCHAR);
                        value =
                            ((Slice)
                                    operator
                                        .getMethodHandle()
                                        .invokeWithArguments(entry.getValue().getValue()))
                                .toStringUtf8();
                      } catch (OperatorNotFoundException e) {
                        value = "<UNREPRESENTABLE VALUE>";
                      } catch (Throwable throwable) {
                        throw Throwables.propagate(throwable);
                      }
                    }
                    table.add(
                        catalogName,
                        tableName.getSchemaName(),
                        tableName.getTableName(),
                        partitionNumber,
                        columnName,
                        value);
                  }
                  partitionNumber++;
                }
              });
    }
    return table.build();
  }