|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pinot.segment.local.utils; |
| 20 | + |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.LinkedHashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import org.apache.pinot.segment.local.function.FunctionEvaluator; |
| 29 | +import org.apache.pinot.segment.local.function.FunctionEvaluatorFactory; |
| 30 | +import org.apache.pinot.spi.config.table.TableConfig; |
| 31 | +import org.apache.pinot.spi.config.table.ingestion.IngestionConfig; |
| 32 | +import org.apache.pinot.spi.config.table.ingestion.TransformConfig; |
| 33 | +import org.apache.pinot.spi.data.FieldSpec; |
| 34 | +import org.apache.pinot.spi.data.Schema; |
| 35 | + |
| 36 | + |
| 37 | +public class ExpressionTransformerUtils { |
| 38 | + |
| 39 | + private ExpressionTransformerUtils() { |
| 40 | + // Utility class - prevent instantiation |
| 41 | + } |
| 42 | + |
| 43 | + public static LinkedHashMap<String, FunctionEvaluator> getTopologicallySortedExpressions( |
| 44 | + TableConfig tableConfig, Schema schema) { |
| 45 | + LinkedHashMap<String, FunctionEvaluator> sortedEvaluators = new LinkedHashMap<>(); |
| 46 | + |
| 47 | + Map<String, FunctionEvaluator> expressionEvaluators = new HashMap<>(); |
| 48 | + IngestionConfig ingestionConfig = tableConfig.getIngestionConfig(); |
| 49 | + if (ingestionConfig != null && ingestionConfig.getTransformConfigs() != null) { |
| 50 | + for (TransformConfig transformConfig : ingestionConfig.getTransformConfigs()) { |
| 51 | + FunctionEvaluator previous = expressionEvaluators.put(transformConfig.getColumnName(), |
| 52 | + FunctionEvaluatorFactory.getExpressionEvaluator(transformConfig.getTransformFunction())); |
| 53 | + Preconditions.checkState(previous == null, |
| 54 | + "Cannot set more than one ingestion transform function on column: %s.", transformConfig.getColumnName()); |
| 55 | + } |
| 56 | + } |
| 57 | + for (FieldSpec fieldSpec : schema.getAllFieldSpecs()) { |
| 58 | + String fieldName = fieldSpec.getName(); |
| 59 | + if (!fieldSpec.isVirtualColumn() && !expressionEvaluators.containsKey(fieldName)) { |
| 60 | + FunctionEvaluator functionEvaluator = FunctionEvaluatorFactory.getExpressionEvaluator(fieldSpec); |
| 61 | + if (functionEvaluator != null) { |
| 62 | + expressionEvaluators.put(fieldName, functionEvaluator); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // Carry out DFS traversal to topologically sort column names based on transform function dependencies. Throw |
| 68 | + // exception if a cycle is discovered. When a name is first seen it is added to discoveredNames set. When a name |
| 69 | + // is completely processed (i.e the name and all of its dependencies have been fully explored and no cycles have |
| 70 | + // been seen), it gets added to the sortedEvaluators list in topologically sorted order. Fully explored |
| 71 | + // names are removed from discoveredNames set. |
| 72 | + Set<String> discoveredNames = new HashSet<>(); |
| 73 | + for (Map.Entry<String, FunctionEvaluator> entry : expressionEvaluators.entrySet()) { |
| 74 | + String columnName = entry.getKey(); |
| 75 | + if (!sortedEvaluators.containsKey(columnName)) { |
| 76 | + topologicalSort(columnName, expressionEvaluators, sortedEvaluators, discoveredNames); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return sortedEvaluators; |
| 81 | + } |
| 82 | + |
| 83 | + private static void topologicalSort(String column, Map<String, FunctionEvaluator> allEvaluators, |
| 84 | + Map<String, FunctionEvaluator> sortedEvaluators, |
| 85 | + Set<String> discoveredNames) { |
| 86 | + FunctionEvaluator functionEvaluator = allEvaluators.get(column); |
| 87 | + if (functionEvaluator == null) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if (discoveredNames.add(column)) { |
| 92 | + List<String> arguments = functionEvaluator.getArguments(); |
| 93 | + for (String arg : arguments) { |
| 94 | + if (!sortedEvaluators.containsKey(arg)) { |
| 95 | + topologicalSort(arg, allEvaluators, sortedEvaluators, discoveredNames); |
| 96 | + } |
| 97 | + } |
| 98 | + sortedEvaluators.put(column, functionEvaluator); |
| 99 | + discoveredNames.remove(column); |
| 100 | + } else { |
| 101 | + throw new IllegalStateException( |
| 102 | + "Expression cycle found for column '" + column + "' in Ingestion Transform " + "Function definitions."); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments