Skip to content

Functional Traversal API

This section is for rule authors who need to navigate the parse tree.

The sqlfluff.utils.functional submodules provide a higher-level API for working with parse-tree segments and raw file slices. Rules using these classes can express traversal and filtering logic more concisely than working with the raw segment tree directly.

Source: sqlfluff/utils/functional/

Segments

Encapsulates a sequence of one or more BaseSegments.

The segments may or may not be contiguous in a parse tree. Provides useful operations on a sequence of segments to simplify rule creation.

Parameters:

ParameterTypeDefaultDescription
_BaseSegment
templated_fileTemplatedFile | NoneNone

Methods

all

python
all(
    predicate=None
) → bool

Do all the segments match?

Parameters:

ParameterTypeDefaultDescription
predicateCallable[[BaseSegment], bool] | NoneNone

Returns:

bool — See return type.

any

python
any(
    predicate=None
) → bool

Do any of the segments match?

Parameters:

ParameterTypeDefaultDescription
predicateCallable[[BaseSegment], bool] | NoneNone

Returns:

bool — See return type.

apply

python
apply(
    fn
) → list[Any]

Apply function to every item.

Parameters:

ParameterTypeDefaultDescription
fnCallable[[BaseSegment], Any]

Returns:

list[Any] — See return type.

children

python
children(
    predicate=None
) → Segments

Returns an object with children of the segments in this object.

Parameters:

ParameterTypeDefaultDescription
predicateCallable[[BaseSegment], bool] | NoneNone

Returns:

Segments — See return type.

find

python
find(
    segment
) → int

Returns index if found, -1 if not found.

Parameters:

ParameterTypeDefaultDescription
segmentBaseSegment | None

Returns:

int — See return type.

first

python
first(
    predicate=None
) → Segments

Returns the first segment (if any) that satisfies the predicates.

Parameters:

ParameterTypeDefaultDescription
predicateCallable[[BaseSegment], bool] | NoneNone

Returns:

Segments — See return type.

get

python
get(
    index=0,
    default=None
) → BaseSegment | None

Return specified item. Returns default if index out of range.

Parameters:

ParameterTypeDefaultDescription
indexint0
defaultBaseSegment | NoneNone

Returns:

BaseSegment | None — See return type.

iterate_segments

python
iterate_segments(
    predicate=None
) → collections.abc.Iterable[Segments]

Loop over each element as a fresh Segments.

Parameters:

ParameterTypeDefaultDescription
predicateCallable[[BaseSegment], bool] | NoneNone

Returns:

collections.abc.Iterable[Segments] — See return type.

last

python
last(
    predicate=None
) → Segments

Returns the last segment (if any) that satisfies the predicates.

Parameters:

ParameterTypeDefaultDescription
predicateCallable[[BaseSegment], bool] | NoneNone

Returns:

Segments — See return type.

recursive_crawl

python
recursive_crawl(
    seg_type,
    recurse_into=True
) → Segments

Recursively crawl for segments of a given type.

Parameters:

ParameterTypeDefaultDescription
seg_typestr
recurse_intoboolTrue

Returns:

Segments — See return type.

recursive_crawl_all

python
recursive_crawl_all() → Segments

Recursively crawl all descendant segments.

Returns:

Segments — See return type.

reversed

python
reversed() → Segments

Return the same segments in reverse order.

Returns:

Segments — See return type.

select

python
select(
    select_if=None,
    loop_while=None,
    start_seg=None,
    stop_seg=None
) → Segments

Retrieve range/subset.

NOTE: Iterates the segments BETWEEN start_seg and stop_seg, i.e. those segments are not included in the loop.

Parameters:

ParameterTypeDefaultDescription
select_ifCallable[[BaseSegment], bool] | NoneNone
loop_whileCallable[[BaseSegment], bool] | NoneNone
start_segBaseSegment | NoneNone
stop_segBaseSegment | NoneNone

Returns:

Segments — See return type.

segment_predicates functions

These predicate functions are passed to Segments.select() and similar methods to filter segments by their properties.

and_

python
and_(
    functions
) → Callable[[BaseSegment], bool]

Returns a function that computes the functions and-ed together.

Parameters:

ParameterTypeDefaultDescription
functionsCallable[[BaseSegment], bool]

Returns:

Callable[[BaseSegment], bool] — See return type.

get_type

python
get_type() → Callable[[BaseSegment], str]

Returns a function that gets segment type.

Returns:

Callable[[BaseSegment], str] — See return type.

is_code

python
is_code() → Callable[[BaseSegment], bool]

Returns a function that checks if segment is code.

Returns:

Callable[[BaseSegment], bool] — See return type.

is_comment

python
is_comment() → Callable[[BaseSegment], bool]

Returns a function that checks if segment is comment.

Returns:

Callable[[BaseSegment], bool] — See return type.

