|
18 | 18 | */ |
19 | 19 | package org.apache.parquet.hadoop.example; |
20 | 20 |
|
| 21 | +import java.util.Optional; |
21 | 22 | import java.util.Stack; |
22 | 23 | import org.apache.parquet.io.InvalidRecordException; |
23 | 24 | import org.apache.parquet.io.api.RecordConsumer; |
@@ -128,50 +129,54 @@ private void validateUnsignedInteger(int value) { |
128 | 129 | Type currentType = getCurrentFieldType(); |
129 | 130 | if (currentType != null && currentType.isPrimitive()) { |
130 | 131 | LogicalTypeAnnotation logicalType = currentType.asPrimitiveType().getLogicalTypeAnnotation(); |
131 | | - if (logicalType instanceof LogicalTypeAnnotation.IntLogicalTypeAnnotation) { |
132 | | - LogicalTypeAnnotation.IntLogicalTypeAnnotation intType = |
133 | | - (LogicalTypeAnnotation.IntLogicalTypeAnnotation) logicalType; |
134 | | - if (!intType.isSigned()) { |
135 | | - switch (intType.getBitWidth()) { |
136 | | - case 8: |
137 | | - if (value < 0 || value > 255) { |
138 | | - throw new InvalidRecordException("Value " + value |
139 | | - + " is out of range for UINT_8 (0-255) in field " + currentType.getName()); |
140 | | - } |
141 | | - break; |
142 | | - case 16: |
143 | | - if (value < 0 || value > 65535) { |
144 | | - throw new InvalidRecordException("Value " + value |
145 | | - + " is out of range for UINT_16 (0-65535) in field " + currentType.getName()); |
146 | | - } |
147 | | - break; |
148 | | - case 32: |
149 | | - case 64: |
150 | | - if (value < 0) { |
151 | | - throw new InvalidRecordException("Negative value " + value |
152 | | - + " is not allowed for unsigned integer type " + currentType.getName()); |
153 | | - } |
154 | | - break; |
| 132 | + logicalType.accept(new LogicalTypeAnnotation.LogicalTypeAnnotationVisitor<Void>() { |
| 133 | + @Override |
| 134 | + public Optional<Void> visit(LogicalTypeAnnotation.IntLogicalTypeAnnotation intType) { |
| 135 | + if (!intType.isSigned()) { |
| 136 | + switch (intType.getBitWidth()) { |
| 137 | + case 8: |
| 138 | + if (value < 0 || value > 255) { |
| 139 | + throw new InvalidRecordException("Value " + value |
| 140 | + + " is out of range for UINT_8 (0-255) in field " + currentType.getName()); |
| 141 | + } |
| 142 | + break; |
| 143 | + case 16: |
| 144 | + if (value < 0 || value > 65535) { |
| 145 | + throw new InvalidRecordException("Value " + value |
| 146 | + + " is out of range for UINT_16 (0-65535) in field " + currentType.getName()); |
| 147 | + } |
| 148 | + break; |
| 149 | + case 32: |
| 150 | + case 64: |
| 151 | + if (value < 0) { |
| 152 | + throw new InvalidRecordException("Negative value " + value |
| 153 | + + " is not allowed for unsigned integer type " + currentType.getName()); |
| 154 | + } |
| 155 | + break; |
| 156 | + } |
155 | 157 | } |
| 158 | + return Optional.empty(); |
156 | 159 | } |
157 | | - } |
| 160 | + }); |
158 | 161 | } |
159 | 162 | } |
160 | 163 |
|
161 | 164 | private void validateUnsignedLong(long value) { |
162 | 165 | Type currentType = getCurrentFieldType(); |
163 | 166 | if (currentType != null && currentType.isPrimitive()) { |
164 | 167 | LogicalTypeAnnotation logicalType = currentType.asPrimitiveType().getLogicalTypeAnnotation(); |
165 | | - if (logicalType instanceof LogicalTypeAnnotation.IntLogicalTypeAnnotation) { |
166 | | - LogicalTypeAnnotation.IntLogicalTypeAnnotation intType = |
167 | | - (LogicalTypeAnnotation.IntLogicalTypeAnnotation) logicalType; |
168 | | - if (!intType.isSigned()) { |
169 | | - if (value < 0) { |
170 | | - throw new InvalidRecordException("Negative value " + value |
171 | | - + " is not allowed for unsigned integer type " + currentType.getName()); |
| 168 | + logicalType.accept(new LogicalTypeAnnotation.LogicalTypeAnnotationVisitor<Void>() { |
| 169 | + @Override |
| 170 | + public Optional<Void> visit(LogicalTypeAnnotation.IntLogicalTypeAnnotation intType) { |
| 171 | + if (!intType.isSigned()) { |
| 172 | + if (value < 0) { |
| 173 | + throw new InvalidRecordException("Negative value " + value |
| 174 | + + " is not allowed for unsigned integer type " + currentType.getName()); |
| 175 | + } |
172 | 176 | } |
| 177 | + return Optional.empty(); |
173 | 178 | } |
174 | | - } |
| 179 | + }); |
175 | 180 | } |
176 | 181 | } |
177 | 182 |
|
|
0 commit comments