org.apache.flink.table.catalog.exceptions.TableNotExistException Java Examples

The following examples show how to use org.apache.flink.table.catalog.exceptions.TableNotExistException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void alterTable(ObjectPath tablePath, CatalogBaseTable newTable, boolean ignoreIfNotExists)
		throws TableNotExistException {
	checkNotNull(tablePath);
	checkNotNull(newTable);

	CatalogBaseTable existingTable = tables.get(tablePath);

	if (existingTable != null) {
		if (existingTable.getClass() != newTable.getClass()) {
			throw new CatalogException(
				String.format("Table types don't match. Existing table is '%s' and new table is '%s'.",
					existingTable.getClass().getName(), newTable.getClass().getName()));
		}

		tables.put(tablePath, newTable.copy());
	} else if (!ignoreIfNotExists) {
		throw new TableNotExistException(getName(), tablePath);
	}
}
 
Example #2
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void alterTable(ObjectPath tablePath, CatalogBaseTable newTable, boolean ignoreIfNotExists)
		throws TableNotExistException {
	checkNotNull(tablePath);
	checkNotNull(newTable);

	CatalogBaseTable existingTable = tables.get(tablePath);

	if (existingTable != null) {
		if (existingTable.getClass() != newTable.getClass()) {
			throw new CatalogException(
				String.format("Table types don't match. Existing table is '%s' and new table is '%s'.",
					existingTable.getClass().getName(), newTable.getClass().getName()));
		}

		tables.put(tablePath, newTable.copy());
	} else if (!ignoreIfNotExists) {
		throw new TableNotExistException(getName(), tablePath);
	}
}
 
Example #3
Source File: CatalogManager.java    From flink with Apache License 2.0 6 votes vote down vote up
private Optional<ResolvedTable> lookupCatalogTable(List<String> path) throws TableNotExistException {
	if (path.size() == 3) {
		Catalog currentCatalog = catalogs.get(path.get(0));
		String currentDatabaseName = path.get(1);
		String tableName = String.join(".", path.subList(2, path.size()));
		ObjectPath objectPath = new ObjectPath(currentDatabaseName, tableName);

		if (currentCatalog != null && currentCatalog.tableExists(objectPath)) {
			CatalogBaseTable table = currentCatalog.getTable(objectPath);
			return Optional.of(ResolvedTable.catalogTable(
				asList(path.get(0), currentDatabaseName, tableName),
				table));
		}
	}

	return Optional.empty();
}
 
Example #4
Source File: HiveCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public CatalogColumnStatistics getTableColumnStatistics(ObjectPath tablePath) throws TableNotExistException, CatalogException {
	Table hiveTable = getHiveTable(tablePath);
	try {
		if (!isTablePartitioned(hiveTable)) {
			List<ColumnStatisticsObj> columnStatisticsObjs = client.getTableColumnStatistics(
					hiveTable.getDbName(), hiveTable.getTableName(), getFieldNames(hiveTable.getSd().getCols()));
			return new CatalogColumnStatistics(HiveStatsUtil.createCatalogColumnStats(columnStatisticsObjs));
		} else {
			// TableColumnStats of partitioned table is unknown, the behavior is same as HIVE
			return CatalogColumnStatistics.UNKNOWN;
		}
	} catch (TException e) {
		throw new CatalogException(String.format("Failed to get table column stats of table %s",
												tablePath.getFullName()), e);
	}
}
 
Example #5
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void dropTable(ObjectPath tablePath, boolean ignoreIfNotExists) throws TableNotExistException {
	checkNotNull(tablePath);

	if (tableExists(tablePath)) {
		tables.remove(tablePath);
		tableStats.remove(tablePath);
		tableColumnStats.remove(tablePath);

		partitions.remove(tablePath);
		partitionStats.remove(tablePath);
		partitionColumnStats.remove(tablePath);
	} else if (!ignoreIfNotExists) {
		throw new TableNotExistException(getName(), tablePath);
	}
}
 
Example #6
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 6 votes vote down vote up
private static TableStats extractTableStats(
		Catalog catalog,
		ObjectIdentifier objectIdentifier) {
	final ObjectPath tablePath = objectIdentifier.toObjectPath();
	try {
		CatalogTableStatistics tableStatistics = catalog.getTableStatistics(tablePath);
		CatalogColumnStatistics columnStatistics = catalog.getTableColumnStatistics(tablePath);
		return convertToTableStats(tableStatistics, columnStatistics);
	} catch (TableNotExistException e) {
		throw new ValidationException(format(
			"Could not get statistic for table: [%s, %s, %s]",
			objectIdentifier.getCatalogName(),
			tablePath.getDatabaseName(),
			tablePath.getObjectName()), e);
	}
}
 
Example #7
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public List<CatalogPartitionSpec> listPartitions(ObjectPath tablePath, CatalogPartitionSpec partitionSpec)
		throws TableNotExistException, TableNotPartitionedException, CatalogException {
	checkNotNull(tablePath);
	checkNotNull(partitionSpec);

	ensurePartitionedTable(tablePath);

	CatalogTable catalogTable = (CatalogTable) getTable(tablePath);
	List<String> partKeys = catalogTable.getPartitionKeys();
	Map<String, String> spec = partitionSpec.getPartitionSpec();
	if (!partKeys.containsAll(spec.keySet())) {
		return new ArrayList<>();
	}

	return partitions.get(tablePath).keySet().stream()
		.filter(ps -> ps.getPartitionSpec().entrySet().containsAll(partitionSpec.getPartitionSpec().entrySet()))
		.collect(Collectors.toList());
}
 
Example #8
Source File: HiveCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public CatalogColumnStatistics getTableColumnStatistics(ObjectPath tablePath) throws TableNotExistException, CatalogException {
	Table hiveTable = getHiveTable(tablePath);
	try {
		if (!isTablePartitioned(hiveTable)) {
			List<ColumnStatisticsObj> columnStatisticsObjs = client.getTableColumnStatistics(
					hiveTable.getDbName(), hiveTable.getTableName(), getFieldNames(hiveTable.getSd().getCols()));
			return new CatalogColumnStatistics(HiveStatsUtil.createCatalogColumnStats(columnStatisticsObjs, hiveVersion));
		} else {
			// TableColumnStats of partitioned table is unknown, the behavior is same as HIVE
			return CatalogColumnStatistics.UNKNOWN;
		}
	} catch (TException e) {
		throw new CatalogException(String.format("Failed to get table column stats of table %s",
												tablePath.getFullName()), e);
	}
}
 
Example #9
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void createPartition(ObjectPath tablePath, CatalogPartitionSpec partitionSpec, CatalogPartition partition, boolean ignoreIfExists)
		throws TableNotExistException, TableNotPartitionedException, PartitionSpecInvalidException, PartitionAlreadyExistsException, CatalogException {
	checkNotNull(tablePath);
	checkNotNull(partitionSpec);
	checkNotNull(partition);

	ensureTableExists(tablePath);
	ensurePartitionedTable(tablePath);
	ensureFullPartitionSpec(tablePath, partitionSpec);

	if (partitionExists(tablePath, partitionSpec)) {
		if (!ignoreIfExists) {
			throw new PartitionAlreadyExistsException(getName(), tablePath, partitionSpec);
		}
	}

	partitions.get(tablePath).put(partitionSpec, partition.copy());
}
 
Example #10
Source File: HiveCatalog.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public List<CatalogPartitionSpec> listPartitions(ObjectPath tablePath)
		throws TableNotExistException, TableNotPartitionedException, CatalogException {
	checkNotNull(tablePath, "Table path cannot be null");

	Table hiveTable = getHiveTable(tablePath);

	ensurePartitionedTable(tablePath, hiveTable);

	try {
		// pass -1 as max_parts to fetch all partitions
		return client.listPartitionNames(tablePath.getDatabaseName(), tablePath.getObjectName(), (short) -1).stream()
			.map(HiveCatalog::createPartitionSpec).collect(Collectors.toList());
	} catch (TException e) {
		throw new CatalogException(
			String.format("Failed to list partitions of table %s", tablePath), e);
	}
}
 
Example #11
Source File: CatalogTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetTable_TableNotExistException() throws Exception {
	catalog.createDatabase(db1, createDb(), false);

	exception.expect(TableNotExistException.class);
	exception.expectMessage("Table (or view) db1.nonexist does not exist in Catalog");
	catalog.getTable(nonExistObjectPath);
}
 
