<commit_before>#warning("TODO: doc")
public struct CommonTableExpression<RowDecoder> {
    var tableName: String
    var request: _FetchRequest
}

extension CommonTableExpression {
    var relationForAll: SQLRelation {
        SQLRelation(
            source: .table(tableName: tableName, alias: nil),
            selectionPromise: DatabasePromise(value: [_AllCTEColumns(request: request, alias: nil)]))
    }
    
    #warning("TODO: doc")
    public func all() -> QueryInterfaceRequest<Void> {
        QueryInterfaceRequest(relation: relationForAll)
    }
}

extension TableRecord {
    #warning("TODO: doc")
    public static func association<Destination>(
        to cte: CommonTableExpression<Destination>,
        on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible)
    -> JoinAssociation<Self, Destination>
    {
        JoinAssociation(
            key: .inflected(cte.tableName),
            condition: .expression(condition),
            relation: cte.relationForAll)
    }
    
    #warning("TODO: doc")
    public static func association<Destination>(
        to cte: CommonTableExpression<Destination>,
        using columns: [Column] = [])
    -> JoinAssociation<Self, Destination>
    {
        JoinAssociation(
            key: .inflected(cte.tableName),
            condition: .using(columns),
            relation: cte.relationForAll)
    }
}

extension CommonTableExpression {
    #warning("TODO: doc")
    public func association<Destination>(
        to cte: CommonTableExpression<Destination>,
        on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible)
    -> JoinAssociation<RowDecoder, Destination>
    {
        JoinAssociation(
            key: .inflected(cte.tableName),
            condition: .expression(condition),
            relation: cte.relationForAll)
    }
    
    #warning("TODO: doc")
    public func association<Destination>(
        to cte: CommonTableExpression<Destination>,
        using columns: [Column] = [])
    -> JoinAssociation<RowDecoder, Destination>
    {
        JoinAssociation(
            key: .inflected(cte.tableName),
            condition: .using(columns),
            relation: cte.relationForAll)
    }
    
    #warning("TODO: doc")
    public func association<Destination>(
        to destination: Destination.Type,
        on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible)
    -> JoinAssociation<RowDecoder, Destination>
    where Destination: TableRecord
    {
        JoinAssociation(
            key: .inflected(Destination.databaseTableName),
            condition: .expression(condition),
            relation: Destination.relationForAll)
    }
    
    #warning("TODO: doc")
    public func association<Destination>(
        to destination: Destination.Type,
        using columns: [Column] = [])
    -> JoinAssociation<RowDecoder, Destination>
    where Destination: TableRecord
    {
        JoinAssociation(
            key: .inflected(Destination.databaseTableName),
            condition: .using(columns),
            relation: Destination.relationForAll)
    }
}

extension _FetchRequest {
    #warning("TODO: doc")
    public func commonTableExpression<RowDecoder>(
        tableName: String,
        type: RowDecoder.Type = RowDecoder.self)
    -> CommonTableExpression<RowDecoder>
    {
        CommonTableExpression(
            tableName: tableName,
            request: self)
    }
}

// MARK: - QueryInterfaceRequest

extension QueryInterfaceRequest {
    #warning("TODO: doc")
    public func with<RowDecoder>(_ cte: CommonTableExpression<RowDecoder>) -> Self {
        with(\.query.ctes[cte.tableName], cte.request)
    }
}

// MARK: - _AllCTEColumns

/// :nodoc:
public struct _AllCTEColumns {
    var request: _FetchRequest
    var alias: TableAlias?
}

extension _AllCTEColumns: SQLSelectable, Refinable {<commit_msg>Expose CTE creation method at a documented level<commit_after>#warning("TODO: doc")
public struct CommonTableExpression<RowDecoder> {
    var tableName: String
    var request: _FetchRequest
}

extension CommonTableExpression {
    var relationForAll: SQLRelation {
        SQLRelation(
            source: .table(tableName: tableName, alias: nil),
            selectionPromise: DatabasePromise(value: [_AllCTEColumns(request: request, alias: nil)]))
    }
    
