kbendick commented on a change in pull request #4073:
URL: https://github.com/apache/iceberg/pull/4073#discussion_r816481583



##########
File path: core/src/main/java/org/apache/iceberg/CatalogUtil.java
##########
@@ -275,6 +290,73 @@ public static FileIO loadFileIO(
     return fileIO;
   }
 
+  public static void initializeListeners(Map<String, String> properties) {
+    Map<String, Map<String, String>> propertiesSummary = Maps.newHashMap();
+    Map<String, String> commonProperties = Maps.newHashMap();
+    for (String key : properties.keySet()) {
+      Optional<Pair<String, String>> listenerInfo = 
CatalogProperties.parseListenerCatalogProperty(key);
+      if (listenerInfo.isPresent()) {
+        String name = listenerInfo.get().first();
+        String property = listenerInfo.get().second();
+        propertiesSummary.computeIfAbsent(name, k -> Maps.newHashMap());
+        propertiesSummary.get(name).put(property, properties.get(key));
+      } else {
+        commonProperties.put(key, properties.get(key));
+      }
+    }
+
+    // inherit all common properties during listener initialization
+    propertiesSummary.forEach((k, v) -> v.putAll(commonProperties));
+
+    for (String listenerName : propertiesSummary.keySet()) {
+      Map<String, String> listenerProperties = 
propertiesSummary.get(listenerName);
+      String listenerImpl = 
listenerProperties.get(CatalogProperties.LISTENER_PROPERTY_IMPL);
+      ValidationException.check(listenerImpl != null,
+          "Cannot initialize listener %s, missing %s property",
+          listenerName, CatalogProperties.LISTENER_PROPERTY_IMPL);
+
+      String eventTypesString = 
listenerProperties.get(CatalogProperties.LISTENER_PROPERTY_EVENT_TYPES);
+      Set<Class<?>> eventTypes = eventTypesString != null ? 
Arrays.stream(eventTypesString.split(","))
+          .map(s -> {
+            try {
+              return Class.forName(s);
+            } catch (ClassNotFoundException e) {
+              throw new ValidationException(e, "Cannot find listener event 
type class %s", s);
+            }
+          })
+          .collect(Collectors.toSet()) : 
CatalogProperties.LISTENER_EVENT_TYPES_DEFAULT;
+
+      eventTypes.forEach(t -> 
CatalogUtil.loadAndRegisterListener(listenerImpl, listenerName, t, 
listenerProperties));
+    }
+  }
+
+  @VisibleForTesting
+  static <T> Listener<T> loadAndRegisterListener(
+      String listenerClass,
+      String listenerName,
+      Class<T> eventType,
+      Map<String, String> properties) {
+    DynConstructors.Ctor<Listener<T>> ctor;
+    try {
+      ctor = DynConstructors.builder(Listener.class).impl(listenerClass, 
eventType).buildChecked();
+    } catch (NoSuchMethodException e) {
+      throw new IllegalArgumentException(String.format(
+              "Cannot initialize Listener, missing no-arg constructor: %s", 
listenerClass), e);
+    }
+
+    Listener<T> listener;
+    try {
+      listener = ctor.newInstance();
+    } catch (ClassCastException e) {
+      throw new IllegalArgumentException(String.format(
+          "Cannot initialize Listener, %s does not implement 
org.apache.iceberg.events.Listener", listenerClass), e);

Review comment:
       Question: Is this the only situation that `ClassClassException` can 
really show up in (or is likely to show up in), especially given the use of 
`DynConstructors` and just general Flink inverted class-loader related 
weirdness? Genuine question.

##########
File path: 
core/src/main/java/org/apache/iceberg/expressions/ExpressionParser.java
##########
@@ -0,0 +1,225 @@
+/*
+ * 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.iceberg.expressions;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Set;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.util.JsonUtil;
+
+public class ExpressionParser {
+
+  private static final String TYPE = "type";
+  private static final String VALUE = "value";
+  private static final String OPERATION = "operation";
+  private static final String LITERALS = "literals";
+  private static final String TERM = "term";
+  private static final String LEFT_OPERAND = "left-operand";
+  private static final String RIGHT_OPERAND = "right-operand";
+  private static final String OPERAND = "operand";
+  private static final String AND = "and";
+  private static final String OR = "or";
+  private static final String NOT = "not";
+  private static final String TRUE = "true";
+  private static final String FALSE = "false";
+  private static final String UNBOUNDED_PREDICATE = "unbounded-predicate";
+  private static final String BOUNDED_LITERAL_PREDICATE = 
"bounded-literal-predicate";
+  private static final String BOUNDED_SET_PREDICATE = "bounded-set-predicate";
+  private static final String BOUNDED_UNARY_PREDICATE = 
"bounded-unary-predicate";
+  private static final String NAMED_REFERENCE = "named-reference";
+  private static final String BOUND_REFERENCE = "bound-reference";
+  private static final String ABOVE_MAX = "above-max";
+  private static final String BELOW_MIN = "below-min";
+
+  private static final Set<Expression.Operation> oneInputs = ImmutableSet.of(
+          Expression.Operation.IS_NULL,
+          Expression.Operation.NOT_NULL,
+          Expression.Operation.IS_NAN,
+          Expression.Operation.NOT_NAN);
+
+
+  private ExpressionParser() {
+  }
+
+  public static String toJson(Expression expression, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(expression, generator);
+      generator.flush();
+      return writer.toString();
+

Review comment:
       Nit: Extra newline.

##########
File path: 
core/src/main/java/org/apache/iceberg/expressions/ExpressionParser.java
##########
@@ -0,0 +1,225 @@
+/*
+ * 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.iceberg.expressions;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Set;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.util.JsonUtil;
+
+public class ExpressionParser {
+
+  private static final String TYPE = "type";
+  private static final String VALUE = "value";
+  private static final String OPERATION = "operation";
+  private static final String LITERALS = "literals";
+  private static final String TERM = "term";
+  private static final String LEFT_OPERAND = "left-operand";
+  private static final String RIGHT_OPERAND = "right-operand";
+  private static final String OPERAND = "operand";
+  private static final String AND = "and";
+  private static final String OR = "or";
+  private static final String NOT = "not";
+  private static final String TRUE = "true";
+  private static final String FALSE = "false";
+  private static final String UNBOUNDED_PREDICATE = "unbounded-predicate";
+  private static final String BOUNDED_LITERAL_PREDICATE = 
"bounded-literal-predicate";
+  private static final String BOUNDED_SET_PREDICATE = "bounded-set-predicate";
+  private static final String BOUNDED_UNARY_PREDICATE = 
"bounded-unary-predicate";
+  private static final String NAMED_REFERENCE = "named-reference";
+  private static final String BOUND_REFERENCE = "bound-reference";
+  private static final String ABOVE_MAX = "above-max";
+  private static final String BELOW_MIN = "below-min";
+
+  private static final Set<Expression.Operation> oneInputs = ImmutableSet.of(
+          Expression.Operation.IS_NULL,
+          Expression.Operation.NOT_NULL,
+          Expression.Operation.IS_NAN,
+          Expression.Operation.NOT_NAN);
+
+
+  private ExpressionParser() {
+  }
+
+  public static String toJson(Expression expression, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(expression, generator);
+      generator.flush();
+      return writer.toString();
+
+    } catch (IOException e) {
+      throw new UncheckedIOException("Failed to write json", e);
+    }
+  }
+
+  public static void toJson(Expression expression, JsonGenerator generator) 
throws IOException {
+    if (expression instanceof And) {
+      toJson((And) expression, generator);
+    } else if (expression instanceof Or) {
+      toJson((Or) expression, generator);
+    } else if (expression instanceof Not) {
+      toJson((Not) expression, generator);
+    } else if (expression instanceof True) {
+      toJson((True) expression, generator);
+    } else if (expression instanceof False) {
+      toJson((False) expression, generator);
+    } else if (expression instanceof Predicate) {
+      toJson((Predicate) expression, generator);
+    } else {
+      throw new IllegalArgumentException("Invalid Operation Type");

Review comment:
       What about using switch statement?

##########
File path: 
core/src/main/java/org/apache/iceberg/expressions/ExpressionParser.java
##########
@@ -0,0 +1,225 @@
+/*
+ * 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.iceberg.expressions;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Set;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet;
+import org.apache.iceberg.util.JsonUtil;
+
+public class ExpressionParser {
+
+  private static final String TYPE = "type";
+  private static final String VALUE = "value";
+  private static final String OPERATION = "operation";
+  private static final String LITERALS = "literals";
+  private static final String TERM = "term";
+  private static final String LEFT_OPERAND = "left-operand";
+  private static final String RIGHT_OPERAND = "right-operand";
+  private static final String OPERAND = "operand";
+  private static final String AND = "and";
+  private static final String OR = "or";
+  private static final String NOT = "not";
+  private static final String TRUE = "true";
+  private static final String FALSE = "false";
+  private static final String UNBOUNDED_PREDICATE = "unbounded-predicate";
+  private static final String BOUNDED_LITERAL_PREDICATE = 
"bounded-literal-predicate";
+  private static final String BOUNDED_SET_PREDICATE = "bounded-set-predicate";
+  private static final String BOUNDED_UNARY_PREDICATE = 
"bounded-unary-predicate";
+  private static final String NAMED_REFERENCE = "named-reference";
+  private static final String BOUND_REFERENCE = "bound-reference";
+  private static final String ABOVE_MAX = "above-max";
+  private static final String BELOW_MIN = "below-min";
+
+  private static final Set<Expression.Operation> oneInputs = ImmutableSet.of(
+          Expression.Operation.IS_NULL,
+          Expression.Operation.NOT_NULL,
+          Expression.Operation.IS_NAN,
+          Expression.Operation.NOT_NAN);
+
+
+  private ExpressionParser() {
+  }
+
+  public static String toJson(Expression expression, boolean pretty) {
+    try {
+      StringWriter writer = new StringWriter();
+      JsonGenerator generator = JsonUtil.factory().createGenerator(writer);
+      if (pretty) {
+        generator.useDefaultPrettyPrinter();
+      }
+      toJson(expression, generator);
+      generator.flush();
+      return writer.toString();
+
+    } catch (IOException e) {
+      throw new UncheckedIOException("Failed to write json", e);
+    }
+  }
+
+  public static void toJson(Expression expression, JsonGenerator generator) 
throws IOException {
+    if (expression instanceof And) {
+      toJson((And) expression, generator);
+    } else if (expression instanceof Or) {
+      toJson((Or) expression, generator);
+    } else if (expression instanceof Not) {
+      toJson((Not) expression, generator);
+    } else if (expression instanceof True) {
+      toJson((True) expression, generator);
+    } else if (expression instanceof False) {
+      toJson((False) expression, generator);
+    } else if (expression instanceof Predicate) {
+      toJson((Predicate) expression, generator);
+    } else {
+      throw new IllegalArgumentException("Invalid Operation Type");

Review comment:
       Nit: Can you log the received unexpected type?
   
   Also, this covers every `Expresson` subclass I can think of. Are there other 
`Expression` classes? Maybe `throw new IllegalArgumentException("Unknown 
Expression type: %s", expression)`




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

For queries about this service, please contact Infrastructure at:
[email protected]



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

Reply via email to