org.apache.flink.table.catalog.CatalogBaseTable Java Examples

The following examples show how to use org.apache.flink.table.catalog.CatalogBaseTable. 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: PulsarMetadataReader.java    From pulsar-flink with Apache License 2.0 6 votes vote down vote up
public void putSchema(ObjectPath tablePath, CatalogBaseTable table) throws IncompatibleSchemaException {
    String topic = objectPath2TopicName(tablePath);
    TableSchema tableSchema = table.getSchema();
    List<String> fieldsRemaining = new ArrayList<>(tableSchema.getFieldCount());
    for (String fieldName : tableSchema.getFieldNames()) {
        if (!PulsarOptions.META_FIELD_NAMES.contains(fieldName)) {
            fieldsRemaining.add(fieldName);
        }
    }

    DataType dataType;

    if (fieldsRemaining.size() == 1) {
        dataType = tableSchema.getFieldDataType(fieldsRemaining.get(0)).get();
    } else {
        List<DataTypes.Field> fieldList = fieldsRemaining.stream()
                .map(f -> DataTypes.FIELD(f, tableSchema.getFieldDataType(f).get()))
                .collect(Collectors.toList());
        dataType = DataTypes.ROW(fieldList.toArray(new DataTypes.Field[0]));
    }

    SchemaInfo si = SchemaUtils.sqlType2PulsarSchema(dataType).getSchemaInfo();
    SchemaUtils.uploadPulsarSchema(admin, topic, si);
}
 
Example #2
Source File: TableEnvHiveConnectorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotNullConstraints() throws Exception {
	Assume.assumeTrue(HiveVersionTestUtil.HIVE_310_OR_LATER);
	TableEnvironment tableEnv = getTableEnvWithHiveCatalog();
	tableEnv.executeSql("create database db1");
	try {
		tableEnv.executeSql("create table db1.tbl (x int,y bigint not null enable rely,z string not null enable norely)");
		CatalogBaseTable catalogTable = hiveCatalog.getTable(new ObjectPath("db1", "tbl"));
		TableSchema tableSchema = catalogTable.getSchema();
		assertTrue("By default columns should be nullable",
				tableSchema.getFieldDataTypes()[0].getLogicalType().isNullable());
		assertFalse("NOT NULL columns should be reflected in table schema",
				tableSchema.getFieldDataTypes()[1].getLogicalType().isNullable());
		assertTrue("NOT NULL NORELY columns should be considered nullable",
				tableSchema.getFieldDataTypes()[2].getLogicalType().isNullable());
	} finally {
		tableEnv.executeSql("drop database db1 cascade");
	}
}
 
Example #3
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 6 votes vote down vote up
private FlinkStatistic getStatistic(
		boolean isTemporary,
		CatalogBaseTable catalogBaseTable,
		ObjectIdentifier tableIdentifier) {
	if (isTemporary || catalogBaseTable instanceof QueryOperationCatalogView) {
		return FlinkStatistic.UNKNOWN();
	}
	if (catalogBaseTable instanceof CatalogTable) {
		Catalog catalog = catalogManager.getCatalog(catalogName).get();
		return FlinkStatistic.builder()
			.tableStats(extractTableStats(catalog, tableIdentifier))
			// this is a temporary solution, FLINK-15123 will resolve this
			.uniqueKeys(extractUniqueKeys(catalogBaseTable.getSchema()))
			.build();
	} else {
		return FlinkStatistic.UNKNOWN();
	}
}
 
Example #4
Source File: DatabaseCalciteSchema.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Table getTable(String tableName) {
	ObjectIdentifier identifier = ObjectIdentifier.of(catalogName, databaseName, tableName);
	return catalogManager.getTable(identifier)
		.map(result -> {
			CatalogBaseTable table = result.getTable();
			FlinkStatistic statistic = getStatistic(result.isTemporary(), table, identifier);
			return new CatalogSchemaTable(
				identifier,
				result,
				statistic,
				catalogManager.getCatalog(catalogName).orElseThrow(IllegalStateException::new),
				isStreamingMode);
		})
		.orElse(null);
}
 
