|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from absl.testing import absltest |
| 16 | +from absl.testing import parameterized |
| 17 | +from arolla import arolla |
| 18 | +from koladata.exceptions import exceptions |
| 19 | +from koladata.expr import expr_eval |
| 20 | +from koladata.expr import input_container |
| 21 | +from koladata.operators import kde_operators |
| 22 | +from koladata.operators import optools |
| 23 | +from koladata.operators.tests.util import qtypes as test_qtypes |
| 24 | +from koladata.testing import testing |
| 25 | +from koladata.types import data_bag |
| 26 | +from koladata.types import data_slice |
| 27 | +from koladata.types import qtypes |
| 28 | + |
| 29 | +kde = kde_operators.kde |
| 30 | +DATA_SLICE = qtypes.DATA_SLICE |
| 31 | +NON_DETERMINISTIC_TOKEN = qtypes.NON_DETERMINISTIC_TOKEN |
| 32 | +bag = data_bag.DataBag.empty |
| 33 | +I = input_container.InputContainer('I') |
| 34 | + |
| 35 | + |
| 36 | +QTYPES = frozenset([ |
| 37 | + ( |
| 38 | + DATA_SLICE, |
| 39 | + arolla.make_tuple_qtype(), |
| 40 | + NON_DETERMINISTIC_TOKEN, |
| 41 | + DATA_SLICE, |
| 42 | + ), |
| 43 | + ( |
| 44 | + DATA_SLICE, |
| 45 | + arolla.make_tuple_qtype(DATA_SLICE), |
| 46 | + NON_DETERMINISTIC_TOKEN, |
| 47 | + DATA_SLICE, |
| 48 | + ), |
| 49 | + ( |
| 50 | + DATA_SLICE, |
| 51 | + arolla.make_tuple_qtype(DATA_SLICE, DATA_SLICE), |
| 52 | + NON_DETERMINISTIC_TOKEN, |
| 53 | + DATA_SLICE, |
| 54 | + ), |
| 55 | +]) |
| 56 | + |
| 57 | +db = data_bag.DataBag.empty() |
| 58 | +ds = lambda vals: data_slice.DataSlice.from_vals(vals).with_bag(db) |
| 59 | + |
| 60 | +OBJ1 = db.obj() |
| 61 | +OBJ2 = db.obj() |
| 62 | + |
| 63 | + |
| 64 | +class ListsConcatListsTest(parameterized.TestCase): |
| 65 | + |
| 66 | + def test_mutability(self): |
| 67 | + self.assertFalse( |
| 68 | + expr_eval.eval( |
| 69 | + kde.lists.concat_lists(db.list([1, 2]), db.list([3])) |
| 70 | + ).is_mutable() |
| 71 | + ) |
| 72 | + |
| 73 | + @parameterized.parameters( |
| 74 | + ((db.list([1, 2, 3]),), db.list([1, 2, 3])), |
| 75 | + ( |
| 76 | + (db.list([[1], [2, 3]]),), |
| 77 | + db.list([[1], [2, 3]]), |
| 78 | + ), |
| 79 | + ( |
| 80 | + (db.list([1]), db.list([2, 3]), db.list([4, 5, 6])), |
| 81 | + db.list([1, 2, 3, 4, 5, 6]), |
| 82 | + ), |
| 83 | + ( |
| 84 | + ( |
| 85 | + db.implode(ds([[0], [1]])), |
| 86 | + db.implode(ds([[2], [3]])), |
| 87 | + db.implode(ds([[4, 5], [6]])), |
| 88 | + ), |
| 89 | + db.implode(ds([[0, 2, 4, 5], [1, 3, 6]])), |
| 90 | + ), |
| 91 | + ( |
| 92 | + # Compatible primitive types follow type promotion. |
| 93 | + (db.list([1, 2, 3]), db.list([4.5, 5.5, 6.5])), |
| 94 | + db.list([1.0, 2.0, 3.0, 4.5, 5.5, 6.5]), |
| 95 | + ), |
| 96 | + ( |
| 97 | + (db.list([1]), db.list([None, 2.5]), db.list(['a', OBJ1, b'b'])), |
| 98 | + db.list([1, None, 2.5, 'a', OBJ1, b'b']), |
| 99 | + ), |
| 100 | + ( |
| 101 | + ( |
| 102 | + db.implode(ds([[0], [None]])), |
| 103 | + db.implode(ds([['a'], [b'b']])), |
| 104 | + db.implode(ds([[4.5, OBJ1], [OBJ2]])), |
| 105 | + ), |
| 106 | + db.implode(ds([[0, 'a', 4.5, OBJ1], [None, b'b', OBJ2]])), |
| 107 | + ), |
| 108 | + ) |
| 109 | + def test_eval(self, lists, expected): |
| 110 | + testing.assert_nested_lists_equal( |
| 111 | + expr_eval.eval(kde.lists.concat_lists(*lists)), expected |
| 112 | + ) |
| 113 | + |
| 114 | + def test_non_deterministic_token(self): |
| 115 | + res_1 = expr_eval.eval( |
| 116 | + kde.lists.concat_lists(db.list([1, 2]), db.list([3])) |
| 117 | + ) |
| 118 | + res_2 = expr_eval.eval( |
| 119 | + kde.lists.concat_lists(db.list([1, 2]), db.list([3])) |
| 120 | + ) |
| 121 | + self.assertNotEqual(res_1.db.fingerprint, res_2.db.fingerprint) |
| 122 | + testing.assert_equal(res_1[:].no_bag(), res_2[:].no_bag()) |
| 123 | + |
| 124 | + expr = kde.lists.concat_lists(db.list([1, 2]), db.list([3])) |
| 125 | + res_1 = expr_eval.eval(expr) |
| 126 | + res_2 = expr_eval.eval(expr) |
| 127 | + self.assertNotEqual(res_1.db.fingerprint, res_2.db.fingerprint) |
| 128 | + testing.assert_equal(res_1[:].no_bag(), res_2[:].no_bag()) |
| 129 | + |
| 130 | + def test_qtype_signatures(self): |
| 131 | + self.assertCountEqual( |
| 132 | + arolla.testing.detect_qtype_signatures( |
| 133 | + kde.lists.concat_lists, |
| 134 | + possible_qtypes=test_qtypes.DETECT_SIGNATURES_QTYPES, |
| 135 | + max_arity=3, |
| 136 | + ), |
| 137 | + QTYPES, |
| 138 | + ) |
| 139 | + |
| 140 | + def test_alias(self): |
| 141 | + self.assertTrue( |
| 142 | + optools.equiv_to_op(kde.lists.concat_lists, kde.concat_lists) |
| 143 | + ) |
| 144 | + |
| 145 | + def test_concat_failure(self): |
| 146 | + a = db.list([1, 2, 3]) |
| 147 | + b = db.list([[1, 2, 3], [4, 5, 6]]) |
| 148 | + with self.assertRaisesRegex( |
| 149 | + exceptions.KodaError, |
| 150 | + r"""cannot find a common schema for provided schemas |
| 151 | +
|
| 152 | + the common schema\(s\) INT32: INT32 |
| 153 | + the first conflicting schema [0-9a-f]{32}:0: LIST\[INT32\]""", |
| 154 | + ): |
| 155 | + expr_eval.eval(kde.lists.concat_lists(a, b)) |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == '__main__': |
| 159 | + absltest.main() |
0 commit comments