Canner / wren-engine

🤖 The semantic engine for LLMs, bringing semantic context to AI agents. 🔥
https://getwren.ai/oss
Apache License 2.0
116 stars 26 forks source link

The aggregation count can't be applied to a CTE #792

Closed goldmedal closed 1 week ago

goldmedal commented 1 week ago

Describe the bug The count(*) syntax can't be applied to a CTE. It's an issue responded to by the Discord community https://discord.com/channels/1227143286951514152/1285568489414856815/1285568647775129682 To Reproduce

Given the request body for preview API

{
  "manifest": {
    "catalog": "wrenai",
    "schema": "public",
    "models": [
      {
        "name": "customers",
        "refSql": "select * from main.customers",
        "columns": [
          {
            "name": "City",
            "type": "VARCHAR",
            "isCalculated": false,
            "notNull": false
          },
          {
            "name": "Id",
            "type": "VARCHAR",
            "isCalculated": false,
            "notNull": false
          },
          {
            "name": "State",
            "type": "VARCHAR",
            "isCalculated": false,
            "notNull": false
          }
        ],
        "primaryKey": "Id"
      }
    ]
  },
  "sql": "with t1 as (select * from customers) select count(*) from t1"
}

Then, got the error message

Caused by: java.lang.IllegalArgumentException: source node name not found: Table{t1, Optional.empty}
    at io.wren.base.sqlrewrite.WrenSqlRewrite.lambda$apply$5(WrenSqlRewrite.java:102)
    at java.base/java.util.Optional.orElseThrow(Optional.java:403)
    at io.wren.base.sqlrewrite.WrenSqlRewrite.lambda$apply$8(WrenSqlRewrite.java:102)
    at java.base/java.lang.Iterable.forEach(Iterable.java:75)
    at io.wren.base.sqlrewrite.WrenSqlRewrite.apply(WrenSqlRewrite.java:100)
    at io.wren.base.sqlrewrite.WrenSqlRewrite.apply(WrenSqlRewrite.java:83)
    at io.wren.base.sqlrewrite.WrenPlanner.rewrite(WrenPlanner.java:51)
    at io.wren.base.sqlrewrite.WrenPlanner.rewrite(WrenPlanner.java:43)
    at io.wren.main.PreviewService.lambda$dryRun$2(PreviewService.java:129)
    at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)

Expected behavior The SQL should work.