Example #5
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
private void registerTableSourceInternal(String name, TableSource<?> tableSource) {
	validateTableSource(tableSource);
	Optional<CatalogBaseTable> table = getCatalogTable(catalogManager.getBuiltInCatalogName(),
		catalogManager.getBuiltInDatabaseName(), name);

	if (table.isPresent()) {
		if (table.get() instanceof ConnectorCatalogTable<?, ?>) {
			ConnectorCatalogTable<?, ?> sourceSinkTable = (ConnectorCatalogTable<?, ?>) table.get();
			if (sourceSinkTable.getTableSource().isPresent()) {
				throw new ValidationException(String.format(
					"Table '%s' already exists. Please choose a different name.", name));
			} else {
				// wrapper contains only sink (not source)
				replaceTableInternal(
					name,
					ConnectorCatalogTable
						.sourceAndSink(tableSource, sourceSinkTable.getTableSink().get(), !IS_STREAM_TABLE));
			}
		} else {
			throw new ValidationException(String.format(
				"Table '%s' already exists. Please choose a different name.", name));
		}
	} else {
		registerTableInternal(name, ConnectorCatalogTable.source(tableSource, !IS_STREAM_TABLE));
	}
}
 
Example #6
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
private void registerTableSinkInternal(String name, TableSink<?> tableSink) {
	Optional<CatalogBaseTable> table = getCatalogTable(catalogManager.getBuiltInCatalogName(),
		catalogManager.getBuiltInDatabaseName(), name);

	if (table.isPresent()) {
		if (table.get() instanceof ConnectorCatalogTable<?, ?>) {
			ConnectorCatalogTable<?, ?> sourceSinkTable = (ConnectorCatalogTable<?, ?>) table.get();
			if (sourceSinkTable.getTableSink().isPresent()) {
				throw new ValidationException(String.format(
					"Table '%s' already exists. Please choose a different name.", name));
			} else {
				// wrapper contains only sink (not source)
				replaceTableInternal(
					name,
					ConnectorCatalogTable
						.sourceAndSink(sourceSinkTable.getTableSource().get(), tableSink, !IS_STREAM_TABLE));
			}
		} else {
			throw new ValidationException(String.format(
				"Table '%s' already exists. Please choose a different name.", name));
		}
	} else {
		registerTableInternal(name, ConnectorCatalogTable.sink(tableSink, !IS_STREAM_TABLE));
	}
}
 
Example #7
Source File: FlinkSqlParser.java    From sylph with Apache License 2.0 5 votes vote down vote up
private void translateJoin(JoinInfo joinInfo, Map<String, CreateTable> batchTables)
{
    Table streamTable = getTable(tableEnv, joinInfo.getStreamTable());
    RowTypeInfo streamRowType = (RowTypeInfo) streamTable.getSchema().toRowType();
    DataStream<Row> inputStream = tableEnv.toAppendStream(streamTable, org.apache.flink.types.Row.class);
    inputStream.getTransformation().setOutputType(streamRowType);

    //get batch table schema
    CreateTable batchTable = requireNonNull(batchTables.get(joinInfo.getBatchTable().getName()), "batch table [" + joinInfo.getJoinTableName() + "] not exits");
    RowTypeInfo batchTableRowType = StreamSqlUtil.schemaToRowTypeInfo(StreamSqlUtil.getTableSchema(batchTable));
    List<SelectField> joinSelectFields = getAllSelectFields(joinInfo, streamRowType, batchTableRowType);

    //It is recommended to do keyby first.
    JoinContext joinContext = JoinContextImpl.createContext(joinInfo, streamRowType, joinSelectFields);
    RealTimeTransForm transForm = getJoinTransForm(joinContext, batchTable);
    DataStream<Row> joinResultStream = AsyncFunctionHelper.translate(inputStream, transForm);

    //set schema
    RowTypeInfo rowTypeInfo = getJoinOutScheam(joinSelectFields);
    joinResultStream.getTransformation().setOutputType(rowTypeInfo);
    //--register tmp joinTable

    Catalog catalog = tableEnv.getCatalog(tableEnv.getCurrentCatalog()).get();
    if (catalog.tableExists(ObjectPath.fromString(joinInfo.getJoinTableName()))) {
        Table table = tableEnv.fromDataStream(joinResultStream);
        CatalogBaseTable tableTable = new QueryOperationCatalogView(table.getQueryOperation());
        try {
            catalog.createTable(ObjectPath.fromString(joinInfo.getJoinTableName()), tableTable, true);
        }
        catch (TableAlreadyExistException | DatabaseNotExistException e) {
            e.printStackTrace();
        }
        //tableEnv.replaceRegisteredTable(joinInfo.getJoinTableName(), new RelTable(table.getRelNode()));
    }
    else {
        tableEnv.registerDataStream(joinInfo.getJoinTableName(), joinResultStream);
    }
    //next update join select query
    joinQueryUpdate(joinInfo, rowTypeInfo.getFieldNames());
}
 
Example #8
Source File: HiveTableUtil.java    From flink with Apache License 2.0 5 votes vote down vote up
public static Table alterTableViaCatalogBaseTable(
		ObjectPath tablePath, CatalogBaseTable baseTable, Table oldHiveTable, HiveConf hiveConf) {
	Table newHiveTable = instantiateHiveTable(tablePath, baseTable, hiveConf);
	// client.alter_table() requires a valid location
	// thus, if new table doesn't have that, it reuses location of the old table
	if (!newHiveTable.getSd().isSetLocation()) {
		newHiveTable.getSd().setLocation(oldHiveTable.getSd().getLocation());
	}
	return newHiveTable;
}
 
Example #9
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 #10
Source File: TableEnvHiveConnectorITCase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testPKConstraint() throws Exception {
	// While PK constraints are supported since Hive 2.1.0, the constraints cannot be RELY in 2.x versions.
	// So let's only test for 3.x.
	Assume.assumeTrue(HiveVersionTestUtil.HIVE_310_OR_LATER);
	TableEnvironment tableEnv = getTableEnvWithHiveCatalog();
	tableEnv.executeSql("create database db1");
	try {
		// test rely PK constraints
		tableEnv.executeSql("create table db1.tbl1 (x tinyint,y smallint,z int, primary key (x,z) disable novalidate rely)");
		CatalogBaseTable catalogTable = hiveCatalog.getTable(new ObjectPath("db1", "tbl1"));
		TableSchema tableSchema = catalogTable.getSchema();
		assertTrue(tableSchema.getPrimaryKey().isPresent());
		UniqueConstraint pk = tableSchema.getPrimaryKey().get();
		assertEquals(2, pk.getColumns().size());
		assertTrue(pk.getColumns().containsAll(Arrays.asList("x", "z")));

		// test norely PK constraints
		tableEnv.executeSql("create table db1.tbl2 (x tinyint,y smallint, primary key (x) disable norely)");
		catalogTable = hiveCatalog.getTable(new ObjectPath("db1", "tbl2"));
		tableSchema = catalogTable.getSchema();
		assertFalse(tableSchema.getPrimaryKey().isPresent());

		// test table w/o PK
		tableEnv.executeSql("create table db1.tbl3 (x tinyint)");
		catalogTable = hiveCatalog.getTable(new ObjectPath("db1", "tbl3"));
		tableSchema = catalogTable.getSchema();
		assertFalse(tableSchema.getPrimaryKey().isPresent());
	} finally {
		tableEnv.executeSql("drop database db1 cascade");
	}
}
 
Example #11
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void createTemporaryTable(String path, CatalogBaseTable table) {
	UnresolvedIdentifier unresolvedIdentifier = parser.parseIdentifier(path);
	ObjectIdentifier objectIdentifier = catalogManager.qualifyIdentifier(
		unresolvedIdentifier);
	catalogManager.createTemporaryTable(table, objectIdentifier, false);
}
 
Example #12
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private void createTemporaryView(UnresolvedIdentifier identifier, Table view) {
	if (((TableImpl) view).getTableEnvironment() != this) {
		throw new TableException(
				"Only table API objects that belong to this TableEnvironment can be registered.");
	}

	ObjectIdentifier tableIdentifier = catalogManager.qualifyIdentifier(identifier);
	QueryOperation queryOperation = qualifyQueryOperation(tableIdentifier, view.getQueryOperation());
	CatalogBaseTable tableTable = new QueryOperationCatalogView(queryOperation);

	catalogManager.createTemporaryTable(tableTable, tableIdentifier, false);
}
 
