Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HIVE-28518: Iceberg: Fix ClassCastException during in-place migration to Iceberg tables with timestamp columns #5590

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -20,6 +20,7 @@
package org.apache.iceberg.mr.hive.serde.objectinspector;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import org.apache.hadoop.hive.common.type.Timestamp;
import org.apache.hadoop.hive.serde2.io.TimestampWritableV2;
Expand Down Expand Up @@ -55,7 +56,13 @@ public Timestamp getPrimitiveJavaObject(Object o) {
if (o == null) {
return null;
}
LocalDateTime time = (LocalDateTime) o;
LocalDateTime time;
if (o instanceof OffsetDateTime) {
OffsetDateTime odt = (OffsetDateTime) o;
time = odt.atZoneSameInstant(TypeInfoFactory.timestampLocalTZTypeInfo.getTimeZone()).toLocalDateTime();
} else {
time = (LocalDateTime) o;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be safe to check if it is LocalDateTime before casting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zratkai Thanks for taking a look into this.

Sure, I will make the necessary changes. Besides LocalDateTime and OffsetDateTime, are there any other classes that might be expected here? Please let me know so I can handle them appropriately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should always be LocalDateTime, ideally. But the Iceberg + Parquet reader constructs OffsetDateTime. So, we should support only LocalDateTime and OffsetDateTime.

Though we expected never fail, it could be a little kinder if we had a better error message like String.format("An unexpected type %s was passed as timestamp", o.getClass)

Copy link
Contributor Author

@ggangadharan ggangadharan Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed update @okumin

if it's okay. I will make the below code changes.

 public Timestamp getPrimitiveJavaObject(Object o) {
    if (o == null) {
      return null;
    }
    LocalDateTime time;
    if (o instanceof LocalDateTime) {
      time = (LocalDateTime) o;
    } else if (o instanceof OffsetDateTime) {
      OffsetDateTime odt = (OffsetDateTime) o;
      time = odt.atZoneSameInstant(TypeInfoFactory.timestampLocalTZTypeInfo.getTimeZone()).toLocalDateTime();
    } else {
      throw new ClassCastException(String.format("An unexpected type %s was passed as timestamp. " +
              "Expected LocalDateTime/OffsetDateTime", o.getClass().getName()));
    }
    return Timestamp.ofEpochMilli(time.toInstant(ZoneOffset.UTC).toEpochMilli(), time.getNano());
  }

Please check and let me know.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update @ayushtkn

}
return Timestamp.ofEpochMilli(time.toInstant(ZoneOffset.UTC).toEpochMilli(), time.getNano());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE EXTERNAL TABLE hive_28518_test(`id` int,`name` string,`dt` timestamp) STORED AS PARQUET;
insert into hive_28518_test values (1, "test name" , cast('2024-08-09 14:08:26.326107' as timestamp));
ALTER TABLE hive_28518_test SET TBLPROPERTIES ('storage_handler'='org.apache.iceberg.mr.hive.HiveIcebergStorageHandler', 'format-version' = '2');
set hive.fetch.task.conversion=more;
select * from hive_28518_test;
set hive.fetch.task.conversion=none;
select * from hive_28518_test;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
PREHOOK: query: CREATE EXTERNAL TABLE hive_28518_test(`id` int,`name` string,`dt` timestamp) STORED AS PARQUET
PREHOOK: type: CREATETABLE
PREHOOK: Output: database:default
PREHOOK: Output: default@hive_28518_test
POSTHOOK: query: CREATE EXTERNAL TABLE hive_28518_test(`id` int,`name` string,`dt` timestamp) STORED AS PARQUET
POSTHOOK: type: CREATETABLE
POSTHOOK: Output: database:default
POSTHOOK: Output: default@hive_28518_test
PREHOOK: query: insert into hive_28518_test values (1, "test name" , cast('2024-08-09 14:08:26.326107' as timestamp))
PREHOOK: type: QUERY
PREHOOK: Input: _dummy_database@_dummy_table
PREHOOK: Output: default@hive_28518_test
POSTHOOK: query: insert into hive_28518_test values (1, "test name" , cast('2024-08-09 14:08:26.326107' as timestamp))
POSTHOOK: type: QUERY
POSTHOOK: Input: _dummy_database@_dummy_table
POSTHOOK: Output: default@hive_28518_test
POSTHOOK: Lineage: hive_28518_test.dt SCRIPT []
POSTHOOK: Lineage: hive_28518_test.id SCRIPT []
POSTHOOK: Lineage: hive_28518_test.name SCRIPT []
PREHOOK: query: ALTER TABLE hive_28518_test SET TBLPROPERTIES ('storage_handler'='org.apache.iceberg.mr.hive.HiveIcebergStorageHandler', 'format-version' = '2')
PREHOOK: type: ALTERTABLE_PROPERTIES
PREHOOK: Input: default@hive_28518_test
PREHOOK: Output: default@hive_28518_test
POSTHOOK: query: ALTER TABLE hive_28518_test SET TBLPROPERTIES ('storage_handler'='org.apache.iceberg.mr.hive.HiveIcebergStorageHandler', 'format-version' = '2')
POSTHOOK: type: ALTERTABLE_PROPERTIES
POSTHOOK: Input: default@hive_28518_test
POSTHOOK: Output: default@hive_28518_test
PREHOOK: query: select * from hive_28518_test
PREHOOK: type: QUERY
PREHOOK: Input: default@hive_28518_test
PREHOOK: Output: hdfs://### HDFS PATH ###
POSTHOOK: query: select * from hive_28518_test
POSTHOOK: type: QUERY
POSTHOOK: Input: default@hive_28518_test
POSTHOOK: Output: hdfs://### HDFS PATH ###
1 test name 2024-08-09 14:08:26.326107
PREHOOK: query: select * from hive_28518_test
PREHOOK: type: QUERY
PREHOOK: Input: default@hive_28518_test
PREHOOK: Output: hdfs://### HDFS PATH ###
POSTHOOK: query: select * from hive_28518_test
POSTHOOK: type: QUERY
POSTHOOK: Input: default@hive_28518_test
POSTHOOK: Output: hdfs://### HDFS PATH ###
1 test name 2024-08-09 14:08:26.326107
Loading