This is an automated email from the ASF dual-hosted git repository.

vladimirsitnikov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/master by this push:
     new eaf0edf  [CALCITE-4229] Add Util.throwAsRuntime and Util.causeOrSelf 
to simplify exception re-throwing
eaf0edf is described below

commit eaf0edfc38c095e733cb37614333dfb7415f331a
Author: Vladimir Sitnikov <[email protected]>
AuthorDate: Sat Sep 5 01:21:27 2020 +0300

    [CALCITE-4229] Add Util.throwAsRuntime and Util.causeOrSelf to simplify 
exception re-throwing
---
 .../CachingLatticeStatisticProvider.java           |  3 +-
 .../rel/metadata/ChainedRelMetadataProvider.java   |  6 +--
 .../rel/metadata/JaninoRelMetadataProvider.java    |  3 +-
 .../calcite/rel/metadata/MetadataFactoryImpl.java  |  3 +-
 .../metadata/ReflectiveRelMetadataProvider.java    |  3 +-
 .../sql/fun/SqlLibraryOperatorTableFactory.java    |  8 ++--
 .../apache/calcite/sql/pretty/SqlPrettyWriter.java |  6 +--
 .../calcite/sql/type/SqlTypeMappingRules.java      |  6 +--
 .../sql/util/ReflectiveSqlOperatorTable.java       |  3 +-
 .../statistic/CachingSqlStatisticProvider.java     |  9 ++--
 .../java/org/apache/calcite/util/ReflectUtil.java  |  3 +-
 .../main/java/org/apache/calcite/util/Util.java    | 49 ++++++++++++++++++++++
 .../calcite/adapter/druid/DruidConnectionImpl.java |  3 +-
 13 files changed, 67 insertions(+), 38 deletions(-)

diff --git 
a/core/src/main/java/org/apache/calcite/materialize/CachingLatticeStatisticProvider.java
 
b/core/src/main/java/org/apache/calcite/materialize/CachingLatticeStatisticProvider.java
index 984b544..544cba2 100644
--- 
a/core/src/main/java/org/apache/calcite/materialize/CachingLatticeStatisticProvider.java
+++ 
b/core/src/main/java/org/apache/calcite/materialize/CachingLatticeStatisticProvider.java
@@ -50,8 +50,7 @@ class CachingLatticeStatisticProvider implements 
LatticeStatisticProvider {
       try {
         counts.add(cache.get(column));
       } catch (UncheckedExecutionException | ExecutionException e) {
-        Util.throwIfUnchecked(e.getCause());
-        throw new RuntimeException(e.getCause());
+        throw Util.throwAsRuntime(Util.causeOrSelf(e));
       }
     }
     return (int) Lattice.getRowCount(lattice.getFactRowCount(), counts);
diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/ChainedRelMetadataProvider.java
 
b/core/src/main/java/org/apache/calcite/rel/metadata/ChainedRelMetadataProvider.java
index e727650..d1cf705 100644
--- 
a/core/src/main/java/org/apache/calcite/rel/metadata/ChainedRelMetadataProvider.java
+++ 
b/core/src/main/java/org/apache/calcite/rel/metadata/ChainedRelMetadataProvider.java
@@ -134,11 +134,7 @@ public class ChainedRelMetadataProvider implements 
RelMetadataProvider {
             return o;
           }
         } catch (InvocationTargetException e) {
-          if (e.getCause() instanceof CyclicMetadataException) {
-            continue;
-          }
-          Util.throwIfUnchecked(e.getCause());
-          throw new RuntimeException(e.getCause());
+          throw Util.throwAsRuntime(Util.causeOrSelf(e));
         }
       }
       return null;
diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/JaninoRelMetadataProvider.java
 
b/core/src/main/java/org/apache/calcite/rel/metadata/JaninoRelMetadataProvider.java
index 1ab362b..78584a1 100644
--- 
a/core/src/main/java/org/apache/calcite/rel/metadata/JaninoRelMetadataProvider.java
+++ 
b/core/src/main/java/org/apache/calcite/rel/metadata/JaninoRelMetadataProvider.java
@@ -468,8 +468,7 @@ public class JaninoRelMetadataProvider implements 
RelMetadataProvider {
       //noinspection unchecked
       return (H) HANDLERS.get(key);
     } catch (UncheckedExecutionException | ExecutionException e) {
-      Util.throwIfUnchecked(e.getCause());
-      throw new RuntimeException(e.getCause());
+      throw Util.throwAsRuntime(Util.causeOrSelf(e));
     }
   }
 
diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/MetadataFactoryImpl.java 
b/core/src/main/java/org/apache/calcite/rel/metadata/MetadataFactoryImpl.java
index 7cef923..c4fa662 100644
--- 
a/core/src/main/java/org/apache/calcite/rel/metadata/MetadataFactoryImpl.java
+++ 
b/core/src/main/java/org/apache/calcite/rel/metadata/MetadataFactoryImpl.java
@@ -64,8 +64,7 @@ public class MetadataFactoryImpl implements MetadataFactory {
       final Metadata apply = cache.get(key).bind(rel, mq);
       return metadataClazz.cast(apply);
     } catch (UncheckedExecutionException | ExecutionException e) {
-      Util.throwIfUnchecked(e.getCause());
-      throw new RuntimeException(e.getCause());
+      throw Util.throwAsRuntime(Util.causeOrSelf(e));
     }
   }
 }
diff --git 
a/core/src/main/java/org/apache/calcite/rel/metadata/ReflectiveRelMetadataProvider.java
 
b/core/src/main/java/org/apache/calcite/rel/metadata/ReflectiveRelMetadataProvider.java
index dd7ed49..6168179 100644
--- 
a/core/src/main/java/org/apache/calcite/rel/metadata/ReflectiveRelMetadataProvider.java
+++ 
b/core/src/main/java/org/apache/calcite/rel/metadata/ReflectiveRelMetadataProvider.java
@@ -187,8 +187,7 @@ public class ReflectiveRelMetadataProvider
                   return handlerMethod.invoke(target, args1);
                 } catch (InvocationTargetException
                     | UndeclaredThrowableException e) {
-                  Util.throwIfUnchecked(e.getCause());
-                  throw new RuntimeException(e.getCause());
+                  throw Util.throwAsRuntime(Util.causeOrSelf(e));
                 } finally {
                   mq.map.remove(rel, key1);
                 }
diff --git 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperatorTableFactory.java
 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperatorTableFactory.java
index c3a81a3..4f89f1e 100644
--- 
a/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperatorTableFactory.java
+++ 
b/core/src/main/java/org/apache/calcite/sql/fun/SqlLibraryOperatorTableFactory.java
@@ -103,8 +103,7 @@ public class SqlLibraryOperatorTableFactory {
               }
             }
           } catch (IllegalArgumentException | IllegalAccessException e) {
-            Util.throwIfUnchecked(e.getCause());
-            throw new RuntimeException(e.getCause());
+            throw Util.throwAsRuntime(Util.causeOrSelf(e));
           }
         }
       }
@@ -152,9 +151,8 @@ public class SqlLibraryOperatorTableFactory {
     try {
       return cache.get(ImmutableSet.copyOf(librarySet));
     } catch (ExecutionException e) {
-      Util.throwIfUnchecked(e.getCause());
-      throw new RuntimeException("populating SqlOperatorTable for library "
-          + librarySet, e);
+      throw Util.throwAsRuntime("populating SqlOperatorTable for library "
+          + librarySet, Util.causeOrSelf(e));
     }
   }
 }
diff --git 
a/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java 
b/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java
index 244199e..8d04713 100644
--- a/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java
+++ b/core/src/main/java/org/apache/calcite/sql/pretty/SqlPrettyWriter.java
@@ -1410,8 +1410,7 @@ public class SqlPrettyWriter implements SqlWriter {
       try {
         method.invoke(o, value);
       } catch (IllegalAccessException | InvocationTargetException e) {
-        Util.throwIfUnchecked(e.getCause());
-        throw new RuntimeException(e.getCause());
+        throw Util.throwAsRuntime(Util.causeOrSelf(e));
       }
     }
 
@@ -1420,8 +1419,7 @@ public class SqlPrettyWriter implements SqlWriter {
       try {
         return method.invoke(o);
       } catch (IllegalAccessException | InvocationTargetException e) {
-        Util.throwIfUnchecked(e.getCause());
-        throw new RuntimeException(e.getCause());
+        throw Util.throwAsRuntime(Util.causeOrSelf(e));
       }
     }
 
