|
| 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 client |
| 21 | + |
| 22 | +import ( |
| 23 | + "bytes" |
| 24 | + "encoding/binary" |
| 25 | + "testing" |
| 26 | +) |
| 27 | + |
| 28 | +func buildNullIndicatorBytes(nulls []bool) []byte { |
| 29 | + var buf bytes.Buffer |
| 30 | + hasNull := false |
| 31 | + for _, n := range nulls { |
| 32 | + if n { |
| 33 | + hasNull = true |
| 34 | + break |
| 35 | + } |
| 36 | + } |
| 37 | + if !hasNull { |
| 38 | + buf.WriteByte(0) |
| 39 | + return buf.Bytes() |
| 40 | + } |
| 41 | + buf.WriteByte(1) |
| 42 | + packed := make([]byte, (len(nulls)+7)/8) |
| 43 | + for i, n := range nulls { |
| 44 | + if n { |
| 45 | + packed[i/8] |= 0b10000000 >> (uint(i) % 8) |
| 46 | + } |
| 47 | + } |
| 48 | + buf.Write(packed) |
| 49 | + return buf.Bytes() |
| 50 | +} |
| 51 | + |
| 52 | +func TestBinaryArrayColumnDecoder_EmptyString(t *testing.T) { |
| 53 | + var buf bytes.Buffer |
| 54 | + buf.Write(buildNullIndicatorBytes([]bool{false})) |
| 55 | + _ = binary.Write(&buf, binary.BigEndian, int32(0)) |
| 56 | + |
| 57 | + col, err := (&BinaryArrayColumnDecoder{}).ReadColumn(bytes.NewReader(buf.Bytes()), TEXT, 1) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("ReadColumn failed: %v", err) |
| 60 | + } |
| 61 | + if col.GetPositionCount() != 1 { |
| 62 | + t.Fatalf("expected positionCount=1, got %d", col.GetPositionCount()) |
| 63 | + } |
| 64 | + if col.IsNull(0) { |
| 65 | + t.Fatal("row 0 should not be null") |
| 66 | + } |
| 67 | + val, err := col.GetBinary(0) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("GetBinary(0) failed: %v", err) |
| 70 | + } |
| 71 | + if len(val.values) != 0 { |
| 72 | + t.Fatalf("expected empty string, got %q", string(val.values)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestBinaryArrayColumnDecoder_NullThenEmptyString(t *testing.T) { |
| 77 | + var buf bytes.Buffer |
| 78 | + buf.Write(buildNullIndicatorBytes([]bool{true, false})) |
| 79 | + _ = binary.Write(&buf, binary.BigEndian, int32(0)) |
| 80 | + |
| 81 | + col, err := (&BinaryArrayColumnDecoder{}).ReadColumn(bytes.NewReader(buf.Bytes()), TEXT, 2) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("ReadColumn failed: %v", err) |
| 84 | + } |
| 85 | + if !col.IsNull(0) { |
| 86 | + t.Error("row 0 should be null") |
| 87 | + } |
| 88 | + if col.IsNull(1) { |
| 89 | + t.Error("row 1 should not be null") |
| 90 | + } |
| 91 | + val, err := col.GetBinary(1) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("GetBinary(1) failed: %v", err) |
| 94 | + } |
| 95 | + if len(val.values) != 0 { |
| 96 | + t.Fatalf("expected empty string, got %q", string(val.values)) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestBinaryArrayColumnDecoder_WithNull(t *testing.T) { |
| 101 | + var buf bytes.Buffer |
| 102 | + buf.Write(buildNullIndicatorBytes([]bool{false, true, false})) |
| 103 | + writeText := func(s string) { |
| 104 | + _ = binary.Write(&buf, binary.BigEndian, int32(len(s))) |
| 105 | + buf.WriteString(s) |
| 106 | + } |
| 107 | + writeText("hello") |
| 108 | + writeText("world") |
| 109 | + |
| 110 | + col, err := (&BinaryArrayColumnDecoder{}).ReadColumn(bytes.NewReader(buf.Bytes()), TEXT, 3) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("ReadColumn failed: %v", err) |
| 113 | + } |
| 114 | + if col.IsNull(0) { |
| 115 | + t.Error("row 0 should not be null") |
| 116 | + } |
| 117 | + if v, _ := col.GetBinary(0); string(v.values) != "hello" { |
| 118 | + t.Errorf("row 0: expected \"hello\", got %q", string(v.values)) |
| 119 | + } |
| 120 | + if !col.IsNull(1) { |
| 121 | + t.Error("row 1 should be null") |
| 122 | + } |
| 123 | + if col.IsNull(2) { |
| 124 | + t.Error("row 2 should not be null") |
| 125 | + } |
| 126 | + if v, _ := col.GetBinary(2); string(v.values) != "world" { |
| 127 | + t.Errorf("row 2: expected \"world\", got %q", string(v.values)) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestInt64ArrayColumnDecoder_WithNull(t *testing.T) { |
| 132 | + var buf bytes.Buffer |
| 133 | + buf.Write(buildNullIndicatorBytes([]bool{false, true, false})) |
| 134 | + _ = binary.Write(&buf, binary.BigEndian, int64(100)) |
| 135 | + _ = binary.Write(&buf, binary.BigEndian, int64(200)) |
| 136 | + |
| 137 | + col, err := (&Int64ArrayColumnDecoder{}).ReadColumn(bytes.NewReader(buf.Bytes()), INT64, 3) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("ReadColumn failed: %v", err) |
| 140 | + } |
| 141 | + if col.IsNull(0) { |
| 142 | + t.Error("row 0 should not be null") |
| 143 | + } |
| 144 | + if v, _ := col.GetLong(0); v != 100 { |
| 145 | + t.Errorf("row 0: expected 100, got %d", v) |
| 146 | + } |
| 147 | + if !col.IsNull(1) { |
| 148 | + t.Error("row 1 should be null") |
| 149 | + } |
| 150 | + if col.IsNull(2) { |
| 151 | + t.Error("row 2 should not be null") |
| 152 | + } |
| 153 | + if v, _ := col.GetLong(2); v != 200 { |
| 154 | + t.Errorf("row 2: expected 200, got %d", v) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestColumnDecoder_ZeroPositionCount(t *testing.T) { |
| 159 | + empty := func() *bytes.Reader { return bytes.NewReader([]byte{}) } |
| 160 | + |
| 161 | + t.Run("Int32ArrayColumnDecoder", func(t *testing.T) { |
| 162 | + col, err := (&Int32ArrayColumnDecoder{}).ReadColumn(empty(), INT32, 0) |
| 163 | + if err != nil { |
| 164 | + t.Fatalf("ReadColumn failed: %v", err) |
| 165 | + } |
| 166 | + if col.GetPositionCount() != 0 { |
| 167 | + t.Errorf("expected positionCount=0, got %d", col.GetPositionCount()) |
| 168 | + } |
| 169 | + }) |
| 170 | + |
| 171 | + t.Run("Int64ArrayColumnDecoder", func(t *testing.T) { |
| 172 | + col, err := (&Int64ArrayColumnDecoder{}).ReadColumn(empty(), INT64, 0) |
| 173 | + if err != nil { |
| 174 | + t.Fatalf("ReadColumn failed: %v", err) |
| 175 | + } |
| 176 | + if col.GetPositionCount() != 0 { |
| 177 | + t.Errorf("expected positionCount=0, got %d", col.GetPositionCount()) |
| 178 | + } |
| 179 | + }) |
| 180 | + |
| 181 | + t.Run("ByteArrayColumnDecoder", func(t *testing.T) { |
| 182 | + col, err := (&ByteArrayColumnDecoder{}).ReadColumn(empty(), BOOLEAN, 0) |
| 183 | + if err != nil { |
| 184 | + t.Fatalf("ReadColumn failed: %v", err) |
| 185 | + } |
| 186 | + if col.GetPositionCount() != 0 { |
| 187 | + t.Errorf("expected positionCount=0, got %d", col.GetPositionCount()) |
| 188 | + } |
| 189 | + }) |
| 190 | + |
| 191 | + t.Run("BinaryArrayColumnDecoder", func(t *testing.T) { |
| 192 | + col, err := (&BinaryArrayColumnDecoder{}).ReadColumn(empty(), TEXT, 0) |
| 193 | + if err != nil { |
| 194 | + t.Fatalf("ReadColumn failed: %v", err) |
| 195 | + } |
| 196 | + if col.GetPositionCount() != 0 { |
| 197 | + t.Errorf("expected positionCount=0, got %d", col.GetPositionCount()) |
| 198 | + } |
| 199 | + }) |
| 200 | +} |
0 commit comments