Example #13
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void registerTableSourceInternal(String name, TableSource<?> tableSource) {
	validateTableSource(tableSource);
	ObjectIdentifier objectIdentifier = catalogManager.qualifyIdentifier(UnresolvedIdentifier.of(name));
	Optional<CatalogBaseTable> table = getTemporaryTable(objectIdentifier);

	if (table.isPresent()) {
		if (table.get() instanceof ConnectorCatalogTable<?, ?>) {
			ConnectorCatalogTable<?, ?> sourceSinkTable = (ConnectorCatalogTable<?, ?>) table.get();
			if (sourceSinkTable.getTableSource().isPresent()) {
				throw new ValidationException(String.format(
					"Table '%s' already exists. Please choose a different name.", name));
			} else {
				// wrapper contains only sink (not source)
				ConnectorCatalogTable sourceAndSink = ConnectorCatalogTable.sourceAndSink(
					tableSource,
					sourceSinkTable.getTableSink().get(),
					!IS_STREAM_TABLE);
				catalogManager.dropTemporaryTable(objectIdentifier, false);
				catalogManager.createTemporaryTable(sourceAndSink, objectIdentifier, false);
			}
		} else {
			throw new ValidationException(String.format(
				"Table '%s' already exists. Please choose a different name.", name));
		}
	} else {
		ConnectorCatalogTable source = ConnectorCatalogTable.source(tableSource, !IS_STREAM_TABLE);
		catalogManager.createTemporaryTable(source, objectIdentifier, false);
	}
}
 
Example #14
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void registerTableSinkInternal(String name, TableSink<?> tableSink) {
	ObjectIdentifier objectIdentifier = catalogManager.qualifyIdentifier(UnresolvedIdentifier.of(name));
	Optional<CatalogBaseTable> table = getTemporaryTable(objectIdentifier);

	if (table.isPresent()) {
		if (table.get() instanceof ConnectorCatalogTable<?, ?>) {
			ConnectorCatalogTable<?, ?> sourceSinkTable = (ConnectorCatalogTable<?, ?>) table.get();
			if (sourceSinkTable.getTableSink().isPresent()) {
				throw new ValidationException(String.format(
					"Table '%s' already exists. Please choose a different name.", name));
			} else {
				// wrapper contains only sink (not source)
				ConnectorCatalogTable sourceAndSink = ConnectorCatalogTable
					.sourceAndSink(sourceSinkTable.getTableSource().get(), tableSink, !IS_STREAM_TABLE);
				catalogManager.dropTemporaryTable(objectIdentifier, false);
				catalogManager.createTemporaryTable(sourceAndSink, objectIdentifier, false);
			}
		} else {
			throw new ValidationException(String.format(
				"Table '%s' already exists. Please choose a different name.", name));
		}
	} else {
		ConnectorCatalogTable sink = ConnectorCatalogTable.sink(tableSink, !IS_STREAM_TABLE);
		catalogManager.createTemporaryTable(sink, objectIdentifier, false);
	}
}
 
Example #15
Source File: KuduCatalog.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@Override
public void createTable(ObjectPath tablePath, CatalogBaseTable table, boolean ignoreIfExists) throws TableAlreadyExistException {
    Map<String, String> tableProperties = table.getProperties();
    TableSchema tableSchema = table.getSchema();

    Set<String> optionalProperties = new HashSet<>(Arrays.asList(KUDU_REPLICAS));
    Set<String> requiredProperties = new HashSet<>(Arrays.asList(KUDU_HASH_COLS));

    if (!tableSchema.getPrimaryKey().isPresent()) {
        requiredProperties.add(KUDU_PRIMARY_KEY_COLS);
    }

    if (!tableProperties.keySet().containsAll(requiredProperties)) {
        throw new CatalogException("Missing required property. The following properties must be provided: " +
                requiredProperties.toString());
    }

    Set<String> permittedProperties = Sets.union(requiredProperties, optionalProperties);
    if (!permittedProperties.containsAll(tableProperties.keySet())) {
        throw new CatalogException("Unpermitted properties were given. The following properties are allowed:" +
                permittedProperties.toString());
    }

    String tableName = tablePath.getObjectName();

    KuduTableInfo tableInfo = KuduTableUtils.createTableInfo(tableName, tableSchema, tableProperties);

    createTable(tableInfo, ignoreIfExists);
}
 