diff --git 
a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeMappingRules.java 
b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeMappingRules.java
index 83359e8..d13350d 100644
--- a/core/src/main/java/org/apache/calcite/sql/type/SqlTypeMappingRules.java
+++ b/core/src/main/java/org/apache/calcite/sql/type/SqlTypeMappingRules.java
@@ -74,8 +74,7 @@ public abstract class SqlTypeMappingRules {
       try {
         map.put(fromType, sets.get(toTypes));
       } catch (UncheckedExecutionException | ExecutionException e) {
-        Util.throwIfUnchecked(e.getCause());
-        throw new RuntimeException("populating SqlTypeAssignmentRules", e);
+        throw Util.throwAsRuntime("populating SqlTypeAssignmentRules", 
Util.causeOrSelf(e));
       }
     }
 
@@ -84,8 +83,7 @@ public abstract class SqlTypeMappingRules {
       try {
         map.putAll(typeMapping);
       } catch (UncheckedExecutionException e) {
-        Util.throwIfUnchecked(e.getCause());
-        throw new RuntimeException("populating SqlTypeAssignmentRules", e);
+        throw Util.throwAsRuntime("populating SqlTypeAssignmentRules", 
Util.causeOrSelf(e));
       }
     }
 
diff --git 
a/core/src/main/java/org/apache/calcite/sql/util/ReflectiveSqlOperatorTable.java
 
b/core/src/main/java/org/apache/calcite/sql/util/ReflectiveSqlOperatorTable.java
index 069957d..158075a 100644
--- 
a/core/src/main/java/org/apache/calcite/sql/util/ReflectiveSqlOperatorTable.java
+++ 
b/core/src/main/java/org/apache/calcite/sql/util/ReflectiveSqlOperatorTable.java
@@ -78,8 +78,7 @@ public abstract class ReflectiveSqlOperatorTable implements 
SqlOperatorTable {
           register(op);
         }
       } catch (IllegalArgumentException | IllegalAccessException e) {
-        Util.throwIfUnchecked(e.getCause());
-        throw new RuntimeException(e.getCause());
+        throw Util.throwAsRuntime(Util.causeOrSelf(e));
       }
     }
   }
diff --git 
a/core/src/main/java/org/apache/calcite/statistic/CachingSqlStatisticProvider.java
 
b/core/src/main/java/org/apache/calcite/statistic/CachingSqlStatisticProvider.java
index 011e62d..37715c9 100644
--- 
a/core/src/main/java/org/apache/calcite/statistic/CachingSqlStatisticProvider.java
+++ 
b/core/src/main/java/org/apache/calcite/statistic/CachingSqlStatisticProvider.java
@@ -51,8 +51,7 @@ public class CachingSqlStatisticProvider implements 
SqlStatisticProvider {
       return (Double) cache.get(key,
           () -> provider.tableCardinality(table));
     } catch (UncheckedExecutionException | ExecutionException e) {
-      Util.throwIfUnchecked(e.getCause());
-      throw new RuntimeException(e.getCause());
+      throw Util.throwAsRuntime(Util.causeOrSelf(e));
     }
   }
 
@@ -69,8 +68,7 @@ public class CachingSqlStatisticProvider implements 
SqlStatisticProvider {
           () -> provider.isForeignKey(fromTable, fromColumns, toTable,
               toColumns));
     } catch (UncheckedExecutionException | ExecutionException e) {
-      Util.throwIfUnchecked(e.getCause());
-      throw new RuntimeException(e.getCause());
+      throw Util.throwAsRuntime(Util.causeOrSelf(e));
     }
   }
 
@@ -81,8 +79,7 @@ public class CachingSqlStatisticProvider implements 
SqlStatisticProvider {
               ImmutableIntList.copyOf(columns));
       return (Boolean) cache.get(key, () -> provider.isKey(table, columns));
     } catch (UncheckedExecutionException | ExecutionException e) {
-      Util.throwIfUnchecked(e.getCause());
-      throw new RuntimeException(e.getCause());
+      throw Util.throwAsRuntime(Util.causeOrSelf(e));
     }
   }
 }
diff --git a/core/src/main/java/org/apache/calcite/util/ReflectUtil.java 
b/core/src/main/java/org/apache/calcite/util/ReflectUtil.java
index 5c3e649..5c18265 100644
--- a/core/src/main/java/org/apache/calcite/util/ReflectUtil.java
+++ b/core/src/main/java/org/apache/calcite/util/ReflectUtil.java
@@ -263,8 +263,7 @@ public abstract class ReflectUtil {
       // visit methods aren't allowed to have throws clauses,
       // so the only exceptions which should come
       // to us are RuntimeExceptions and Errors
-      Util.throwIfUnchecked(ex.getTargetException());
-      throw new RuntimeException(ex.getTargetException());
+      throw Util.throwAsRuntime(Util.causeOrSelf(ex));
     }
     return true;
   }
diff --git a/core/src/main/java/org/apache/calcite/util/Util.java 
b/core/src/main/java/org/apache/calcite/util/Util.java
index e60a18d..8926c8f 100644
--- a/core/src/main/java/org/apache/calcite/util/Util.java
+++ b/core/src/main/java/org/apache/calcite/util/Util.java
@@ -40,6 +40,7 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
 
+import org.apiguardian.api.API;
 import org.slf4j.Logger;
 
 import java.io.BufferedReader;
@@ -912,6 +913,43 @@ public class Util {
   }
 
   /**
+   * This method rethrows input throwable as is (if its unchecked) or
+   * wraps it with {@link RuntimeException} and throws.
+   * <p>The typical usage would be {@code throw throwAsRuntime(...)}, where 
{@code throw} statement
+   * is needed so Java compiler knows the execution stops at that line.</p>
+   *
+   * @param throwable input throwable
+   * @return the method never returns, it always throws an unchecked exception
+   */
+  @API(since = "1.26", status = API.Status.EXPERIMENTAL)
+  public static RuntimeException throwAsRuntime(Throwable throwable) {
+    throwIfUnchecked(throwable);
+    throw new RuntimeException(throwable);
+  }
+
+  /**
+   * This method rethrows input throwable as is (if its unchecked) with an 
extra message or
+   * wraps it with {@link RuntimeException} and throws.
+   * <p>The typical usage would be {@code throw throwAsRuntime(...)}, where 
{@code throw} statement
+   * is needed so Java compiler knows the execution stops at that line.</p>
+   *
+   * @param throwable input throwable
+   * @return the method never returns, it always throws an unchecked exception
+   */
+  @API(since = "1.26", status = API.Status.EXPERIMENTAL)
+  public static RuntimeException throwAsRuntime(String message, Throwable 
throwable) {
+    if (throwable instanceof RuntimeException) {
+      throwable.addSuppressed(new Throwable(message));
+      throw (RuntimeException) throwable;
+    }
+    if (throwable instanceof Error) {
+      throwable.addSuppressed(new Throwable(message));
+      throw (Error) throwable;
+    }
+    throw new RuntimeException(message, throwable);
+  }
+
+  /**
    * Wraps an exception with {@link RuntimeException} and return it.
    * If the exception is already an instance of RuntimeException,
    * returns it directly.
@@ -924,6 +962,17 @@ public class Util {
   }
 
   /**
+   * Returns cause of the given throwable if it is non-null or the throwable 
itself.
+   * @param throwable input throwable
+   * @return cause of the given throwable if it is non-null or the throwable 
itself
+   */
+  @API(since = "1.26", status = API.Status.EXPERIMENTAL)
+  public static Throwable causeOrSelf(Throwable throwable) {
+    Throwable cause = throwable.getCause();
+    return cause != null ? cause : throwable;
+  }
+
+  /**
    * Retrieves messages in a exception and writes them to a string. In the
    * string returned, each message will appear on a different line.
    *
diff --git 
a/druid/src/main/java/org/apache/calcite/adapter/druid/DruidConnectionImpl.java 
b/druid/src/main/java/org/apache/calcite/adapter/druid/DruidConnectionImpl.java
index 66df54a..0ac6619 100644
--- 
a/druid/src/main/java/org/apache/calcite/adapter/druid/DruidConnectionImpl.java
+++ 
b/druid/src/main/java/org/apache/calcite/adapter/druid/DruidConnectionImpl.java
@@ -693,8 +693,7 @@ class DruidConnectionImpl implements DruidConnection {
       final Throwable e = throwableHolder.get();
       if (e != null) {
         throwableHolder.set(null);
-        Util.throwIfUnchecked(e);
-        throw new RuntimeException(e);
+        throw Util.throwAsRuntime(e);
       }
     }
   }

Reply via email to