|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.connector.read; |
| 19 | + |
| 20 | +import java.io.Serializable; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +import org.apache.spark.annotation.Evolving; |
| 24 | +import org.apache.spark.sql.types.StructType; |
| 25 | + |
| 26 | +/** |
| 27 | + * Variant access information that describes how variant fields are accessed in a query. |
| 28 | + * <p> |
| 29 | + * This class captures the information needed by data sources to optimize reading variant columns. |
| 30 | + * Instead of reading the entire variant value, the data source can read only the fields that |
| 31 | + * are actually accessed, represented as a structured schema. |
| 32 | + * <p> |
| 33 | + * For example, if a query accesses `variant_get(v, '$.a', 'int')` and |
| 34 | + * `variant_get(v, '$.b', 'string')`, the extracted schema would be |
| 35 | + * `struct<0:int, 1:string>` where field ordinals correspond to the access order. |
| 36 | + * |
| 37 | + * @since 4.1.0 |
| 38 | + */ |
| 39 | +@Evolving |
| 40 | +public final class VariantAccessInfo implements Serializable { |
| 41 | + private final String columnName; |
| 42 | + private final StructType extractedSchema; |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates variant access information for a variant column. |
| 46 | + * |
| 47 | + * @param columnName The name of the variant column |
| 48 | + * @param extractedSchema The schema representing extracted fields from the variant. |
| 49 | + * Each field represents one variant field access, with field names |
| 50 | + * typically being ordinals (e.g., "0", "1", "2") and metadata |
| 51 | + * containing variant-specific information like JSON path. |
| 52 | + */ |
| 53 | + public VariantAccessInfo(String columnName, StructType extractedSchema) { |
| 54 | + this.columnName = Objects.requireNonNull(columnName, "columnName cannot be null"); |
| 55 | + this.extractedSchema = |
| 56 | + Objects.requireNonNull(extractedSchema, "extractedSchema cannot be null"); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the name of the variant column. |
| 61 | + */ |
| 62 | + public String columnName() { |
| 63 | + return columnName; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Returns the schema representing fields extracted from the variant column. |
| 68 | + * <p> |
| 69 | + * The schema structure is: |
| 70 | + * <ul> |
| 71 | + * <li>Field names: Typically ordinals ("0", "1", "2", ...) representing access order</li> |
| 72 | + * <li>Field types: The target data type for each field extraction</li> |
| 73 | + * <li>Field metadata: Contains variant-specific information such as JSON path, |
| 74 | + * timezone, and error handling mode</li> |
| 75 | + * </ul> |
| 76 | + * <p> |
| 77 | + * Data sources should use this schema to determine what fields to extract from the variant |
| 78 | + * and what types they should be converted to. |
| 79 | + */ |
| 80 | + public StructType extractedSchema() { |
| 81 | + return extractedSchema; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean equals(Object o) { |
| 86 | + if (this == o) return true; |
| 87 | + if (o == null || getClass() != o.getClass()) return false; |
| 88 | + VariantAccessInfo that = (VariantAccessInfo) o; |
| 89 | + return columnName.equals(that.columnName) && |
| 90 | + extractedSchema.equals(that.extractedSchema); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public int hashCode() { |
| 95 | + return Objects.hash(columnName, extractedSchema); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public String toString() { |
| 100 | + return "VariantAccessInfo{" + |
| 101 | + "columnName='" + columnName + '\'' + |
| 102 | + ", extractedSchema=" + extractedSchema + |
| 103 | + '}'; |
| 104 | + } |
| 105 | +} |
0 commit comments