dongjoon-hyun commented on code in PR #56992: URL: https://github.com/apache/spark/pull/56992#discussion_r3525560266
########## sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/types/ops/OrcTypeOps.scala: ########## @@ -0,0 +1,169 @@ +/* + * 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.spark.sql.execution.datasources.orc.types.ops + +import org.apache.hadoop.hive.ql.io.sarg.PredicateLeaf +import org.apache.hadoop.io.WritableComparable +import org.apache.orc.TypeDescription + +import org.apache.spark.sql.catalyst.expressions.SpecializedGetters +import org.apache.spark.sql.types.{DataType, TimestampLTZNanosType, TimestampNTZNanosType, TimeType} + +/** + * Optional trait for ORC storage-format integration in the Types Framework. + * + * Implement this trait to enable ORC read/write support for a framework type. Each framework + * type that supports ORC provides a concrete implementation and registers it in the companion + * object's apply() method. + * + * The trait covers the ORC concerns that are homogeneous across the row-based code paths: + * - Schema mapping: Spark DataType -> ORC schema string (OrcUtils.getOrcSchemaString) and the + * ORC TypeDescription category (OrcUtils.orcTypeDescription). ORC has no native TIME / + * nanosecond-timestamp category, so framework types map onto an existing physical ORC + * category and round-trip the true Spark type via the CATALYST_TYPE_ATTRIBUTE_NAME attribute + * (stamped uniformly by the caller, so the ops only chooses the category). + * - Value write (serialize): Catalyst value -> ORC WritableComparable + * (OrcSerializer.newConverter) + * - Row-based read (deserialize): ORC WritableComparable -> Catalyst value + * (OrcDeserializer.newWriter) + * - Predicate pushdown: PredicateLeaf.Type mapping + literal casting (OrcFilters) + * + * DELIBERATELY NOT ON THE TRAIT: + * - Vectorized read. ORC's vectorized path (OrcAtomicColumnVector, a Java class) dispatches via + * boolean `instanceof` flags set in the constructor plus typed accessor methods + * (getTimestampNTZNanos/getTimestampLTZNanos), NOT via an `Ops(dt).map(_.x)` closure. It has + * no per-type extension seam, so it stays inline. This mirrors Parquet, whose vectorized read + * is likewise not routed through ParquetTypeOps (it dispatches on the Spark type inline in + * ParquetVectorUpdaterFactory.getUpdater). + * - supportDataType. OrcFileFormat.supportDataType / OrcTable.supportsDataType already admit + * every `AtomicType` via `case _: AtomicType => true`, so framework types are supported with + * no per-type arm; no gate method is needed (unlike Parquet, whose default differs). + * + * DISPATCH PATTERN: framework FIRST at each row-path integration site. Each ORC infrastructure + * method wraps itself with: + * {{{ + * OrcTypeOps(dt).map(_.method(...)).getOrElse(methodDefault(dt, ...)) + * }}} + * The original inline code is extracted to a *Default method unchanged. For a framework-managed + * type the ops handles it; for any other type OrcTypeOps(dt) is None and the *Default fallback + * executes the original path. + * + * DECOUPLING NOTE: makeDeserializer takes the Catalyst setter callbacks it needs + * ((Int, Long) => Unit and (Int, Any) => Unit) rather than OrcDeserializer's CatalystDataUpdater, + * because that updater is a sealed trait nested in OrcDeserializer and is not visible to this + * sub-package. The callbacks are the only two setter shapes the current framework types use. + * + * @see TimeTypeOrcOps for a reference implementation (primitive Long-backed type) + * @see TimestampLTZNanosOrcOps / TimestampNTZNanosOrcOps for OrcTimestamp-backed types + * @since 4.4.0 + */ +private[orc] trait OrcTypeOps extends Serializable { + + // ==================== Schema Mapping ==================== + + /** + * The ORC schema-string fragment for this type (OrcUtils.getOrcSchemaString). Examples: + * TimeType -> "bigint" (LongType.catalogString), TimestampLTZNanosType -> "timestamp with local + * time zone", TimestampNTZNanosType -> "timestamp". + */ + def orcSchemaString: String + + /** + * The ORC TypeDescription category for this type (OrcUtils.orcTypeDescription). The caller + * stamps CATALYST_TYPE_ATTRIBUTE_NAME = sparkType.typeName onto the returned descriptor, so the + * ops only chooses the physical category. + */ + def orcCategory: TypeDescription.Category + + // ==================== Value Write (serialize) ==================== + + /** + * Creates a converter that turns a Catalyst value at an ordinal into an ORC WritableComparable + * (OrcSerializer.newConverter). + * + * @param reuseObj whether the serializer may reuse a single mutable Writable across rows + * (OrcSerializer passes this through; the primitive Long-backed TimeType reuses, + * the OrcTimestamp-backed nanos types do not). + */ + def makeSerializer(reuseObj: Boolean): (SpecializedGetters, Int) => WritableComparable[_] + + // ==================== Row-Based Read (deserialize) ==================== + + /** + * Creates a writer that sets a decoded ORC value into the Catalyst row at an ordinal + * (OrcDeserializer.newWriter). The WritableComparable is the raw ORC value (LongWritable for + * LONG-category types, OrcTimestamp for the timestamp categories). + * + * @param setLong callback into the row's setLong (used by primitive Long-backed types) + * @param set callback into the row's generic set (used by object-backed types, e.g. nanos) + */ + def makeDeserializer( + setLong: (Int, Long) => Unit, + set: (Int, Any) => Unit): (Int, WritableComparable[_]) => Unit + + // ==================== Predicate Pushdown ==================== + + /** + * The ORC PredicateLeaf.Type for this type (OrcFilters.getPredicateLeafType), or None to opt out + * of pushdown (the caller then treats the column as non-convertible). TimeType returns Review Comment: This might be misleading because `OrcFilters.getPredicateLeafType` simply throws exceptions. If a new future type simply returns `None`, it means a failure in the planning, doesn't it? > the caller then treats the column as non-convertible https://github.com/apache/spark/blob/6a0e671b7672982dd4138c368ea71660ca70e604/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/orc/OrcFilters.scala#L151 -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
