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

Give query case sensitive treatment in query hash #4254

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Make case insensitive hash of query text

Revision ID: 1038c2174f5d
Revises: fd4fc850d7ea
Create Date: 2023-07-16 23:10:12.885949

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import table

from redash.utils import gen_query_hash

# revision identifiers, used by Alembic.
revision = '1038c2174f5d'
down_revision = 'fd4fc850d7ea'
branch_labels = None
depends_on = None



def change_query_hash(conn, table, query_text_to):
for record in conn.execute(table.select()):
query_text = query_text_to(record.query)
conn.execute(
table
.update()
.where(table.c.id == record.id)
.values(query_hash=gen_query_hash(query_text)))


def upgrade():
queries = table(
'queries',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('query', sa.Text),
sa.Column('query_hash', sa.String(length=10)))

conn = op.get_bind()
change_query_hash(conn, queries, query_text_to=str)


def downgrade():
queries = table(
'queries',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('query', sa.Text),
sa.Column('query_hash', sa.String(length=10)))

conn = op.get_bind()
change_query_hash(conn, queries, query_text_to=str.lower)
6 changes: 3 additions & 3 deletions redash/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ def slugify(s):

def gen_query_hash(sql):
"""Return hash of the given query after stripping all comments, line breaks
and multiple spaces, and lower casing all text.
and multiple spaces.

TODO: possible issue - the following queries will get the same id:
The following queries will get different ids:
1. SELECT 1 FROM table WHERE column='Value';
2. SELECT 1 FROM table where column='value';
"""
sql = COMMENTS_REGEX.sub("", sql)
sql = "".join(sql.split()).lower()
osule marked this conversation as resolved.
Show resolved Hide resolved
sql = "".join(sql.split())
return hashlib.md5(sql.encode("utf-8")).hexdigest()


Expand Down