# SQL Parse Issue

**URL:** <https://community.dremio.com/t/sql-parse-issue/9799>\
**Category:** Uncategorized\
**Created:** [October 7, 2022, 12:16pm UTC](https://community.dremio.com/t/sql-parse-issue/9799 "2022-10-07T12:16:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![drew](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@drew](https://community.dremio.com/u/drew)\
**Post date:** [October 7, 2022, 12:16pm UTC](https://community.dremio.com/t/sql-parse-issue/9799/1 "2022-10-07T12:16:11Z")

</div>

Hello,

I am struggling to figure out why the following statement fails to parse. It looks like Dremio is dropping a (.

Here is the SQL:

```auto
  create or replace view "zais data mart (dev)"."mart"."application"."loanz"."instrument" as (
    WITH baseline_plus_delta AS (
    SELECT
        *
    FROM
        "zais data mart (dev)"."staging"."ratings"."moodys"."stg_instrument"
    WHERE
        op_type = 'BASELINE' AND
        asof = '2022-06-27 12:00:00'
    UNION ALL
        SELECT
            *
        FROM
            "zais data mart (dev)"."staging"."ratings"."moodys"."stg_instrument"
        WHERE
            op_type <> 'BASELINE' AND
            asof > '2022-06-27 12:00:00'
),
deduped_baseline_plus_delta AS (
    SELECT
        DISTINCT *
    FROM
        baseline_plus_delta
),
current_active_ids AS (
    SELECT
        instrument_id AS instrument_id,
        max(asof) AS asof,
        max(created_at) AS created_at
    FROM
        deduped_baseline_plus_delta
    WHERE
        op_type != 'DELETE'
    GROUP BY
        instrument_id
),
markit_mappings_deduped AS (
    SELECT
        *
    FROM
        "zais data mart (dev)"."mart"."application"."loanz"."markit_mapping_moodys"
),
final as (
    SELECT
        i.*,
        m.*
    FROM
        current_active_ids c
    INNER JOIN deduped_baseline_plus_delta i ON
        i.instrument_id = c.instrument_id AND
        i.asof = c.asof AND
        i.created_at = c.created_at
    INNER JOIN markit_mappings_deduped m ON
        c.instrument_id = m.provider_id
)

SELECT
    *
FROM
    final
  )

```

Here is a snippet of the stack trace in the Jobs section:

```auto
Caused By (com.dremio.exec.planner.sql.parser.impl.ParseException) Encountered "SELECT" at line 1, column 31.
Was expecting:
    "(" ...

```

Here is another snippet from the stack trace with the parsed SQL:

```auto
         PARSE ERROR: Failure parsing the query.

SQL Query WITH "baseline_plus_delta" AS SELECT *
FROM "zais data mart (dev)"."staging"."ratings"."moodys"."stg_instrument"
WHERE "op_type" = 'BASELINE' AND "asof" = '2022-06-27 12:00:00'
UNION ALL
SELECT *
FROM "zais data mart (dev)"."staging"."ratings"."moodys"."stg_instrument"
WHERE "op_type" <> 'BASELINE' AND "asof" > '2022-06-27 12:00:00', "deduped_baseline_plus_delta" AS (SELECT DISTINCT *
FROM "baseline_plus_delta"), "current_active_ids" AS (SELECT "instrument_id" AS "instrument_id", MAX("asof") AS "asof", MAX("created_at") AS "created_at"
FROM "deduped_baseline_plus_delta"
WHERE "op_type" <> 'DELETE'
GROUP BY "instrument_id"), "markit_mappings_deduped" AS (SELECT *
FROM "zais data mart (dev)"."mart"."application"."loanz"."markit_mapping_moodys"), "final" AS (SELECT "i".*, "m".*
FROM "current_active_ids" AS "c"
INNER JOIN "deduped_baseline_plus_delta" AS "i" ON "i"."instrument_id" = "c"."instrument_id" AND "i"."asof" = "c"."asof" AND "i"."created_at" = "c"."created_at"
INNER JOIN "markit_mappings_deduped" AS "m" ON "c"."instrument_id" = "m"."provider_id") (SELECT *
FROM "final")
startLine 1
startColumn 31
endLine 1
endColumn 36
SQL Query WITH "baseline_plus_delta" AS SELECT *
FROM "zais data mart (dev)"."staging"."ratings"."moodys"."stg_instrument"
WHERE "op_type" = 'BASELINE' AND "asof" = '2022-06-27 12:00:00'
UNION ALL
SELECT *
FROM "zais data mart (dev)"."staging"."ratings"."moodys"."stg_instrument"
WHERE "op_type" <> 'BASELINE' AND "asof" > '2022-06-27 12:00:00', "deduped_baseline_plus_delta" AS (SELECT DISTINCT *
FROM "baseline_plus_delta"), "current_active_ids" AS (SELECT "instrument_id" AS "instrument_id", MAX("asof") AS "asof", MAX("created_at") AS "created_at"
FROM "deduped_baseline_plus_delta"
WHERE "op_type" <> 'DELETE'
GROUP BY "instrument_id"), "markit_mappings_deduped" AS (SELECT *
FROM "zais data mart (dev)"."mart"."application"."loanz"."markit_mapping_moodys"), "final" AS (SELECT "i".*, "m".*
FROM "current_active_ids" AS "c"
INNER JOIN "deduped_baseline_plus_delta" AS "i" ON "i"."instrument_id" = "c"."instrument_id" AND "i"."asof" = "c"."asof" AND "i"."created_at" = "c"."created_at"
INNER JOIN "markit_mappings_deduped" AS "m" ON "c"."instrument_id" = "m"."provider_id") (SELECT *
FROM "final")

```

It looks like when parsing it is dropping the initial ( after the WITH statement. If I remove the create/replace part of the statement the query runs fine. It just doesn’t work when wrapped in a create/replace view statement.

We are using onprem Dremio version 22.0.0-202206221430090603-1fa4049f

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![drew](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@drew](https://community.dremio.com/u/drew)\
**Post date:** [October 7, 2022, 5:54pm UTC](https://community.dremio.com/t/sql-parse-issue/9799/2 "2022-10-07T17:54:13Z")

</div>

After some debugging, we found that the UNION ALL statement seems to cause the Dremio SQL parser to incorrectly parse which drops the leading ( from the WITH statement.

Still doing some investigation, but that is probably what is going on.

---

<div class="post-metadata">

**Author:** ![balaji.ramaswamy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.dremio.com/balaji.ramaswamy/32/843_2.png) [@balaji.ramaswamy](https://community.dremio.com/u/balaji.ramaswamy)\
**Post date:** [October 8, 2022, 3:39am UTC](https://community.dremio.com/t/sql-parse-issue/9799/3 "2022-10-08T03:39:44Z")

</div>

@drew I have seen similar issues, you are absolutely right, saving the SQL via “Save View As” would work. I will report this internally and see what is going on

---

<div class="post-metadata">

**Author:** ![balaji.ramaswamy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.dremio.com/balaji.ramaswamy/32/843_2.png) [@balaji.ramaswamy](https://community.dremio.com/u/balaji.ramaswamy)\
**Post date:** [October 8, 2022, 3:50am UTC](https://community.dremio.com/t/sql-parse-issue/9799/4 "2022-10-08T03:50:37Z")

</div>

@drew Are you able to send us the profile so we can try to reproduce the issue

---

<div class="post-metadata">

**Author:** ![drew](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@drew](https://community.dremio.com/u/drew)\
**Post date:** [October 8, 2022, 12:15pm UTC](https://community.dremio.com/t/sql-parse-issue/9799/5 "2022-10-08T12:15:54Z")

</div>

[43242e78-6bb7-4353-b1bf-76e1a9230cb7.zip](https://community.dremio.com/uploads/short-url/herug5HMWqH73XfkUNZxdrJLDNB.zip) (18.7 KB)  
See attached profile. Thanks for your help
