Skip to content

_with function and parse_one function are not consistent #6503

@GaryLiuGTA

Description

@GaryLiuGTA

Before you file an issue

  • Make sure you specify the "read" dialect eg. parse_one(sql, read="spark")
  • Make sure you specify the "write" dialect eg. ast.sql(dialect="duckdb")
  • Check if the issue still exists on main

Fully reproducible code snippet
Please include a fully reproducible code snippet or the input sql, dialect, and expected output.

Official Documentation
Please include links to official SQL documentation related to your issue.

The WITH and CTE expressions are different when they are added with with_ method, compared with parsing from original query.

Below code can be used to see the differences.

from sqlglot import parse_one
from sqlglot.diff import diff, Keep

cte_query = "SELECT a.x FROM a"
main_query = "SELECT x FROM cte"
full_query = "WITH cte AS (SELECT a.x FROM a) SELECT x FROM cte"

cte_tree = parse_one(cte_query)
from_cte = parse_one(main_query).with_(alias="cte", as_=cte_tree)
from_str = parse_one(full_query)

delta = [d for d in diff(from_cte, from_str) if not isinstance(d, Keep)]

Two queries/syntax trees are semantically the same, but diff function returns 2 Update:

  1. WITH statement. When assembly CTE with with_ function, it added recursive key, and set the default value False.
Image
  1. CTE statemtn. When assembly CTE with with_ function, it added 'scalarkey, and set the default valueFalse`
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions