Retorna o sinal de um valor de entrada. Essa é uma função adicional do MaxCompute V2.0.
Sintaxe
double sign(<number>)
Parâmetros
number: obrigatório. Um valor do tipo DOUBLE, BIGINT, INT, SMALLINT, TINYINT, FLOAT, DECIMAL ou STRING.
Valor de retorno
Retorna um valor do tipo DOUBLE. O valor de retorno varia de acordo com as seguintes regras:
Se o valor de number for positivo, o retorno será 1,0.
Se o valor de number for negativo, o retorno será -1,0.
Se o valor de number for 0, o retorno será 0,0.
Se o valor de number for nulo, o retorno será null.
Dados de exemplo
Esta seção fornece dados source de exemplo e casos de uso para ajudar você a entender como utilizar a função. Neste tópico, criamos uma tabela chamada mf_math_fun_t e inserimos dados nela. Comandos de exemplo:
CREATE TABLE IF NOT EXISTS mf_math_fun_t(
int_data INT,
bigint_data BIGINT,
double_data DOUBLE,
decimal_data DECIMAL,
float_data FLOAT,
string_data STRING
);
INSERT INTO mf_math_fun_t VALUES
(null, -10, 0.525, 0.525BD, CAST(0.525 AS FLOAT), '10'),
(-20, null, -0.1, -0.1BD, CAST(-0.1 AS FLOAT), '-10'),
(0, -1, null, 20.45BD, CAST(-1 AS FLOAT), '30'),
(-40, 4, 0.89, null, CAST(0.89 AS FLOAT), '-30'),
(5, -50, -1, -1BD, null, '50'),
(-60, 6, 1.5, 1.5BD, CAST(1.5 AS FLOAT), '-50'),
(-1, -70, -7.5, -7.5BD, CAST(-7.5 AS FLOAT),null ),
(-80, 1, -10.2, -10.2BD, CAST(-10.2 AS FLOAT), '-1' ),
(9, -90, 2.58, 2.58BD, CAST(2.58 AS FLOAT), '0'),
(-100, 10, -5.8, -5.8BD, CAST(-5.8 AS FLOAT), '-90');
Consulte os dados da tabela mf_math_fun_t. Comando de exemplo:
SELECT * FROM mf_math_fun_t;
-- The following result is returned:
+------------+-------------+-------------+--------------+------------+-------------+
| int_data | bigint_data | double_data | decimal_data | float_data | string_data |
+------------+-------------+-------------+--------------+------------+-------------+
| NULL | -10 | 0.525 | 0.525 | 0.525 | 10 |
| -20 | NULL | -0.1 | -0.1 | -0.1 | -10 |
| 0 | -1 | NULL | 20.45 | -1.0 | 30 |
| -40 | 4 | 0.89 | NULL | 0.89 | -30 |
| 5 | -50 | -1.0 | -1 | NULL | 50 |
| -60 | 6 | 1.5 | 1.5 | 1.5 | -50 |
| -1 | -70 | -7.5 | -7.5 | -7.5 | NULL |
| -80 | 1 | -10.2 | -10.2 | -10.2 | -1 |
| 9 | -90 | 2.58 | 2.58 | 2.58 | 0 |
| -100 | 10 | -5.8 | -5.8 | -5.8 | -90 |
+------------+-------------+-------------+--------------+------------+-------------+
Exemplo: dados estáticos
-- The return value is -1.0.
select sign(-2.5);
-- The return value is 1.0.
select sign(2.5);
-- The return value is 0.0.
select sign(0);
-- The return value is null.
select sign(null);
Exemplo: dados de tabela
Obtenha os sinais dos valores em todas as colunas com base no Sample data. Comando de exemplo:
-- Enable the MaxCompute V2.0 data type edition. Commit this command along with SQL statements.
set odps.sql.type.system.odps2=true;
select sign(int_data) as int_new, sign(bigint_data) as bigint_new, sign(double_data) as double_new, sign(decimal_data) as decimal_new, sign(float_data) as float_new, sign(string_data) as string_new from mf_math_fun_t;
O resultado retornado é o seguinte:
+------------+------------+------------+-------------+------------+------------+
| int_new | bigint_new | double_new | decimal_new | float_new | string_new |
+------------+------------+------------+-------------+------------+------------+
| NULL | -1.0 | 1.0 | 1 | 1.0 | 1.0 |
| -1.0 | NULL | -1.0 | -1 | -1.0 | -1.0 |
| 0.0 | -1.0 | NULL | 1 | -1.0 | 1.0 |
| -1.0 | 1.0 | 1.0 | NULL | 1.0 | -1.0 |
| 1.0 | -1.0 | -1.0 | -1 | NULL | 1.0 |
| -1.0 | 1.0 | 1.0 | 1 | 1.0 | -1.0 |
| -1.0 | -1.0 | -1.0 | -1 | -1.0 | NULL |
| -1.0 | 1.0 | -1.0 | -1 | -1.0 | -1.0 |
| 1.0 | -1.0 | 1.0 | 1 | 1.0 | 0.0 |
| -1.0 | 1.0 | -1.0 | -1 | -1.0 | -1.0 |
+------------+------------+------------+-------------+------------+------------+
Funções relacionadas
SIGN é uma função matemática. Para obter mais informações sobre funções relacionadas à computação e conversão de dados, consulte Mathematical Functions Overview.