jeqo commented on code in PR #12637: URL: https://github.com/apache/kafka/pull/12637#discussion_r1167632114
########## connect/transforms/src/main/java/org/apache/kafka/connect/transforms/field/MultiFieldPaths.java: ########## @@ -0,0 +1,578 @@ +/* + * 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.kafka.connect.transforms.field; + +import org.apache.kafka.connect.data.Field; +import org.apache.kafka.connect.data.Schema; +import org.apache.kafka.connect.data.Schema.Type; +import org.apache.kafka.connect.data.SchemaBuilder; +import org.apache.kafka.connect.data.Struct; +import org.apache.kafka.connect.transforms.util.SchemaUtil; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Multiple field paths to access data objects ({@code Struct} or {@code Map}) efficiently, + * instead of using {@see SingleFieldPath} individually. + * <p> + * If the SMT requires accessing a single field on the same data object, + * use {@code FieldPath} instead. + * <p> + * Invariants: + * <li> + * <ul>Tree values contain either a nested tree or a field path</ul> + * <ul>A tree cannot contain paths that are a subset of other paths (e.g. foo and foo.bar in V2 should collide and fail)</ul> + * </li> + * + * See KIP-821. + * + * @see SingleFieldPath + * @see FieldSyntaxVersion + */ +public class MultiFieldPaths implements FieldPath { + + final Map<String, Object> pathTree; + final List<SingleFieldPath> paths; + + MultiFieldPaths(List<SingleFieldPath> paths) { + this.paths = paths.stream().filter(Objects::nonNull).collect(Collectors.toList()); + pathTree = buildPathTree(this.paths, 0, new HashMap<>()); + } + + public static Builder newBuilder(FieldSyntaxVersion syntaxVersion) { + return new Builder(syntaxVersion); + } + + public static MultiFieldPaths of(SingleFieldPath path) { + return new MultiFieldPaths(Collections.singletonList(path)); + } + + public static MultiFieldPaths of(SingleFieldPath... paths) { + return new MultiFieldPaths(Arrays.asList(paths)); + } + + public static MultiFieldPaths of(List<SingleFieldPath> paths) { + return new MultiFieldPaths(paths); + } + + public static MultiFieldPaths of(Set<String> fields, FieldSyntaxVersion syntaxVersion) { + return new MultiFieldPaths(fields.stream() + .map(f -> SingleFieldPath.of(f, syntaxVersion)) + .collect(Collectors.toList())); + } + + public static MultiFieldPaths of(List<String> fields, FieldSyntaxVersion syntaxVersion) { + return new MultiFieldPaths(fields.stream() + .map(f -> SingleFieldPath.of(f, syntaxVersion)) + .collect(Collectors.toList())); + } + + Map<String, Object> buildPathTree(List<SingleFieldPath> paths, int stepIdx, Map<String, Object> pathTree) { + if (paths.size() == 1) { // optimize for paths with a single member + SingleFieldPath path = paths.get(0); + if (path != null) { + if (path.stepAt(stepIdx + 1) == null) { // if last path step + pathTree.put(path.stepAt(stepIdx), path); + } else { + pathTree.put(path.stepAt(stepIdx), + buildPathTree(paths, stepIdx + 1, new HashMap<>())); + } + } + } else { + // group paths by prefix + final Map<String, List<SingleFieldPath>> groups = new HashMap<>(); + for (SingleFieldPath path : paths) { + if (path != null) { + String step = path.stepAt(stepIdx); + if (step != null) { + groups.computeIfPresent(step, (s, fieldPaths) -> { + for (SingleFieldPath other : fieldPaths) { + // avoid overlapping paths + if (!path.equals(other) + && (other.stepAt(stepIdx + 1) == null + || path.stepAt(stepIdx + 1) == null)) { + throw new IllegalArgumentException( + "Path " + other + " and " + path + " are overlapping. " + + "Paths need to point to leaf values"); + } + } + if (!fieldPaths.contains(path)) { + fieldPaths.add(path); + } + return fieldPaths; + }); + groups.computeIfAbsent(step, s -> { + List<SingleFieldPath> fieldPaths = new ArrayList<>(); + fieldPaths.add(path); + return fieldPaths; + }); + } + } + } Review Comment: I like this! I was trying to be cautious of users adding overlapping paths. But by using Sets we could conflate them and only use the children paths. Giving a try now. -- 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. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org