Red β€” sql-tail

SQL-backed tail

Status: implemented; regression coverage is in t/89-sql-tail.rakutest.

Behavior

  • tail(n) returns a lazy ResultSeq containing the last n elements in their original order.

  • Honor explicit sorting. Without sorting, use the primary key in ascending order, including composite primary keys.

  • tail() executes the query and returns the last projected value or model, preserving the current empty-query behavior.

  • Without an explicit order or available primary key, retain the in-memory behavior. Throw a typed exception when $*RED-FALLBACK = False.

  • Keep inherited forms such as Callable arguments in memory.

Implementation

  • Represent ordering expressions and directions separately. Normalize the existing negative-expression convention for descending order.

  • Preserve row identity as internal metadata through projections. Export keys across SELECT boundaries as hidden fields, without adding them to results.

  • Aggregates, DISTINCT, and grouping invalidate the original row identity; require explicit ordering or use the fallback for these queries.

  • Reverse every ordering component, apply LIMIT n, wrap the selection, and restore the original order in the outer SELECT.

  • Preserve any preceding head/skip selection in a separate boundary before applying tail.

  • Fuse subsequent safe maps and filters into the outer SELECT. Never push those filters before the selection of the last elements.

  • Preserve converters, types, unique aliases, bind ordering, and independent sibling queries. Export ordering values without ASC/DESC in selected fields.

Example:

SELECT last_rows.data_1 * 10 AS bla
FROM (
    SELECT bla.bla AS data_1
    FROM bla
    WHERE bla.bla > 1
    ORDER BY bla.bla DESC
    LIMIT 7
) AS last_rows
WHERE last_rows.data_1 * 10 < 100
  AND last_rows.data_1 * 10 <= 50
ORDER BY last_rows.data_1 ASC

Verification

  • Cover the complete example, returning [40, 50], with a lazy ResultSeq and exactly one SQL execution.

  • Cover models, scalars, and containers; ascending, descending, and composite ordering; implicit simple/composite primary keys; and NULLs in both drivers.

  • Cover tail(0), empty queries, oversized counts, tail(), repeated tails, head/skip combinations, and subsequent filters and sorting.

  • Verify hidden fields, converters, binds, fallback without keys, and typed rejection when fallback is disabled.

  • Run the full SQLite suite and relevant PostgreSQL tests. Update the cookbook and Changes in English.

Defaults

  • Introduce implicit primary-key ordering only when tail needs it, without changing the global ordering of model queries.

  • Do not append automatic tie-breakers to explicit ordering. Include primary keys explicitly when deterministic ordering of ties is required.

  • Respect each driver's NULL ordering when reversing and restoring directions.

Red v0.2.7

A Raku ORM

Authors

  • Fernando Correa de Oliveira

License

Artistic-2.0

Dependencies

DBIishDB::PgUUIDJSON::Fast

Test Dependencies

Provides

  • CX::Red::Bool
  • MetamodelX::Red::Comparate
  • MetamodelX::Red::Describable
  • MetamodelX::Red::Dirtable
  • MetamodelX::Red::Id
  • MetamodelX::Red::Migration
  • MetamodelX::Red::Model
  • MetamodelX::Red::OnDB
  • MetamodelX::Red::Populatable
  • MetamodelX::Red::PreFetch
  • MetamodelX::Red::Refreshable
  • MetamodelX::Red::Relationship
  • MetamodelX::Red::Specialisable
  • MetamodelX::Red::SubModelHOW
  • MetamodelX::Red::Supply
  • MetamodelX::Red::View
  • MetamodelX::Red::VirtualView
  • Red
  • Red::AST
  • Red::AST::AddForeignKeyOnTable
  • Red::AST::BeginTransaction
  • Red::AST::Between
  • Red::AST::Case
  • Red::AST::Chained
  • Red::AST::ChangeColumn
  • Red::AST::Comment
  • Red::AST::CommitTransaction
  • Red::AST::Constraints
  • Red::AST::CreateColumn
  • Red::AST::CreateIndex
  • Red::AST::CreateTable
  • Red::AST::CreateView
  • Red::AST::DateTimeFuncs
  • Red::AST::Delete
  • Red::AST::Divisible
  • Red::AST::DropColumn
  • Red::AST::Empty
  • Red::AST::Function
  • Red::AST::Generic::Infix
  • Red::AST::Generic::Postfix
  • Red::AST::Generic::Prefix
  • Red::AST::Infix
  • Red::AST::Infixes
  • Red::AST::Insert
  • Red::AST::Intersect
  • Red::AST::IsDefined
  • Red::AST::JsonItem
  • Red::AST::JsonOperation
  • Red::AST::JsonRemoveItem
  • Red::AST::JsonSetItem
  • Red::AST::LastInsertedRow
  • Red::AST::Minus
  • Red::AST::MultiSelect
  • Red::AST::Next
  • Red::AST::Operator
  • Red::AST::Optimizer::AND
  • Red::AST::Optimizer::Case
  • Red::AST::Optimizer::OR
  • Red::AST::Ordering
  • Red::AST::ProductSource
  • Red::AST::ProjectionField
  • Red::AST::Reference
  • Red::AST::ReleaseSavepoint
  • Red::AST::RollbackToSavepoint
  • Red::AST::RollbackTransaction
  • Red::AST::Savepoint
  • Red::AST::Select
  • Red::AST::StringFuncs
  • Red::AST::TableComment
  • Red::AST::Unary
  • Red::AST::Union
  • Red::AST::Update
  • Red::AST::Value
  • Red::Attr::Column
  • Red::Attr::Index
  • Red::Attr::Query
  • Red::Attr::Relationship
  • Red::Class
  • Red::Cli
  • Red::Cli::Column
  • Red::Cli::Relationship
  • Red::Cli::Table
  • Red::Column
  • Red::ColumnMethods
  • Red::Config
  • Red::DB
  • Red::Database
  • Red::DefaultResultSeq
  • Red::Do
  • Red::Driver
  • Red::Driver::Cache
  • Red::Driver::Cache::Memory
  • Red::Driver::CacheInvalidateOnWrite
  • Red::Driver::CacheWithStrKey
  • Red::Driver::CommonSQL
  • Red::Driver::Mock
  • Red::Driver::Pg
  • Red::Driver::SQLite
  • Red::Driver::SQLite::SQLiteMaster
  • Red::Driver::SQLite::SchemaReader
  • Red::Event
  • Red::Formatter
  • Red::Formatter::ResultSeq
  • Red::Formatter::ResultSeq::Vertical
  • Red::FromRelationship
  • Red::HiddenFromSQLCommenting
  • Red::Index
  • Red::LockType
  • Red::Migration::Column
  • Red::Migration::Migration
  • Red::Migration::Table
  • Red::Model
  • Red::Operators
  • Red::Phaser
  • Red::PrepareCode
  • Red::Projection
  • Red::ResultAssociative
  • Red::ResultSeq
  • Red::ResultSeq::Iterator
  • Red::ResultSeqMethods
  • Red::ResultSeqSeq
  • Red::Schema
  • Red::SchemaReader
  • Red::Statement
  • Red::Traits
  • Red::Transaction::Guard
  • Red::Type
  • Red::Type::Json
  • Red::Type::Map
  • Red::Utils
  • X::Red::Exceptions

Documentation

The Camelia image is copyright 2009 by Larry Wall. "Raku" is a trademark of the Yet Another Society. All rights reserved.

Built with Podlite β€” the markup and publishing tools behind this site.