Example #16
Source File: TableEnvironmentTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnect() {
	final TableEnvironmentMock tableEnv = TableEnvironmentMock.getStreamingInstance();

	tableEnv
		.connect(new ConnectorDescriptorMock(TableSourceFactoryMock.CONNECTOR_TYPE_VALUE, 1, true))
		.withFormat(new FormatDescriptorMock("my_format", 1))
		.withSchema(new Schema()
			.field("my_field_0", "INT")
			.field("my_field_1", "BOOLEAN")
			.field("my_part_1", "BIGINT")
			.field("my_part_2", "STRING"))
		.withPartitionKeys(Arrays.asList("my_part_1", "my_part_2"))
		.inAppendMode()
		.createTemporaryTable("my_table");

	CatalogManager.TableLookupResult lookupResult = tableEnv.catalogManager.getTable(ObjectIdentifier.of(
		EnvironmentSettings.DEFAULT_BUILTIN_CATALOG,
		EnvironmentSettings.DEFAULT_BUILTIN_DATABASE,
		"my_table"))
		.orElseThrow(AssertionError::new);

	assertThat(lookupResult.isTemporary(), equalTo(true));

	CatalogBaseTable catalogBaseTable = lookupResult.getTable();
	assertTrue(catalogBaseTable instanceof CatalogTable);
	CatalogTable table = (CatalogTable) catalogBaseTable;
	assertCatalogTable(table);
	assertCatalogTable(CatalogTableImpl.fromProperties(table.toProperties()));
}
 
Example #17
Source File: TableEnvironmentTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnect() throws Exception {
	final TableEnvironmentMock tableEnv = TableEnvironmentMock.getStreamingInstance();

	tableEnv
		.connect(new ConnectorDescriptorMock(TableSourceFactoryMock.CONNECTOR_TYPE_VALUE, 1, true))
		.withFormat(new FormatDescriptorMock("my_format", 1))
		.withSchema(new Schema()
			.field("my_field_0", "INT")
			.field("my_field_1", "BOOLEAN"))
		.inAppendMode()
		.registerTableSource("my_table");

	final Catalog catalog = tableEnv.getCatalog(EnvironmentSettings.DEFAULT_BUILTIN_CATALOG)
		.orElseThrow(AssertionError::new);

	final CatalogBaseTable table = catalog
		.getTable(new ObjectPath(EnvironmentSettings.DEFAULT_BUILTIN_DATABASE, "my_table"));

	assertThat(
		table.getSchema(),
		equalTo(
			TableSchema.builder()
				.field("my_field_0", DataTypes.INT())
				.field("my_field_1", DataTypes.BOOLEAN())
				.build()));

	final ConnectorCatalogTable<?, ?> connectorCatalogTable = (ConnectorCatalogTable<?, ?>) table;

	assertThat(
		connectorCatalogTable.getTableSource().isPresent(),
		equalTo(true));
}
 
Example #18
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private void replaceTableInternal(String name, CatalogBaseTable table) {
	try {
		ObjectPath path = new ObjectPath(catalogManager.getBuiltInDatabaseName(), name);
		Optional<Catalog> catalog = catalogManager.getCatalog(catalogManager.getBuiltInCatalogName());
		if (catalog.isPresent()) {
			catalog.get().alterTable(
				path,
				table,
				false);
		}
	} catch (Exception e) {
		throw new TableException("Could not register table", e);
	}
}
 
Example #19
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
protected void registerTableInternal(String name, CatalogBaseTable table) {
	try {
		checkValidTableName(name);
		ObjectPath path = new ObjectPath(catalogManager.getBuiltInDatabaseName(), name);
		Optional<Catalog> catalog = catalogManager.getCatalog(catalogManager.getBuiltInCatalogName());
		if (catalog.isPresent()) {
			catalog.get().createTable(
				path,
				table,
				false);
		}
	} catch (Exception e) {
		throw new TableException("Could not register table", e);
	}
}
 
Example #20
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void registerTable(String name, Table table) {
	if (((TableImpl) table).getTableEnvironment() != this) {
		throw new TableException(
			"Only tables that belong to this TableEnvironment can be registered.");
	}

	CatalogBaseTable tableTable = new QueryOperationCatalogView(table.getQueryOperation());
	registerTableInternal(name, tableTable);
}
 
Example #21
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 #22
Source File: CatalogSchemaTable.java    From flink with Apache License 2.0 4 votes vote down vote up
public CatalogBaseTable getCatalogTable() {
	return lookupResult.getTable();
}
 
Example #23
Source File: TableEnvironmentImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
private Optional<CatalogBaseTable> getTemporaryTable(ObjectIdentifier identifier) {
	return catalogManager.getTable(identifier)
		.filter(CatalogManager.TableLookupResult::isTemporary)
		.map(CatalogManager.TableLookupResult::getTable);
}
 
