Class: QgsExpressionNode

Abstract base class for all nodes that can appear in an expression.

Note

This is an abstract class, with methods which must be implemented by a subclass.

The following methods must be implemented: clone(), dump(), isStatic(), needsGeometry(), nodeType(), referencedColumns(), referencedFunctions(), referencedVariables()

Class Hierarchy

Inheritance diagram of qgis.core.QgsExpressionNode

Subclasses

QgsExpressionNodeBetweenOperator

SQL-like BETWEEN and NOT BETWEEN predicates.

QgsExpressionNodeBinaryOperator

A binary expression operator, which operates on two values.

QgsExpressionNodeColumnRef

An expression node which takes its value from a feature’s field.

QgsExpressionNodeCondition

An expression node for CASE WHEN clauses.

QgsExpressionNodeFunction

An expression node for expression functions.

QgsExpressionNodeInOperator

An expression node for value IN or NOT IN clauses.

QgsExpressionNodeIndexOperator

An indexing expression operator, which allows use of square brackets [] to reference map and array items.

QgsExpressionNodeLiteral

An expression node for literal values.

QgsExpressionNodeUnaryOperator

A unary node is either negative as in boolean (not) or as in numbers (minus).

Abstract Methods

clone

Generate a clone of this node.

dump

Dump this node into a serialized (part) of an expression.

isStatic

Returns True if this node can be evaluated for a static value.

needsGeometry

Abstract virtual method which returns if the geometry is required to evaluate this expression.

nodeType

Gets the type of this node.

referencedColumns

Abstract virtual method which returns a list of columns required to evaluate this node.

referencedFunctions

Returns a set of all functions which are used in this expression.

referencedVariables

Returns a set of all variables which are used in this expression.

Methods

cachedStaticValue

Returns the node's static cached value.

effectiveNode

Returns a reference to the simplest node which represents this node, after any compilation optimizations have been applied.

eval

Evaluate this node with the given context and parent.

hasCachedStaticValue

Returns True if the node can be replaced by a static cached value.

prepare

Prepare this node for evaluation.

Attributes

ntBetweenOperator

ntBinaryOperator

ntColumnRef

ntCondition

ntFunction

ntInOperator

ntIndexOperator

ntLiteral

ntUnaryOperator

parserFirstColumn

First column in the parser this node was found.

parserFirstLine

First line in the parser this node was found.

parserLastColumn

Last column in the parser this node was found.

parserLastLine

Last line in the parser this node was found.

class qgis.core.QgsExpressionNode[source]

Bases: object

class NamedNode

Bases: object

Named node

name: str

Node name

node: QgsExpressionNode

Node

class NodeList[source]

Bases: object

A list of expression nodes.

append(self, node: QgsExpressionNode | None)[source]

Takes ownership of the provided node

Parameters:

node (Optional[QgsExpressionNode])

append(self, node: QgsExpressionNode.NamedNode | None)[source]

Adds a named node. Takes ownership of the provided node.

Parameters:

node (Optional[QgsExpressionNode.NamedNode])

at(self, i: int) QgsExpressionNode | None[source]

Gets the node at position i in the list.

Parameters:

i (int)

Return type:

Optional[QgsExpressionNode]

clone(self) QgsExpressionNode.NodeList | None[source]

Creates a deep copy of this list. Ownership is transferred to the caller

Return type:

Optional[QgsExpressionNode.NodeList]

count(self) int[source]

Returns the number of nodes in the list.

Return type:

int

virtual dump(self) str[source]

Returns a string dump of the expression node.

Return type:

str

hasNamedNodes(self) bool[source]

Returns True if list contains any named nodes

Return type:

bool

list(self) List[QgsExpressionNode]

Gets a list of all the nodes.

Return type:

List[QgsExpressionNode]

names(self) List[str][source]

Returns a list of names for nodes. Unnamed nodes will be indicated by an empty string in the list.

Return type:

List[str]

reserve(self, size: int)[source]

Reserves size for the node list.

Added in version 3.34.

Parameters:

size (int)

class NodeType

Bases: int

cachedStaticValue(self) Any[source]

Returns the node’s static cached value. Only valid if hasCachedStaticValue() is True.

Added in version 3.18.

Return type:

Any

abstract clone(self) QgsExpressionNode | None[source]

Generate a clone of this node. Ownership is transferred to the caller.

