diff --git a/caluma/caluma_analytics/pivot_table.py b/caluma/caluma_analytics/pivot_table.py index 2c625bf77..8adf2da7a 100644 --- a/caluma/caluma_analytics/pivot_table.py +++ b/caluma/caluma_analytics/pivot_table.py @@ -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) diff --git a/caluma/caluma_analytics/sql.py b/caluma/caluma_analytics/sql.py index d7d4c5be7..eef23997a 100644 --- a/caluma/caluma_analytics/sql.py +++ b/caluma/caluma_analytics/sql.py @@ -209,14 +209,15 @@ 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 @@ -224,8 +225,8 @@ 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 @@ -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 @@ -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 diff --git a/caluma/caluma_analytics/tests/__snapshots__/test_run_analytics_cmdline.ambr b/caluma/caluma_analytics/tests/__snapshots__/test_run_analytics_cmdline.ambr index 55033e682..df9e3ad51 100644 --- a/caluma/caluma_analytics/tests/__snapshots__/test_run_analytics_cmdline.ambr +++ b/caluma/caluma_analytics/tests/__snapshots__/test_run_analytics_cmdline.ambr @@ -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 ( @@ -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' @@ -106,7 +106,7 @@ ) AS "document_2a07e_27f15" ON (case_ac50e.document_id = "document_2a07e_27f15".id) - ) AS analytics_2a8de + ) AS analytics_39820 -- PARAMS: ''', @@ -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 ( @@ -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' @@ -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 @@ -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 ( @@ -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' @@ -285,7 +285,7 @@ ) AS "document_2a07e_27f15" ON (case_ac50e.document_id = "document_2a07e_27f15".id) - ) AS analytics_2a8de + ) AS analytics_39820 -- PARAMS: ''', @@ -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 ( @@ -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' @@ -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