Jackie-Jiang commented on a change in pull request #5238: Evaluate schema 
transform expressions during ingestion
URL: https://github.com/apache/incubator-pinot/pull/5238#discussion_r408477289
 
 

 ##########
 File path: 
pinot-spi/src/main/java/org/apache/pinot/spi/utils/SchemaFieldExtractorUtils.java
 ##########
 @@ -0,0 +1,105 @@
+/**
+ * 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.pinot.spi.utils;
+
+import com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.apache.pinot.spi.data.FieldSpec;
+import org.apache.pinot.spi.data.Schema;
+import org.apache.pinot.spi.data.TimeFieldSpec;
+import org.apache.pinot.spi.data.TimeGranularitySpec;
+import org.apache.pinot.spi.data.function.evaluators.ExpressionEvaluator;
+import 
org.apache.pinot.spi.data.function.evaluators.ExpressionEvaluatorFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Extracts names of the source fields from the schema
+ */
+public class SchemaFieldExtractorUtils {
+  public static final String MAP_KEY_COLUMN_SUFFIX = "__KEYS";
+  public static final String MAP_VALUE_COLUMN_SUFFIX = "__VALUES";
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(SchemaFieldExtractorUtils.class);
+
+  /**
+   * Extracts the source fields from the schema
+   * For field specs with a transform expression defined, use the arguments 
provided to the function
+   * Otherwise, use the column name as is
+   * TODO: for now, we assume that arguments to transform function are in the 
source i.e. there's no columns which are derived from transformed columns
+   */
+  public static List<String> extract(Schema schema) {
+    Set<String> sourceFieldNames = new HashSet<>();
+    for (FieldSpec fieldSpec : schema.getAllFieldSpecs()) {
+      if (!fieldSpec.isVirtualColumn()) {
+        ExpressionEvaluator expressionEvaluator = 
ExpressionEvaluatorFactory.getExpressionEvaluator(fieldSpec);
+
+        if (expressionEvaluator != null) {
+          sourceFieldNames.addAll(expressionEvaluator.getArguments());
+        } else {
+          sourceFieldNames.add(fieldSpec.getName());
+        }
+      }
+    }
+    return new ArrayList<>(sourceFieldNames);
+  }
+
+  /**
+   * Validates that for a field spec with transform function, the source 
column name and destination column name are exclusive
+   * i.e. do not allow using source column name for destination column
+   * 1. Transform function of a field spec should not use the destination 
column
+   * 2. TimeFieldSpec - cannot have same name for incoming and outgoing field 
spec, if the specs are different
+   */
+  public static boolean validate(Schema schema) {
+    for (FieldSpec fieldSpec : schema.getAllFieldSpecs()) {
+      if (!fieldSpec.isVirtualColumn()) {
+        String column = fieldSpec.getName();
+        String transformFunction = fieldSpec.getTransformFunction();
+        if (transformFunction != null) {
+          ExpressionEvaluator expressionEvaluator = 
ExpressionEvaluatorFactory.getExpressionEvaluator(fieldSpec);
+          if (expressionEvaluator != null) {
+            List<String> arguments = expressionEvaluator.getArguments();
+            // output column used as input
+            if (arguments.contains(column)) {
+              LOGGER.error("The arguments of transform function: {}, should 
not contain the destination column: {}",
+                  transformFunction, column);
+              return false;
+            }
+          }
+        } else if (fieldSpec.getFieldType().equals(FieldSpec.FieldType.TIME)) {
+          TimeFieldSpec timeFieldSpec = (TimeFieldSpec) fieldSpec;
+          TimeGranularitySpec incomingGranularitySpec = 
timeFieldSpec.getIncomingGranularitySpec();
+          TimeGranularitySpec outgoingGranularitySpec = 
timeFieldSpec.getOutgoingGranularitySpec();
+          // different incoming and outgoing spec, but same name
+          if (outgoingGranularitySpec != null && 
!incomingGranularitySpec.equals(outgoingGranularitySpec)
 
 Review comment:
   (nit) outgoingGranularitySpec will never be null because it will return 
incoming spec if outgoing is not set

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to