Return type:

Optional[QgsExpressionNode]

Returns:

a deep copy of this node.

abstract dump(self) str[source]

Dump this node into a serialized (part) of an expression. The returned expression does not necessarily literally match the original expression, it’s just guaranteed to behave the same way.

Return type:

str

effectiveNode(self) QgsExpressionNode | None[source]

Returns a reference to the simplest node which represents this node, after any compilation optimizations have been applied.

Eg. a node like “CASE WHEN true THEN “some_field” WHEN other condition THEN … END” can effectively be replaced entirely by a QgsExpressionNodeColumnRef referencing the “some_field” field, as the CASE WHEN … will ALWAYS evaluate to “some_field”.

Returns a reference to the current object if no optimizations were applied.

Added in version 3.20.

Return type:

Optional[QgsExpressionNode]

eval(self, parent: QgsExpression | None, context: QgsExpressionContext | None) Any[source]

Evaluate this node with the given context and parent. This will return a cached value if it has been determined to be static during the prepare() execution.

Parameters:
Return type:

Any

hasCachedStaticValue(self) bool[source]

Returns True if the node can be replaced by a static cached value.

Added in version 3.18.

Return type:

bool

abstract isStatic(self, parent: QgsExpression | None, context: QgsExpressionContext | None) bool[source]

Returns True if this node can be evaluated for a static value. This is used during the prepare() step and in case it returns True, the value of this node will already be evaluated and the result cached (and therefore not re-evaluated in subsequent calls to eval()). In case this returns True, prepareNode() will never be called.

Parameters:
Return type:

bool

abstract needsGeometry(self) bool[source]

Abstract virtual method which returns if the geometry is required to evaluate this expression.

This needs to call :py:func:`~QgsExpressionNode.needsGeometry` recursively on any child nodes.

Return type:

bool

Returns:

True if a geometry is required to evaluate this expression

abstract nodeType(self) QgsExpressionNode.NodeType[source]

Gets the type of this node.

Return type:

QgsExpressionNode.NodeType

Returns:

The type of this node

ntBetweenOperator = 8
ntBinaryOperator = 1
ntColumnRef = 5
ntCondition = 6
ntFunction = 3
ntInOperator = 2
ntIndexOperator = 7
ntLiteral = 4
ntUnaryOperator = 0
parserFirstColumn: int

First column in the parser this node was found.

Note

This might not be complete for all nodes. Currently only QgsExpressionNode has this complete

parserFirstLine: int

First line in the parser this node was found.

Note

This might not be complete for all nodes. Currently only QgsExpressionNode has this complete

parserLastColumn: int

Last column in the parser this node was found.

Note

This might not be complete for all nodes. Currently only QgsExpressionNode has this complete

parserLastLine: int

Last line in the parser this node was found.

Note

This might not be complete for all nodes. Currently only QgsExpressionNode has this complete

prepare(self, parent: QgsExpression | None, context: QgsExpressionContext | None) bool[source]

Prepare this node for evaluation. This will check if the node content is static and in this case cache the value. If it’s not static it will call prepareNode() to allow the node to do initialization work like for example resolving a column name to an attribute index.

Parameters:
Return type:

bool

abstract referencedColumns(self) Set[str]

Abstract virtual method which returns a list of columns required to evaluate this node.

When reimplementing this, you need to return any column that is required to evaluate this node and in addition recursively collect all the columns required to evaluate child nodes.

Warning

If the expression has been prepared via a call to QgsExpression.prepare(), or a call to QgsExpressionNode.prepare() for a node has been made, then some nodes in the expression may have been determined to evaluate to a static pre-calculatable value. In this case the results will omit attribute indices which are used by these pre-calculated nodes, regardless of their actual referenced columns. If you are seeking to use these functions to introspect an expression you must take care to do this with an unprepared expression node.

Return type:

Set[str]

Returns:

A list of columns required to evaluate this expression

abstract referencedFunctions(self) Set[str]

Returns a set of all functions which are used in this expression.

Note

In contrast to the referencedColumns() function this method is not affected by any previous calls to QgsExpressionNode.prepare().

Return type:

Set[str]

abstract referencedVariables(self) Set[str]

Returns a set of all variables which are used in this expression.

Note

In contrast to the referencedColumns() function this method is not affected by any previous calls to QgsExpressionNode.prepare().

Return type:

Set[str]