is_keyword

python
is_keyword(
    keyword_name
) → Callable[[BaseSegment], bool]

Returns a function that determines if it's a matching keyword.

Parameters:

ParameterTypeDefaultDescription
keyword_namestr

Returns:

Callable[[BaseSegment], bool] — See return type.

is_meta

python
is_meta() → Callable[[BaseSegment], bool]

Returns a function that checks if segment is meta.

Returns:

Callable[[BaseSegment], bool] — See return type.

is_raw

python
is_raw() → Callable[[BaseSegment], bool]

Returns a function that checks if segment is raw.

Returns:

Callable[[BaseSegment], bool] — See return type.

is_templated

python
is_templated() → Callable[[BaseSegment], bool]

Returns a function that checks if segment is templated.

Returns:

Callable[[BaseSegment], bool] — See return type.

is_type

python
is_type(
    seg_type
) → Callable[[BaseSegment], bool]

Returns a function that determines if segment is one of the types.

Parameters:

ParameterTypeDefaultDescription
seg_typestr

Returns:

Callable[[BaseSegment], bool] — See return type.

is_whitespace

python
is_whitespace() → Callable[[BaseSegment], bool]

Returns a function that checks if segment is whitespace.

Returns:

Callable[[BaseSegment], bool] — See return type.

not_

python
not_(
    fn
) → Callable[[BaseSegment], bool]

Returns a function that computes: not fn().

Parameters:

ParameterTypeDefaultDescription
fnCallable[[BaseSegment], bool]

Returns:

Callable[[BaseSegment], bool] — See return type.

or_

python
or_(
    functions
) → Callable[[BaseSegment], bool]

Returns a function that computes the functions or-ed together.

Parameters:

ParameterTypeDefaultDescription
functionsCallable[[BaseSegment], bool]

Returns:

Callable[[BaseSegment], bool] — See return type.

raw_is

python
raw_is(
    raws
) → Callable[[BaseSegment], bool]

Returns a function that determines if segment matches one of the raw inputs.

Parameters:

ParameterTypeDefaultDescription
rawsstr

Returns:

Callable[[BaseSegment], bool] — See return type.

raw_slices

python
raw_slices(
    segment,
    templated_file
) → RawFileSlices

Returns raw slices for a segment.

Parameters:

ParameterTypeDefaultDescription
segmentBaseSegment
templated_fileTemplatedFile | None

Returns:

RawFileSlices — See return type.

raw_upper_is

python
raw_upper_is(
    raws
) → Callable[[BaseSegment], bool]

Returns a function that determines if segment matches one of the raw inputs.

Parameters:

ParameterTypeDefaultDescription
rawsstr

Returns:

Callable[[BaseSegment], bool] — See return type.

templated_slices

python
templated_slices(
    segment,
    templated_file
) → TemplatedFileSlices

Returns raw slices for a segment.

Parameters:

ParameterTypeDefaultDescription
segmentBaseSegment
templated_fileTemplatedFile | None

Returns:

TemplatedFileSlices — See return type.

RawFileSlices

Encapsulates a sequence of one or more RawFileSlice.

The slices may or may not be contiguous in a file. Provides useful operations on a sequence of slices to simplify rule creation.

Parameters:

ParameterTypeDefaultDescription
_RawFileSlice
templated_fileTemplatedFile

Methods

all

python
all(
    predicate=None
) → bool

Do all the raw slices match?

Parameters:

ParameterTypeDefaultDescription
predicateCallable[[RawFileSlice], bool] | NoneNone

Returns:

bool — See return type.

any

python
any(
    predicate=None
) → bool

Do any of the raw slices match?

Parameters:

ParameterTypeDefaultDescription
predicateCallable[[RawFileSlice], bool] | NoneNone

Returns:

bool — See return type.

select

python
select(
    select_if=None,
    loop_while=None,
    start_slice=None,
    stop_slice=None
) → RawFileSlices

Retrieve range/subset.

NOTE: Iterates the slices BETWEEN start_slice and stop_slice, i.e. those slices are not included in the loop.

Parameters:

ParameterTypeDefaultDescription
select_ifCallable[[RawFileSlice], bool] | NoneNone
loop_whileCallable[[RawFileSlice], bool] | NoneNone
start_sliceRawFileSlice | NoneNone
stop_sliceRawFileSlice | NoneNone

Returns:

RawFileSlices — See return type.

raw_file_slice_predicates functions

Predicate functions for use with RawFileSlices.select(), mirroring the role of segment_predicates for the raw slice layer.

is_slice_type

python
is_slice_type(
    slice_types
) → Callable[[RawFileSlice], bool]

Returns a function that determines if segment is one of the types.

Parameters:

ParameterTypeDefaultDescription
slice_typesstr

Returns:

Callable[[RawFileSlice], bool] — See return type.

Released under the MIT License.