{"record":{"id":"b7c918fb8443fd15","repo":"apache/superset","slug":"fetch-values-predicate-failed-sql-validation-ms","errorCode":null,"errorMessage":"Fetch values predicate failed SQL validation: %(msg)s","messagePattern":"Fetch values predicate failed SQL validation: (.+?)","errorType":"validation","errorClass":"QueryObjectValidationError","httpStatus":400,"severity":"error","filePath":"superset/connectors/sqla/models.py","lineNumber":1821,"sourceCode":"        fetch_values_predicate = self.fetch_values_predicate\n        if template_processor:\n            fetch_values_predicate = template_processor.process_template(\n                fetch_values_predicate\n            )\n        try:\n            # Re-validate the rendered predicate with the same parser policy\n            # as stored column and metric expressions before embedding it.\n            validate_stored_expression(\n                self.database, self.catalog, self.schema, fetch_values_predicate\n            )\n            return self.text(fetch_values_predicate)\n        except (SupersetSecurityException, QueryClauseValidationException) as ex:\n            message = (\n                ex.error.message\n                if isinstance(ex, SupersetSecurityException)\n                else ex.message\n            )\n            raise QueryObjectValidationError(\n                _(\n                    \"Fetch values predicate failed SQL validation: %(msg)s\",\n                    msg=message,\n                )\n            ) from ex\n        except (TemplateError, SupersetSyntaxErrorException) as ex:\n            msg = getattr(ex, \"message\", str(ex))\n            raise QueryObjectValidationError(\n                _(\n                    \"Error in jinja expression in fetch values predicate: %(msg)s\",\n                    msg=msg,\n                )\n            ) from ex\n\n    def get_template_processor(self, **kwargs: Any) -> BaseTemplateProcessor:\n        return get_template_processor(table=self, database=self.database, **kwargs)\n\n    def get_sqla_table(self) -> TableClause:","sourceCodeStart":1803,"sourceCodeEnd":1839,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/connectors/sqla/models.py#L1803-L1839","documentation":"QueryObjectValidationError raised in SqlaTable.get_fetch_values_predicate (models.py:1821) when the fetch_values_predicate SQL fails stored-expression validation (validate_stored_expression → SupersetSecurityException) or query clause validation (QueryClauseValidationException). The predicate is Jinja-rendered, then re-checked with the same sqlglot parser policy as column/metric expressions; if the rendered text is not a valid single expression, the query is aborted.","triggerScenarios":"Setting a fetch_values_predicate on a dataset whose SQL (after Jinja rendering) does not parse as a single SQL expression — e.g. renders to an empty string, contains a set operation, or has engine-invalid syntax — and then querying a chart that uses fetch values filtering.","commonSituations":"fetch_values_predicate with Jinja that renders empty when its filter is absent; predicate valid in one engine but queried under another; pasting a full WHERE clause (with the WHERE keyword) or multi-statement text into the field.","solutions":["Edit the dataset and make fetch_values_predicate a single valid SQL expression (no leading WHERE, no UNION; e.g. \"country = 'US'\").","Guard Jinja so it always renders to valid SQL in all contexts (|default chains).","Check the embedded %(msg)s from the wrapped SupersetSecurityException/QueryClauseValidationException for the parser's reason.","Temporarily clear the predicate to confirm it is the source, then re-add incrementally."],"exampleFix":"-- before (fetch_values_predicate)\nWHERE country = '{{ filter_values('country')[0] }}'\n\n-- after\ncountry = '{{ filter_values('country') | default(['US'], true) | first }}'","handlingStrategy":"validation","validationCode":"from sqlglot import parse_one\nfrom sqlglot.errors import ParseError\n\ndef fetch_values_predicate_valid(predicate: str) -> bool:\n    rendered_guarded = predicate  # assume Jinja guarded upstream\n    try:\n        parse_one(f\"SELECT {rendered_guarded}\")\n        return True\n    except ParseError:\n        return False","typeGuard":null,"tryCatchPattern":"from superset.exceptions import QueryObjectValidationError\n\ntry:\n    predicate = table.get_fetch_values_predicate(template_processor=processor)\nexcept QueryObjectValidationError as ex:\n    if \"Fetch values predicate failed SQL validation\" in str(ex):\n        log_dataset_config_error(table.id, ex)\n    raise","preventionTips":["Keep fetch_values_predicate a single SQL expression without leading WHERE.","Guard its Jinja with defaults so it renders valid SQL in every context.","Validate the predicate with sqlglot after test-rendering before saving the dataset."],"tags":["jinja","fetch-values-predicate","dataset","validation"],"backgroundTag":null,"analyzedSha":"f4587218dd19d046c3e4d00063e7d27f8a2ed354","analyzedAt":"2026-08-14T22:39:27.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}