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 static void assertReadFields(RecordCursor cursor, List<ColumnMetadata> schema) {
   for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) {
     ColumnMetadata column = schema.get(columnIndex);
     if (!cursor.isNull(columnIndex)) {
       Type type = column.getType();
       if (BOOLEAN.equals(type)) {
         cursor.getBoolean(columnIndex);
       } else if (BIGINT.equals(type)) {
         cursor.getLong(columnIndex);
       } else if (TIMESTAMP.equals(type)) {
         cursor.getLong(columnIndex);
       } else if (DOUBLE.equals(type)) {
         cursor.getDouble(columnIndex);
       } else if (VARCHAR.equals(type) || VARBINARY.equals(type)) {
         try {
           cursor.getSlice(columnIndex);
         } catch (RuntimeException e) {
           throw new RuntimeException("column " + column, e);
         }
       } else {
         fail("Unknown primitive type " + columnIndex);
       }
     }
   }
 }
 private static void assertPrimitiveField(
     Map<String, ColumnMetadata> map, int position, String name, Type type, boolean partitionKey) {
   assertTrue(map.containsKey(name));
   ColumnMetadata column = map.get(name);
   assertEquals(column.getOrdinalPosition(), position);
   assertEquals(column.getType(), type, name);
   assertEquals(column.isPartitionKey(), partitionKey, name);
 }
  private static void buildColumnInfo(
      ConnectorTableMetadata tableMetadata,
      ImmutableList.Builder<String> names,
      ImmutableList.Builder<Type> types) {
    for (ColumnMetadata column : tableMetadata.getColumns()) {
      // TODO: also verify that the OutputFormat supports the type
      if (!HiveRecordSink.isTypeSupported(column.getType())) {
        throw new PrestoException(
            NOT_SUPPORTED,
            format(
                "Cannot create table with unsupported type: %s",
                column.getType().getDisplayName()));
      }
      names.add(column.getName());
      types.add(column.getType());
    }

    if (tableMetadata.isSampled()) {
      names.add(SAMPLE_WEIGHT_COLUMN_NAME);
      types.add(BIGINT);
    }
  }
  public OperatorFactory createTableScanOperator(
      Session session, int operatorId, String tableName, String... columnNames) {
    checkArgument(session.getCatalog().isPresent(), "catalog not set");
    checkArgument(session.getSchema().isPresent(), "schema not set");

    // look up the table
    QualifiedTableName qualifiedTableName =
        new QualifiedTableName(session.getCatalog().get(), session.getSchema().get(), tableName);
    TableHandle tableHandle = metadata.getTableHandle(session, qualifiedTableName).orElse(null);
    checkArgument(tableHandle != null, "Table %s does not exist", qualifiedTableName);

    // lookup the columns
    Map<String, ColumnHandle> allColumnHandles = metadata.getColumnHandles(session, tableHandle);
    ImmutableList.Builder<ColumnHandle> columnHandlesBuilder = ImmutableList.builder();
    ImmutableList.Builder<Type> columnTypesBuilder = ImmutableList.builder();
    for (String columnName : columnNames) {
      ColumnHandle columnHandle = allColumnHandles.get(columnName);
      checkArgument(
          columnHandle != null, "Table %s does not have a column %s", tableName, columnName);
      columnHandlesBuilder.add(columnHandle);
      ColumnMetadata columnMetadata =
          metadata.getColumnMetadata(session, tableHandle, columnHandle);
      columnTypesBuilder.add(columnMetadata.getType());
    }
    List<ColumnHandle> columnHandles = columnHandlesBuilder.build();
    List<Type> columnTypes = columnTypesBuilder.build();

    // get the split for this table
    List<TableLayoutResult> layouts =
        metadata.getLayouts(session, tableHandle, Constraint.alwaysTrue(), Optional.empty());
    Split split = getLocalQuerySplit(layouts.get(0).getLayout().getHandle());

    return new OperatorFactory() {
      @Override
      public List<Type> getTypes() {
        return columnTypes;
      }

      @Override
      public Operator createOperator(DriverContext driverContext) {
        OperatorContext operatorContext =
            driverContext.addOperatorContext(operatorId, "BenchmarkSource");
        ConnectorPageSource pageSource =
            pageSourceManager.createPageSource(session, split, columnHandles);
        return new PageSourceOperator(pageSource, columnTypes, operatorContext);
      }

      @Override
      public void close() {}
    };
  }
Exemple #6
0
  public OperatorFactory createTableScanOperator(
      final int operatorId, String tableName, String... columnNames) {
    // look up the table
    TableHandle tableHandle =
        metadata
            .getTableHandle(
                session,
                new QualifiedTableName(session.getCatalog(), session.getSchema(), tableName))
            .orNull();
    checkArgument(tableHandle != null, "Table %s does not exist", tableName);

    // lookup the columns
    ImmutableList.Builder<ColumnHandle> columnHandlesBuilder = ImmutableList.builder();
    ImmutableList.Builder<Type> columnTypesBuilder = ImmutableList.builder();
    for (String columnName : columnNames) {
      ColumnHandle columnHandle = metadata.getColumnHandle(tableHandle, columnName).orNull();
      checkArgument(
          columnHandle != null, "Table %s does not have a column %s", tableName, columnName);
      columnHandlesBuilder.add(columnHandle);
      ColumnMetadata columnMetadata = metadata.getColumnMetadata(tableHandle, columnHandle);
      columnTypesBuilder.add(columnMetadata.getType());
    }
    final List<ColumnHandle> columnHandles = columnHandlesBuilder.build();
    final List<Type> columnTypes = columnTypesBuilder.build();

    // get the split for this table
    final Split split = getLocalQuerySplit(tableHandle);

    return new OperatorFactory() {
      @Override
      public List<Type> getTypes() {
        return columnTypes;
      }

      @Override
      public Operator createOperator(DriverContext driverContext) {
        OperatorContext operatorContext =
            driverContext.addOperatorContext(operatorId, "BenchmarkSource");
        return dataStreamProvider.createNewDataStream(operatorContext, split, columnHandles);
      }

      @Override
      public void close() {}
    };
  }
  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();
  }