leiyiz commented on a change in pull request #12580:
URL: https://github.com/apache/beam/pull/12580#discussion_r470771467



##########
File path: sdks/python/apache_beam/testing/benchmarks/nexmark/queries/query3.py
##########
@@ -0,0 +1,164 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""
+Query 3, 'Local Item Suggestion'. Who is selling in OR, ID or CA in category
+10, and for what auction ids? In CQL syntax::
+
+  SELECT Istream(P.name, P.city, P.state, A.id)
+  FROM Auction A [ROWS UNBOUNDED], Person P [ROWS UNBOUNDED]
+  WHERE A.seller = P.id
+    AND (P.state = `OR' OR P.state = `ID' OR P.state = `CA')
+    AND A.category = 10;
+
+We'll implement this query to allow 'new auction' events to come before the
+'new person' events for the auction seller. Those auctions will be stored until
+the matching person is seen. Then all subsequent auctions for a person will use
+the stored person record.
+"""
+
+from __future__ import absolute_import
+
+import logging
+
+import apache_beam as beam
+from apache_beam.coders import coders
+from apache_beam.testing.benchmarks.nexmark.models import name_city_state_id
+from apache_beam.testing.benchmarks.nexmark.models import nexmark_model
+from apache_beam.testing.benchmarks.nexmark.queries import nexmark_query_util
+from apache_beam.transforms import trigger
+from apache_beam.transforms import userstate
+from apache_beam.transforms import window
+from apache_beam.transforms.userstate import on_timer
+from apache_beam.utils.timestamp import Duration
+
+
+def load(events, metadata=None):
+  num_events_in_pane = 30
+  windowed_events = (
+      events
+      | beam.WindowInto(
+          window.GlobalWindows(),
+          trigger=trigger.Repeatedly(trigger.AfterCount(num_events_in_pane)),
+          accumulation_mode=trigger.AccumulationMode.DISCARDING,
+          allowed_lateness=Duration.of(0)))
+  auction_by_seller_id = (
+      windowed_events
+      | nexmark_query_util.JustAuctions()
+      | 'query3_filter_category' >> beam.Filter(lambda auc: auc.category == 10)
+      | 'query3_key_by_seller' >> beam.ParDo(
+          nexmark_query_util.AuctionBySellerFn()))
+  person_by_id = (
+      windowed_events
+      | nexmark_query_util.JustPerson()
+      | 'query3_filter_region' >> beam.Filter(
+          lambda person: person.state == 'OR' or person.state == 'ID' or 
person.
+          state == 'CA')
+      | 'query3_key_by_person_id' >> beam.ParDo(
+          nexmark_query_util.PersonByIdFn()))
+  return ({
+      nexmark_query_util.AUCTION_TAG: auction_by_seller_id,
+      nexmark_query_util.PERSON_TAG: person_by_id,
+  }
+          | beam.CoGroupByKey()
+          | 'query3_join' >> beam.ParDo(
+              JoinFn(metadata.get('max_auction_waiting_time')))
+          | 'query3_output' >> beam.Map(
+              lambda t: name_city_state_id.NameCiyStateId(
+                  t[1].name, t[1].city, t[1].state, t[0].id)))
+
+
+class JoinFn(beam.DoFn):
+  """
+  Join auctions and person by person id and emit their product one pair at
+  a time.
+
+  We know a person may submit any number of auctions. Thus new person event
+  must have the person record stored in persistent state in order to match
+  future auctions by that person.
+
+  However we know that each auction is associated with at most one person, so
+  only need to store auction records in persistent state until we have seen the
+  corresponding person record. And of course may have already seen that record.
+  """
+
+  AUCTIONS = 'auctions_state'
+  PERSON = 'person_state'
+  PERSON_EXPIRING = 'person_state_expiring'
+
+  auction_spec = userstate.ReadModifyWriteStateSpec(

Review comment:
       Cool, will do that




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to