Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nullability checks on SQL Views #192

Open
fingeromer opened this issue Dec 6, 2023 · 2 comments
Open

Support nullability checks on SQL Views #192

fingeromer opened this issue Dec 6, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@fingeromer
Copy link

fingeromer commented Dec 6, 2023

SafeQl recognizes fields as nullable when inner joining with a materialized view:
image

The View:

CREATE MATERIALIZED VIEW issues_base AS
    SELECT issue.id AS issue_id,
        issue.cve_code,
        image.name AS image_name,
        image.id AS image_id,
        jira_issue_key,
        array_agg(issue_instance.id) AS issue_instance_ids,
        client_id,
        issue.ignored,
        bool_or(
            CASE
                WHEN (is_native)
                    THEN state = '2'
                ELSE
                    last_executed_at IS NOT NULL
            END
        ) AS is_executed
  FROM issue,
      image,
      issue_instance,
      client_library_instance,
      live_k8s_image_builds
  WHERE image.id = issue.image_id
  AND image.id = live_k8s_image_builds.image_id
  AND issue_instance.issue_id = issue.id
  AND issue_instance.image_build_id = live_k8s_image_builds.id
  AND issue_instance.client_library_instance_id = client_library_instance.id
  AND issue.resolved IS NOT TRUE
  GROUP BY issue.id, image.id;

The query:

          SELECT issue_id AS "issueId",
              cve_code AS "cveCode",
              image_name AS "imageName",
              image_id AS "imageId",
              jira_issue_key AS "jiraIssueKey",
              issue_instance_ids AS "issueInstanceIds",
              ignored
          FROM issues_base
          WHERE client_id = ${clientId};
@Newbie012
Copy link
Collaborator

Newbie012 commented Dec 6, 2023

TL;DR - SafeQL (currently) doesn't support nullability checks for SQL views.

SafeQL's approach is "everything is nullable until proven otherwise" (for safety reasons). It infers whether a target result (e.g., selected column) is nullable or not, by first analyzing what it is. If it's a computation, it's not null for sure. If it's a table column, it's not null as long as that table column is NOT NULL as well, and so on.

Unlike querying columns from tables, PostgresSQL doesn't know that the column cve_code that was called from view issues_base is from table issue. SafeQL already traverses through the queries for more precise checks, but currently, it doesn't traverse over the views to determine nullability checks.

As a workaround for now, SafeQL is smart enough to know that a column is not nullable if you do one of the following options:

  • add WHERE issue_id IS NOT NULL
  • add a COALESCE with a fallback value.

@Newbie012 Newbie012 changed the title Wrong nullable fields Support nullability checks on SQL Views Dec 6, 2023
@Newbie012 Newbie012 added the enhancement New feature or request label Dec 6, 2023
@fingeromer
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants