@Override
  public void createTable(ConnectorSession session, ConnectorTableMetadata tableMetadata) {
    checkArgument(!isNullOrEmpty(tableMetadata.getOwner()), "Table owner is null or empty");

    SchemaTableName schemaTableName = tableMetadata.getTable();
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();

    ImmutableList.Builder<String> columnNames = ImmutableList.builder();
    ImmutableList.Builder<Type> columnTypes = ImmutableList.builder();

    buildColumnInfo(tableMetadata, columnNames, columnTypes);

    ImmutableList.Builder<FieldSchema> partitionKeys = ImmutableList.builder();
    ImmutableList.Builder<FieldSchema> columns = ImmutableList.builder();

    List<String> names = columnNames.build();
    List<String> typeNames =
        columnTypes
            .build()
            .stream()
            .map(HiveType::toHiveType)
            .map(HiveType::getHiveTypeName)
            .collect(toList());

    for (int i = 0; i < names.size(); i++) {
      if (tableMetadata.getColumns().get(i).isPartitionKey()) {
        partitionKeys.add(new FieldSchema(names.get(i), typeNames.get(i), null));
      } else {
        columns.add(new FieldSchema(names.get(i), typeNames.get(i), null));
      }
    }

    Path targetPath = getTargetPath(schemaName, tableName, schemaTableName);

    HiveStorageFormat hiveStorageFormat = getHiveStorageFormat(session, this.hiveStorageFormat);
    SerDeInfo serdeInfo = new SerDeInfo();
    serdeInfo.setName(tableName);
    serdeInfo.setSerializationLib(hiveStorageFormat.getSerDe());

    StorageDescriptor sd = new StorageDescriptor();
    sd.setLocation(targetPath.toString());

    sd.setCols(columns.build());
    sd.setSerdeInfo(serdeInfo);
    sd.setInputFormat(hiveStorageFormat.getInputFormat());
    sd.setOutputFormat(hiveStorageFormat.getOutputFormat());

    Table table = new Table();
    table.setDbName(schemaName);
    table.setTableName(tableName);
    table.setOwner(tableMetadata.getOwner());
    table.setTableType(TableType.MANAGED_TABLE.toString());
    String tableComment = "Created by Presto";
    table.setParameters(ImmutableMap.of("comment", tableComment));
    table.setPartitionKeys(partitionKeys.build());
    table.setSd(sd);

    metastore.createTable(table);
  }
Exemplo n.º 2
0
 private void dropTable(SchemaTableName table) {
   try {
     metastoreClient.dropTable(table.getSchemaName(), table.getTableName());
   } catch (RuntimeException e) {
     Logger.get(getClass()).warn(e, "Failed to drop table: %s", table);
   }
 }
 private Database getDatabase(String database) {
   try {
     return metastore.getDatabase(database);
   } catch (NoSuchObjectException e) {
     throw new SchemaNotFoundException(database);
   }
 }
  private boolean checkTablePermission(
      Identity identity, SchemaTableName tableName, HivePrivilege... requiredPrivileges) {
    if (INFORMATION_SCHEMA_NAME.equals(tableName.getSchemaName())) {
      return true;
    }

    Set<HivePrivilege> privilegeSet =
        metastore.getTablePrivileges(
            identity.getUser(), tableName.getSchemaName(), tableName.getTableName());
    return privilegeSet.containsAll(ImmutableSet.copyOf(requiredPrivileges));
  }
 @Override
 public HiveTableHandle getTableHandle(ConnectorSession session, SchemaTableName tableName) {
   checkNotNull(tableName, "tableName is null");
   try {
     metastore.getTable(tableName.getSchemaName(), tableName.getTableName());
     return new HiveTableHandle(
         connectorId, tableName.getSchemaName(), tableName.getTableName(), session);
   } catch (NoSuchObjectException e) {
     // table was not found
     return null;
   }
 }
  @Override
  public void dropView(ConnectorSession session, SchemaTableName viewName) {
    String view = getViews(session, viewName.toSchemaTablePrefix()).get(viewName);
    if (view == null) {
      throw new ViewNotFoundException(viewName);
    }

    try {
      metastore.dropTable(viewName.getSchemaName(), viewName.getTableName());
    } catch (TableNotFoundException e) {
      throw new ViewNotFoundException(e.getTableName());
    }
  }
 private ConnectorTableMetadata getTableMetadata(SchemaTableName tableName) {
   try {
     Table table = metastore.getTable(tableName.getSchemaName(), tableName.getTableName());
     if (table.getTableType().equals(TableType.VIRTUAL_VIEW.name())) {
       throw new TableNotFoundException(tableName);
     }
     List<HiveColumnHandle> handles = hiveColumnHandles(typeManager, connectorId, table, false);
     List<ColumnMetadata> columns =
         ImmutableList.copyOf(transform(handles, columnMetadataGetter(table, typeManager)));
     return new ConnectorTableMetadata(tableName, columns, table.getOwner());
   } catch (NoSuchObjectException e) {
     throw new TableNotFoundException(tableName);
   }
 }
  @Override
  public void renameTable(ConnectorTableHandle tableHandle, SchemaTableName newTableName) {
    if (!allowRenameTable) {
      throw new PrestoException(
          PERMISSION_DENIED, "Renaming tables is disabled in this Hive catalog");
    }

    HiveTableHandle handle = checkType(tableHandle, HiveTableHandle.class, "tableHandle");
    metastore.renameTable(
        handle.getSchemaName(),
        handle.getTableName(),
        newTableName.getSchemaName(),
        newTableName.getTableName());
  }
  private static List<String> listAllDataPaths(
      HiveMetastore metastore, String schemaName, String tableName) {
    ImmutableList.Builder<String> locations = ImmutableList.builder();
    Table table = metastore.getTable(schemaName, tableName).get();
    if (table.getSd().getLocation() != null) {
      // For unpartitioned table, there should be nothing directly under this directory.
      // But including this location in the set makes the directory content assert more
      // extensive, which is desirable.
      locations.add(table.getSd().getLocation());
    }

    Optional<List<String>> partitionNames = metastore.getPartitionNames(schemaName, tableName);
    if (partitionNames.isPresent()) {
      metastore
          .getPartitionsByNames(schemaName, tableName, partitionNames.get())
          .stream()
          .map(partition -> partition.getSd().getLocation())
          .filter(location -> !location.startsWith(table.getSd().getLocation()))
          .forEach(locations::add);
    }

    return locations.build();
  }
