Skip to content

Table extraction excludes CREATE TABLE target #54

@karakanb

Description

@karakanb

build_scope does not include the target table of a CREATE TABLE AS SELECT statement. sqlglot includes it.

sqlglot

# /// script
# requires-python = ">=3.11"
# dependencies = ["sqlglot"]
# ///

from sqlglot import exp, parse

sql = "CREATE TABLE out_table AS SELECT 1 AS id"
tables = [t.name for stmt in parse(sql, dialect="bigquery") for t in stmt.find_all(exp.Table)]

print(f"SQL:    {sql}")
print(f"Tables: {tables}")

polyglot repro

use polyglot_sql::{self as pgsql, DialectType};

fn main() {
    let sql = "CREATE TABLE out_table AS SELECT 1 AS id";
    let expr = &pgsql::parse(sql, DialectType::BigQuery).unwrap()[0];
    let scope = pgsql::scope::build_scope(expr);
    let tables: Vec<&String> = scope.sources.keys().collect();

    println!("SQL:    {}", sql);
    println!("Tables: {:?}", tables);
}

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