    #warning("TODO: doc")
    public func all() -> QueryInterfaceRequest<Void> {
        QueryInterfaceRequest(relation: relationForAll)
    }
}

extension TableRecord {
    #warning("TODO: doc")
    public static func association<Destination>(
        to cte: CommonTableExpression<Destination>,
        on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible)
    -> JoinAssociation<Self, Destination>
    {
        JoinAssociation(
            key: .inflected(cte.tableName),
            condition: .expression(condition),
            relation: cte.relationForAll)
    }
    
    #warning("TODO: doc")
    public static func association<Destination>(
        to cte: CommonTableExpression<Destination>,
        using columns: [Column] = [])
    -> JoinAssociation<Self, Destination>
    {
        JoinAssociation(
            key: .inflected(cte.tableName),
            condition: .using(columns),
            relation: cte.relationForAll)
    }
}

extension CommonTableExpression {
    #warning("TODO: doc")
    public func association<Destination>(
        to cte: CommonTableExpression<Destination>,
        on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible)
    -> JoinAssociation<RowDecoder, Destination>
    {
        JoinAssociation(
            key: .inflected(cte.tableName),
            condition: .expression(condition),
            relation: cte.relationForAll)
    }
    
    #warning("TODO: doc")
    public func association<Destination>(
        to cte: CommonTableExpression<Destination>,
        using columns: [Column] = [])
    -> JoinAssociation<RowDecoder, Destination>
    {
        JoinAssociation(
            key: .inflected(cte.tableName),
            condition: .using(columns),
            relation: cte.relationForAll)
    }
    
    #warning("TODO: doc")
    public func association<Destination>(
        to destination: Destination.Type,
        on condition: @escaping (TableAlias, TableAlias) -> SQLExpressible)
    -> JoinAssociation<RowDecoder, Destination>
    where Destination: TableRecord
    {
        JoinAssociation(
            key: .inflected(Destination.databaseTableName),
            condition: .expression(condition),
            relation: Destination.relationForAll)
    }
    
    #warning("TODO: doc")
    public func association<Destination>(
        to destination: Destination.Type,
        using columns: [Column] = [])
    -> JoinAssociation<RowDecoder, Destination>
    where Destination: TableRecord
    {
        JoinAssociation(
            key: .inflected(Destination.databaseTableName),
            condition: .using(columns),
            relation: Destination.relationForAll)
    }
}

// MARK: - _FetchRequest

extension _FetchRequest {
    #warning("TODO: doc")
    /// :nodoc:
    public func _commonTableExpression<RowDecoder>(
        tableName: String,
        type: RowDecoder.Type)
    -> CommonTableExpression<RowDecoder>
    {
        CommonTableExpression(
            tableName: tableName,
            request: self)
    }
}

// MARK: - QueryInterfaceRequest

extension QueryInterfaceRequest {
    #warning("TODO: doc")
    public func commonTableExpression<RowDecoder>(
        tableName: String,
        type: RowDecoder.Type = RowDecoder.self)
    -> CommonTableExpression<RowDecoder>
    {
        _commonTableExpression(tableName: tableName, type: RowDecoder.self)
    }
    
    #warning("TODO: doc")
    public func with<RowDecoder>(_ cte: CommonTableExpression<RowDecoder>) -> Self {
        with(\.query.ctes[cte.tableName], cte.request)
    }
}

// MARK: - SQLRequest

extension SQLRequest {
    #warning("TODO: doc")
    public func commonTableExpression<RowDecoder>(
        tableName: String,
        type: RowDecoder.Type = RowDecoder.self)
    -> CommonTableExpression<RowDecoder>
    {
        _commonTableExpression(tableName: tableName, type: RowDecoder.self)
    }
}

// MARK: - _AllCTEColumns

/// :nodoc:
public struct _AllCTEColumns {
    var request: _FetchRequest
    var alias: TableAlias?
}

extension _AllCTEColumns: SQLSelectable, Refinable {
{'fraction_non_alphanumeric': 0.1025488327775131, 'fraction_numerical': 0.0, 'mean_word_length': 2.779027902790279, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '823446', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2285, 'n_words': 493}