Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,18 @@ object IcebergReflection extends Logging {
val opsMethod = table.getClass.getDeclaredMethod("operations")
opsMethod.setAccessible(true)
val ops = opsMethod.invoke(table)
val currentMethod = ops.getClass.getDeclaredMethod("current")
currentMethod.setAccessible(true)
val metadata = currentMethod.invoke(ops)
val formatVersionMethod = metadata.getClass.getMethod("formatVersion")
Some(formatVersionMethod.invoke(metadata).asInstanceOf[Int])
findMethodInHierarchy(ops.getClass, "current")
.flatMap { currentMethod =>
val metadata = currentMethod.invoke(ops)
val formatVersionMethod = metadata.getClass.getMethod("formatVersion")
Some(formatVersionMethod.invoke(metadata).asInstanceOf[Int])
}
.orElse {
logError(
"Iceberg reflection failure: Failed to get format version: " +
"current() method not found in operations class hierarchy")
None
}
} catch {
case e: Exception =>
logError(s"Iceberg reflection failure: Failed to get format version: ${e.getMessage}")
Expand Down Expand Up @@ -327,9 +334,12 @@ object IcebergReflection extends Logging {
operationsMethod.setAccessible(true)
val operations = operationsMethod.invoke(table)

val currentMethod = operations.getClass.getDeclaredMethod("current")
currentMethod.setAccessible(true)
Some(currentMethod.invoke(operations))
findMethodInHierarchy(operations.getClass, "current").map(_.invoke(operations)).orElse {
logError(
"Iceberg reflection failure: Failed to get table metadata: " +
"current() method not found in operations class hierarchy")
None
}
} catch {
case e: Exception =>
logError(s"Iceberg reflection failure: Failed to get table metadata: ${e.getMessage}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.comet.iceberg

import java.util.Collections

import org.scalatest.funsuite.AnyFunSuite

import org.apache.iceberg.BaseMetastoreTableOperations
import org.apache.iceberg.BaseTable
import org.apache.iceberg.Schema
import org.apache.iceberg.TableMetadata
import org.apache.iceberg.io.FileIO
import org.apache.iceberg.types.Types

class IcebergReflectionSuite extends AnyFunSuite {

/** Mimics HiveTableOperations/GlueTableOperations which inherit current(). */
class StubTableOperations extends BaseMetastoreTableOperations {
override protected def tableName(): String = "test"
override def refresh(): TableMetadata = null
override def io(): FileIO = null
}

test("getTableMetadata succeeds when operations class inherits current()") {
val ops = new StubTableOperations()
val schema = new Schema(Types.NestedField.required(1, "id", Types.IntegerType.get()))
val expectedMetadata = TableMetadata.newTableMetadata(
schema,
org.apache.iceberg.PartitionSpec.unpartitioned(),
"file:///tmp/test-table",
Collections.emptyMap[String, String]())
val metadataField = classOf[BaseMetastoreTableOperations]
.getDeclaredField("currentMetadata")
metadataField.setAccessible(true)
metadataField.set(ops, expectedMetadata)
// current() checks shouldRefresh (default true) and calls refresh() instead of
// returning currentMetadata. Set to false so current() returns our stubbed metadata.
val refreshField = classOf[BaseMetastoreTableOperations]
.getDeclaredField("shouldRefresh")
refreshField.setAccessible(true)
refreshField.set(ops, false)

val table = new BaseTable(ops, "test-table")
val metadata = IcebergReflection.getTableMetadata(table)
assert(metadata.isDefined)
assert(metadata.get.isInstanceOf[TableMetadata])
}
}
Loading