twalthr commented on code in PR #30092: URL: https://github.com/apache/flink/pull/30092#discussion_r4683615925
########## docs/data/functions/variant/is_valid_variant.yml: ########## @@ -0,0 +1,56 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License + +name: IS_VALID_VARIANT +summary: Returns whether a `VARIANT` follows the Parquet Variant encoding. + +syntax: + sql: IS_VALID_VARIANT(variant) + java: $("variant").isValidVariant() + python: col("variant").is_valid_variant() + +arguments: + - name: variant + type: VARIANT + required: true + description: The value to check. + +returns: + description: TRUE if `variant` follows the Parquet Variant encoding at any depth. Every part can be read, the fields of each object are sorted by their keys with no key twice, and every key and string is UTF-8. FALSE otherwise, such as when its binary encoding is malformed or uses a type that this version doesn't know. + nulls: Returns NULL if `variant` is NULL. + type: "`BOOLEAN`, NOT NULL if `variant` is NOT NULL." + +notes: + - An invalid value comes from bytes that Flink didn't encode, such as a `VARIANT` that another engine wrote. Use `IS_VALID_VARIANT` to filter such values before reading or storing them. A value that can't be read makes `VARIANT_GET` and a cast fail the job, while `TRY_VARIANT_GET` returns NULL. In an object with 32 or more fields whose keys aren't sorted, a field lookup can miss a field that exists. Review Comment: ```suggestion - An invalid value comes from bytes that Flink didn't encode, such as a `VARIANT` that another engine wrote. Use `IS_VALID_VARIANT` to filter such values before reading or storing them. A value that can't be read makes `VARIANT_GET` and a cast fail the job, while `TRY_VARIANT_GET` returns NULL. In an object with 32 or more fields whose keys aren't sorted, a field lookup can miss a field that exists when the engine uses binary search. ``` ########## docs/data/functions/variant/is_valid_variant.yml: ########## @@ -0,0 +1,56 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License + +name: IS_VALID_VARIANT +summary: Returns whether a `VARIANT` follows the Parquet Variant encoding. + +syntax: + sql: IS_VALID_VARIANT(variant) + java: $("variant").isValidVariant() + python: col("variant").is_valid_variant() + +arguments: + - name: variant + type: VARIANT + required: true + description: The value to check. + +returns: + description: TRUE if `variant` follows the Parquet Variant encoding at any depth. Every part can be read, the fields of each object are sorted by their keys with no key twice, and every key and string is UTF-8. FALSE otherwise, such as when its binary encoding is malformed or uses a type that this version doesn't know. Review Comment: List all special cases the validator checks here. Including precision 38, time checks, supported VARIANT data types etc. ########## flink-core/src/main/java/org/apache/flink/types/variant/VariantValidator.java: ########## @@ -0,0 +1,504 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.types.variant; + +import org.apache.flink.annotation.Internal; + +import java.util.Arrays; + +import static org.apache.flink.types.variant.BinaryVariantUtil.ARRAY; +import static org.apache.flink.types.variant.BinaryVariantUtil.BASIC_TYPE_BITS; +import static org.apache.flink.types.variant.BinaryVariantUtil.BASIC_TYPE_MASK; +import static org.apache.flink.types.variant.BinaryVariantUtil.BINARY; +import static org.apache.flink.types.variant.BinaryVariantUtil.DECIMAL16; +import static org.apache.flink.types.variant.BinaryVariantUtil.DECIMAL4; +import static org.apache.flink.types.variant.BinaryVariantUtil.DECIMAL8; +import static org.apache.flink.types.variant.BinaryVariantUtil.LONG_STR; +import static org.apache.flink.types.variant.BinaryVariantUtil.MAX_DECIMAL16_PRECISION; +import static org.apache.flink.types.variant.BinaryVariantUtil.MAX_DECIMAL4_PRECISION; +import static org.apache.flink.types.variant.BinaryVariantUtil.MAX_DECIMAL8_PRECISION; +import static org.apache.flink.types.variant.BinaryVariantUtil.OBJECT; +import static org.apache.flink.types.variant.BinaryVariantUtil.SHORT_STR; +import static org.apache.flink.types.variant.BinaryVariantUtil.SIZE_LIMIT; +import static org.apache.flink.types.variant.BinaryVariantUtil.TIME; +import static org.apache.flink.types.variant.BinaryVariantUtil.TYPE_INFO_MASK; +import static org.apache.flink.types.variant.BinaryVariantUtil.U32_SIZE; +import static org.apache.flink.types.variant.BinaryVariantUtil.VERSION; +import static org.apache.flink.types.variant.BinaryVariantUtil.VERSION_MASK; +import static org.apache.flink.types.variant.BinaryVariantUtil.arrayOffsetSize; +import static org.apache.flink.types.variant.BinaryVariantUtil.arraySizeBytes; +import static org.apache.flink.types.variant.BinaryVariantUtil.metadataOffsetSize; +import static org.apache.flink.types.variant.BinaryVariantUtil.objectIdSize; +import static org.apache.flink.types.variant.BinaryVariantUtil.objectOffsetSize; +import static org.apache.flink.types.variant.BinaryVariantUtil.objectSizeBytes; +import static org.apache.flink.types.variant.BinaryVariantUtil.primitiveSize; +import static org.apache.flink.types.variant.BinaryVariantUtil.readLong; +import static org.apache.flink.util.SwarUtils.isAscii; +import static org.apache.flink.util.Utf8Utils.firstInvalidUtf8ByteIndex; + +/** + * Checks whether a binary variant follows the Parquet Variant encoding spec. + * + * <p>A variant is valid when: + * + * <ul> + * <li>Its metadata is complete. If its header says the dictionary is sorted, the keys are sorted + * and unique. + * <li>Every value, at any depth, has a type this version knows, lies within its parent, and holds + * a value the getters accept. + * <li>The fields of each object are listed in the unsigned byte order of their keys, and no key + * appears twice. + * <li>Every dictionary key and string is UTF-8. + * </ul> + * + * <p>Bits that the spec reserves are ignored. + * + * <p>Values may share bytes as long as the check reads no more bytes of the value than it has: one + * for each value, plus the bytes of each string. A variant without shared bytes always stays within + * that bound, because each of its values and strings has bytes of its own. The bound keeps the work + * linear in the size of the variant. + * + * <p>Keys are compared directly until that costs about as much as sorting the dictionary. From then + * on, the check sorts the dictionary once and compares the ranks of the keys, so many objects with + * long keys can't make the check quadratic. + * + * <p>An instance reuses its buffers between calls, so it must not be shared between threads. + */ +@Internal +public final class VariantValidator { + + private static final int SORTED_STRINGS_MASK = 0x10; + + private static final long MICROS_PER_DAY = 86_400_000_000L; + + private static final long MAX_DECIMAL4_UNSCALED = 999_999_999L; + private static final long MAX_DECIMAL8_UNSCALED = 999_999_999_999_999_999L; + + // 10^38, the smallest unscaled value with more digits than DECIMAL16 allows, in two halves. + private static final long TEN_POW_38_HIGH = 0x4B3B4CA85A86C47AL; + private static final long TEN_POW_38_LOW = 0x098A224000000000L; + + // A frame holds the position of an object or array, the index of its next child, and the end + // that its bytes must fit in. + private static final int FRAME_SIZE = 3; + private static final int INITIAL_STACK_SIZE = 32 * FRAME_SIZE; + private static final int MAX_RETAINED_STACK_SIZE = 1024 * FRAME_SIZE; + + private static final int[] NO_KEYS = new int[0]; + private static final int MAX_RETAINED_KEYS = 1024; + + private int[] stack = new int[INITIAL_STACK_SIZE]; + + // The metadata of the current call, and what the check found out about its dictionary. + private byte[] metadata; + private int dictionarySize; + private int keyOffsetSize; + private int keyStart; + private boolean keysIncreasing; + private long keyCompareBudget; + private boolean keysRanked; + + // Once the keys are ranked, keyRanks holds the rank of each id in key order. Equal keys have + // the same rank. keyOrder is the buffer the ranking sorts the ids in. + private int[] keyOrder = NO_KEYS; + private int[] keyRanks = NO_KEYS; + + /** Returns whether the variant follows the spec. Never throws. */ + public boolean isValid(BinaryVariant variant) { + return isValid(variant.rawValue(), variant.getMetadata(), variant.getPos()); + } + + /** + * Returns whether {@code value} and {@code metadata} form a variant that follows the spec, + * including the checks of the {@link BinaryVariant} constructor. Never throws. + */ + public boolean isValid(byte[] value, byte[] metadata) { Review Comment: instead of boolean throw an error, we can offer a method in the future that passes this error to the user. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
