Hive - where子句中的计算列

问题描述 投票:1回答:2

我正在运行一个如下所示的配置单元查询。

SELECT from_utc_timestamp(arrival_date, "IST") AS `Date`
    FROM table_name
    WHERE 1 BETWEEN '2018-12-01 00:00:00'
            AND '2018-12-02 00:00:00'; 

这里1指的是我的第一个选择列(转换为IST时区)。但它没有返回任何行。

arrival_date列上的示例数据:

select arrival_date from table_name;

2019-01-01 21:34:12
2019-01-04 06:12:46

然后我尝试了这个,

SELECT from_utc_timestamp(arrival_date, "IST") AS `Date`
FROM table_name
WHERE from_utc_timestamp(arrival_date, "IST") 
BETWEEN '2018-12-01 00:00:00'
            AND '2018-12-02 00:00:00';

现在我得到了数据。

但是在where子句中我再次转换数据,这可能导致TB级大小表上的性能问题。

如何在where子句中使用计算列?

sql hive bigdata where-clause
2个回答
1
投票

表格日期是UTC格式,参数在IST,然后您可以将参数转换为UTC:

SELECT from_utc_timestamp(arrival_date, "IST") AS `Date`
    FROM table_name 
 WHERE arrival_date BETWEEN to_utc_timestamp('2018-12-01 00:00:00', "IST")
                        AND to_utc_timestamp('2018-12-02 00:00:00', "IST");

最好的方法是尽可能单独计算参数,并以UTC格式传递日期。例如,使用带参数的shell和call hive脚本。

例如在shell中执行:

date_start_IST="2018-12-01 00:00:00"
date_end_IST="2018-12-02 00:00:00"

date_start_UTC=$( export TZ='GMT' && date -d 'TZ="Asia/Kolkata" '"$date_start_IST" +"%F %H:%M:%S" )
date_end_UTC=$( export TZ='GMT' && date -d 'TZ="Asia/Kolkata" '"$date_end_IST" +"%F %H:%M:%S" )

echo "$date_start_UTC, $date_end_UTC"
# prints 2018-11-30 18:30:00, 2018-12-01 18:30:00 

#call Hive script:

hive -hiveconf date_start_UTC="$date_start_UTC" -hiveconf date_end_UTC="$date_end_UTC" -f your_script.hql

在脚本your_script.hql中:

SELECT from_utc_timestamp(arrival_date, "IST") AS `Date`
        FROM table_name 
     WHERE arrival_date BETWEEN '${hivecong:date_start_UTC}'
                            AND '${hivecong:date_end_UTC}';

通过这种方式,分区修剪将起作用(如果表由arrival_date分区),因为没有函数应用于谓词,优化器甚至可以在执行之前派生分区。

如果它没有被分区,并且文件是ORC,则谓词下推将起作用。

如果它没有被分区而不是ORC,那么它将是全扫描,无论它是谓词和列中的函数都没关系。


0
投票

一种方法可以是子查询

select * from (SELECT from_utc_timestamp(arrival_date, "IST") AS `Date`
FROM table_name
) a where a.Date BETWEEN '2018-12-01 00:00:00'
            AND '2018-12-02 00:00:00';
© www.soinside.com 2019 - 2024. All rights reserved.