wuchong commented on a change in pull request #13957: URL: https://github.com/apache/flink/pull/13957#discussion_r519108294
########## File path: flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/filesystem/FileSystemTableSink.java ########## @@ -251,6 +258,14 @@ private RowDataPartitionComputer partitionComputer() { //noinspection unchecked return Optional.of(FileInputFormatCompactReader.factory((FileInputFormat<RowData>) format)); } + } else if (deserializationFormat != null) { + // NOTE, we need pass full format types to deserializationFormat + DeserializationSchema<RowData> decoder = deserializationFormat.createRuntimeDecoder( + createSourceContext(context), getFormatDataType()); + int[] projectedFields = IntStream.of(0, schema.getFieldCount()).toArray(); Review comment: `IntStream.range`? ########## File path: flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/filesystem/DeserializationSchemaAdapter.java ########## @@ -0,0 +1,316 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.filesystem; + +import org.apache.flink.api.common.io.DelimitedInputFormat; +import org.apache.flink.api.common.serialization.DeserializationSchema; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.connector.file.src.FileSourceSplit; +import org.apache.flink.connector.file.src.reader.BulkFormat; +import org.apache.flink.connector.file.src.util.ArrayResultIterator; +import org.apache.flink.core.fs.FileInputSplit; +import org.apache.flink.core.fs.Path; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.table.api.TableSchema; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.runtime.typeutils.InternalTypeInfo; +import org.apache.flink.table.types.DataType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.utils.PartitionPathUtils; +import org.apache.flink.util.Collector; +import org.apache.flink.util.InstantiationUtil; +import org.apache.flink.util.UserCodeClassLoader; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.List; +import java.util.Queue; +import java.util.stream.Collectors; + +import static org.apache.flink.connector.file.src.util.CheckpointedPosition.NO_OFFSET; +import static org.apache.flink.table.data.vector.VectorizedColumnBatch.DEFAULT_SIZE; + +/** + * Adapter to turn a {@link DeserializationSchema} into a {@link BulkFormat}. + */ +public class DeserializationSchemaAdapter implements BulkFormat<RowData, FileSourceSplit> { + + private static final int BATCH_SIZE = 100; + + // NOTE, deserializationSchema produce full format fields with original order + private final DeserializationSchema<RowData> deserializationSchema; + + private final String[] fieldNames; + private final DataType[] fieldTypes; + private final int[] projectFields; + private final RowType projectedRowType; + + private final List<String> partitionKeys; + private final String defaultPartValue; + + private final int[] toProjectedField; + private final RowData.FieldGetter[] formatFieldGetters; + + public DeserializationSchemaAdapter( + DeserializationSchema<RowData> deserializationSchema, + TableSchema schema, + int[] projectFields, + List<String> partitionKeys, + String defaultPartValue) { + this.deserializationSchema = deserializationSchema; + this.fieldNames = schema.getFieldNames(); + this.fieldTypes = schema.getFieldDataTypes(); + this.projectFields = projectFields; + this.partitionKeys = partitionKeys; + this.defaultPartValue = defaultPartValue; + + List<String> projectedNames = Arrays.stream(projectFields) + .mapToObj(idx -> schema.getFieldNames()[idx]) + .collect(Collectors.toList()); + + this.projectedRowType = RowType.of( + Arrays.stream(projectFields).mapToObj(idx -> + schema.getFieldDataTypes()[idx].getLogicalType()).toArray(LogicalType[]::new), + projectedNames.toArray(new String[0])); + + List<String> formatFields = Arrays.stream(schema.getFieldNames()) + .filter(field -> !partitionKeys.contains(field)) + .collect(Collectors.toList()); + + List<String> formatProjectedFields = projectedNames.stream() + .filter(field -> !partitionKeys.contains(field)) + .collect(Collectors.toList()); + + this.toProjectedField = formatProjectedFields.stream() + .mapToInt(projectedNames::indexOf) + .toArray(); + + this.formatFieldGetters = new RowData.FieldGetter[formatProjectedFields.size()]; + for (int i = 0; i < formatProjectedFields.size(); i++) { + String name = formatProjectedFields.get(i); + this.formatFieldGetters[i] = RowData.createFieldGetter( + schema.getFieldDataType(name).get().getLogicalType(), + formatFields.indexOf(name)); + } + } + + private DeserializationSchema<RowData> createDeserialization() throws IOException { + try { + DeserializationSchema<RowData> deserialization = InstantiationUtil.clone(deserializationSchema); + deserialization.open(new DeserializationSchema.InitializationContext() { + @Override + public MetricGroup getMetricGroup() { + throw new UnsupportedOperationException("MetricGroup is unsupported in BulkFormat."); + } + + @Override + public UserCodeClassLoader getUserCodeClassLoader() { + return (UserCodeClassLoader) Thread.currentThread().getContextClassLoader(); + } + }); + return deserialization; + } catch (Exception e) { + throw new IOException(e); + } + } + + @Override + public Reader createReader(Configuration config, FileSourceSplit split) throws IOException { + return new Reader(config, split); + } + + @Override + public Reader restoreReader(Configuration config, FileSourceSplit split) throws IOException { + Reader reader = new Reader(config, split); + reader.seek(split.getReaderPosition().get().getRecordsAfterOffset()); + return null; Review comment: return reader? ########## File path: flink-formats/flink-json/src/test/java/org/apache/flink/formats/json/debezium/DebeziumJsonFileSystemITCase.java ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.formats.json.debezium; + +import org.apache.flink.table.planner.runtime.utils.StreamingTestBase; +import org.apache.flink.types.Row; +import org.apache.flink.util.CloseableIterator; +import org.apache.flink.util.CollectionUtil; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static java.lang.String.format; + +/** + * Test Filesystem connector with DebeziumJson. + */ +public class DebeziumJsonFileSystemITCase extends StreamingTestBase { + + private static final List<String> EXPECTED = Arrays.asList( + "+I(101,scooter,Small 2-wheel scooter,3.14)", + "+I(102,car battery,12V car battery,8.1)", + "+I(103,12-pack drill bits,12-pack of drill bits with sizes ranging from #40 to #3,0.8)", + "+I(104,hammer,12oz carpenter's hammer,0.75)", + "+I(105,hammer,14oz carpenter's hammer,0.875)", + "+I(106,hammer,16oz carpenter's hammer,1.0)", + "+I(107,rocks,box of assorted rocks,5.3)", + "+I(108,jacket,water resistent black wind breaker,0.1)", + "+I(109,spare tire,24 inch spare tire,22.2)", + "-D(106,hammer,16oz carpenter's hammer,1.0)", // -U + "+I(106,hammer,18oz carpenter hammer,1.0)", // +U + "-D(107,rocks,box of assorted rocks,5.3)", // -U + "+I(107,rocks,box of assorted rocks,5.1)", // +U + "+I(110,jacket,water resistent white wind breaker,0.2)", + "+I(111,scooter,Big 2-wheel scooter ,5.18)", + "-D(110,jacket,water resistent white wind breaker,0.2)", // -U + "+I(110,jacket,new water resistent white wind breaker,0.5)", // +U + "-D(111,scooter,Big 2-wheel scooter ,5.18)", // -U + "+I(111,scooter,Big 2-wheel scooter ,5.17)", // +U + "-D(111,scooter,Big 2-wheel scooter ,5.17)" + ); + + private File source; + private File sink; + + private void prepareTables(boolean isPartition) throws IOException { + byte[] bytes = readBytes("debezium-data-schema-exclude.txt"); + source = TEMPORARY_FOLDER.newFolder(); + File file; + if (isPartition) { + File partition = new File(source, "p=1"); + partition.mkdirs(); + file = new File(partition, "my_file"); + } else { + file = new File(source, "my_file"); + } + file.createNewFile(); + Files.write(file.toPath(), bytes); + + sink = TEMPORARY_FOLDER.newFolder(); + + env().setParallelism(1); + } + + private void createTable(String name, String path, boolean isPartition) { + tEnv().executeSql(format("create table %s (", name) + + "id int, name string, description string, weight float" + + (isPartition ? ", p int) partitioned by (p) " : ")") + + " with (" + + "'connector'='filesystem'," + + "'format'='debezium-json'," + + format("'path'='%s'", path) + + ")"); + } + + @Test + public void testNonPartition() throws Exception { + prepareTables(false); + createTable("source", source.toURI().toString(), false); + createTable("sink", sink.toURI().toString(), false); + + tEnv().executeSql("insert into sink select * from source").await(); + CloseableIterator<Row> iter = tEnv().executeSql("select * from sink").collect(); Review comment: Select some fields to trigger the projection push down? We can write one more column into the sink, e.g `UPPER(name)`, and then select out the `upper_name` instead of the `name. ########## File path: flink-table/flink-table-runtime-blink/src/test/java/org/apache/flink/table/data/BinaryRowDataTest.java ########## @@ -94,6 +94,10 @@ */ public class BinaryRowDataTest { + public static void main(String[] args) { Review comment: remove. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org