You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "giovanni-prestipino (via GitHub)" <gi...@apache.org> on 2024/04/17 14:17:31 UTC

[I] [Bug]: Side effect during the for loop execution in beam Pipeline [beam]

giovanni-prestipino opened a new issue, #31014:
URL: https://github.com/apache/beam/issues/31014

   ### What happened?
   
   When using for loop, to execute PTransform, all loops seem to use the last value of an iterator.
   
   ```python
   import apache_beam as beam
   
   def main():
       with beam.Pipeline() as p:
           p_f = p | beam.Create([1, 2, 3, 4, 5])
   
           for i in range(0, 3):
               p_f = p_f | f"{i}" >> beam.Map(lambda x: x + i)
   
           p_f | beam.Map(print)
   
   if __name__ == "__main__":
       main()
   ```
   
   As a result of the code, I expected to print 
   ```
   4
   5
   6
   7
   8
   ```
   
   But I got:
   ```
   7
   8
   9
   10
   11
   ```
   
   Moreover, I found a workaround defining a new sum function:
   ```python
   import apache_beam as beam
   
   
   def _sum(pcoll, i):
       return pcoll | f"{i}" >> beam.Map(lambda x: x + i)
   
   def main():
       with beam.Pipeline() as p:
           p_f = p | beam.Create([1, 2, 3, 4, 5])
   
           for i in range(0, 3):
               p_f = _sum(p_f, i)
   
           p_f | beam.Map(print)
   
   if __name__ == "__main__":
       main()
   ```
   
   I am using the following versions:
   - `apache_beam==2.55.0`
   - `python==3.10.12`
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [X] Component: Python SDK
   - [ ] Component: Java SDK
   - [ ] Component: Go SDK
   - [ ] Component: Typescript SDK
   - [ ] Component: IO connector
   - [ ] Component: Beam YAML
   - [ ] Component: Beam examples
   - [ ] Component: Beam playground
   - [ ] Component: Beam katas
   - [ ] Component: Website
   - [ ] Component: Spark Runner
   - [ ] Component: Flink Runner
   - [ ] Component: Samza Runner
   - [ ] Component: Twister2 Runner
   - [ ] Component: Hazelcast Jet Runner
   - [ ] Component: Google Cloud Dataflow Runner


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Bug]: Side effect during the for loop execution in beam Pipeline [beam]

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on issue #31014:
URL: https://github.com/apache/beam/issues/31014#issuecomment-2061888279

   For your first example, I think this is expected. the pipeline won't be executed after the entire pipeline is construct. After that, the variable `i` has the final value, which will be passed in for the final execution. 


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Bug]: Side effect during the for loop execution in beam Pipeline [beam]

Posted by "giovanni-prestipino (via GitHub)" <gi...@apache.org>.
giovanni-prestipino commented on issue #31014:
URL: https://github.com/apache/beam/issues/31014#issuecomment-2061386438

   Maybe the problem is that the loop parameter should be passed to the Map as a side input:
   
   ```python
   def main():
       with beam.Pipeline() as p:
           p_f = p | beam.Create([1, 2, 3, 4, 5])
   
           for i in range(0, 3):
               p_f = p_f | f"{i}" >> beam.Map(lambda x, k: x + k, k=i)
   
           p_f | beam.Map(print)
   
   if __name__ == "__main__":
       main()
   ``


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Bug]: Side effect during the for loop execution in beam Pipeline [beam]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on issue #31014:
URL: https://github.com/apache/beam/issues/31014#issuecomment-3703659226

   This issue has been marked as stale due to 150 days of inactivity. It will be closed in 30 days if no further activity occurs. If you think that’s incorrect or this issue still needs to be addressed, please simply write any comment. If closed, you can reopen the issue at any time. Thank you for your contributions.


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Bug]: Side effect during the for loop execution in beam Pipeline [beam]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed issue #31014: [Bug]: Side effect during the for loop execution in beam Pipeline
URL: https://github.com/apache/beam/issues/31014


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [I] [Bug]: Side effect during the for loop execution in beam Pipeline [beam]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on issue #31014:
URL: https://github.com/apache/beam/issues/31014#issuecomment-3828417006

   This issue has been closed due to lack of activity. If you think that is incorrect, you can reopen the issue at any time.


-- 
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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org