yuqi1129 commented on code in PR #11664:
URL: https://github.com/apache/gravitino/pull/11664#discussion_r3418968591


##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/util/TableMetadataTriggerExpressionUtils.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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.gravitino.maintenance.optimizer.recommender.util;
+
+import com.google.common.base.Preconditions;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.gravitino.rel.Table;
+
+/**
+ * Utility for translating table metadata into variables usable in a {@code 
trigger-expr} or {@code
+ * score-expr} expression.
+ */
+public class TableMetadataTriggerExpressionUtils {
+
+  private TableMetadataTriggerExpressionUtils() {}
+
+  /**
+   * Builds a context map for evaluating table metadata trigger expressions.
+   *
+   * <p>This method creates a map containing:
+   *
+   * <ul>
+   *   <li>Built-in table metadata: {@code column_count}, {@code 
partition_count}, {@code
+   *       sort_order_count}
+   *   <li>All table properties from {@link Table#properties()}, with numeric 
strings converted to
+   *       {@code long} values and all others kept as {@code String} values
+   * </ul>
+   *
+   * @param tableMetadata the table metadata to extract properties from
+   * @return a context map suitable for expression evaluation
+   */
+  public static Map<String, Object> buildTableMetadataContext(Table 
tableMetadata) {
+    Preconditions.checkArgument(tableMetadata != null, "Table metadata is 
null");
+    Map<String, Object> context = new HashMap<>();
+    context.put(
+        TableMetadataTriggerExpression.COLUMN_COUNT.getName(),
+        tableMetadata.columns() != null ? tableMetadata.columns().length : 0);
+    context.put(
+        TableMetadataTriggerExpression.PARTITION_COUNT.getName(),
+        tableMetadata.partitioning() != null ? 
tableMetadata.partitioning().length : 0);
+    context.put(
+        TableMetadataTriggerExpression.SORT_ORDER_COUNT.getName(),
+        tableMetadata.sortOrder() != null ? tableMetadata.sortOrder().length : 
0);
+
+    if (tableMetadata.properties() != null) {
+      tableMetadata
+          .properties()
+          .forEach(
+              (k, v) -> {
+                try {
+                  context.put(k, Long.parseLong(v));

Review Comment:
   I would suggest we use `NumberUtils.isCreatable` to replace this one. 



##########
maintenance/optimizer/src/main/java/org/apache/gravitino/maintenance/optimizer/recommender/handler/BaseExpressionStrategyHandler.java:
##########
@@ -219,10 +238,44 @@ private boolean evaluateBool(String expression, 
List<StatisticEntry<?>> statisti
     }
   }
 
-  private static Map<String, Object> buildExpressionContext(
-      Strategy strategy, List<StatisticEntry<?>> statistics) {
-    Map<String, Object> context = new HashMap<>();
+  private Optional<Boolean> tryToEvaluateBool(
+      String expression, List<StatisticEntry<?>> statistics) {
+    Map<String, Object> context = buildExpressionContext(statistics);
+    try {
+      return expressionEvaluator.tryToEvaluateBool(expression, context);
+    } catch (RuntimeException e) {
+      LOG.warn("Failed to evaluate expression '{}' with context {}", 
expression, context, e);
+      return Optional.of(false);

Review Comment:
   It should be `Optional.of(null)` according to the Java docs in 
expressionEvaluator.



-- 
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]

Reply via email to