Exemplo n.º 10
0
 @Override
 public List<SchemaTableName> listViews(ConnectorSession session, String schemaNameOrNull) {
   ImmutableList.Builder<SchemaTableName> tableNames = ImmutableList.builder();
   for (String schemaName : listSchemas(session, schemaNameOrNull)) {
     try {
       for (String tableName : metastore.getAllViews(schemaName)) {
         tableNames.add(new SchemaTableName(schemaName, tableName));
       }
     } catch (NoSuchObjectException e) {
       // schema disappeared during listing operation
     }
   }
   return tableNames.build();
 }
Exemplo n.º 11
0
  @Override
  public void dropTable(ConnectorTableHandle tableHandle) {
    HiveTableHandle handle = checkType(tableHandle, HiveTableHandle.class, "tableHandle");
    SchemaTableName tableName = schemaTableName(tableHandle);

    if (!allowDropTable) {
      throw new PrestoException(PERMISSION_DENIED, "DROP TABLE is disabled in this Hive catalog");
    }

    try {
      Table table = metastore.getTable(handle.getSchemaName(), handle.getTableName());
      if (!handle.getSession().getUser().equals(table.getOwner())) {
        throw new PrestoException(
            PERMISSION_DENIED,
            format(
                "Unable to drop table '%s': owner of the table is different from session user",
                table));
      }
      metastore.dropTable(handle.getSchemaName(), handle.getTableName());
    } catch (NoSuchObjectException e) {
      throw new TableNotFoundException(tableName);
    }
  }
Exemplo n.º 12
0
 @Override
 public Map<String, ColumnHandle> getColumnHandles(ConnectorTableHandle tableHandle) {
   SchemaTableName tableName = schemaTableName(tableHandle);
   try {
     Table table = metastore.getTable(tableName.getSchemaName(), tableName.getTableName());
     ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();
     for (HiveColumnHandle columnHandle :
         hiveColumnHandles(typeManager, connectorId, table, false)) {
       columnHandles.put(columnHandle.getName(), columnHandle);
     }
     return columnHandles.build();
   } catch (NoSuchObjectException e) {
     throw new TableNotFoundException(tableName);
   }
 }
Exemplo n.º 13
0
 @Override
 public ColumnHandle getSampleWeightColumnHandle(ConnectorTableHandle tableHandle) {
   SchemaTableName tableName = schemaTableName(tableHandle);
   try {
     Table table = metastore.getTable(tableName.getSchemaName(), tableName.getTableName());
     for (HiveColumnHandle columnHandle :
         hiveColumnHandles(typeManager, connectorId, table, true)) {
       if (columnHandle.getName().equals(SAMPLE_WEIGHT_COLUMN_NAME)) {
         return columnHandle;
       }
     }
     return null;
   } catch (NoSuchObjectException e) {
     throw new TableNotFoundException(tableName);
   }
 }
Exemplo n.º 14
0
  @Override
  public void createView(
      ConnectorSession session, SchemaTableName viewName, String viewData, boolean replace) {
    if (replace) {
      try {
        dropView(session, viewName);
      } catch (ViewNotFoundException ignored) {
      }
    }

    Map<String, String> properties =
        ImmutableMap.<String, String>builder()
            .put("comment", "Presto View")
            .put(PRESTO_VIEW_FLAG, "true")
            .build();

    FieldSchema dummyColumn = new FieldSchema("dummy", STRING_TYPE_NAME, null);

    StorageDescriptor sd = new StorageDescriptor();
    sd.setCols(ImmutableList.of(dummyColumn));
    sd.setSerdeInfo(new SerDeInfo());

    Table table = new Table();
    table.setDbName(viewName.getSchemaName());
    table.setTableName(viewName.getTableName());
    table.setOwner(session.getUser());
    table.setTableType(TableType.VIRTUAL_VIEW.name());
    table.setParameters(properties);
    table.setViewOriginalText(encodeViewData(viewData));
    table.setViewExpandedText("/* Presto View */");
    table.setSd(sd);

    try {
      metastore.createTable(table);
    } catch (TableAlreadyExistsException e) {
      throw new ViewAlreadyExistsException(e.getTableName());
    }
  }
Exemplo n.º 15
0
  @Override
  public Map<SchemaTableName, String> getViews(ConnectorSession session, SchemaTablePrefix prefix) {
    ImmutableMap.Builder<SchemaTableName, String> views = ImmutableMap.builder();
    List<SchemaTableName> tableNames;
    if (prefix.getTableName() != null) {
      tableNames =
          ImmutableList.of(new SchemaTableName(prefix.getSchemaName(), prefix.getTableName()));
    } else {
      tableNames = listViews(session, prefix.getSchemaName());
    }

    for (SchemaTableName schemaTableName : tableNames) {
      try {
        Table table =
            metastore.getTable(schemaTableName.getSchemaName(), schemaTableName.getTableName());
        if (HiveUtil.isPrestoView(table)) {
          views.put(schemaTableName, decodeViewData(table.getViewOriginalText()));
        }
      } catch (NoSuchObjectException ignored) {
      }
    }

    return views.build();
  }
 private boolean checkDatabasePermission(
     Identity identity, String schemaName, HivePrivilege... requiredPrivileges) {
   Set<HivePrivilege> privilegeSet =
       metastore.getDatabasePrivileges(identity.getUser(), schemaName);
   return privilegeSet.containsAll(ImmutableSet.copyOf(requiredPrivileges));
 }
Exemplo n.º 17
0
 @Override
 public List<String> listSchemaNames(ConnectorSession session) {
   return metastore.getAllDatabases();
 }
Exemplo n.º 18
0
  @Override
  public void commitCreateTable(
      ConnectorOutputTableHandle tableHandle, Collection<Slice> fragments) {
    HiveOutputTableHandle handle =
        checkType(tableHandle, HiveOutputTableHandle.class, "tableHandle");

    // verify no one raced us to create the target directory
    Path targetPath = new Path(handle.getTargetPath());

    // rename if using a temporary directory
    if (handle.hasTemporaryPath()) {
      if (pathExists(targetPath)) {
        SchemaTableName table = new SchemaTableName(handle.getSchemaName(), handle.getTableName());
        throw new PrestoException(
            HIVE_PATH_ALREADY_EXISTS,
            format(
                "Unable to commit creation of table '%s': target directory already exists: %s",
                table, targetPath));
      }
      // rename the temporary directory to the target
      rename(new Path(handle.getTemporaryPath()), targetPath);
    }

    // create the table in the metastore
    List<String> types =
        handle
            .getColumnTypes()
            .stream()
            .map(HiveType::toHiveType)
            .map(HiveType::getHiveTypeName)
            .collect(toList());

    boolean sampled = false;
    ImmutableList.Builder<FieldSchema> columns = ImmutableList.builder();
    for (int i = 0; i < handle.getColumnNames().size(); i++) {
      String name = handle.getColumnNames().get(i);
      String type = types.get(i);
      if (name.equals(SAMPLE_WEIGHT_COLUMN_NAME)) {
        columns.add(new FieldSchema(name, type, "Presto sample weight column"));
        sampled = true;
      } else {
        columns.add(new FieldSchema(name, type, null));
      }
    }

    HiveStorageFormat hiveStorageFormat = handle.getHiveStorageFormat();

    SerDeInfo serdeInfo = new SerDeInfo();
    serdeInfo.setName(handle.getTableName());
    serdeInfo.setSerializationLib(hiveStorageFormat.getSerDe());
    serdeInfo.setParameters(ImmutableMap.<String, String>of());

    StorageDescriptor sd = new StorageDescriptor();
    sd.setLocation(targetPath.toString());
    sd.setCols(columns.build());
    sd.setSerdeInfo(serdeInfo);
    sd.setInputFormat(hiveStorageFormat.getInputFormat());
    sd.setOutputFormat(hiveStorageFormat.getOutputFormat());
    sd.setParameters(ImmutableMap.<String, String>of());

    Table table = new Table();
    table.setDbName(handle.getSchemaName());
    table.setTableName(handle.getTableName());
    table.setOwner(handle.getTableOwner());
    table.setTableType(TableType.MANAGED_TABLE.toString());
    String tableComment = "Created by Presto";
    if (sampled) {
      tableComment =
          "Sampled table created by Presto. Only query this table from Hive if you understand how Presto implements sampling.";
    }
    table.setParameters(ImmutableMap.of("comment", tableComment));
    table.setPartitionKeys(ImmutableList.<FieldSchema>of());
    table.setSd(sd);

    metastore.createTable(table);
  }
 @Override
 public void checkCanSetCatalogSessionProperty(Identity identity, String propertyName) {
   if (!metastore.getRoles(identity.getUser()).contains(ADMIN_ROLE_NAME)) {
     denySetCatalogSessionProperty(connectorId, propertyName);
   }
 }