Skip to content

Ppl Options #14843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
bd0ce8f
create main branch of ppl
cherylEnkidu Feb 27, 2025
84691ab
Add stage API
cherylEnkidu Feb 27, 2025
8519a5e
Add stage API
cherylEnkidu Mar 3, 2025
454893b
Add stage API
cherylEnkidu Mar 7, 2025
ed1a19b
API changes
cherylEnkidu Mar 12, 2025
819853c
Add cpp stages and expressions
wu-hui Mar 11, 2025
1be141b
Add more APIs
cherylEnkidu Mar 18, 2025
8fd5f8a
add Field
cherylEnkidu Mar 18, 2025
039b919
merge in main branch
cherylEnkidu Mar 19, 2025
5127bd0
Add UserDataReader support for constant and expr in pipelines
wu-hui Mar 17, 2025
14d2e5c
Add bridge
cherylEnkidu Mar 20, 2025
9895936
merge in push-pqrpmwtupmwv
cherylEnkidu Mar 20, 2025
5d701f8
Add UserDataReader support for constant and expr in pipelines
wu-hui Mar 17, 2025
b2af1c6
solve merge conflicts
cherylEnkidu Mar 20, 2025
cec7f6b
Fix merge conflicts
cherylEnkidu Mar 20, 2025
30b9251
solve conflicts
cherylEnkidu Mar 20, 2025
1ad093a
format and fix conflicts
cherylEnkidu Mar 20, 2025
b59a4fd
Remove placeholder for the test case
cherylEnkidu Mar 21, 2025
7d73155
Support tests
cherylEnkidu Mar 25, 2025
828bb12
Fix bug in decoding
cherylEnkidu Mar 27, 2025
d1d0aa4
Add some convert functions
cherylEnkidu Apr 1, 2025
113da45
add tests
cherylEnkidu Apr 2, 2025
39803e5
replace Any with sendable
cherylEnkidu Apr 2, 2025
6c78523
Update PipelineResult
cherylEnkidu Apr 3, 2025
9598764
add expression and stages
cherylEnkidu Apr 14, 2025
5a7f61f
merge changes
cherylEnkidu Apr 17, 2025
a5f558f
add tests
cherylEnkidu Apr 17, 2025
be5b0b2
change implementation details
cherylEnkidu Apr 24, 2025
e699c17
Add get for ppl result
cherylEnkidu Apr 24, 2025
daf1b68
revert test settings
cherylEnkidu Apr 24, 2025
5b2bd88
Change cmake version
cherylEnkidu Apr 25, 2025
04a16e2
merge in cmake settings
cherylEnkidu Apr 25, 2025
d324c5d
Merge branch 'cheryllin/ppl' into cheryllin/pplapi
cherylEnkidu Apr 26, 2025
23305c0
remove unused variable
cherylEnkidu Apr 28, 2025
6dd3515
revert settinggs
cherylEnkidu Apr 28, 2025
8ff71c2
add expressions
cherylEnkidu May 6, 2025
2372405
Add documentation to Pipeline.swift
cherylEnkidu May 6, 2025
c7ff4b7
merge in main
cherylEnkidu May 6, 2025
d5be6a0
Merge branch 'cheryllin/ppl' into cheryllin/pplapi
cherylEnkidu May 6, 2025
591816b
Fix replace with
cherylEnkidu May 7, 2025
0098829
change tests
cherylEnkidu May 8, 2025
4fb738d
Address feebacks 2
cherylEnkidu May 12, 2025
fd8b97e
first draft
cherylEnkidu May 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
change implementation details
  • Loading branch information
cherylEnkidu committed Apr 24, 2025
commit be5b0b264dc42a5698b5d1eef7dcfa5d32d86b8a
148 changes: 74 additions & 74 deletions Firestore/Source/API/FIRPipelineBridge.mm

Large diffs are not rendered by default.

24 changes: 5 additions & 19 deletions Firestore/Swift/Source/Helper/PipelineHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,12 @@ enum Helper {
}
}

