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
Support tests
  • Loading branch information
cherylEnkidu committed Mar 25, 2025
commit 7d73155c9cd69ce12fc92612c4886897678d9ac3
40 changes: 34 additions & 6 deletions Firestore/Source/API/FIRPipelineBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

#import "FIRPipelineBridge.h"

#import <FirebaseCore/FIRTimestamp.h>

#include <memory>

#import "Firestore/Source/API/FIRDocumentReference+Internal.h"
#import "Firestore/Source/API/FIRFirestore+Internal.h"
#import "Firestore/Source/API/FIRPipelineBridge+Internal.h"
#import "Firestore/Source/API/FSTUserDataReader.h"
#import "Firestore/Source/API/FSTUserDataWriter.h"
#import "Firestore/Source/API/converters.h"

#include "Firestore/Protos/nanopb/google/firestore/v1/document.nanopb.h"

Expand All @@ -42,6 +45,7 @@
using firebase::firestore::api::Expr;
using firebase::firestore::api::Field;
using firebase::firestore::api::FunctionExpr;
using firebase::firestore::api::MakeFIRTimestamp;
using firebase::firestore::api::Pipeline;
using firebase::firestore::api::Where;
using firebase::firestore::util::MakeCallback;
Expand Down Expand Up @@ -173,7 +177,7 @@ - (id)initWithExpr:(FIRExprBridge *)expr {

@interface __FIRPipelineSnapshotBridge ()

@property(nonatomic, strong, readwrite) NSArray<__FIRPipelineSnapshotBridge *> *results;
@property(nonatomic, strong, readwrite) NSArray<__FIRPipelineResultBridge *> *results;

@end

Expand Down Expand Up @@ -206,27 +210,51 @@ - (id)initWithCppSnapshot:(api::PipelineSnapshot)snapshot {
return results_;
}

- (FIRTimestamp *)execution_time {
if (!snapshot_.has_value()) {
return nil;
} else {
return MakeFIRTimestamp(snapshot_.value().execution_time().timestamp());
}
}

@end

@implementation __FIRPipelineResultBridge {
api::PipelineResult _result;
std::shared_ptr<api::Firestore> _db;
}

- (FIRDocumentReference *)reference {
- (nullable FIRDocumentReference *)reference {
if (!_result.internal_key().has_value()) return nil;

return [[FIRDocumentReference alloc] initWithKey:_result.internal_key().value() firestore:_db];
}

- (NSString *)documentID {
- (nullable NSString *)documentID {
if (!_result.document_id().has_value()) {
return nil;
}

return MakeNSString(_result.document_id().value());
}

- (nullable FIRTimestamp *)create_time {
if (!_result.create_time().has_value()) {
return nil;
}

return MakeFIRTimestamp(_result.create_time().value().timestamp());
}

- (nullable FIRTimestamp *)update_time {
if (!_result.update_time().has_value()) {
return nil;
}

return MakeFIRTimestamp(_result.update_time().value().timestamp());
}

- (id)initWithCppResult:(api::PipelineResult)result db:(std::shared_ptr<api::Firestore>)db {
self = [super init];
if (self) {
Expand All @@ -237,15 +265,15 @@ - (id)initWithCppResult:(api::PipelineResult)result db:(std::shared_ptr<api::Fir
return self;
}

- (nullable NSDictionary<NSString *, id> *)data {
- (NSDictionary<NSString *, id> *)data {
return [self dataWithServerTimestampBehavior:FIRServerTimestampBehaviorNone];
}

- (nullable NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
- (NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
(FIRServerTimestampBehavior)serverTimestampBehavior {
absl::optional<firebase::firestore::google_firestore_v1_Value> data =
_result.internal_value()->Get();
if (!data) return nil;
if (!data) return [NSDictionary dictionary];

FSTUserDataWriter *dataWriter =
[[FSTUserDataWriter alloc] initWithFirestore:_db
Expand Down
29 changes: 19 additions & 10 deletions Firestore/Source/Public/FirebaseFirestore/FIRPipelineBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#import "FIRDocumentSnapshot.h"

@class FIRTimestamp;

NS_ASSUME_NONNULL_BEGIN

NS_SWIFT_NAME(ExprBridge)
Expand Down Expand Up @@ -59,26 +61,33 @@ NS_SWIFT_NAME(WhereStageBridge)

@end

NS_SWIFT_NAME(__PipelineSnapshotBridge)
@interface __FIRPipelineSnapshotBridge : NSObject
NS_SWIFT_NAME(__PipelineResultBridge)
@interface __FIRPipelineResultBridge : NSObject

@property(nonatomic, strong, readonly) NSArray<__FIRPipelineSnapshotBridge *> *results;
@property(nonatomic, strong, readonly, nullable) FIRDocumentReference *reference;

@end
@property(nonatomic, copy, readonly, nullable) NSString *documentID;

NS_SWIFT_NAME(__PipelineResultBridge)
@interface __FIRPipelineResultBridge : NSObject
@property(nonatomic, strong, readonly, nullable) FIRTimestamp *create_time;

@property(nonatomic, strong, readonly) FIRDocumentReference *reference;
@property(nonatomic, strong, readonly, nullable) FIRTimestamp *update_time;

@property(nonatomic, copy, readonly) NSString *documentID;
- (NSDictionary<NSString *, id> *)data;

- (nullable NSDictionary<NSString *, id> *)data;
- (nullable NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
- (NSDictionary<NSString *, id> *)dataWithServerTimestampBehavior:
(FIRServerTimestampBehavior)serverTimestampBehavior;

@end

NS_SWIFT_NAME(__PipelineSnapshotBridge)
@interface __FIRPipelineSnapshotBridge : NSObject

@property(nonatomic, strong, readonly) NSArray<__FIRPipelineResultBridge *> *results;

@property(nonatomic, strong, readonly) FIRTimestamp *execution_time;

@end

NS_SWIFT_NAME(PipelineBridge)
@interface FIRPipelineBridge : NSObject

Expand Down
3 changes: 3 additions & 0 deletions Firestore/Swift/Source/Helper/PipelineHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

enum Helper {
static func valueToDefaultExpr(_ value: Any) -> any Expr {
if value is Constant {
return value as! Expr
}
return Field("PLACEHOLDER")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

public struct SampleOption {
let percentage: Double?
let count: Int64?

private init(percentage: Double?, count: Int64?) {
self.percentage = percentage
self.count = count
}

public init(percentage: Double) {
self.init(percentage: percentage, count: nil)
}

public init(count: Int64) {
self.init(percentage: nil, count: count)
}
protocol SelectableInternal: Sendable {
var alias: String { get }
var expr: Expr { get }
}
2 changes: 1 addition & 1 deletion Firestore/Swift/Source/SwiftAPI/Pipeline/Expr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ public func <= (lhs: Expr, rhs: @autoclosure () throws -> Any) rethrows -> Boole
}

public func == (lhs: Expr, rhs: @autoclosure () throws -> Any) rethrows -> BooleanExpr {
try BooleanExpr("eq", [lhs, rhs() as! Expr])
try BooleanExpr("eq", [lhs, Helper.valueToDefaultExpr(rhs())])
}

public func != (lhs: Expr, rhs: @autoclosure () throws -> Any) rethrows -> BooleanExpr {
Expand Down
13 changes: 7 additions & 6 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Expr/Field.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

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

public var alias: String
var alias: String

var expr: any Expr {
return self
}

public let fieldName: String

Expand All @@ -24,8 +29,4 @@ public class Field: ExprBridge, Expr, Selectable, BridgeWrapper, @unchecked Send
alias = fieldName
bridge = FieldBridge(alias)
}

public var expr: any Expr {
return self
}
}
4 changes: 2 additions & 2 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/ExprWithAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
public struct ExprWithAlias: Selectable {
public var alias: String

public var expr: any Expr
public var expr: Expr

init(_ expr: some Expr, _ alias: String) {
init(_ expr: Expr, _ alias: String) {
self.alias = alias
self.expr = expr
}
Expand Down
14 changes: 7 additions & 7 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Pipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct Pipeline {
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: PipelineSnapshot(result!))
continuation.resume(returning: PipelineSnapshot(result!, pipeline: self))
}
}
}
Expand Down Expand Up @@ -288,9 +288,9 @@ public struct Pipeline {
/// This stage allows you to emit a map value as a document. Each key of the map becomes a
/// field on the document that contains the corresponding value.
///
/// - Parameter field: The `Selectable` field containing the nested map.
/// - Parameter field: The `Expr` field containing the nested map.
/// - Returns: A new `Pipeline` object with this stage appended to the stage list.
public func replace(with field: Selectable) -> Pipeline {
public func replace(with expr: Expr) -> Pipeline {
// Implementation
return self
}
Expand All @@ -312,9 +312,9 @@ public struct Pipeline {
/// This stage will filter documents pseudo-randomly. The parameter specifies how number of
/// documents to be returned.
///
/// - Parameter documents: The number of documents to sample.
/// - Parameter count: The number of documents to sample.
/// - Returns: A new `Pipeline` object with this stage appended to the stage list.
public func sample(_ count: Int64) -> Pipeline {
public func sample(count: Int64) -> Pipeline {
// Implementation
return self
}
Expand All @@ -324,9 +324,9 @@ public struct Pipeline {
/// This stage will filter documents pseudo-randomly. The `options` parameter specifies how
/// sampling will be performed. See `SampleOptions` for more information.
///
/// - Parameter options: The `SampleOptions` specifies how sampling is performed.
/// - Parameter percentage: The percentage of documents to sample.
/// - Returns: A new `Pipeline` object with this stage appended to the stage list.
public func sample(with option: SampleOption) -> Pipeline {
public func sample(percentage: Double) -> Pipeline {
// Implementation
return self
}
Expand Down
19 changes: 9 additions & 10 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/PipelineResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@ import Foundation

@available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
public struct PipelineResult {
init(ref: DocumentReference? = nil,
id: String? = nil,
createTime: Timestamp? = nil,
updateTime: Timestamp? = nil,
data: [String: Any] = [:]) {
self.ref = ref
self.id = id
self.createTime = createTime
self.updateTime = updateTime
self.data = data
let bridge: __PipelineResultBridge

init(_ bridge: __PipelineResultBridge) {
self.bridge = bridge
ref = self.bridge.reference
id = self.bridge.documentID
data = self.bridge.data()
createTime = self.bridge.create_time
updateTime = self.bridge.update_time
}

/// The reference of the document, if the query returns the `__name__` field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public struct PipelineSnapshot {
/// The time at which the pipeline producing this result was executed.
public let executionTime: Timestamp

init(pipeline: Pipeline, results: [PipelineResult], executionTime: Timestamp) {
let bridge: __PipelineSnapshotBridge

init(_ bridge: __PipelineSnapshotBridge, pipeline: Pipeline) {
self.bridge = bridge
self.pipeline = pipeline
self.results = results
self.executionTime = executionTime
executionTime = self.bridge.execution_time
results = self.bridge.results.map { PipelineResult($0) }
}
}
5 changes: 1 addition & 4 deletions Firestore/Swift/Source/SwiftAPI/Pipeline/Selectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

public protocol Selectable: Sendable {
var alias: String { get }
var expr: Expr { get }
}
public protocol Selectable: Sendable {}
25 changes: 0 additions & 25 deletions Firestore/Swift/Source/SwiftAPI/PipelineSnapshot.swift

This file was deleted.

7 changes: 2 additions & 5 deletions Firestore/Swift/Tests/Integration/PipelineApiTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,10 @@ final class PipelineTests: FSTIntegrationTestCase {

func testSampleStage() async throws {
// Sample 25 books, if the collection contains at least 25 documents
_ = db.pipeline().collection("books").sample(25)

// Sample 25 books, if the collection contains at least 25 documents
_ = db.pipeline().collection("books").sample(with: SampleOption(count: 10))
_ = db.pipeline().collection("books").sample(count: 10)

// Sample 10 percent of the collection of books
_ = db.pipeline().collection("books").sample(with: SampleOption(percentage: 10))
_ = db.pipeline().collection("books").sample(percentage: 10)
}

func testUnionStage() async throws {
Expand Down
8 changes: 8 additions & 0 deletions Firestore/core/src/api/pipeline_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class PipelineResult {
std::shared_ptr<model::ObjectValue> internal_value() const;
absl::optional<absl::string_view> document_id() const;

absl::optional<model::SnapshotVersion> create_time() const {
return create_time_;
}

absl::optional<model::SnapshotVersion> update_time() const {
return update_time_;
}

const absl::optional<model::DocumentKey>& internal_key() const {
return internal_key_;
}
Expand Down
Loading