代码之家  ›  专栏  ›  技术社区  ›  mas

Snowflake JavaScript存储过程在预期失败的情况下返回成功

  •  0
  • mas  · 技术社区  · 3 月前

    我最近一直在尝试Snowflake的JavaScript存储过程,但遇到了一个意外的行为。我创建了一个简单的存储过程,如Snowflake文档中所述 here .

    以下是我创建的存储过程:

    create procedure broken()
          returns varchar not null
          language javascript
          as
          $$
          var result = "";
          try {
              snowflake.execute( {sqlText: "Invalid Command!;"} );
              result = "Succeeded";
              }
          catch (err)  {
              result =  "Failed: Code: " + err.code + "\n  State: " + err.state;
              result += "\n  Message: " + err.message;
              result += "\nStack Trace:\n" + err.stackTraceTxt; 
              }
          return result;
          $$
          ;
    

    此存储过程的目的是通过执行无效的SQL命令故意失败,然后使用try-catch块捕获错误。

    当我调用存储过程时,它确实会返回预期的错误消息,指示失败:

    call broken();
    

    输出

    +---------------------------------------------------------+
    | BROKEN                                                  |
    |---------------------------------------------------------|
    | Failed: Code: 1003                                      |
    |   State: 42000                                          |
    |   Message: SQL compilation error:                       |
    | syntax error line 1 at position 0 unexpected 'Invalid'. |
    | Stack Trace:                                            |
    | Snowflake.execute, line 4 position 20                   |
    +---------------------------------------------------------+
    

    然而,在检查Snowflake的查询历史记录时,我注意到该过程被标记为成功运行。这似乎是矛盾的,因为该程序明确旨在失败。我希望它被标记为执行失败。

    有人能解释为什么Snowflake认为这是一次成功的跑步,尽管预期会失败吗?是否有不同的方法来处理此类场景并确保失败的存储过程执行正确反映在查询历史记录中?如有任何见解或建议,我们将不胜感激。非常感谢。

    1 回复  |  直到 3 月前
        1
  •  1
  •   Simeon Pilgrim    3 月前

    返回一个字符串“I HAVE FAILED”是成功的,因为它发生了,其中as,实际上失败了:

    create procedure actual_failure()
        returns varchar not null
        language javascript as
    $$
        snowflake.execute( {sqlText: "Invalid Command!;"} );
        return "Succeeded";
    $$
    ;
    

    然后

    call actual_failure();
    

    实际上失败了。

    enter image description here