Decode function failure in hive 1.1.0 - hive

I tried to perform conversion with decode function in hive 1.1.0.But it is throwing error.I have referred the syntax from hive functions, but still I'm getting error from decode function.Always I'm getting
SemanticException Decode() requires exactly two arguments
hive> select * from tbl_test;
OK
1 aaaa
2 bbbb
3 cccc
4 dddd
Time taken: 0.12 seconds, Fetched: 4 row(s)
hive> select decode(col1,1,'hi','hello') from tbl_test;
FAILED: SemanticException [Error 10015]: Line 1:7 Arguments length
mismatch ''hello'': Decode() requires exactly two arguments
hive> select decode(col1,1,'hi',null) from tbl_test;
FAILED: SemanticException [Error 10015]: Line 1:7 Arguments length
mismatch 'TOK_NULL': Decode() requires exactly two arguments
hive> select decode(col1,1,'hi') from tbl_test;
FAILED: SemanticException [Error 10015]: Line 1:7 Arguments length
mismatch ''hi'': Decode() requires exactly two arguments
hive> select decode(col1,1,'hi',"hello") from tbl_test;
FAILED: SemanticException [Error 10015]: Line 1:7 Arguments length
mismatch '"hello"': Decode() requires exactly two arguments

decode function in Hive is not the same as decode function in Oracle.
Use case or if conditional statement:
select case col1 when 1 then 'hi' else 'hello' end from tbl_test;

Related

TDengine arithmetic query operation causing coredump using none numeric operands

I was trying the arithmetic operation using select clause. I found when using numbers as operands the result is looking fine, but none numeric values like string/string or mix of number/string TDengine core dumps. I was thinking may be the ASCII values could be used in the calculation initially but expecting at least TDengine throws an error. Anyone know if using the none numeric value is the standard way for arithmetic query?
taos> select 2 + 3.0 from tb;
2 + 3.0 |
============================
5.000000000 |
5.000000000 |
Query OK, 2 row(s) in set (0.014870s)
taos> select 'a' + 'b' from tb;
taos: /root/wz_test/TDengine/src/client/src/tscSQLParser.c:3506: functionCompatibleCheck: Assertion `numOfExpr > 0' failed.
Aborted (core dumped)
taos> select "a" - 1 from db.tb;
taos: /root/wz_test/TDengine/src/client/src/tscSQLParser.c:3506: functionCompatibleCheck: Assertion `numOfExpr > 0' failed.
Aborted (core dumped)
I think it should be fixed in the later version.

HiveQL How to convert milliseconds in decimal format to mm:ss.SSS

I'm trying to convert decimal timestamp in milliseconds to mm:ss.SSS . The method only works for integers, and the output is not what I desired:
select from_unixtime(cast(1911.13/1000 as bigint), 'mm:ss.SSS');
1911.13 milliseconds yields the output, 00:01.000, which is not correct, should be 00:01.911
I have tried to convert to double, but received errors
select from_unixtime(cast(1911.13/1000 as double), 'mm:ss.SSS');
error:[Code: 10014, SQL State: 42000] Error while compiling statement: FAILED: SemanticException [Error 10014]: Line 1:7 Wrong arguments ''mm:ss.SSS'': No matching method for class org.apache.hadoop.hive.ql.udf.UDFFromUnixTime with (double, string). Possible choices: FUNC(bigint) FUNC(bigint, string) FUNC(int) FUNC(int, string)
Any help will be appreciated!
Anything after decimal is milliseconds. So, you can split the string into two parts. Convert before decimal part using from_unixtime and concat it with after decimal part to get the whole data.
select cast(from_unixtime(1911, 'mm:ss') as string)|| rpad('.130',4,'0') as col_ms
Pls note, millisecond .10 or .1 or .100 means 100 milliseconds, so i used rpad with 0 to pad it.
You can create a generic sql as well.
select cast(from_unixtime( cast(substr((cast(x/y) as string) ,1,instr((cast(x/y) as string),'.')) as bigint,'mm:ss') || || rpad(substr((cast(x/y) as string),instr((cast(x/y) as string),'.')),4,'0') as col_ms

Splitting comma separated values to multiple columns

I am trying to split a comma separated column to multiple columns using SQL such that each separated value is under its own column on Snowflake
This is a sample of my table
"2015-01-01","00:52:44",161144,"3.1.0","x86_64","mingw32","Formula","1.1-2","US",1
This is what I have tried:
SELECT * FROM "TUTORIAL"."PUBLIC"."CRAN_LOGS" STRING_SPLIT('date','time','size','r_version','r_arch','r_os','package','version','country','ip_id',';')
This is giving me the error message:
SQL compilation error: syntax error line 1 at position 59 unexpected
''date''. syntax error line 1 at position 149 unexpected ')'.
You are looking for split_part
SPLIT_PART(<string>, <delimiter>, <partNumber>)
Just be careful with column names though. Some of them could be reserved keywords in SQL and might throw an error
with your_table as
(select 'date,time,size,r_version,r_arch,r_os,package,version,country,ip_id' as col)
select split_part(col,',',1) as date,
split_part(col,',',2) as time,
split_part(col,',',3) as size,
split_part(col,',',4) as r_version,
split_part(col,',',5) as r_arch,
split_part(col,',',6) as r_os,
split_part(col,',',7) as package,
split_part(col,',',8) as version,
split_part(col,',',9) as country,
split_part(col,',',10) as ip_id
from your_table;

ROUND second argument only takes constant + hive

The following:
hive> create table t1 (val double, digit int);
hive> insert into t1 values(10,2);
hive> insert into t1 values(156660,3);
hive> insert into t1 values(8765450,4);
hive> select round(val, digit) from round_test;
Gives this error:
FAILED: SemanticException [Error 10014]: Line 1:7 Wrong arguments
'digit': ROUND second argument only takes constant
Its working fine in impala.
Could somebody help me please point out where the error is coming from?
BigDecimal a = new BigDecimal(value);
BigDecimal roundOff = a.setScale(places, BigDecimal.ROUND_HALF_EVEN);
return roundOff.doubleValue();
Thanks Mark for your quick response.
I've already used UDF to solve this issue. As this is a known issue HIVE-4523. Thought some patch has already applied.
The error says that the secund argument of ROUND must be a costant. i.e. with hive you can't use a column as secund argument for your ROUND function. If you need to do that I'd suggest you to create you UDF.

Error on query with "Replace"

I Have an error on a oracle server but I don't understand why is not work.
I use the software oracle sql developer.
The query is:
SELECT * FROM TestView WHERE REPLACE(TestView.Row2, '.', ',') > 0 ;
The value who is in TestVue.Row2 : '46.08','-46.47','1084.05','66500',...
"TestView" is a view who check to return a row without empty value
When I Execute the query I have always an error who says:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
Thanks for your help
Zoners
You have a row in your table, which cannot be parsed into number format. Most likely it is a blank column.
SELECT * FROM TestVue WHERE TestVue.Row2 IS NULL ;
Use a NVL Function to avoid errors caused by null field and a TO_NUMBER function to cast into a number before comparision with zero
SELECT * FROM TestView WHERE TO_NUMBER(REPLACE(NVL(TestView.Row2,'0'), '.', ',')) > 0 ;
You should be able to find the row with something like:
select Row2
from TestVuw
where length(trim(translate(Row2, '+-.,0123456789', ' '))) = 0
Of course, this allows multiple +, -, ., and ,. It can be further refined using a regular expression, but this usually works to find the bad ones.