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

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


The following commit(s) were added to refs/heads/master by this push:
     new 87212d791 IMPALA-12811: Exception during re-analyze can be lost
87212d791 is described below

commit 87212d791b5db2122f2f5d32369dc542bddcea3a
Author: Zoltan Borok-Nagy <[email protected]>
AuthorDate: Thu Feb 15 10:36:46 2024 +0100

    IMPALA-12811: Exception during re-analyze can be lost
    
    When there is an AnalysisException during re-analyze, we try to print
    the re-written and original statements via invoking toSql() on them.
    But toSql() fails because the analysis of the statement was incomplete,
    so it throws another exception (typically an IllegalStateException
    without any relevant information about the original issue).
    
    This patch puts the original LOG.error() in a try-catch, then wraps the
    original AnalysisException into a new exception that just mentions that
    the error occurred after query rewrite.
    
    Testing:
     * added a column masking test with an invalid masking function
    
    Change-Id: Ie6e36b08703c07a2a8d68a4ec0e8ddd65ba03199
    Reviewed-on: http://gerrit.cloudera.org:8080/21037
    Reviewed-by: Impala Public Jenkins <[email protected]>
    Tested-by: Impala Public Jenkins <[email protected]>
---
 .../org/apache/impala/analysis/AnalysisContext.java   | 19 +++++++++++++++----
 .../queries/QueryTest/ranger_column_masking.test      | 12 ++++++++++++
 tests/authorization/test_ranger.py                    |  5 +++++
 3 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/fe/src/main/java/org/apache/impala/analysis/AnalysisContext.java 
b/fe/src/main/java/org/apache/impala/analysis/AnalysisContext.java
index 3c02439fc..ec1e34b03 100644
--- a/fe/src/main/java/org/apache/impala/analysis/AnalysisContext.java
+++ b/fe/src/main/java/org/apache/impala/analysis/AnalysisContext.java
@@ -646,10 +646,9 @@ public class AnalysisContext {
       analysisResult_.stmt_.analyze(analysisResult_.analyzer_);
       analysisResult_.analyzer_.setEnablePrivChecks(true); // Always restore
     } catch (AnalysisException e) {
-      LOG.error(String.format("Error analyzing the rewritten query.\n" +
-          "Original SQL: %s\nRewritten SQL: %s", analysisResult_.stmt_.toSql(),
-          analysisResult_.stmt_.toSql(REWRITTEN)), e);
-      throw e;
+      logRewriteErrorNoThrow(analysisResult_.stmt_, e);
+      throw new AnalysisException("An error occurred after query rewrite: " +
+          e.getMessage(), e);
     }
     // Restore the original result types and column labels.
     analysisResult_.stmt_.castResultExprs(origResultTypes);
@@ -660,6 +659,18 @@ public class AnalysisContext {
     if (isExplain) analysisResult_.stmt_.setIsExplain();
   }
 
+  private void logRewriteErrorNoThrow(StatementBase stmt,
+      AnalysisException analysisException) {
+    try {
+      LOG.error(String.format("Error analyzing the rewritten query.\n" +
+          "Original SQL: %s\nRewritten SQL: %s", stmt.toSql(),
+          stmt.toSql(REWRITTEN)), analysisException);
+    } catch (Exception e) {
+      LOG.error("An exception occurred during printing out " +
+          "the rewritten SQL statement.", e);
+    }
+  }
+
   public Analyzer getAnalyzer() { return analysisResult_.getAnalyzer(); }
   public EventSequence getTimeline() { return timeline_; }
   // This should only be called after analyzeAndAuthorize().
diff --git 
a/testdata/workloads/functional-query/queries/QueryTest/ranger_column_masking.test
 
b/testdata/workloads/functional-query/queries/QueryTest/ranger_column_masking.test
index e0cb3cff9..f36db71a6 100644
--- 
a/testdata/workloads/functional-query/queries/QueryTest/ranger_column_masking.test
+++ 
b/testdata/workloads/functional-query/queries/QueryTest/ranger_column_masking.test
@@ -951,3 +951,15 @@ 
regex:'.*/xxxx-xxxxxxxxx/xxxxxxxxxxxx/xxxx=xxxx/xxxxx=x/090401.txt',700,NULL,1,1
 ---- TYPES
 STRING, INT, BOOLEAN, TINYINT, SMALLINT, INT, BIGINT, FLOAT, DOUBLE, STRING, 
STRING, TIMESTAMP, INT, INT, INT, BOOLEAN, TINYINT, SMALLINT, INT, BIGINT, 
FLOAT, DOUBLE, STRING, STRING, TIMESTAMP, INT, INT, STRING
 ====
+---- QUERY
+# Query table with invalid masking function
+SELECT * FROM functional_parquet.alltypessmall;
+---- CATCH
+AnalysisException: An error occurred after query rewrite: Could not resolve 
column/field reference: 'invalid_col'
+====
+---- QUERY
+# CTAS statement with invalid masking function
+CREATE TABLE $UNIQUE_DB.invalid_masked_tbl AS SELECT * FROM 
functional_parquet.alltypessmall;
+---- CATCH
+AnalysisException: An error occurred after query rewrite: Could not resolve 
column/field reference: 'invalid_col'
+====
diff --git a/tests/authorization/test_ranger.py 
b/tests/authorization/test_ranger.py
index 73b5f2a05..f83680614 100644
--- a/tests/authorization/test_ranger.py
+++ b/tests/authorization/test_ranger.py
@@ -1827,6 +1827,11 @@ class TestRanger(CustomClusterTestSuite):
         unique_name + str(policy_cnt), user, "functional_parquet",
         "iceberg_v2_delete_positional", "data", "MASK_NULL")
       policy_cnt += 1
+      # Add invalid column masking policy to trigger an error during 
re-analyze.
+      TestRanger._add_column_masking_policy(
+        unique_name + str(policy_cnt), user, "functional_parquet",
+        "alltypessmall", "string_col", "CUSTOM", "concat(string_col, 
invalid_col)")
+      policy_cnt += 1
       self.execute_query_expect_success(admin_client, "refresh authorization",
                                         user=ADMIN)
       self.run_test_case("QueryTest/ranger_column_masking", vector,

Reply via email to