如何比较具有不同数据类型组的两列 [英] How to compare two columns with different data type groups

查看:132
本文介绍了如何比较具有不同数据类型组的两列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我昨天提出的问题的扩展:在不同组中对数据类型进行比较时,如何处理潜在的数据丢失

This is an extension of a question I posed yesterday: How to handle potential data loss when performing comparisons across data types in different groups

在HIVE中,是否可能在 SELECT 子句内联的不同数据类型组中的两列之间执行比较?我首先需要确定每列的传入元数据是什么,然后提供选择要使用的 CAST 的逻辑。

In HIVE, is it possible to perform comparisons between two columns that are in different data type groups inline within the SELECT clause? I need to first determine what the incoming meta data is for each column and then provide logic that picks what CAST to use.

CASE 
    WHEN  Column1 <=> Column2 THEN 0 -- Error occurs here if data types are in different data type groups (i.e., BIGINT <=> STRING)
    ELSE 1
END

如果不可能,是否有变通方法来检索元数据并根据某些规则执行CAST?例如,如果:

If this is not possible, is there a workaround to retrieve the meta data and perform CAST'ing based on certain rules? For example, if:


  1. Column1是BIGINT

  2. Column2是STRING

然后将CAST Column2设置为BIGINT,然后执行比较。需要最简单的解决方案。我希望不必对生成 SELECT 语句的基础系统进行大量代码更改。

Then CAST Column2 as BIGINT and then perform the comparison. The simplest solution possible is desired. I'm hoping to not have to make substantial code changes to the underlying system that is generating the SELECT statement.

更新2:
有一个后端系统生成SQOOP查询,并且在该系统中有一个函数可以修改SELECT子句以比较两列。我们遇到的问题是,这些列曾经在我们运行的早期版本的HIVE / Cloudera上隐式转换。现在,我们要升级到不支持隐式数据类型转换的版本,我们需要找到最简单的解决方案以继续支持数据类型转换。不幸的是,后端没有可用的元数据,因此我试图在 SELECT 子句

推荐答案

隐式广播是个好主意:

CASE 
    WHEN  Column1 <=> cast(Column2 as bigint) THEN 0 
    ELSE 1
END

可能强制转换,强制转换的结果为NULL。

If not possible to cast, the result of cast is NULL.

这篇关于如何比较具有不同数据类型组的两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