static func selectablesToMap(selectables: [Any]) -> [String: Expr] {
var result = [String: Expr]()
for selectable in selectables {
if let stringSelectable = selectable as? String {
result[stringSelectable] = Field(stringSelectable)
} else if let fieldSelectable = selectable as? Field {
result[fieldSelectable.alias] = fieldSelectable.expr
} else if let exprAliasSelectable = selectable as? ExprWithAlias {
result[exprAliasSelectable.alias] = exprAliasSelectable.expr
}
static func selectablesToMap(selectables: [Selectable]) -> [String: Expr] {
let exprMap = selectables.reduce(into: [String: Expr]()) { result, selectable in
let value = selectable as! SelectableWrapper
result[value.alias] = value.expr
}
return result
}

static func vectorToExpr(_ value: VectorValue) -> Expr {
return Field("PLACEHOLDER")
}

static func timeUnitToExpr(_ value: TimeUnit) -> Expr {
return Field("PLACEHOLDER")
return exprMap
}

static func map(_ elements: [String: Sendable]) -> FunctionExpr {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ protocol BridgeWrapper {
protocol AggregateBridgeWrapper {
var bridge: AggregateFunctionBridge { get }
}

protocol SelectableWrapper: Sendable {
var alias: String { get }
var expr: Expr { get }
}
18 changes: 0 additions & 18 deletions Firestore/Swift/Source/SelectableInternal.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class AggregateFunction: AggregateBridgeWrapper, @unchecked Sendable {
self.agrs = agrs
bridge = AggregateFunctionBridge(
name: functionName,
args: self.agrs.map { ($0 as! BridgeWrapper).bridge
args: self.agrs.map { $0.toBridge()
}
)
}
Expand Down
13 changes: 0 additions & 13 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Count.swift

This file was deleted.

15 changes: 8 additions & 7 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#endif // SWIFT_PACKAGE
import Foundation

// TODO: the implementation of `Expr` is not complete
public protocol Expr: Sendable {
func `as`(_ name: String) -> ExprWithAlias

Expand Down Expand Up @@ -607,7 +608,7 @@ public extension Expr {
}

func cosineDistance(_ other: VectorValue) -> FunctionExpr {
return FunctionExpr("cosine_distance", [self, Helper.vectorToExpr(other)])
return FunctionExpr("cosine_distance", [self, Helper.sendableToExpr(other)])
}

func cosineDistance(_ other: [Double]) -> FunctionExpr {
Expand All @@ -619,7 +620,7 @@ public extension Expr {
}

func dotProduct(_ other: VectorValue) -> FunctionExpr {
return FunctionExpr("dot_product", [self, Helper.vectorToExpr(other)])
return FunctionExpr("dot_product", [self, Helper.sendableToExpr(other)])
}

func dotProduct(_ other: [Double]) -> FunctionExpr {
Expand All @@ -631,7 +632,7 @@ public extension Expr {
}

func euclideanDistance(_ other: VectorValue) -> FunctionExpr {
return FunctionExpr("euclidean_distance", [self, Helper.vectorToExpr(other)])
return FunctionExpr("euclidean_distance", [self, Helper.sendableToExpr(other)])
}

func euclideanDistance(_ other: [Double]) -> FunctionExpr {
Expand All @@ -643,7 +644,7 @@ public extension Expr {
}

func manhattanDistance(_ other: VectorValue) -> FunctionExpr {
return FunctionExpr("manhattan_distance", [self, Helper.vectorToExpr(other)])
return FunctionExpr("manhattan_distance", [self, Helper.sendableToExpr(other)])
}

func manhattanDistance(_ other: [Double]) -> FunctionExpr {
Expand Down Expand Up @@ -683,7 +684,7 @@ public extension Expr {
func timestampAdd(_ unit: TimeUnit, _ amount: Int) -> FunctionExpr {
return FunctionExpr(
"timestamp_add",
[self, Helper.timeUnitToExpr(unit), Helper.sendableToExpr(amount)]
[self, Helper.sendableToExpr(unit), Helper.sendableToExpr(amount)]
)
}

Expand All @@ -694,7 +695,7 @@ public extension Expr {
func timestampSub(_ unit: TimeUnit, _ amount: Int) -> FunctionExpr {
return FunctionExpr(
"timestamp_sub",
[self, Helper.timeUnitToExpr(unit), Helper.sendableToExpr(amount)]
[self, Helper.sendableToExpr(unit), Helper.sendableToExpr(amount)]
)
}

Expand Down Expand Up @@ -780,7 +781,7 @@ public extension Expr {
}

extension Expr {
func exprToExprBridge() -> ExprBridge {
func toBridge() -> ExprBridge {
return (self as! BridgeWrapper).bridge
}
}
Expand Down
7 changes: 5 additions & 2 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Expr/Constant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ public struct Constant: Expr, BridgeWrapper, @unchecked Sendable {
// Initializer for optional values (including nil)
init(_ value: Any?) {
self.value = value
// TODO:
bridge = ConstantBridge(value)
if value == nil {
bridge = ConstantBridge(NSNull())
} else {
bridge = ConstantBridge(value!)
}
}

// Initializer for numbers
Expand Down
4 changes: 2 additions & 2 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Expr/Field.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

public class Field: ExprBridge, Expr, Selectable, BridgeWrapper, SelectableInternal,
public class Field: ExprBridge, Expr, Selectable, BridgeWrapper, SelectableWrapper,
@unchecked Sendable {
var bridge: ExprBridge

var alias: String

var expr: any Expr {
var expr: Expr {
return self
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class FunctionExpr: Expr, BridgeWrapper, @unchecked Sendable {
self.agrs = agrs
bridge = FunctionExprBridge(
name: functionName,
args: self.agrs.map { ($0 as! BridgeWrapper).bridge
args: self.agrs.map { $0.toBridge()
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class BooleanExpr: FunctionExpr, @unchecked Sendable {
try BooleanExpr("or", [lhs, rhs()])
}

// not
public static prefix func ! (lhs: BooleanExpr) -> BooleanExpr {
return BooleanExpr("not", [lhs])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

public struct ExprWithAlias: Selectable, SelectableInternal, Sendable {
public struct ExprWithAlias: Selectable, SelectableWrapper, Sendable {
public var alias: String

public var expr: Expr
Expand Down
13 changes: 0 additions & 13 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Min.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Firestore/Swift/Source/SwiftAPI/Pipeline/Ordering.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Ordering: @unchecked Sendable {
init(expr: Expr, direction: Direction) {
self.expr = expr
self.direction = direction
bridge = OrderingBridge(expr: expr.exprToExprBridge(), direction: direction.rawValue)
bridge = OrderingBridge(expr: expr.toBridge(), direction: direction.rawValue)
}
}

Expand Down
14 changes: 9 additions & 5 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Pipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public struct Pipeline: @unchecked Sendable {
/// - Parameter selections: `String` values representing field names.
/// - Returns: A new `Pipeline` object with this stage appended to the stage list.
public func select(_ selection: String, _ additionalSelections: String...) -> Pipeline {
let selections = [selection] + additionalSelections
let selections = ([selection] + additionalSelections).map { Field($0) }
return Pipeline(
stages: stages + [Select(selections: selections + additionalSelections)],
stages: stages + [Select(selections: selections)],
db: db
)
}
Expand Down Expand Up @@ -188,8 +188,8 @@ public struct Pipeline: @unchecked Sendable {
/// - Parameter selections: The fields to include in the output documents, specified as
/// `String` values representing field names.
public func distinct(_ group: String, _ additionalGroups: String...) -> Pipeline {
let groups = [group] + additionalGroups
return Pipeline(stages: stages + [Distinct(groups: groups + additionalGroups)], db: db)
let selections = ([group] + additionalGroups).map { Field($0) }
return Pipeline(stages: stages + [Distinct(groups: selections)], db: db)
}

/// Returns a set of distinct `Expr` values from the inputs to this stage.
Expand Down Expand Up @@ -276,7 +276,11 @@ public struct Pipeline: @unchecked Sendable {
/// - Returns: A new `Pipeline` object with this stage appended.
public func aggregate(_ accumulator: [AggregateWithAlias],
groups: [String]? = nil) -> Pipeline {
return Pipeline(stages: stages + [Aggregate(accumulators: accumulator, groups: groups)], db: db)
let selectables = groups?.map { Field($0) }
return Pipeline(
stages: stages + [Aggregate(accumulators: accumulator, groups: selectables)],
db: db
)
}

/// Performs a vector similarity search, ordering the result set by most similar to least
Expand Down
22 changes: 18 additions & 4 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/PipelineResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import Foundation

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
public struct PipelineResult<T>: @unchecked Sendable {
public struct PipelineResult: @unchecked Sendable {
let bridge: __PipelineResultBridge

init(_ bridge: __PipelineResultBridge) {
self.bridge = bridge
ref = self.bridge.reference
id = self.bridge.documentID
data = self.bridge.data() as! T
data = self.bridge.data()
createTime = self.bridge.create_time
updateTime = self.bridge.update_time
}
Expand All @@ -45,12 +45,26 @@ public struct PipelineResult<T>: @unchecked Sendable {
public let updateTime: Timestamp?

/// Retrieves all fields in the result as a dictionary.
public let data: T
public let data: [String: Sendable]

/// Retrieves the field specified by `fieldPath`.
/// - Parameter fieldPath: The field path (e.g., "foo" or "foo.bar").
/// - Returns: The data at the specified field location or `nil` if no such field exists.
public func get(_ fieldPath: Any) -> Sendable? {
public func get(_ fieldName: String) -> Sendable? {
return data[fieldName]
}

/// Retrieves the field specified by `fieldPath`.
/// - Parameter fieldPath: The field path (e.g., "foo" or "foo.bar").
/// - Returns: The data at the specified field location or `nil` if no such field exists.
public func get(_ fieldPath: FieldPath) -> Sendable? {
return "PLACEHOLDER"
}

/// Retrieves the field specified by `fieldPath`.
/// - Parameter fieldPath: The field path (e.g., "foo" or "foo.bar").
/// - Returns: The data at the specified field location or `nil` if no such field exists.
public func get(_ field: Field) -> Sendable? {
return data[field.fieldName]
}
}
13 changes: 2 additions & 11 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/PipelineSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct PipelineSnapshot: Sendable {
public let pipeline: Pipeline

/// An array of all the results in the `PipelineSnapshot`.
let results_cache: [PipelineResult<[String: Sendable]>]
let results_cache: [PipelineResult]

/// The time at which the pipeline producing this result was executed.
public let executionTime: Timestamp
Expand All @@ -39,16 +39,7 @@ public struct PipelineSnapshot: Sendable {
results_cache = self.bridge.results.map { PipelineResult($0) }
}

public func results() -> [PipelineResult<[String: Sendable]>] {
public func results() -> [PipelineResult] {
return results_cache
}

public func results<T: Decodable>(decodeAsType: T.Type = T.self,
decoder: Firestore
.Decoder = .init()) async throws -> [PipelineResult<
T
>] {
return try decoder
.decode(T.self, from: results, in: nil as DocumentReference?) as! [PipelineResult<T>]
}
}
11 changes: 6 additions & 5 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/PipelineSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,23 @@ public struct PipelineSource: @unchecked Sendable {
}

public func database() -> Pipeline {
return Pipeline(stages: [CollectionSource(collection: "placeholder")], db: db)
return Pipeline(stages: [DatabaseSource()], db: db)
}

public func documents(_ docs: [DocumentReference]) -> Pipeline {
return Pipeline(stages: [CollectionSource(collection: "placeholder")], db: db)
let paths = docs.map { $0.path }
return Pipeline(stages: [DocumentsSource(paths: paths)], db: db)
}

public func documents(_ paths: [String]) -> Pipeline {
return Pipeline(stages: [CollectionSource(collection: "placeholder")], db: db)
return Pipeline(stages: [DocumentsSource(paths: paths)], db: db)
}

public func create(from query: Query) -> Pipeline {
return Pipeline(stages: [CollectionSource(collection: "placeholder")], db: db)
return Pipeline(stages: [QuerySource(query: query)], db: db)
}

public func create(from aggregateQuery: AggregateQuery) -> Pipeline {
return Pipeline(stages: [CollectionSource(collection: "placeholder")], db: db)
return Pipeline(stages: [AggregateQuerySource(aggregateQuery: aggregateQuery)], db: db)
}
}
15 changes: 0 additions & 15 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Sum.swift

This file was deleted.

8 changes: 0 additions & 8 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/TimeUnit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,4 @@ public struct TimeUnit: Sendable, Equatable, Hashable {
init(kind: Kind) {
rawValue = kind.rawValue
}

public init(rawValue: String) {
if let kind = Kind(rawValue: rawValue) {
self.rawValue = kind.rawValue
} else {
fatalError("Invalid TimeUnit: \(rawValue)")
}
}
}
Loading
Loading