简体   繁体   English

RLIKE 表达式给出“不匹配的输入”错误

[英]RLIKE expression giving 'mismatched input' error

I am querying a Hive table through Redash.我正在通过 Redash 查询 Hive 表。 I have a query similar to the following:我有一个类似于以下的查询:

SELECT 
    CAST(id AS INT) as id,
    COUNT(sales) AS num_sales
FROM 
    sales_table
WHERE 
    id RLIKE '\d*'
GROUP BY 
    id

I am trying to select only IDs which are a string of digits, hence the WHERE clause.我试图只选择一串数字的 ID,因此是WHERE子句。 This is giving me this error:这给了我这个错误:

Error running query: line 42:20: mismatched input 'RLIKE'. Expecting: '%', '*', '+', '-', '.', '/', 'AT', '[', '||', <expression>

You can try:你可以试试:

WHERE id RLIKE '^[0-9]+$'

Note that the above expression ensures that id is made of digits only (and non-empty).请注意,上述表达式确保id仅由数字组成(并且非空)。 You original regexp was checking if id contained 0 to n digits (that's what the * quantifier means): this is way to permissive, as it would basically allow anything that is not null.您原来的正则表达式正在检查id包含0 到 n位数字(这就是*量词的含义):这是允许的方式,因为它基本上允许任何非空的内容。

1) 1)

Hive regex requires double escaping Hive 正则表达式需要双重转义

also, for full match you should use anchoring, therefore -此外,对于完全匹配,您应该使用锚定,因此 -

id RLIKE '^\\d+$'

if empty string is also an option then -如果空字符串也是一个选项,那么 -

id RLIKE '^\\d*$'

2) 2)
You can take the opposite approach and search for strings that does not contain a non-digit -您可以采用相反的方法并搜索不包含非数字的字符串 -

id  NOT RLIKE '\\D'

3) 3)
In Hive a false casting results in null (and not an exception) therefore you could use -在 Hive 中,错误的强制转换会导致 null(而不是异常),因此您可以使用 -

cast(id as int) is not null

or或者

int(id) is not null

In Hive, the code should compile with either REGEXP or RLIKE .在 Hive 中,代码应该使用REGEXPRLIKE编译。 However, your regular expression does not do what you want.但是,您的正则表达式不能满足您的要求。 You want:你要:

WHERE id RLIKE '^[0-9]*$'

That is, all characters are digits, rather than it just containing a single digit.也就是说,所有字符都是数字,而不是只包含一个数字。 Well, actually, your version would match any string, because the digit is optional.嗯,实际上,您的版本会匹配任何字符串,因为数字是可选的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM