-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathimpala_math_functions.xml
More file actions
2033 lines (1642 loc) · 59.2 KB
/
impala_math_functions.xml
File metadata and controls
2033 lines (1642 loc) · 59.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="math_functions">
<title>Impala Mathematical Functions</title>
<titlealts audience="PDF">
<navtitle>Mathematical Functions</navtitle>
</titlealts>
<prolog>
<metadata>
<data name="Category" value="Impala"/>
<data name="Category" value="Impala Functions"/>
<data name="Category" value="SQL"/>
<data name="Category" value="Data Analysts"/>
<data name="Category" value="Developers"/>
<data name="Category" value="Querying"/>
</metadata>
</prolog>
<conbody>
<p>
Mathematical functions, or arithmetic functions, perform numeric calculations that are
typically more complex than basic addition, subtraction, multiplication, and division. For
example, these functions include trigonometric, logarithmic, and base conversion
operations.
</p>
<note>
In Impala, exponentiation uses the <codeph>pow()</codeph> function rather than an
exponentiation operator such as <codeph>**</codeph>.
</note>
<p conref="../shared/impala_common.xml#common/related_info"/>
<p>
The mathematical functions operate mainly on these data types:
<xref href="impala_int.xml#int"/>, <xref href="impala_bigint.xml#bigint"/>,
<xref href="impala_smallint.xml#smallint"/>, <xref href="impala_tinyint.xml#tinyint"/>,
<xref href="impala_double.xml#double"/>, <xref href="impala_float.xml#float"/>, and
<xref href="impala_decimal.xml#decimal"/>. For the operators that perform the standard
operations such as addition, subtraction, multiplication, and division, see
<xref href="impala_operators.xml#arithmetic_operators"/>.
</p>
<p>
Functions that perform bitwise operations are explained in
<xref href="impala_bit_functions.xml#bit_functions"/>.
</p>
<p>
<b>Function reference:</b>
</p>
<p>
Impala supports the following mathematical functions:
</p>
<ul>
<li>
<xref href="#math_functions/abs">ABS</xref>
</li>
<li>
<xref href="#math_functions/acos">ACOS</xref>
</li>
<li>
<xref href="#math_functions/asin">ASIN</xref>
</li>
<li>
<xref href="#math_functions/atan">ATAN</xref>
</li>
<li>
<xref href="#math_functions/atan2">ATAN2</xref>
</li>
<li>
<xref href="#math_functions/bin">BIN</xref>
</li>
<li>
<xref href="#math_functions/ceil">CEIL, CEILING, DCEIL</xref>
</li>
<li>
<xref href="#math_functions/conv">CONV</xref>
</li>
<li>
<xref href="#math_functions/cos">COS</xref>
</li>
<li>
<xref href="#math_functions/cosh">COSH</xref>
</li>
<li>
<xref href="#math_functions/cot">COT</xref>
</li>
<li>
<xref href="#math_functions/degrees">DEGREES</xref>
</li>
<li>
<xref href="#math_functions/e">E</xref>
</li>
<li>
<xref href="#math_functions/exp">EXP, DEXP</xref>
</li>
<li>
<xref href="#math_functions/factorial">FACTORIAL</xref>
</li>
<li>
<xref href="#math_functions/floor">FLOOR, DFLOOR</xref>
</li>
<li>
<xref href="#math_functions/fmod">FMOD</xref>
</li>
<li>
<xref href="#math_functions/fnv_hash">FNV_HASH</xref>
</li>
<li>
<xref href="#math_functions/greatest">GREATEST</xref>
</li>
<li>
<xref href="#math_functions/hex">HEX</xref>
</li>
<li>
<xref href="#math_functions/is_inf">IS_INF</xref>
</li>
<li>
<xref href="#math_functions/is_nan">IS_NAN</xref>
</li>
<li>
<xref href="#math_functions/least">LEAST</xref>
</li>
<li>
<xref href="#math_functions/ln">LN</xref>
</li>
<li>
<xref href="#math_functions/log">LOG</xref>
</li>
<li>
<xref href="#math_functions/log10">LOG10, DLOG10</xref>
</li>
<li>
<xref href="#math_functions/log2">LOG2</xref>
</li>
<li>
<xref href="#math_functions/max_int">MAX_INT, MAX_TINYINT, MAX_SMALLINT, MAX_BIGINT</xref>
</li>
<li>
<xref href="#math_functions/min_int">MIN_INT, MIN_TINYINT, MIN_SMALLINT, MIN_BIGINT</xref>
</li>
<li>
<xref href="#math_functions/mod">MOD</xref>
</li>
<li>
<xref href="#math_functions/murmur_hash">MURMUR_HASH</xref>
</li>
<li>
<xref href="#math_functions/negative">NEGATIVE</xref>
</li>
<li>
<xref href="#math_functions/pi">PI</xref>
</li>
<li>
<xref href="#math_functions/pmod">PMOD</xref>
</li>
<li>
<xref href="#math_functions/positive">POSITIVE</xref>
</li>
<li>
<xref href="#math_functions/pow">POW, POWER, DPOW, FPOW</xref>
</li>
<li>
<xref href="#math_functions/precision">PRECISION</xref>
</li>
<li>
<xref href="#math_functions/quotient">QUOTIENT</xref>
</li>
<li>
<xref href="#math_functions/radians">RADIANS</xref>
</li>
<li>
<xref href="#math_functions/rand">RAND, RANDOM</xref>
</li>
<li>
<xref href="#math_functions/round">ROUND, DROUND</xref>
</li>
<li>
<xref href="#math_functions/scale">SCALE</xref>
</li>
<li>
<xref href="#math_functions/sign">SIGN</xref>
</li>
<li>
<xref href="#math_functions/sin">SIN</xref>
</li>
<li>
<xref href="#math_functions/sinh">SINH</xref>
</li>
<li>
<xref href="#math_functions/sqrt">SQRT</xref>
</li>
<li>
<xref href="#math_functions/tan">TAN</xref>
</li>
<li>
<xref href="#math_functions/tanh">TANH</xref>
</li>
<li>
<xref href="#math_functions/truncate">TRUNCATE, DTRUNC, TRUNC</xref>
</li>
<li>
<xref href="#math_functions/unhex">UNHEX</xref>
</li>
<li>
<xref href="#math_functions/width_bucket">WIDTH_BUCKET</xref>
</li>
</ul>
<dl>
<dlentry rev="1.4.0" id="abs">
<dt rev="1.4.0 2.0.1">
ABS(numeric_type a)
</dt>
<dd rev="1.4.0">
<b>Purpose:</b> Returns the absolute value of the argument.
<p
rev="2.0.1"
conref="../shared/impala_common.xml#common/return_type_same"/>
<p>
<b>Usage notes:</b> Use this function to ensure all return values are positive. This
is different than the <codeph>POSITIVE()</codeph> function, which returns its
argument unchanged (even if the argument was negative).
</p>
</dd>
</dlentry>
<dlentry id="acos">
<dt>
ACOS(DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Returns the arccosine of the argument.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="asin">
<dt>
ASIN(DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Returns the arcsine of the argument.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="atan">
<dt>
ATAN(DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Returns the arctangent of the argument.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="atan2" rev="2.3.0 IMPALA-1771">
<dt rev="2.3.0 IMPALA-1771">
ATAN2(DOUBLE a, DOUBLE b)
</dt>
<dd rev="2.3.0 IMPALA-1771">
<b>Purpose:</b> Returns the arctangent of the two arguments, with the signs of the
arguments used to determine the quadrant of the result.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="bin">
<dt>
BIN(BIGINT a)
</dt>
<dd>
<b>Purpose:</b> Returns the binary representation of an integer value, that is, a
string of 0 and 1 digits.
<p>
<b>Return type:</b> <codeph>STRING</codeph>
</p>
</dd>
</dlentry>
<dlentry rev="1.4.0" id="ceil">
<dt rev="1.4.0">
CEIL(DOUBLE a), CEIL(DECIMAL(p,s) a), CEILING(DOUBLE a), CEILING(DECIMAL(p,s) a),
DCEIL(DOUBLE a), DCEIL(DECIMAL(p,s) a)
</dt>
<dd rev="1.4.0">
<b>Purpose:</b> Returns the smallest integer that is greater than or equal to the
argument.
<p>
<b>Return type:</b> Same as the input value
</p>
</dd>
</dlentry>
<dlentry id="conv">
<dt>
CONV(BIGINT n, INT from_base, INT to_base), CONV(STRING s, INT from_base, INT to_base)
</dt>
<dd>
<b>Purpose:</b> Returns a string representation of the first argument converted from
<codeph>from_base</codeph> to <codeph>to_base</codeph>. The first argument can be
specified as a number or a string. For example, <codeph>CONV(100, 2, 10)</codeph> and
<codeph>CONV('100', 2, 10)</codeph> both return <codeph>'4'</codeph>.
<p>
<b>Return type:</b> <codeph>STRING</codeph>
</p>
<p>
<b>Usage notes:</b>
</p>
<p>
If <codeph>to_base</codeph> is negative, the first argument is treated as signed,
and otherwise, it is treated as unsigned. For example:
</p>
<ul>
<li>
<codeph>conv(-17, 10, -2) </codeph>returns <codeph>'-10001'</codeph>,<codeph>
-17</codeph> in base 2.
</li>
<li>
<codeph>conv(-17, 10, 10)</codeph> returns
<codeph>'18446744073709551599'</codeph>. <codeph>-17</codeph> is interpreted as an
unsigned, 2^64-17, and then the value is returned in base 10.
</li>
</ul>
<p>
The function returns <codeph>NULL</codeph> when the following illegal arguments are
specified:
</p>
<ul>
<li>
Any argument is <codeph>NULL</codeph>.
</li>
<li>
<codeph>from_base</codeph> or <codeph>to_base</codeph> is below
<codeph>-36</codeph> or above <codeph>36</codeph>.
</li>
<li>
<codeph>from_base</codeph> or <codeph>to_base</codeph> is <codeph>-1</codeph>,
<codeph>0</codeph>, or <codeph>1</codeph>.
</li>
<li>
The first argument represents a positive number and <codeph>from_base</codeph> is
a negative number.
</li>
</ul>
<p>
If the first argument represents a negative number and <codeph>from_base</codeph> is
a negative number, the function returns <codeph>0</codeph>.
</p>
<p>
If the first argument represents a number larger than the maximum
<codeph>bigint</codeph>, the function returns:
</p>
<ul>
<li>
The string representation of -1 in <codeph>to_base</codeph> if
<codeph>to_base</codeph> is negative.
</li>
<li>
The string representation of 18446744073709551615' (2^64 - 1) in
<codeph>to_base</codeph> if <codeph>to_base</codeph> is positive.
</li>
</ul>
<p>
If the first argument does not represent a valid number in
<codeph>from_base</codeph>, e.g. 3 in base 2 or '1a23' in base 10, the digits in the
first argument are evaluated from left-to-right and used if a valid digit in
<codeph>from_base</codeph>. The invalid digit and the digits to the right are
ignored.
</p>
<p>
For example:
<ul>
<li>
<codeph> conv(445, 5, 10)</codeph> is converted to <codeph>conv(44, 5,
10)</codeph> and returns <codeph>'24'</codeph>.
</li>
<li>
<codeph> conv('1a23', 10, 16)</codeph> is converted to <codeph>conv('1', 10 ,
16)</codeph> and returns <codeph>'1'</codeph>.
</li>
</ul>
</p>
</dd>
</dlentry>
<dlentry id="cos">
<dt>
COS(DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Returns the cosine of the argument.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="cosh" rev="2.3.0 IMPALA-1771">
<dt rev="2.3.0 IMPALA-1771">
COSH(DOUBLE a)
</dt>
<dd rev="2.3.0 IMPALA-1771">
<b>Purpose:</b> Returns the hyperbolic cosine of the argument.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="cot" rev="2.3.0 IMPALA-1771">
<dt rev="2.3.0 IMPALA-1771">
COT(DOUBLE a)
</dt>
<dd rev="2.3.0 IMPALA-1771">
<b>Purpose:</b> Returns the cotangent of the argument.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
<p conref="../shared/impala_common.xml#common/added_in_230"/>
</dd>
</dlentry>
<dlentry id="degrees">
<dt>
DEGREES(DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Converts argument value from radians to degrees.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="e">
<dt>
E()
</dt>
<dd>
<b>Purpose:</b> Returns the
<xref
href="https://en.wikipedia.org/wiki/E_(mathematical_constant"
scope="external" format="html">mathematical
constant e</xref>.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="exp">
<dt>
EXP(DOUBLE a), DEXP(DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Returns the
<xref
href="https://en.wikipedia.org/wiki/E_(mathematical_constant"
scope="external" format="html">mathematical
constant e</xref> raised to the power of the argument.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry rev="2.3.0" id="factorial">
<dt rev="2.3.0">
FACTORIAL(integer_type a)
</dt>
<dd rev="2.3.0">
<b>Purpose:</b> Computes the
<xref
href="https://en.wikipedia.org/wiki/Factorial" scope="external"
format="html">factorial</xref>
of an integer value. It works with any integer type.
<p
conref="../shared/impala_common.xml#common/added_in_230"/>
<p>
<b>Usage notes:</b> You can use either the <codeph>factorial()</codeph> function or
the <codeph>!</codeph> operator. The factorial of 0 is 1. Likewise, the
<codeph>factorial()</codeph> function returns 1 for any negative value. The maximum
positive value for the input argument is 20; a value of 21 or greater overflows the
range for a <codeph>BIGINT</codeph> and causes an error.
</p>
<p>
<b>Return type:</b> <codeph>BIGINT</codeph>
</p>
<p conref="../shared/impala_common.xml#common/added_in_230"/>
<codeblock>select factorial(5);
+--------------+
| factorial(5) |
+--------------+
| 120 |
+--------------+
select 5!;
+-----+
| 5! |
+-----+
| 120 |
+-----+
select factorial(0);
+--------------+
| factorial(0) |
+--------------+
| 1 |
+--------------+
select factorial(-100);
+-----------------+
| factorial(-100) |
+-----------------+
| 1 |
+-----------------+
</codeblock>
</dd>
</dlentry>
<dlentry id="floor">
<dt>
FLOOR(DOUBLE a), FLOOR(DECIMAL(p,s) a), DFLOOR(DOUBLE a), DFLOOR(DECIMAL(p,s) a)
</dt>
<dd>
<b>Purpose:</b> Returns the largest integer that is less than or equal to the
argument.
<p>
<b>Return type:</b> Same as the input type
</p>
</dd>
</dlentry>
<dlentry id="fmod">
<dt>
FMOD(DOUBLE a, DOUBLE b), FMOD(FLOAT a, FLOAT b)
</dt>
<dd>
<b>Purpose:</b> Returns the modulus of a floating-point number.
<p>
<b>Return type:</b> <codeph>FLOAT</codeph> or <codeph>DOUBLE</codeph>, depending on
type of arguments
</p>
<p conref="../shared/impala_common.xml#common/added_in_111"/>
<p conref="../shared/impala_common.xml#common/usage_notes_blurb"/>
<p>
Because this function operates on <codeph>DOUBLE</codeph> or <codeph>FLOAT</codeph>
values, it is subject to potential rounding errors for values that cannot be
represented precisely. Prefer to use whole numbers, or values that you know can be
represented precisely by the <codeph>DOUBLE</codeph> or <codeph>FLOAT</codeph>
types.
</p>
<p conref="../shared/impala_common.xml#common/example_blurb"/>
<p>
The following examples show equivalent operations with the <codeph>fmod()</codeph>
function and the <codeph>%</codeph> arithmetic operator, for values not subject to
any rounding error.
</p>
<codeblock>select fmod(10,3);
+-------------+
| fmod(10, 3) |
+-------------+
| 1 |
+-------------+
select fmod(5.5,2);
+--------------+
| fmod(5.5, 2) |
+--------------+
| 1.5 |
+--------------+
select 10 % 3;
+--------+
| 10 % 3 |
+--------+
| 1 |
+--------+
select 5.5 % 2;
+---------+
| 5.5 % 2 |
+---------+
| 1.5 |
+---------+
</codeblock>
<p>
The following examples show operations with the <codeph>fmod()</codeph> function for
values that cannot be represented precisely by the <codeph>DOUBLE</codeph> or
<codeph>FLOAT</codeph> types, and thus are subject to rounding error.
<codeph>fmod(9.9,3.0)</codeph> returns a value slightly different than the expected
0.9 because of rounding. <codeph>fmod(9.9,3.3)</codeph> returns a value quite
different from the expected value of 0 because of rounding error during intermediate
calculations.
</p>
<codeblock>select fmod(9.9,3.0);
+--------------------+
| fmod(9.9, 3.0) |
+--------------------+
| 0.8999996185302734 |
+--------------------+
select fmod(9.9,3.3);
+-------------------+
| fmod(9.9, 3.3) |
+-------------------+
| 3.299999713897705 |
+-------------------+
</codeblock>
</dd>
</dlentry>
<dlentry rev="1.2.2" id="fnv_hash">
<dt rev="1.2.2">
FNV_HASH(type v),
</dt>
<dd rev="1.2.2">
<b>Purpose:</b> Returns a consistent 64-bit value derived from the input argument, for
convenience of implementing hashing logic in an application.
<p>
<b>Return type:</b> <codeph>BIGINT</codeph>
</p>
<p conref="../shared/impala_common.xml#common/usage_notes_blurb"/>
<p>
You might use the return value in an application where you perform load balancing,
bucketing, or some other technique to divide processing or storage.
</p>
<p>
Because the result can be any 64-bit value, to restrict the value to a particular
range, you can use an expression that includes the <codeph>ABS()</codeph> function
and the <codeph>%</codeph> (modulo) operator. For example, to produce a hash value
in the range 0-9, you could use the expression <codeph>ABS(FNV_HASH(x)) %
10</codeph>.
</p>
<p>
This function implements the same algorithm that Impala uses internally for hashing,
on systems where the CRC32 instructions are not available.
</p>
<p>
This function implements the
<xref
href="http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function"
scope="external" format="html">Fowler–Noll–Vo
hash function</xref>, in particular the FNV-1a variation. This is not a perfect hash
function: some combinations of values could produce the same result value. It is not
suitable for cryptographic use.
</p>
<p>
Similar input values of different types could produce different hash values, for
example the same numeric value represented as <codeph>SMALLINT</codeph> or
<codeph>BIGINT</codeph>, <codeph>FLOAT</codeph> or <codeph>DOUBLE</codeph>, or
<codeph>DECIMAL(5,2)</codeph> or <codeph>DECIMAL(20,5)</codeph>.
</p>
<p conref="../shared/impala_common.xml#common/example_blurb"/>
<codeblock>[localhost:21000] > create table h (x int, s string);
[localhost:21000] > insert into h values (0, 'hello'), (1,'world'), (1234567890,'antidisestablishmentarianism');
[localhost:21000] > select x, fnv_hash(x) from h;
+------------+----------------------+
| x | fnv_hash(x) |
+------------+----------------------+
| 0 | -2611523532599129963 |
| 1 | 4307505193096137732 |
| 1234567890 | 3614724209955230832 |
+------------+----------------------+
[localhost:21000] > select s, fnv_hash(s) from h;
+------------------------------+---------------------+
| s | fnv_hash(s) |
+------------------------------+---------------------+
| hello | 6414202926103426347 |
| world | 6535280128821139475 |
| antidisestablishmentarianism | -209330013948433970 |
+------------------------------+---------------------+
[localhost:21000] > select s, abs(fnv_hash(s)) % 10 from h;
+------------------------------+-------------------------+
| s | abs(fnv_hash(s)) % 10.0 |
+------------------------------+-------------------------+
| hello | 8 |
| world | 6 |
| antidisestablishmentarianism | 4 |
+------------------------------+-------------------------+</codeblock>
<p>
For short argument values, the high-order bits of the result have relatively low
entropy:
</p>
<codeblock>[localhost:21000] > create table b (x boolean);
[localhost:21000] > insert into b values (true), (true), (false), (false);
[localhost:21000] > select x, fnv_hash(x) from b;
+-------+---------------------+
| x | fnv_hash(x) |
+-------+---------------------+
| true | 2062020650953872396 |
| true | 2062020650953872396 |
| false | 2062021750465500607 |
| false | 2062021750465500607 |
+-------+---------------------+</codeblock>
<p>
<b>Added in:</b> Impala 1.2.2
</p>
</dd>
</dlentry>
<dlentry rev="1.4.0" id="greatest">
<dt rev="1.4.0">
GREATEST(BIGINT a[, BIGINT b ...]), GREATEST(DOUBLE a[, DOUBLE b ...]),
GREATEST(DECIMAL(p,s) a[, DECIMAL(p,s) b ...]), GREATEST(STRING a[, STRING b ...]),
GREATEST(TIMESTAMP a[, TIMESTAMP b ...])
</dt>
<dd rev="1.4.0">
<b>Purpose:</b> Returns the largest value from a list of expressions.
<p conref="../shared/impala_common.xml#common/return_same_type"/>
</dd>
</dlentry>
<dlentry id="hex">
<dt>
HEX(BIGINT a), HEX(STRING a)
</dt>
<dd>
<b>Purpose:</b> Returns the hexadecimal representation of an integer value, or of the
characters in a string.
<p>
<b>Return type:</b> <codeph>STRING</codeph>
</p>
</dd>
</dlentry>
<dlentry rev="1.4.0" id="is_inf">
<dt rev="1.4.0">
IS_INF(DOUBLE a)
</dt>
<dd rev="1.4.0">
<b>Purpose:</b> Tests whether a value is equal to the special value <q>inf</q>,
signifying infinity.
<p>
<b>Return type:</b> <codeph>BOOLEAN</codeph>
</p>
<p conref="../shared/impala_common.xml#common/usage_notes_blurb"/>
<p conref="../shared/impala_common.xml#common/infinity_and_nan"/>
</dd>
</dlentry>
<dlentry rev="1.4.0" id="is_nan">
<dt rev="1.4.0">
IS_NAN(DOUBLE a)
</dt>
<dd rev="1.4.0">
<b>Purpose:</b> Tests whether a value is equal to the special value <q>NaN</q>,
signifying <q>not a number</q>.
<p>
<b>Return type:</b> <codeph>BOOLEAN</codeph>
</p>
<p conref="../shared/impala_common.xml#common/usage_notes_blurb"/>
<p conref="../shared/impala_common.xml#common/infinity_and_nan"/>
</dd>
</dlentry>
<dlentry rev="1.4.0" id="least">
<dt rev="1.4.0">
LEAST(BIGINT a[, BIGINT b ...]), LEAST(DOUBLE a[, DOUBLE b ...]), LEAST(DECIMAL(p,s)
a[, DECIMAL(p,s) b ...]), LEAST(STRING a[, STRING b ...]), LEAST(TIMESTAMP a[,
TIMESTAMP b ...])
</dt>
<dd rev="1.4.0">
<b>Purpose:</b> Returns the smallest value from a list of expressions.
<p conref="../shared/impala_common.xml#common/return_same_type"/>
</dd>
</dlentry>
<dlentry id="ln">
<dt>
LN(DOUBLE a), DLOG1(DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Returns the
<xref
href="https://en.wikipedia.org/wiki/Natural_logarithm"
scope="external" format="html">natural
logarithm</xref> of the argument.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="log">
<dt>
LOG(DOUBLE base, DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Returns the logarithm of the second argument to the specified base.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>
</dd>
</dlentry>
<dlentry id="log10">
<dt>
LOG10(DOUBLE a), DLOG10(DOUBLE a)
</dt>
<dd>
<b>Purpose:</b> Returns the logarithm of the argument to the base 10.
<p>
<b>Return type:</b> <codeph>DOUBLE</codeph>
</p>