-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconversion.html
201 lines (186 loc) · 10.4 KB
/
conversion.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>7.5. Conversion Functions — Presto 323 Documentation</title>
<link rel="stylesheet" href="../_static/presto.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '323',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="7.6. Mathematical Functions and Operators" href="math.html" />
<link rel="prev" title="7.4. Lambda Expressions" href="lambda.html" />
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-133457846-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-133457846-1');
</script>
</head>
<body>
<div class="header">
<h1 class="heading"><a href="../index.html">
<span>Presto 323 Documentation</span></a></h1>
<h2 class="heading"><span>7.5. Conversion Functions</span></h2>
</div>
<div class="topnav">
<p class="nav">
<span class="left">
« <a href="lambda.html">7.4. Lambda Expressions</a>
</span>
<span class="right">
<a href="math.html">7.6. Mathematical Functions and Operators</a> »
</span>
</p>
</div>
<div class="content">
<div class="section" id="conversion-functions">
<h1>7.5. Conversion Functions</h1>
<p>Presto will implicitly convert numeric and character values to the
correct type if such a conversion is possible. Presto will not convert
between character and numeric types. For example, a query that expects
a varchar will not automatically convert a bigint value to an
equivalent varchar.</p>
<p>When necessary, values can be explicitly cast to a particular type.</p>
<div class="section" id="id1">
<h2>Conversion Functions</h2>
<dl class="function">
<dt id="cast">
<code class="descname">cast</code><span class="sig-paren">(</span><em>value AS type</em><span class="sig-paren">)</span> → type</dt>
<dd><p>Explicitly cast a value as a type. This can be used to cast a
varchar to a numeric value type and vice versa.</p>
</dd></dl>
<dl class="function">
<dt id="try_cast">
<code class="descname">try_cast</code><span class="sig-paren">(</span><em>value AS type</em><span class="sig-paren">)</span> → type</dt>
<dd><p>Like <a class="reference internal" href="#cast" title="cast"><code class="xref py py-func docutils literal"><span class="pre">cast()</span></code></a>, but returns null if the cast fails.</p>
</dd></dl>
</div>
<div class="section" id="formatting">
<h2>Formatting</h2>
<dl class="function">
<dt id="format">
<code class="descname">format</code><span class="sig-paren">(</span><em>format</em>, <em>args...</em><span class="sig-paren">)</span> → varchar</dt>
<dd><p>Returns a formatted string using the specified <a class="reference external" href="https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax">format string</a>
and arguments:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">format</span><span class="p">(</span><span class="s1">'%s%%'</span><span class="p">,</span> <span class="mi">123</span><span class="p">);</span> <span class="c1">-- '123%'</span>
<span class="k">SELECT</span> <span class="n">format</span><span class="p">(</span><span class="s1">'%.5f'</span><span class="p">,</span> <span class="n">pi</span><span class="p">());</span> <span class="c1">-- '3.14159'</span>
<span class="k">SELECT</span> <span class="n">format</span><span class="p">(</span><span class="s1">'%03d'</span><span class="p">,</span> <span class="mi">8</span><span class="p">);</span> <span class="c1">-- '008'</span>
<span class="k">SELECT</span> <span class="n">format</span><span class="p">(</span><span class="s1">'%,.2f'</span><span class="p">,</span> <span class="mi">1234567</span><span class="p">.</span><span class="mi">89</span><span class="p">);</span> <span class="c1">-- '1,234,567.89'</span>
<span class="k">SELECT</span> <span class="n">format</span><span class="p">(</span><span class="s1">'%-7s,%7s'</span><span class="p">,</span> <span class="s1">'hello'</span><span class="p">,</span> <span class="s1">'world'</span><span class="p">);</span> <span class="c1">-- 'hello , world'</span>
<span class="k">SELECT</span> <span class="n">format</span><span class="p">(</span><span class="s1">'%2$s %3$s %1$s'</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">);</span> <span class="c1">-- 'b c a'</span>
<span class="k">SELECT</span> <span class="n">format</span><span class="p">(</span><span class="s1">'%1$tA, %1$tB %1$te, %1$tY'</span><span class="p">,</span> <span class="nb">date</span> <span class="s1">'2006-07-04'</span><span class="p">);</span> <span class="c1">-- 'Tuesday, July 4, 2006'</span>
</pre></div>
</div>
</dd></dl>
</div>
<div class="section" id="data-size">
<h2>Data Size</h2>
<p>The <code class="docutils literal"><span class="pre">parse_presto_data_size</span></code> function supports the following units:</p>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="38%" />
<col width="41%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Unit</th>
<th class="head">Description</th>
<th class="head">Value</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal"><span class="pre">B</span></code></td>
<td>Bytes</td>
<td>1</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">kB</span></code></td>
<td>Kilobytes</td>
<td>1024</td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">MB</span></code></td>
<td>Megabytes</td>
<td>1024<sup>2</sup></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">GB</span></code></td>
<td>Gigabytes</td>
<td>1024<sup>3</sup></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">TB</span></code></td>
<td>Terabytes</td>
<td>1024<sup>4</sup></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">PB</span></code></td>
<td>Petabytes</td>
<td>1024<sup>5</sup></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">EB</span></code></td>
<td>Exabytes</td>
<td>1024<sup>6</sup></td>
</tr>
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">ZB</span></code></td>
<td>Zettabytes</td>
<td>1024<sup>7</sup></td>
</tr>
<tr class="row-even"><td><code class="docutils literal"><span class="pre">YB</span></code></td>
<td>Yottabytes</td>
<td>1024<sup>8</sup></td>
</tr>
</tbody>
</table>
<dl class="function">
<dt id="parse_presto_data_size">
<code class="descname">parse_presto_data_size</code><span class="sig-paren">(</span><em>string) -> decimal(38</em><span class="sig-paren">)</span></dt>
<dd><p>Parses <code class="docutils literal"><span class="pre">string</span></code> of format <code class="docutils literal"><span class="pre">value</span> <span class="pre">unit</span></code> into a number, where
<code class="docutils literal"><span class="pre">value</span></code> is the fractional number of <code class="docutils literal"><span class="pre">unit</span></code> values:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">parse_presto_data_size</span><span class="p">(</span><span class="s1">'1B'</span><span class="p">);</span> <span class="c1">-- 1</span>
<span class="k">SELECT</span> <span class="n">parse_presto_data_size</span><span class="p">(</span><span class="s1">'1kB'</span><span class="p">);</span> <span class="c1">-- 1024</span>
<span class="k">SELECT</span> <span class="n">parse_presto_data_size</span><span class="p">(</span><span class="s1">'1MB'</span><span class="p">);</span> <span class="c1">-- 1048576</span>
<span class="k">SELECT</span> <span class="n">parse_presto_data_size</span><span class="p">(</span><span class="s1">'2.3MB'</span><span class="p">);</span> <span class="c1">-- 2411724</span>
</pre></div>
</div>
</dd></dl>
</div>
<div class="section" id="miscellaneous">
<h2>Miscellaneous</h2>
<dl class="function">
<dt id="typeof">
<code class="descname">typeof</code><span class="sig-paren">(</span><em>expr</em><span class="sig-paren">)</span> → varchar</dt>
<dd><p>Returns the name of the type of the provided expression:</p>
<div class="highlight-sql"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">typeof</span><span class="p">(</span><span class="mi">123</span><span class="p">);</span> <span class="c1">-- integer</span>
<span class="k">SELECT</span> <span class="n">typeof</span><span class="p">(</span><span class="s1">'cat'</span><span class="p">);</span> <span class="c1">-- varchar(3)</span>
<span class="k">SELECT</span> <span class="n">typeof</span><span class="p">(</span><span class="n">cos</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">.</span><span class="mi">5</span><span class="p">);</span> <span class="c1">-- double</span>
</pre></div>
</div>
</dd></dl>
</div>
</div>
</div>
<div class="bottomnav">
<p class="nav">
<span class="left">
« <a href="lambda.html">7.4. Lambda Expressions</a>
</span>
<span class="right">
<a href="math.html">7.6. Mathematical Functions and Operators</a> »
</span>
</p>
</div>
<div class="footer" role="contentinfo">
</div>
</body>
</html>