use of org.apache.flink.table.catalog.CatalogBaseTable in project flink-mirror by flink-ci.
the class HiveParserDDLSemanticAnalyzer method convertDropTable.
private Operation convertDropTable(HiveParserASTNode ast, TableType expectedType) {
String tableName = HiveParserBaseSemanticAnalyzer.getUnescapedName((HiveParserASTNode) ast.getChild(0));
boolean ifExists = (ast.getFirstChildWithType(HiveASTParser.TOK_IFEXISTS) != null);
ObjectIdentifier identifier = parseObjectIdentifier(tableName);
CatalogBaseTable baseTable = getCatalogBaseTable(identifier, true);
if (expectedType == TableType.VIRTUAL_VIEW) {
if (baseTable instanceof CatalogTable) {
throw new ValidationException("DROP VIEW for a table is not allowed");
}
return new DropViewOperation(identifier, ifExists, false);
} else {
if (baseTable instanceof CatalogView) {
throw new ValidationException("DROP TABLE for a view is not allowed");
}
return new DropTableOperation(identifier, ifExists, false);
}
}
use of org.apache.flink.table.catalog.CatalogBaseTable in project flink-mirror by flink-ci.
the class HiveParserDDLSemanticAnalyzer method convertAlterTable.
private Operation convertAlterTable(HiveParserASTNode input) throws SemanticException {
Operation operation = null;
HiveParserASTNode ast = (HiveParserASTNode) input.getChild(1);
String[] qualified = HiveParserBaseSemanticAnalyzer.getQualifiedTableName((HiveParserASTNode) input.getChild(0));
String tableName = HiveParserBaseSemanticAnalyzer.getDotName(qualified);
HashMap<String, String> partSpec = null;
HiveParserASTNode partSpecNode = (HiveParserASTNode) input.getChild(2);
if (partSpecNode != null) {
partSpec = getPartSpec(partSpecNode);
}
CatalogBaseTable alteredTable = getAlteredTable(tableName, false);
switch(ast.getType()) {
case HiveASTParser.TOK_ALTERTABLE_RENAME:
operation = convertAlterTableRename(tableName, ast, false);
break;
case HiveASTParser.TOK_ALTERTABLE_ADDCOLS:
operation = convertAlterTableModifyCols(alteredTable, tableName, ast, false);
break;
case HiveASTParser.TOK_ALTERTABLE_REPLACECOLS:
operation = convertAlterTableModifyCols(alteredTable, tableName, ast, true);
break;
case HiveASTParser.TOK_ALTERTABLE_RENAMECOL:
operation = convertAlterTableChangeCol(alteredTable, qualified, ast);
break;
case HiveASTParser.TOK_ALTERTABLE_ADDPARTS:
operation = convertAlterTableAddParts(qualified, ast);
break;
case HiveASTParser.TOK_ALTERTABLE_DROPPARTS:
operation = convertAlterTableDropParts(qualified, ast);
break;
case HiveASTParser.TOK_ALTERTABLE_PROPERTIES:
operation = convertAlterTableProps(alteredTable, tableName, null, ast, false, false);
break;
case HiveASTParser.TOK_ALTERTABLE_DROPPROPERTIES:
operation = convertAlterTableProps(alteredTable, tableName, null, ast, false, true);
break;
case HiveASTParser.TOK_ALTERTABLE_UPDATESTATS:
operation = convertAlterTableProps(alteredTable, tableName, partSpec, ast, false, false);
break;
case HiveASTParser.TOK_ALTERTABLE_FILEFORMAT:
operation = convertAlterTableFileFormat(alteredTable, ast, tableName, partSpec);
break;
case HiveASTParser.TOK_ALTERTABLE_LOCATION:
operation = convertAlterTableLocation(alteredTable, ast, tableName, partSpec);
break;
case HiveASTParser.TOK_ALTERTABLE_SERIALIZER:
operation = convertAlterTableSerde(alteredTable, ast, tableName, partSpec);
break;
case HiveASTParser.TOK_ALTERTABLE_SERDEPROPERTIES:
operation = convertAlterTableSerdeProps(alteredTable, ast, tableName, partSpec);
break;
case HiveASTParser.TOK_ALTERTABLE_TOUCH:
case HiveASTParser.TOK_ALTERTABLE_ARCHIVE:
case HiveASTParser.TOK_ALTERTABLE_UNARCHIVE:
case HiveASTParser.TOK_ALTERTABLE_PARTCOLTYPE:
case HiveASTParser.TOK_ALTERTABLE_SKEWED:
case HiveASTParser.TOK_ALTERTABLE_EXCHANGEPARTITION:
case HiveASTParser.TOK_ALTERTABLE_MERGEFILES:
case HiveASTParser.TOK_ALTERTABLE_RENAMEPART:
case HiveASTParser.TOK_ALTERTABLE_SKEWED_LOCATION:
case HiveASTParser.TOK_ALTERTABLE_BUCKETS:
case HiveASTParser.TOK_ALTERTABLE_CLUSTER_SORT:
case HiveASTParser.TOK_ALTERTABLE_COMPACT:
case HiveASTParser.TOK_ALTERTABLE_UPDATECOLSTATS:
case HiveASTParser.TOK_ALTERTABLE_DROPCONSTRAINT:
case HiveASTParser.TOK_ALTERTABLE_ADDCONSTRAINT:
handleUnsupportedOperation(ast);
break;
default:
throw new ValidationException("Unknown AST node for ALTER TABLE: " + ast);
}
return operation;
}
use of org.apache.flink.table.catalog.CatalogBaseTable in project flink-mirror by flink-ci.
the class HiveCatalog method alterTable.
@Override
public void alterTable(ObjectPath tablePath, CatalogBaseTable newCatalogTable, boolean ignoreIfNotExists) throws TableNotExistException, CatalogException {
checkNotNull(tablePath, "tablePath cannot be null");
checkNotNull(newCatalogTable, "newCatalogTable cannot be null");
Table hiveTable;
try {
hiveTable = getHiveTable(tablePath);
} catch (TableNotExistException e) {
if (!ignoreIfNotExists) {
throw e;
}
return;
}
CatalogBaseTable existingTable = instantiateCatalogTable(hiveTable);
if (existingTable.getTableKind() != newCatalogTable.getTableKind()) {
throw new CatalogException(String.format("Table types don't match. Existing table is '%s' and new table is '%s'.", existingTable.getTableKind(), newCatalogTable.getTableKind()));
}
disallowChangeCatalogTableType(existingTable.getOptions(), newCatalogTable.getOptions());
boolean isHiveTable = isHiveTable(hiveTable.getParameters());
if (isHiveTable) {
AlterTableOp op = HiveTableUtil.extractAlterTableOp(newCatalogTable.getOptions());
if (op == null) {
// the alter operation isn't encoded as properties
hiveTable = HiveTableUtil.alterTableViaCatalogBaseTable(tablePath, newCatalogTable, hiveTable, hiveConf, false);
} else {
alterTableViaProperties(op, hiveTable, (CatalogTable) newCatalogTable, hiveTable.getParameters(), newCatalogTable.getOptions(), hiveTable.getSd());
}
} else {
hiveTable = HiveTableUtil.alterTableViaCatalogBaseTable(tablePath, newCatalogTable, hiveTable, hiveConf, ManagedTableListener.isManagedTable(this, newCatalogTable));
}
if (isHiveTable) {
hiveTable.getParameters().remove(CONNECTOR.key());
}
try {
client.alter_table(tablePath.getDatabaseName(), tablePath.getObjectName(), hiveTable);
} catch (TException e) {
throw new CatalogException(String.format("Failed to alter table %s", tablePath.getFullName()), e);
}
}
use of org.apache.flink.table.catalog.CatalogBaseTable in project flink-mirror by flink-ci.
the class HiveCatalog method createTable.
@Override
public void createTable(ObjectPath tablePath, CatalogBaseTable table, boolean ignoreIfExists) throws TableAlreadyExistException, DatabaseNotExistException, CatalogException {
checkNotNull(tablePath, "tablePath cannot be null");
checkNotNull(table, "table cannot be null");
if (!databaseExists(tablePath.getDatabaseName())) {
throw new DatabaseNotExistException(getName(), tablePath.getDatabaseName());
}
boolean managedTable = ManagedTableListener.isManagedTable(this, table);
Table hiveTable = HiveTableUtil.instantiateHiveTable(tablePath, table, hiveConf, managedTable);
UniqueConstraint pkConstraint = null;
List<String> notNullCols = new ArrayList<>();
boolean isHiveTable = isHiveTable(table.getOptions());
if (isHiveTable) {
pkConstraint = table.getSchema().getPrimaryKey().orElse(null);
String nnColStr = hiveTable.getParameters().remove(NOT_NULL_COLS);
if (nnColStr != null) {
notNullCols.addAll(Arrays.asList(nnColStr.split(HiveDDLUtils.COL_DELIMITER)));
} else {
for (int i = 0; i < table.getSchema().getFieldDataTypes().length; i++) {
if (!table.getSchema().getFieldDataTypes()[i].getLogicalType().isNullable()) {
notNullCols.add(table.getSchema().getFieldNames()[i]);
}
}
}
// remove the 'connector' option for hive table
hiveTable.getParameters().remove(CONNECTOR.key());
}
try {
if (pkConstraint != null || !notNullCols.isEmpty()) {
// extract constraint traits from table properties
String pkTraitStr = hiveTable.getParameters().remove(PK_CONSTRAINT_TRAIT);
byte pkTrait = pkTraitStr == null ? HiveDDLUtils.defaultTrait() : Byte.parseByte(pkTraitStr);
List<Byte> pkTraits = Collections.nCopies(pkConstraint == null ? 0 : pkConstraint.getColumns().size(), pkTrait);
List<Byte> nnTraits;
String nnTraitsStr = hiveTable.getParameters().remove(NOT_NULL_CONSTRAINT_TRAITS);
if (nnTraitsStr != null) {
String[] traits = nnTraitsStr.split(HiveDDLUtils.COL_DELIMITER);
Preconditions.checkArgument(traits.length == notNullCols.size(), "Number of NOT NULL columns and constraint traits mismatch");
nnTraits = Arrays.stream(traits).map(Byte::new).collect(Collectors.toList());
} else {
nnTraits = Collections.nCopies(notNullCols.size(), HiveDDLUtils.defaultTrait());
}
client.createTableWithConstraints(hiveTable, hiveConf, pkConstraint, pkTraits, notNullCols, nnTraits);
} else {
client.createTable(hiveTable);
}
} catch (AlreadyExistsException e) {
if (!ignoreIfExists) {
throw new TableAlreadyExistException(getName(), tablePath, e);
}
} catch (TException e) {
throw new CatalogException(String.format("Failed to create table %s", tablePath.getFullName()), e);
}
}
use of org.apache.flink.table.catalog.CatalogBaseTable in project flink-mirror by flink-ci.
the class HiveCatalog method getTable.
// ------ tables ------
@Override
public CatalogBaseTable getTable(ObjectPath tablePath) throws TableNotExistException, CatalogException {
checkNotNull(tablePath, "tablePath cannot be null");
Table hiveTable = getHiveTable(tablePath);
return instantiateCatalogTable(hiveTable);
}
Aggregations