1 // Licensed to the Apache Software Foundation (ASF) under one or more
2 // contributor license agreements. See the NOTICE file distributed with
3 // this work for additional information regarding copyright ownership.
4 // The ASF licenses this file to You under the Apache License, Version 2.0
5 // (the "License"); you may not use this file except in compliance with
6 // the License. You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 using Apache.Arrow.Types;
20 namespace Apache.Arrow.Tests
22 public class DictionaryArrayTests
25 public void CreateTest()
27 (StringArray originalDictionary, Int32Array originalIndicesArray, DictionaryArray dictionaryArray) =
28 CreateSimpleTestData();
30 Assert.Equal(dictionaryArray.Dictionary, originalDictionary);
31 Assert.Equal(dictionaryArray.Indices, originalIndicesArray);
35 public void SliceTest()
37 (StringArray originalDictionary, Int32Array originalIndicesArray, DictionaryArray dictionaryArray) =
38 CreateSimpleTestData();
40 int batchLength = originalIndicesArray.Length;
41 for (int offset = 0; offset < batchLength; offset++)
43 for (int length = 1; offset + length <= batchLength; length++)
45 var sliced = dictionaryArray.Slice(offset, length) as DictionaryArray;
46 var actualSlicedDictionary = sliced.Dictionary as StringArray;
47 var actualSlicedIndicesArray = sliced.Indices as Int32Array;
49 var expectedSlicedIndicesArray = originalIndicesArray.Slice(offset, length) as Int32Array;
51 //Dictionary is not sliced.
52 Assert.Equal(originalDictionary.Data, actualSlicedDictionary.Data);
53 Assert.Equal(expectedSlicedIndicesArray.ToList(), actualSlicedIndicesArray.ToList());
58 private Tuple<StringArray, Int32Array, DictionaryArray> CreateSimpleTestData()
60 StringArray originalDictionary = new StringArray.Builder().AppendRange(new[] { "a", "b", "c" }).Build();
61 Int32Array originalIndicesArray = new Int32Array.Builder().AppendRange(new[] { 0, 0, 1, 1, 2, 2 }).Build();
62 var dictionaryArray = new DictionaryArray(new DictionaryType(Int32Type.Default, StringType.Default, false), originalIndicesArray, originalDictionary);
64 return Tuple.Create(originalDictionary, originalIndicesArray, dictionaryArray);