Skip to content

Commit

Permalink
Merge pull request #1952 from open-dynaMIX/fix_pivot_table_quoting
Browse files Browse the repository at this point in the history
fix(analytics): properly quote expressions
  • Loading branch information
winged authored Jan 27, 2023
2 parents 48d657c + 3ff7f36 commit f8b5a3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion caluma/caluma_analytics/pivot_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_sql_and_params(self):
key = f"analytics_result_{field.alias}"

# old field might be directly selected in the base query,
# in which case we need to alias it's expression. If it's
# in which case we need to alias its expression. If it's
# not directly in the base, it won't be in the top-level selects
# list, but will be aliased to the known name in a subquery.
old_field = selects_by_alias.get(key, key)
Expand Down
23 changes: 12 additions & 11 deletions caluma/caluma_analytics/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,24 @@ def annotate(self, query: Query):

def expr(self, query):
# TODO: should the table alias be prefixed in all field expr()s?
alias = query.self_alias()
alias = quote_identifier(query.self_alias())
extract = quote_identifier(self.extract)
if self.answer_value_mode:
# Caluma form answers are in a JSON field named "value", from where
# we need to extract the actual value, otherwise, for strings, we
# would get the quoted version back
extractor_op = "#>>'{}'" # noqa
return f"({alias}.{self.extract} {extractor_op})"
return f"{alias}.{self.extract}"
return f"({alias}.{extract} {extractor_op})"
return f"{alias}.{extract}"


@dataclass
class DateExprField(AttrField):
extract_part: Optional[str] = field(default=None)

def expr(self, query):
q_id = quote_identifier(self.extract)
return f"EXTRACT({self.extract_part} FROM {q_id})"
extract = quote_identifier(self.extract)
return f"EXTRACT({self.extract_part} FROM {extract})"


@dataclass
Expand All @@ -234,12 +235,12 @@ class JSONExtractorField(AttrField):

def expr(self, query):
key_param = query.makeparam(self.json_key)
q_id = quote_identifier(self.extract)
self_alias = query.self_alias()
extract = quote_identifier(self.extract)
self_alias = quote_identifier(query.self_alias())
# Extract text from JSON field, so that it comes from the DB
# as actual text
extractor_op = "#>>'{}'" # noqa
return f"""(({self_alias}.{q_id} -> {key_param}) {extractor_op})"""
return f"""(({self_alias}.{extract} -> {key_param}) {extractor_op})"""


@dataclass
Expand All @@ -248,11 +249,11 @@ class HStoreExtractorField(AttrField):

def expr(self, query):
key_param = query.makeparam(self.hstore_key)
q_id = quote_identifier(self.extract)
self_alias = query.self_alias()
extract = quote_identifier(self.extract)
self_alias = quote_identifier(query.self_alias())
# Extract text from HStore field, so that it comes from the DB
# as actual text
return f"""({self_alias}.{q_id} -> {key_param})"""
return f"""({self_alias}.{extract} -> {key_param})"""


@dataclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"caluma_form_answer" INNER JOIN "caluma_form_question" ON ("caluma_form_answer"."question_id" = "caluma_form_question"."slug") -- qs ref
)
SELECT * FROM (SELECT
case_ac50e.id AS "analytics_result_case_id",
"case_ac50e"."id" AS "analytics_result_case_id",
"analytics_result_blablub"
FROM case_ac50e AS "case_ac50e"
LEFT JOIN (
Expand All @@ -91,7 +91,7 @@
FROM document_2a07e AS "document_2a07e"
LEFT JOIN (
SELECT DISTINCT ON (document_id)
(answer_af542.value #>>'{}') AS "analytics_result_blablub",
("answer_af542"."value" #>>'{}') AS "analytics_result_blablub",
"document_id"
FROM answer_af542 AS "answer_af542"
WHERE "question_id" = 'top_question'
Expand All @@ -106,7 +106,7 @@
) AS "document_2a07e_27f15" ON (case_ac50e.document_id = "document_2a07e_27f15".id)


) AS analytics_2a8de
) AS analytics_39820
-- PARAMS:

''',
Expand Down Expand Up @@ -171,7 +171,7 @@
"caluma_form_answer" INNER JOIN "caluma_form_question" ON ("caluma_form_answer"."question_id" = "caluma_form_question"."slug") -- qs ref
)
SELECT * FROM (SELECT
case_ac50e.id AS "analytics_result_case_id",
"case_ac50e"."id" AS "analytics_result_case_id",
"analytics_result_blablub"
FROM case_ac50e AS "case_ac50e"
LEFT JOIN (
Expand All @@ -181,7 +181,7 @@
FROM document_2a07e AS "document_2a07e"
LEFT JOIN (
SELECT DISTINCT ON (document_id)
(answer_af542.value #>>'{}') AS "analytics_result_blablub",
("answer_af542"."value" #>>'{}') AS "analytics_result_blablub",
"document_id"
FROM answer_af542 AS "answer_af542"
WHERE "question_id" = 'top_question'
Expand All @@ -196,7 +196,7 @@
) AS "document_2a07e_27f15" ON (case_ac50e.document_id = "document_2a07e_27f15".id)


) AS analytics_2a8de
) AS analytics_39820
WHERE "analytics_result_blablub" IN (%(flt_analytics_result_blablub_8201d)s, %(flt_analytics_result_blablub_8e5e6)s)
-- PARAMS:
-- flt_analytics_result_blablub_8201d: Shelly Watson
Expand Down Expand Up @@ -260,7 +260,7 @@
"caluma_form_answer" INNER JOIN "caluma_form_question" ON ("caluma_form_answer"."question_id" = "caluma_form_question"."slug") -- qs ref
)
SELECT * FROM (SELECT
case_ac50e.id AS "analytics_result_case_id",
"case_ac50e"."id" AS "analytics_result_case_id",
"analytics_result_blablub"
FROM case_ac50e AS "case_ac50e"
LEFT JOIN (
Expand All @@ -270,7 +270,7 @@
FROM document_2a07e AS "document_2a07e"
LEFT JOIN (
SELECT DISTINCT ON (document_id)
(answer_af542.value #>>'{}') AS "analytics_result_blablub",
("answer_af542"."value" #>>'{}') AS "analytics_result_blablub",
"document_id"
FROM answer_af542 AS "answer_af542"
WHERE "question_id" = 'top_question'
Expand All @@ -285,7 +285,7 @@
) AS "document_2a07e_27f15" ON (case_ac50e.document_id = "document_2a07e_27f15".id)


) AS analytics_2a8de
) AS analytics_39820
-- PARAMS:

''',
Expand Down Expand Up @@ -346,7 +346,7 @@
"caluma_form_answer" INNER JOIN "caluma_form_question" ON ("caluma_form_answer"."question_id" = "caluma_form_question"."slug") -- qs ref
)
SELECT * FROM (SELECT
case_ac50e.id AS "analytics_result_case_id",
"case_ac50e"."id" AS "analytics_result_case_id",
"analytics_result_blablub"
FROM case_ac50e AS "case_ac50e"
LEFT JOIN (
Expand All @@ -356,7 +356,7 @@
FROM document_2a07e AS "document_2a07e"
LEFT JOIN (
SELECT DISTINCT ON (document_id)
(answer_af542.value #>>'{}') AS "analytics_result_blablub",
("answer_af542"."value" #>>'{}') AS "analytics_result_blablub",
"document_id"
FROM answer_af542 AS "answer_af542"
WHERE "question_id" = 'top_question'
Expand All @@ -371,7 +371,7 @@
) AS "document_2a07e_27f15" ON (case_ac50e.document_id = "document_2a07e_27f15".id)


) AS analytics_2a8de
) AS analytics_39820
WHERE "analytics_result_blablub" IN (%(flt_analytics_result_blablub_8201d)s, %(flt_analytics_result_blablub_8e5e6)s)
-- PARAMS:
-- flt_analytics_result_blablub_8201d: Shelly Watson
Expand Down

0 comments on commit f8b5a3d

Please sign in to comment.