|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.plugin.hive.ion; |
| 15 | + |
| 16 | +import com.amazon.ion.IonWriter; |
| 17 | +import com.amazon.ion.system.IonBinaryWriterBuilder; |
| 18 | +import com.amazon.ion.system.IonTextWriterBuilder; |
| 19 | +import com.google.common.collect.ImmutableMap; |
| 20 | +import io.trino.hive.formats.ion.IonDecoderConfig; |
| 21 | +import io.trino.spi.TrinoException; |
| 22 | + |
| 23 | +import java.io.OutputStream; |
| 24 | +import java.util.Locale; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.regex.Matcher; |
| 27 | +import java.util.regex.Pattern; |
| 28 | + |
| 29 | +import static io.trino.plugin.hive.HiveErrorCode.HIVE_UNSUPPORTED_FORMAT; |
| 30 | + |
| 31 | +public final class IonSerDeProperties |
| 32 | +{ |
| 33 | + // Reader properties |
| 34 | + public static final String STRICT_PATH_TYPING_PROPERTY = "ion.path_extractor.strict"; |
| 35 | + public static final String STRICT_PATH_TYPING_DEFAULT = "false"; |
| 36 | + public static final String PATH_EXTRACTOR_PROPERTY = "ion.(\\w+).path_extractor"; |
| 37 | + public static final String PATH_EXTRACTION_CASE_SENSITIVITY = "ion.path_extractor.case_sensitive"; |
| 38 | + public static final String PATH_EXTRACTION_CASE_SENSITIVITY_DEFAULT = "false"; |
| 39 | + private static final Pattern pathExtractorPattern = Pattern.compile(PATH_EXTRACTOR_PROPERTY); |
| 40 | + |
| 41 | + // unimplemented reader properties |
| 42 | + public static final String FAIL_ON_OVERFLOW_PROPERTY = "ion.fail_on_overflow"; |
| 43 | + public static final String FAIL_ON_OVERFLOW_PROPERTY_DEFAULT = "true"; |
| 44 | + public static final String COLUMN_FAIL_ON_OVERFLOW_PROPERTY = "ion.\\w+.fail_on_overflow"; |
| 45 | + public static final String IGNORE_MALFORMED = "ion.ignore_malformed"; |
| 46 | + public static final String IGNORE_MALFORMED_DEFAULT = "false"; |
| 47 | + |
| 48 | + // Writer properties |
| 49 | + public static final String ION_ENCODING_PROPERTY = "ion.encoding"; |
| 50 | + public static final String TEXT_ENCODING = "text"; |
| 51 | + public static final String BINARY_ENCODING = "binary"; |
| 52 | + |
| 53 | + // unimplemented writer properties |
| 54 | + public static final String ION_TIMESTAMP_OFFSET_PROPERTY = "ion.timestamp.serialization_offset"; |
| 55 | + public static final String ION_TIMESTAMP_OFFSET_DEFAULT = "Z"; |
| 56 | + public static final String ION_SERIALIZE_NULL_AS_PROPERTY = "ion.serialize_null"; |
| 57 | + public static final String ION_SERIALIZE_NULL_AS_DEFAULT = "OMIT"; |
| 58 | + public static final String ION_SERIALIZE_AS_PROPERTY = "ion.\\w+.serialize_as"; |
| 59 | + |
| 60 | + private static final Pattern unsupportedPropertiesRegex = Pattern.compile( |
| 61 | + ION_SERIALIZE_AS_PROPERTY + "|" + COLUMN_FAIL_ON_OVERFLOW_PROPERTY); |
| 62 | + |
| 63 | + private static final Map<String, String> defaultOnlyProperties = Map.of( |
| 64 | + // reader properties |
| 65 | + FAIL_ON_OVERFLOW_PROPERTY, FAIL_ON_OVERFLOW_PROPERTY_DEFAULT, |
| 66 | + IGNORE_MALFORMED, IGNORE_MALFORMED_DEFAULT, |
| 67 | + |
| 68 | + // writer properties |
| 69 | + ION_TIMESTAMP_OFFSET_PROPERTY, ION_TIMESTAMP_OFFSET_DEFAULT, |
| 70 | + ION_SERIALIZE_NULL_AS_PROPERTY, ION_SERIALIZE_NULL_AS_DEFAULT); |
| 71 | + |
| 72 | + private IonSerDeProperties() {} |
| 73 | + |
| 74 | + public static IonDecoderConfig decoderConfigFor(Map<String, String> propertiesMap) |
| 75 | + { |
| 76 | + ImmutableMap.Builder<String, String> extractionsBuilder = ImmutableMap.builder(); |
| 77 | + |
| 78 | + for (Map.Entry<String, String> property : propertiesMap.entrySet()) { |
| 79 | + Matcher matcher = pathExtractorPattern.matcher(property.getKey()); |
| 80 | + if (matcher.matches()) { |
| 81 | + extractionsBuilder.put(matcher.group(1), property.getValue()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + Boolean strictTyping = Boolean.parseBoolean( |
| 86 | + propertiesMap.getOrDefault(STRICT_PATH_TYPING_PROPERTY, STRICT_PATH_TYPING_DEFAULT)); |
| 87 | + Boolean caseSensitive = Boolean.parseBoolean( |
| 88 | + propertiesMap.getOrDefault(PATH_EXTRACTION_CASE_SENSITIVITY, PATH_EXTRACTION_CASE_SENSITIVITY_DEFAULT)); |
| 89 | + |
| 90 | + return new IonDecoderConfig(extractionsBuilder.buildOrThrow(), strictTyping, caseSensitive); |
| 91 | + } |
| 92 | + |
| 93 | + public enum IonEncoding |
| 94 | + { |
| 95 | + BINARY { |
| 96 | + @Override |
| 97 | + public IonWriter createWriter(OutputStream outputStream) |
| 98 | + { |
| 99 | + return IonBinaryWriterBuilder.standard().build(outputStream); |
| 100 | + } |
| 101 | + }, |
| 102 | + |
| 103 | + TEXT { |
| 104 | + @Override |
| 105 | + public IonWriter createWriter(OutputStream outputStream) |
| 106 | + { |
| 107 | + return IonTextWriterBuilder.minimal().build(outputStream); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + public abstract IonWriter createWriter(OutputStream outputStream); |
| 112 | + } |
| 113 | + |
| 114 | + public static IonEncoding getIonEncoding(Map<String, String> schema) |
| 115 | + { |
| 116 | + String encodingStr = schema.getOrDefault(ION_ENCODING_PROPERTY, BINARY_ENCODING); |
| 117 | + return switch (encodingStr.toLowerCase(Locale.ROOT)) { |
| 118 | + case TEXT_ENCODING -> IonEncoding.TEXT; |
| 119 | + case BINARY_ENCODING -> IonEncoding.BINARY; |
| 120 | + default -> throw new TrinoException(HIVE_UNSUPPORTED_FORMAT, |
| 121 | + "Unsupported Ion encoding format: " + encodingStr); |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + public static boolean hasUnsupportedProperty(Map<String, String> properties) |
| 126 | + { |
| 127 | + return properties.entrySet().stream() |
| 128 | + .anyMatch((entry) -> { |
| 129 | + String key = entry.getKey(); |
| 130 | + String value = entry.getValue(); |
| 131 | + if (!key.startsWith("ion.")) { |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + if (!defaultOnlyProperties.getOrDefault(key, value).equals(value)) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + |
| 139 | + return unsupportedPropertiesRegex.matcher(key).matches(); |
| 140 | + }); |
| 141 | + } |
| 142 | +} |
0 commit comments