当同一查询在 Postgres 上正常工作时出现 H2 SQL 语法错误

H2 SQL syntax error when same query works fine on Postgres

我一直在使用 slick 进行数据库交互,并使用 h2 内存数据库进行测试。所以,我刚刚添加了一个新方法,我在其中编写了简单的 SQL 查询而不是光滑的查询。

光滑版本:3.3.0 HikariCp:3.3.0 H2:1.4.200

因此它失败并出现语法错误异常的普通 sql 查询。

 def filterTasksByGivenConstraints(taskIds: List[Long], constraints: String): Future[Vector[Int]] = {
          val ids = taskIds.mkString("'", "','", "'")
          
          println(s"SELECT id FROM task where id in ($ids) and $constraints")

          val query = sql"""SELECT "id" FROM "task" WHERE "id" IN (#$ids) and #$constraints""".as[Int]
           db.get.run(query)
              .recover {
                case ex: Exception =>
                  logger.error(s"An exception has occurred while fetching filtered tasks $ids for given constraints $constraints and exception is: $ex")
                  throw ex.getCause
            
        }
      }

在 println 中打印的查询:

SELECT id FROM task where id in ('1') and ("expire_on" <= '2020-07-18') and (("expire_on" at time zone 'EST')::date >= '2020-07-16'::date and ("expire_on" at time zone 'EST')::date <= '2020-07-18'::date) and (("claim_number" = 'foo20') or ("referred_to" = '21'))

异常:

15:16:01.063 [scala-execution-context-global-23] ERROR io.inbox.stream.db.DBOperations - {"Level":"ERROR","Message":"An exception has occurred while fetching filtered tasks '1' for given constraints (\"expire_on\" <= '2020-07-18') and ((\"expire_on\" at time zone 'EST')::date >= '2020-07-16'::date and (\"expire_on\" at time zone 'EST')::date <= '2020-07-18'::date) and ((\"claim_number\" = 'foo20') or (\"referred_to\" = '21')) and exception is: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement \"SELECT \"\"id\"\" FROM \"\"task\"\" WHERE \"\"id\"\" IN ('1') AND (\"\"expire_on\"\" <= '2020-07-18') AND ((\"\"expire_on\"\" AT[*] TIME ZONE 'EST')::DATE >= '2020-07-16'::DATE AND (\"\"expire_on\"\" AT TIME ZONE 'EST')::DATE <= '2020-07-18'::DATE) AND ((\"\"claim_number\"\" = 'foo20') OR (\"\"referred_to\"\" = '21')) \"; expected \"(, ., [, ::, *, /, %, +, -, ||, ~, !~, NOT, LIKE, ILIKE, REGEXP, IS, IN, BETWEEN, AND, OR, ,, )\"; SQL statement:\nSELECT \"id\" FROM \"task\" WHERE \"id\" IN ('1') and (\"expire_on\" <= '2020-07-18') and ((\"expire_on\" at time zone 'EST')::date >= '2020-07-16'::date and (\"expire_on\" at time zone 'EST')::date <= '2020-07-18'::date) and ((\"claim_number\" = 'foo20') or (\"referred_to\" = '21')) [42001-197]","TimeStamp":"2020-08-13 15:16:01.063","api_app_log":"io.inbox.stream.db.DBOperations"}

我什至 运行 在 Postgres 上,打印的查询工作正常。我找不到 h2 的问题。

时区说明符 … AT TIME ZONE …… AT LOCAL 仅适用于 H2 1.4.200。