|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.texera.amber.operator |
| 21 | + |
| 22 | +/** The Python a sampler's standalone code needs, emitted once per script via |
| 23 | + * [[StandaloneCodeGenerator.standaloneHelpers]]. |
| 24 | + */ |
| 25 | +object SamplingHelpers { |
| 26 | + |
| 27 | + /** |
| 28 | + * A Python transcription of `java.util.Random`, for operators whose executor |
| 29 | + * draws from one. |
| 30 | + * |
| 31 | + * A sampler decides per row whether to keep it, so which rows survive is |
| 32 | + * fixed by the exact sequence the generator produces. Seeding Python's |
| 33 | + * `random` or numpy's with the engine's seed selects a different set, and |
| 34 | + * the script would then report a different sample than the workflow it came |
| 35 | + * from. Only the same generator gives the same rows. |
| 36 | + */ |
| 37 | + val JavaRandom: String = |
| 38 | + """# java.util.Random, transcribed so sampling matches the engine. |
| 39 | + |class _TexeraJavaRandom: |
| 40 | + | _MASK = (1 << 48) - 1 |
| 41 | + | _MULTIPLIER = 0x5DEECE66D |
| 42 | + | _ADDEND = 0xB |
| 43 | + | |
| 44 | + | def __init__(self, seed): |
| 45 | + | self._seed = (seed ^ self._MULTIPLIER) & self._MASK |
| 46 | + | |
| 47 | + | def _next(self, bits): |
| 48 | + | self._seed = (self._seed * self._MULTIPLIER + self._ADDEND) & self._MASK |
| 49 | + | value = self._seed >> (48 - bits) |
| 50 | + | return value - (1 << 32) if value >= (1 << 31) else value |
| 51 | + | |
| 52 | + | def next_double(self): |
| 53 | + | return ((self._next(26) << 27) + self._next(27)) * (2.0 ** -53) |
| 54 | + | |
| 55 | + | def next_int(self, bound): |
| 56 | + | if bound <= 0: |
| 57 | + | raise ValueError("bound must be positive") |
| 58 | + | if bound & (-bound) == bound: |
| 59 | + | return (bound * self._next(31)) >> 31 |
| 60 | + | while True: |
| 61 | + | bits = self._next(31) |
| 62 | + | value = bits % bound |
| 63 | + | # Java rejects a draw by letting this sum overflow an int. |
| 64 | + | # Python would carry it and never reject. |
| 65 | + | probe = bits - value + (bound - 1) |
| 66 | + | if ((probe + (1 << 31)) % (1 << 32)) - (1 << 31) >= 0: |
| 67 | + | return value""".stripMargin |
| 68 | +} |
0 commit comments