Skip to content

CTE column names #275

Description

@niyue

I got some query like below (A CTE with a column renamed):

WITH cte AS (
            SELECT
                id as pid
            FROM
                projects
        )
        SELECT pid FROM cte

Currently, parser.columns property returns ['id', 'pid'], however, I expect the parser.columns to return columns not containing pid since it is accessing columns via CTE. Currently, CTE names are not included in the parser.tables property, but the columns for CTEs are included in the parser.columns property, which seems not consistent.

I would like to know:

  1. if the current behavior is correct in this case?
  2. if the current behavior is expected, is there any approach that users could avoid parsing columns accessed via CTE?

Thanks.

Activity

  1. collerek commented on Dec 16, 2021

    @collerek
    Collaborator

    For now parser does not infer tables so you need to prepend colum with source. Change select pid from cte to select cte.pid from cte and it will work as expected.

  2. niyue commented on Dec 16, 2021

    @niyue
    ContributorAuthor

    Change select pid from cte to select cte.pid from cte and it will work as expected

    Thanks for the prompt reply. Unfortunately this is not possible in some cases since SQL development and parsing/analysis may be done by different parties/teams.

    In my case, SQL statements are authored by several dev teams in the organization; then security team collects these SQL statements from logs and implements the SQL parser for aggregated analysis, so it is not possible for security team to alter the SQL used in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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