use of org.apache.flink.table.catalog.CatalogBaseTable in project flink by apache.
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 by apache.
the class HiveCatalogTest method testRetrieveFlinkProperties.
@Test
public void testRetrieveFlinkProperties() throws Exception {
ObjectPath hiveObjectPath = new ObjectPath(HiveCatalog.DEFAULT_DB, "testRetrieveProperties");
Map<String, String> options = getLegacyFileSystemConnectorOptions("/test_path");
options.put(CONNECTOR.key(), "jdbc");
options.put("url", "jdbc:clickhouse://host:port/testUrl1");
options.put("flink.url", "jdbc:clickhouse://host:port/testUrl2");
hiveCatalog.createTable(hiveObjectPath, new CatalogTableImpl(schema, options, null), false);
CatalogBaseTable hiveTable = hiveCatalog.getTable(hiveObjectPath);
assertThat(hiveTable.getOptions()).containsEntry("url", "jdbc:clickhouse://host:port/testUrl1");
assertThat(hiveTable.getOptions()).containsEntry("flink.url", "jdbc:clickhouse://host:port/testUrl2");
}
use of org.apache.flink.table.catalog.CatalogBaseTable in project flink by apache.
the class HiveCatalogTest method testGetNoSchemaGenericTable.
@Test
public void testGetNoSchemaGenericTable() throws Exception {
ObjectPath hiveObjectPath = new ObjectPath(HiveCatalog.DEFAULT_DB, "testGetNoSchemaGenericTable");
Map<String, String> properties = new HashMap<>();
properties.put(CONNECTOR.key(), "jdbc");
hiveCatalog.createTable(hiveObjectPath, new CatalogTableImpl(TableSchema.builder().build(), properties, null), false);
CatalogBaseTable catalogTable = hiveCatalog.getTable(hiveObjectPath);
assertThat(catalogTable.getSchema()).isEqualTo(TableSchema.builder().build());
}
use of org.apache.flink.table.catalog.CatalogBaseTable in project flink by apache.
the class PostgresCatalogTest method testSerialDataTypes.
@Test
public void testSerialDataTypes() throws TableNotExistException {
CatalogBaseTable table = catalog.getTable(new ObjectPath(PostgresCatalog.DEFAULT_DATABASE, TABLE_SERIAL_TYPE));
assertEquals(getSerialTable().schema, table.getUnresolvedSchema());
}
use of org.apache.flink.table.catalog.CatalogBaseTable in project flink by apache.
the class PostgresCatalogTest method testArrayDataTypes.
@Test
public void testArrayDataTypes() throws TableNotExistException {
CatalogBaseTable table = catalog.getTable(new ObjectPath(PostgresCatalog.DEFAULT_DATABASE, TABLE_ARRAY_TYPE));
assertEquals(getArrayTable().schema, table.getUnresolvedSchema());
}
Aggregations