Example #24
Source File: PostgresCatalogTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testPrimitiveDataTypes() throws TableNotExistException {
	CatalogBaseTable table = catalog.getTable(new ObjectPath(PostgresCatalog.DEFAULT_DATABASE, TABLE_PRIMITIVE_TYPE));

	assertEquals(getPrimitiveTable().schema, table.getSchema());
}
 
Example #25
Source File: CatalogSchemaTable.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
	final FlinkTypeFactory flinkTypeFactory = (FlinkTypeFactory) typeFactory;
	TableSchema tableSchema = lookupResult.getResolvedSchema();
	final DataType[] fieldDataTypes = tableSchema.getFieldDataTypes();
	CatalogBaseTable catalogTable = lookupResult.getTable();
	if (!isStreamingMode
			&& catalogTable instanceof ConnectorCatalogTable
			&& ((ConnectorCatalogTable<?, ?>) catalogTable).getTableSource().isPresent()) {
		// If the table source is bounded, materialize the time attributes to normal TIMESTAMP type.
		// Now for ConnectorCatalogTable, there is no way to
		// deduce if it is bounded in the table environment, so the data types in TableSchema
		// always patched with TimeAttribute.
		// See ConnectorCatalogTable#calculateSourceSchema
		// for details.

		// Remove the patched time attributes type to let the TableSourceTable handle it.
		// We should remove this logic if the isBatch flag in ConnectorCatalogTable is fixed.
		// TODO: Fix FLINK-14844.
		for (int i = 0; i < fieldDataTypes.length; i++) {
			LogicalType lt = fieldDataTypes[i].getLogicalType();
			if (lt instanceof TimestampType
				&& (((TimestampType) lt).getKind() == TimestampKind.PROCTIME
				|| ((TimestampType) lt).getKind() == TimestampKind.ROWTIME)) {
				int precision = ((TimestampType) lt).getPrecision();
				fieldDataTypes[i] = DataTypes.TIMESTAMP(precision);
			}
		}
	}

	// The following block is a workaround to support tables defined by TableEnvironment.connect() and
	// the actual table sources implement DefinedProctimeAttribute/DefinedRowtimeAttributes.
	// It should be removed after we remove DefinedProctimeAttribute/DefinedRowtimeAttributes.
	Optional<TableSource<?>> sourceOpt = findAndCreateTableSource();
	if (isStreamingMode
		&& tableSchema.getTableColumns().stream().noneMatch(TableColumn::isGenerated)
		&& tableSchema.getWatermarkSpecs().isEmpty()
		&& sourceOpt.isPresent()) {
		TableSource<?> source = sourceOpt.get();
		if (TableSourceValidation.hasProctimeAttribute(source)
				|| TableSourceValidation.hasRowtimeAttribute(source)) {
			// If the table is defined by TableEnvironment.connect(), and use the legacy proctime and rowtime
			// descriptors, the TableSchema should fallback to ConnectorCatalogTable#calculateSourceSchema
			tableSchema = ConnectorCatalogTable.calculateSourceSchema(source, false);
		}
	}

	return TableSourceUtil.getSourceRowType(
		flinkTypeFactory,
		tableSchema,
		scala.Option.empty(),
		isStreamingMode);
}
 
Example #26
Source File: FactoryUtilTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public CatalogBaseTable copy() {
	return null;
}
 
Example #27
Source File: FlinkCalciteCatalogReader.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Translate this {@link CatalogSchemaTable} into Flink source table.
 */
private static FlinkPreparingTableBase toPreparingTable(
		RelOptSchema relOptSchema,
		List<String> names,
		RelDataType rowType,
		CatalogSchemaTable schemaTable) {
	final CatalogBaseTable baseTable = schemaTable.getCatalogTable();
	if (baseTable instanceof QueryOperationCatalogView) {
		return convertQueryOperationView(relOptSchema,
			names,
			rowType,
			(QueryOperationCatalogView) baseTable);
	} else if (baseTable instanceof ConnectorCatalogTable) {
		ConnectorCatalogTable<?, ?> connectorTable = (ConnectorCatalogTable<?, ?>) baseTable;
		if ((connectorTable).getTableSource().isPresent()) {
			return convertSourceTable(relOptSchema,
				rowType,
				schemaTable.getTableIdentifier(),
				connectorTable,
				schemaTable.getStatistic(),
				schemaTable.isStreamingMode());
		} else {
			throw new ValidationException("Cannot convert a connector table " +
				"without source.");
		}
	} else if (baseTable instanceof CatalogView) {
		return convertCatalogView(
			relOptSchema,
			names,
			rowType,
			schemaTable.getStatistic(),
			(CatalogView) baseTable);
	} else if (baseTable instanceof CatalogTable) {
		return convertCatalogTable(relOptSchema,
			names,
			rowType,
			(CatalogTable) baseTable,
			schemaTable);
	} else {
		throw new ValidationException("Unsupported table type: " + baseTable);
	}
}
 
Example #28
Source File: HiveTableUtil.java    From flink with Apache License 2.0 4 votes vote down vote up
public static Table instantiateHiveTable(ObjectPath tablePath, CatalogBaseTable table, HiveConf hiveConf) {
	if (!(table instanceof CatalogTableImpl) && !(table instanceof CatalogViewImpl)) {
		throw new CatalogException(
				"HiveCatalog only supports CatalogTableImpl and CatalogViewImpl");
	}
	// let Hive set default parameters for us, e.g. serialization.format
	Table hiveTable = org.apache.hadoop.hive.ql.metadata.Table.getEmptyTable(tablePath.getDatabaseName(),
			tablePath.getObjectName());
	hiveTable.setCreateTime((int) (System.currentTimeMillis() / 1000));

	Map<String, String> properties = new HashMap<>(table.getProperties());
	// Table comment
	if (table.getComment() != null) {
		properties.put(HiveCatalogConfig.COMMENT, table.getComment());
	}

	boolean isGeneric = HiveCatalog.isGenericForCreate(properties);

	// Hive table's StorageDescriptor
	StorageDescriptor sd = hiveTable.getSd();
	HiveTableUtil.setDefaultStorageFormat(sd, hiveConf);

	if (isGeneric) {
		DescriptorProperties tableSchemaProps = new DescriptorProperties(true);
		tableSchemaProps.putTableSchema(Schema.SCHEMA, table.getSchema());

		if (table instanceof CatalogTable) {
			tableSchemaProps.putPartitionKeys(((CatalogTable) table).getPartitionKeys());
		}

		properties.putAll(tableSchemaProps.asMap());
		properties = maskFlinkProperties(properties);
		hiveTable.setParameters(properties);
	} else {
		HiveTableUtil.initiateTableFromProperties(hiveTable, properties, hiveConf);
		List<FieldSchema> allColumns = HiveTableUtil.createHiveColumns(table.getSchema());
		// Table columns and partition keys
		if (table instanceof CatalogTableImpl) {
			CatalogTable catalogTable = (CatalogTableImpl) table;

			if (catalogTable.isPartitioned()) {
				int partitionKeySize = catalogTable.getPartitionKeys().size();
				List<FieldSchema> regularColumns = allColumns.subList(0, allColumns.size() - partitionKeySize);
				List<FieldSchema> partitionColumns = allColumns.subList(allColumns.size() - partitionKeySize, allColumns.size());

				sd.setCols(regularColumns);
				hiveTable.setPartitionKeys(partitionColumns);
			} else {
				sd.setCols(allColumns);
				hiveTable.setPartitionKeys(new ArrayList<>());
			}
		} else {
			sd.setCols(allColumns);
		}
		// Table properties
		hiveTable.getParameters().putAll(properties);
	}

	if (table instanceof CatalogViewImpl) {
		// TODO: [FLINK-12398] Support partitioned view in catalog API
		hiveTable.setPartitionKeys(new ArrayList<>());

		CatalogView view = (CatalogView) table;
		hiveTable.setViewOriginalText(view.getOriginalQuery());
		hiveTable.setViewExpandedText(view.getExpandedQuery());
		hiveTable.setTableType(TableType.VIRTUAL_VIEW.name());
	}

	return hiveTable;
}
 
Example #29
Source File: JavaCatalogTableTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public CatalogBaseTable copy() {
	return this;
}
 
Example #30
Source File: JdbcCatalog.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public CatalogBaseTable getTable(ObjectPath tablePath) throws TableNotExistException, CatalogException {
	return internal.getTable(tablePath);
}