{"record":{"id":"a7d61b4b3ec7eac4","repo":"pathwaycom/pathway","slug":"invalid-expression-in-restricted-context","errorCode":null,"errorMessage":"invalid expression in restricted context","messagePattern":"invalid expression in restricted context","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/pathway/internals/graph_runner/expression_evaluator.py","lineNumber":842,"sourceCode":"                return fun(*arg_values, **kwarg_values)\n\n            return wrapped, (*args, *kwargs.values())\n        else:\n            return fun, args\n\n\nclass TableRestrictedRowwiseEvaluator(\n    RowwiseEvaluator, context_type=clmn.TableRestrictedRowwiseContext\n):\n    context: clmn.TableRestrictedRowwiseContext\n\n    def eval_column_val(\n        self,\n        expression: expr.ColumnReference,\n        eval_state: RowwiseEvalState | None = None,\n    ):\n        if expression.table != self.context.table:\n            raise ValueError(\"invalid expression in restricted context\")\n        return super().eval_column_val(expression, eval_state)\n\n\nclass FilterEvaluator(ExpressionEvaluator, context_type=clmn.FilterContext):\n    context: clmn.FilterContext\n\n    def run(self, output_storage: Storage) -> api.Table:\n        input_storage = self.state.get_storage(self.context.input_universe())\n        filtering_column_path = input_storage.get_path(self.context.filtering_column)\n        properties = self._table_properties(output_storage)\n        return self.scope.filter_table(\n            self.state.get_table(input_storage._universe),\n            filtering_column_path,\n            properties,\n        )\n\n\nclass ForgetEvaluator(ExpressionEvaluator, context_type=clmn.ForgetContext):","sourceCodeStart":824,"sourceCodeEnd":860,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/internals/graph_runner/expression_evaluator.py#L824-L860","documentation":"A TableRestrictedRowwiseContext (e.g. expressions inside table.restrict or similar table-scoped operations) only permits column references belonging to its own table. eval_column_val checks expression.table against the context table and raises this ValueError when a reference from a different table leaks in — the operation cannot compute over columns outside its scope.","triggerScenarios":"Inside a restricted-context callback, referencing another table's column: table.restrict(...) bodies or select-with-join contexts that use pw.this plus a captured column from a second table; passing join result expressions back into one side's restricted operation; closures capturing table2.col while operating on table1.","commonSituations":"Refactoring joins: authors pull a column reference from the wrong side; copy-pasting a select body from a join into a single-table restricted operation; mixing pw.this with explicitly-named table references inside restricted APIs.","solutions":["Perform the cross-table access in the join/select where both tables are in scope, then run the restricted operation on the result","Inside the restricted context, reference only columns of the context table (pw.this.* or that table's references)","If you need a constant/foreign value, materialize it as a column in the scoped table first (e.g. via a join or table.with_columns) before restriction"],"exampleFix":"// before\nresult = t1.restrict(...expressions using t2.col...)\n// after\njoined = t1.join(t2, t1.k == t2.k).select(t1.val, extra=t2.col)\nresult = joined.restrict(...)  # only joined.* referenced inside","handlingStrategy":"validation","validationCode":"def refs_only_context_table(expressions, context_table) -> bool:\n    return all(getattr(e, \"table\", context_table) == context_table for e in expressions)","typeGuard":"from pathway.internals.expression import ColumnReference\n\ndef ref_belongs_to(ref: ColumnReference, table) -> bool:\n    return isinstance(ref, ColumnReference) and ref.table == table","tryCatchPattern":null,"preventionTips":["Inside restricted contexts, reference only the context table's columns","Do cross-table column access in a join first, then restrict the result","Materialize needed foreign values as columns of the scoped table before restriction"],"tags":["pathway","restricted-context","column-reference","scope","join"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}