Example #12
Source File: CatalogTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenameTable_TableNotExistException() throws Exception {
	catalog.createDatabase(db1, createDb(), false);

	exception.expect(TableNotExistException.class);
	exception.expectMessage("Table (or view) db1.t1 does not exist in Catalog");
	catalog.renameTable(path1, t2, false);
}
 
Example #13
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void alterTableStatistics(ObjectPath tablePath, CatalogTableStatistics tableStatistics, boolean ignoreIfNotExists)
		throws TableNotExistException {
	checkNotNull(tablePath);
	checkNotNull(tableStatistics);

	if (tableExists(tablePath)) {
		tableStats.put(tablePath, tableStatistics.copy());
	} else if (!ignoreIfNotExists) {
		throw new TableNotExistException(getName(), tablePath);
	}
}
 
Example #14
Source File: SqlToOperationConverterTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@After
public void after() throws TableNotExistException {
	final ObjectPath path1 = new ObjectPath(catalogManager.getCurrentDatabase(), "t1");
	final ObjectPath path2 = new ObjectPath(catalogManager.getCurrentDatabase(), "t2");
	catalog.dropTable(path1, true);
	catalog.dropTable(path2, true);
}
 
Example #15
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CatalogBaseTable getTable(ObjectPath tablePath) throws TableNotExistException {
	checkNotNull(tablePath);

	if (!tableExists(tablePath)) {
		throw new TableNotExistException(getName(), tablePath);
	} else {
		return tables.get(tablePath).copy();
	}
}
 
Example #16
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Table getTable(String tableName) {

	ObjectPath tablePath = new ObjectPath(databaseName, tableName);

	try {
		if (!catalog.tableExists(tablePath)) {
			return null;
		}

		CatalogBaseTable table = catalog.getTable(tablePath);

		if (table instanceof QueryOperationCatalogView) {
			return QueryOperationCatalogViewTable.createCalciteTable(((QueryOperationCatalogView) table));
		} else if (table instanceof ConnectorCatalogTable) {
			return convertConnectorTable((ConnectorCatalogTable<?, ?>) table);
		} else if (table instanceof CatalogTable) {
			return convertCatalogTable(tablePath, (CatalogTable) table);
		} else {
			throw new TableException("Unsupported table type: " + table);
		}
	} catch (TableNotExistException | CatalogException e) {
		// TableNotExistException should never happen, because we are checking it exists
		// via catalog.tableExists
		throw new TableException(format(
			"A failure occurred when accessing table. Table path [%s, %s, %s]",
			catalogName,
			databaseName,
			tableName), e);
	}
}
 
Example #17
Source File: HiveCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CatalogTableStatistics getTableStatistics(ObjectPath tablePath) throws TableNotExistException,
		CatalogException {
	Table hiveTable = getHiveTable(tablePath);
	if (!isTablePartitioned(hiveTable)) {
		return createCatalogTableStatistics(hiveTable.getParameters());
	} else {
		return CatalogTableStatistics.UNKNOWN;
	}
}
 
Example #18
Source File: HiveCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CatalogBaseTable getTable(ObjectPath tablePath) throws TableNotExistException, CatalogException {
	checkNotNull(tablePath, "tablePath cannot be null");

	Table hiveTable = getHiveTable(tablePath);
	return instantiateCatalogTable(hiveTable, hiveConf);
}
 
Example #19
Source File: HiveCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void renameTable(ObjectPath tablePath, String newTableName, boolean ignoreIfNotExists)
		throws TableNotExistException, TableAlreadyExistException, CatalogException {
	checkNotNull(tablePath, "tablePath cannot be null");
	checkArgument(!StringUtils.isNullOrWhitespaceOnly(newTableName), "newTableName cannot be null or empty");

	try {
		// alter_table() doesn't throw a clear exception when target table doesn't exist.
		// Thus, check the table existence explicitly
		if (tableExists(tablePath)) {
			ObjectPath newPath = new ObjectPath(tablePath.getDatabaseName(), newTableName);
			// alter_table() doesn't throw a clear exception when new table already exists.
			// Thus, check the table existence explicitly
			if (tableExists(newPath)) {
				throw new TableAlreadyExistException(getName(), newPath);
			} else {
				Table table = getHiveTable(tablePath);
				table.setTableName(newTableName);
				client.alter_table(tablePath.getDatabaseName(), tablePath.getObjectName(), table);
			}
		} else if (!ignoreIfNotExists) {
			throw new TableNotExistException(getName(), tablePath);
		}
	} catch (TException e) {
		throw new CatalogException(
			String.format("Failed to rename table %s", tablePath.getFullName()), e);
	}
}
 
Example #20
Source File: CatalogTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreatePartition_TableNotExistException() throws Exception {
	catalog.createDatabase(db1, createDb(), false);

	exception.expect(TableNotExistException.class);
	exception.expectMessage(
		String.format("Table (or view) %s does not exist in Catalog %s.", path1.getFullName(), TEST_CATALOG_NAME));
	catalog.createPartition(path1, createPartitionSpec(), createPartition(), false);
}
 
Example #21
Source File: CatalogTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenameTable_TableNotExistException() throws Exception {
	catalog.createDatabase(db1, createDb(), false);

	exception.expect(TableNotExistException.class);
	exception.expectMessage("Table (or view) db1.t1 does not exist in Catalog");
	catalog.renameTable(path1, t2, false);
}
 
Example #22
Source File: HiveCatalogITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testTableWithPrimaryKey() {
	EnvironmentSettings.Builder builder = EnvironmentSettings.newInstance().useBlinkPlanner();
	EnvironmentSettings settings = builder.build();
	TableEnvironment tableEnv = TableEnvironment.create(settings);
	tableEnv.getConfig().getConfiguration().setInteger(TABLE_EXEC_RESOURCE_DEFAULT_PARALLELISM, 1);

	tableEnv.registerCatalog("catalog1", hiveCatalog);
	tableEnv.useCatalog("catalog1");

	final String createTable = "CREATE TABLE pk_src (\n" +
			"  uuid varchar(40) not null,\n" +
			"  price DECIMAL(10, 2),\n" +
			"  currency STRING,\n" +
			"  ts6 TIMESTAMP(6),\n" +
			"  ts AS CAST(ts6 AS TIMESTAMP(3)),\n" +
			"  WATERMARK FOR ts AS ts,\n" +
			"  constraint ct1 PRIMARY KEY(uuid) NOT ENFORCED)\n" +
			"  WITH (\n" +
			"    'connector.type' = 'filesystem'," +
			"    'connector.path' = 'file://fakePath'," +
			"    'format.type' = 'csv')";

	tableEnv.executeSql(createTable);

	TableSchema tableSchema = tableEnv.getCatalog(tableEnv.getCurrentCatalog())
			.map(catalog -> {
				try {
					final ObjectPath tablePath = ObjectPath.fromString(catalog.getDefaultDatabase() + '.' + "pk_src");
					return catalog.getTable(tablePath).getSchema();
				} catch (TableNotExistException e) {
					return null;
				}
			}).orElse(null);
	assertNotNull(tableSchema);
	assertEquals(
			tableSchema.getPrimaryKey(),
			Optional.of(UniqueConstraint.primaryKey("ct1", Collections.singletonList("uuid"))));
	tableEnv.executeSql("DROP TABLE pk_src");
}
 
Example #23
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 5 votes vote down vote up
private Table convertConnectorTable(
		ConnectorCatalogTable<?, ?> table,
		ObjectPath tablePath) throws TableNotExistException {
	if (table.getTableSource().isPresent()) {
		TableSource<?> tableSource = table.getTableSource().get();
		if (!(tableSource instanceof StreamTableSource ||
				tableSource instanceof LookupableTableSource)) {
			throw new TableException(
					"Only StreamTableSource and LookupableTableSource can be used in Blink planner.");
		}
		if (!isStreamingMode && tableSource instanceof StreamTableSource &&
				!((StreamTableSource<?>) tableSource).isBounded()) {
			throw new TableException("Only bounded StreamTableSource can be used in batch mode.");
		}

		TableStats tableStats = TableStats.UNKNOWN;
		// TODO supports stats for partitionable table
		if (!table.isPartitioned()) {
			CatalogTableStatistics tableStatistics = catalog.getTableStatistics(tablePath);
			CatalogColumnStatistics columnStatistics = catalog.getTableColumnStatistics(tablePath);
			tableStats = convertToTableStats(tableStatistics, columnStatistics);
		}
		return new TableSourceTable<>(
				tableSource,
				isStreamingMode,
				FlinkStatistic.builder().tableStats(tableStats).build());
	} else {
		Optional<TableSinkTable> tableSinkTable = table.getTableSink()
			.map(tableSink -> new TableSinkTable<>(
				tableSink,
				FlinkStatistic.UNKNOWN()));
		if (tableSinkTable.isPresent()) {
			return tableSinkTable.get();
		} else {
			throw new TableException("Cannot convert a connector table " +
				"without either source or sink.");
		}
	}
}
 
Example #24
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public List<CatalogPartitionSpec> listPartitions(ObjectPath tablePath)
		throws TableNotExistException, TableNotPartitionedException, CatalogException {
	checkNotNull(tablePath);

	ensureTableExists(tablePath);
	ensurePartitionedTable(tablePath);

	return new ArrayList<>(partitions.get(tablePath).keySet());
}
 
Example #25
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Check if the given partitionSpec is full partition spec for the given table.
 */
private boolean isFullPartitionSpec(ObjectPath tablePath, CatalogPartitionSpec partitionSpec) throws TableNotExistException {
	CatalogBaseTable baseTable = getTable(tablePath);

	if (!(baseTable instanceof CatalogTable)) {
		return false;
	}

	CatalogTable table = (CatalogTable) baseTable;
	List<String> partitionKeys = table.getPartitionKeys();
	Map<String, String> spec = partitionSpec.getPartitionSpec();

	// The size of partition spec should not exceed the size of partition keys
	return partitionKeys.size() == spec.size() && spec.keySet().containsAll(partitionKeys);
}
 
Example #26
Source File: CatalogStatisticsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void alterTableStatistics(
		Catalog catalog,
		String tableName) throws TableNotExistException, TablePartitionedException {
	catalog.alterTableStatistics(new ObjectPath(databaseName, tableName),
			new CatalogTableStatistics(100, 10, 1000L, 2000L), true);
	catalog.alterTableColumnStatistics(new ObjectPath(databaseName, tableName), createColumnStats(), true);
}
 
Example #27
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public CatalogTableStatistics getTableStatistics(ObjectPath tablePath) throws TableNotExistException {
	checkNotNull(tablePath);

	if (!tableExists(tablePath)) {
		throw new TableNotExistException(getName(), tablePath);
	}
	if (!isPartitionedTable(tablePath)) {
		CatalogTableStatistics result = tableStats.get(tablePath);
		return result != null ? result.copy() : CatalogTableStatistics.UNKNOWN;
	} else {
		return CatalogTableStatistics.UNKNOWN;
	}
}
 
Example #28
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void alterTableStatistics(ObjectPath tablePath, CatalogTableStatistics tableStatistics, boolean ignoreIfNotExists)
		throws TableNotExistException {
	checkNotNull(tablePath);
	checkNotNull(tableStatistics);

	if (tableExists(tablePath)) {
		tableStats.put(tablePath, tableStatistics.copy());
	} else if (!ignoreIfNotExists) {
		throw new TableNotExistException(getName(), tablePath);
	}
}
 
Example #29
Source File: GenericInMemoryCatalog.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void alterTableColumnStatistics(ObjectPath tablePath, CatalogColumnStatistics columnStatistics,
		boolean ignoreIfNotExists) throws TableNotExistException {
	checkNotNull(tablePath);
	checkNotNull(columnStatistics);

	if (tableExists(tablePath)) {
		tableColumnStats.put(tablePath, columnStatistics.copy());
	} else if (!ignoreIfNotExists) {
		throw new TableNotExistException(getName(), tablePath);
	}
}
 
Example #30
Source File: CatalogManager.java    From flink with Apache License 2.0 5 votes vote down vote up
private Optional<ResolvedTable> lookupPath(List<String> prefix, List<String> userPath) {
	try {
		List<String> path = new ArrayList<>(prefix);
		path.addAll(userPath);

		Optional<ResolvedTable> potentialTable = lookupCatalogTable(path);

		if (!potentialTable.isPresent()) {
			potentialTable = lookupExternalTable(path);
		}
		return potentialTable;
	} catch (TableNotExistException e) {
		return Optional.empty();
	}
}