Skip to content

Remapping Postgres minified aliases can result in data loss #15646

Open

Description

@frostzt

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

As of right now the way minified aliases are remapped can actually cause data corruption in Postgres. This I believe is due to the idea that we're using RegExp's to replace strings present in the generated minified query.

if (options?.minifyAliases && this.options.includeAliases) {
    for (const [alias, original] of _.toPairs(this.options.includeAliases)
        // Sorting to replace the longest aliases first to prevent alias collision
        .sort((a, b) => b[1].length - a[1].length)) {
        const reg = new RegExp(_.escapeRegExp(original), 'g');

        sql = sql.replace(reg, alias);
    }
}

The above I think is for remapping aliases for fields and the same is also done for remapping joins that are minified, which also produces the same problem.

References

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity

  1. added
    type: bug
    pending-approval
    on Feb 3, 2023
  2. removed
    pending-approval
    on Feb 19, 2023
  3. frostzt commented on Mar 9, 2023

    @frostzt
    MemberAuthor

    @ephys so I am thinking of excluding fields that are in the where clause. Is that right? To be honest need a little enlightenment here 😅

  4. ephys commented on Mar 9, 2023

    @ephys
    Member

    No the entire concept of using a regexp to modify SQL must be removed, it's inherently unsafe. The question is: what will it break and how can we provide a better solution?

  5. frostzt commented on Mar 21, 2023

    @frostzt
    MemberAuthor

    Hey @ephys sorry for taking long finally got work done and can again put in some time here, I came up with an approach could be naive but I think I can improve more on it!

    So I am thinking in the following terms;

    1. We make a Map that keeps track of a unique number (can be just incrementing) and a string with it for example
    { "0": "a_huge_string_that_was_given_to_me" }
    1. Now I am thinking of keeping incrementing them even if we receive same string twice so let's say we got this query
    SELECT
        "table_one.a_huge_string_that_was_given_to_me",
        "table_two.a_huge_string_that_was_given_to_me",
    FROM "table_one" INNER JOIN "table_two" ...

    I am gonna loop over store a_huge_string_that_was_given_to_me like this

    {
      "0": "table_one.a_huge_string_that_was_given_to_me",
      "1": "table_two.a_huge_string_that_was_given_to_me",
    }
    1. Since this Map will be available globally I will create a function that will traverse the string and each time I encounter these numbers or any specific identifier that we assign (I am thinking like those numbers but we can be specific) we just call this function and that function gets the number and gets replaced by that original column name!

    Does this make sense or is not a good way of approaching it?

  6. ephys commented on Mar 21, 2023

    @ephys
    Member

    To be honest I don't think I understood your plan

    If you're considering transforming the final SQL but with an alternative approach, that's not going to work. You'd need to use an actual SQL parser for this, I don't recommend it

  7. frostzt commented on Mar 22, 2023

    @frostzt
    MemberAuthor

    Sorry @ephys I think I wasn't able to explain it properly but I created a quick Repl here to show the gist of it, it certainly can be improved I am still thinking on it!

  8. added theissue type on Mar 9, 2025
  9. removed
    type: bug
    on Mar 9, 2025
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

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions