sqlfluff.utils.functional: Functional Traversal API

These newer modules provide a higher-level API for rules working with segments and slices. Rules that need to navigate or search the parse tree may benefit from using these. Eventually, the plan is for all rules to use these modules. As of December 30, 2021, 17+ rules use these modules.

The modules listed below are submodules of sqlfluff.utils.functional.

segments Module

Surrogate class for working with Segment collections.

class Segments(*segments: BaseSegment, templated_file: TemplatedFile | None = None)

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.

all(predicate: Callable[[BaseSegment], bool] | None = None) bool

Do all the segments match?

any(predicate: Callable[[BaseSegment], bool] | None = None) bool

Do any of the segments match?

apply(fn: Callable[[BaseSegment], Any]) List[Any]

Apply function to every item.

children(predicate: Callable[[BaseSegment], bool] | None = None) Segments

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

find(segment: BaseSegment | None) int

Returns index if found, -1 if not found.

first(predicate: Callable[[BaseSegment], bool] | None = None) Segments

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

get(index: int = 0, *, default: BaseSegment | None = None) BaseSegment | None

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

iterate_segments(predicate: Callable[[BaseSegment], bool] | None = None) Iterable[Segments]

Loop over each element as a fresh Segments.

last(predicate: Callable[[BaseSegment], bool] | None = None) Segments

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

property raw_segments: Segments

Get raw segments underlying the segments.

property raw_slices: RawFileSlices

Raw slices of the segments, sorted in source file order.

recursive_crawl(*seg_type: str, recurse_into: bool = True) Segments

Recursively crawl for segments of a given type.

recursive_crawl_all() Segments

Recursively crawl all descendant segments.

reversed() Segments

Return the same segments in reverse order.

select(select_if: Callable[[BaseSegment], bool] | None = None, loop_while: Callable[[BaseSegment], bool] | None = None, start_seg: BaseSegment | None = None, stop_seg: BaseSegment | None = 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.

segment_predicates Module

Defines commonly used segment predicates for rule writers.

For consistency, all the predicates in this module are implemented as functions returning functions. This avoids rule writers having to remember the distinction between normal functions and functions returning functions.

This is not necessarily a complete set of predicates covering all possible requirements. Rule authors can define their own predicates as needed, either as regular functions, lambda, etc.

and_(*functions: Callable[[BaseSegment], bool]) Callable[[BaseSegment], bool]

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

get_type() Callable[[BaseSegment], str]

Returns a function that gets segment type.

is_code() Callable[[BaseSegment], bool]

Returns a function that checks if segment is code.

is_comment() Callable[[BaseSegment], bool]

Returns a function that checks if segment is comment.

is_keyword(*keyword_name: str) Callable[[BaseSegment], bool]

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

is_meta() Callable[[BaseSegment], bool]

Returns a function that checks if segment is meta.

is_raw() Callable[[BaseSegment], bool]

Returns a function that checks if segment is raw.

is_templated() Callable[[BaseSegment], bool]

Returns a function that checks if segment is templated.

is_type(*seg_type: str) Callable[[BaseSegment], bool]

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

is_whitespace() Callable[[BaseSegment], bool]

Returns a function that checks if segment is whitespace.

not_(fn: Callable[[BaseSegment], bool]) Callable[[BaseSegment], bool]

Returns a function that computes: not fn().

or_(*functions: Callable[[BaseSegment], bool]) Callable[[BaseSegment], bool]

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

raw_is(*raws: str) Callable[[BaseSegment], bool]

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

raw_slices(segment: BaseSegment, templated_file: TemplatedFile | None) RawFileSlices

Returns raw slices for a segment.

raw_upper_is(*raws: str) Callable[[BaseSegment], bool]

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

templated_slices(segment: BaseSegment, templated_file: TemplatedFile | None) TemplatedFileSlices

Returns raw slices for a segment.

raw_file_slices Module

Surrogate class for working with RawFileSlice collections.

class RawFileSlices(*raw_slices: RawFileSlice, templated_file: TemplatedFile | None = None)

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.

all(predicate: Callable[[RawFileSlice], bool] | None = None) bool

Do all the raw slices match?

any(predicate: Callable[[RawFileSlice], bool] | None = None) bool

Do any of the raw slices match?

select(select_if: Callable[[RawFileSlice], bool] | None = None, loop_while: Callable[[RawFileSlice], bool] | None = None, start_slice: RawFileSlice | None = None, stop_slice: RawFileSlice | None = 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.

raw_file_slice_predicates Module

Defines commonly used raw file slice predicates for rule writers.

For consistency, all the predicates in this module are implemented as functions returning functions. This avoids rule writers having to remember the distinction between normal functions and functions returning functions.

This is not necessarily a complete set of predicates covering all possible requirements. Rule authors can define their own predicates as needed, either as regular functions, lambda, etc.

is_slice_type(*slice_types: str) Callable[[RawFileSlice], bool]

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