forked from docs/doc-exports
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com> Co-authored-by: Lu, Huayi <luhuayi@huawei.com> Co-committed-by: Lu, Huayi <luhuayi@huawei.com>
69 lines
14 KiB
HTML
69 lines
14 KiB
HTML
<a name="EN-US_TOPIC_0000001233510131"></a><a name="EN-US_TOPIC_0000001233510131"></a>
|
|
|
|
<h1 class="topictitle1">Overview</h1>
|
|
<div id="body8662426"><div class="section" id="EN-US_TOPIC_0000001233510131__s5eea9597369541dfa80d64e9ebdd3869"><h4 class="sectiontitle">Context</h4><p id="EN-US_TOPIC_0000001233510131__a965f9fcc928348c2a87d27e1476df41b">SQL is a typed language. That is, every data item has an associated data type which determines its behavior and allowed usage. <span id="EN-US_TOPIC_0000001233510131__text1641535030">GaussDB(DWS)</span> has an extensible type system that is more general and flexible than other SQL implementations. Hence, most type conversion behavior in <span id="EN-US_TOPIC_0000001233510131__text478432129">GaussDB(DWS)</span> is governed by general rules. This allows the use of mixed-type expressions.</p>
|
|
<p id="EN-US_TOPIC_0000001233510131__a1bdbfa7595174ab0bb9a0a2b75698f51">The <span id="EN-US_TOPIC_0000001233510131__text1775035368">GaussDB(DWS)</span> scanner/parser divides lexical elements into five fundamental categories: integers, floating-point numbers, strings, identifiers, and keywords. Constants of most non-numeric types are first classified as strings. The SQL language definition allows specifying type names with constant strings. Example:</p>
|
|
<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233510131__s7da2fdb54d9e4a66b57b88ad97ed2df4"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
|
|
<span class="normal">2</span>
|
|
<span class="normal">3</span>
|
|
<span class="normal">4</span>
|
|
<span class="normal">5</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="nb">text</span><span class="w"> </span><span class="s1">'Origin'</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="ss">"label"</span><span class="p">,</span><span class="w"> </span><span class="n">point</span><span class="w"> </span><span class="s1">'(0,0)'</span><span class="w"> </span><span class="k">AS</span><span class="w"> </span><span class="ss">"value"</span><span class="p">;</span>
|
|
<span class="w"> </span><span class="n">label</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">value</span>
|
|
<span class="c1">--------+-------</span>
|
|
<span class="w"> </span><span class="n">Origin</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
<p id="EN-US_TOPIC_0000001233510131__a43273ab374b9411dbcf827472ea97736">has two literal constants, of type <strong id="EN-US_TOPIC_0000001233510131__b842352706103821">text</strong> and <strong id="EN-US_TOPIC_0000001233510131__b842352706103826">point</strong>. If a type is not specified for a string literal, then the placeholder type <strong id="EN-US_TOPIC_0000001233510131__b91729302566">unknown</strong> is assigned initially.</p>
|
|
<p id="EN-US_TOPIC_0000001233510131__a1c47516ebb9b4fc0b6b57a252ed87200">There are four fundamental SQL constructs requiring distinct type conversion rules in the <span id="EN-US_TOPIC_0000001233510131__text88368503">GaussDB(DWS)</span> parser:</p>
|
|
<ul id="EN-US_TOPIC_0000001233510131__u3edc1903e2244967936d1e3558aa54a9"><li id="EN-US_TOPIC_0000001233510131__l0761a6f7a6564b7c982f65c7329974c4">Function calls<p id="EN-US_TOPIC_0000001233510131__a960fd9870504400c8180f6509437a8d7"><a name="EN-US_TOPIC_0000001233510131__l0761a6f7a6564b7c982f65c7329974c4"></a><a name="l0761a6f7a6564b7c982f65c7329974c4"></a>Much of the SQL type system is built around a rich set of functions. Functions can have one or more arguments. Since SQL permits function overloading, the function name alone does not uniquely identify the function to be called. The parser must select the right function based on the data types of the supplied arguments.</p>
|
|
</li><li id="EN-US_TOPIC_0000001233510131__lbc1f68199e4b446a86eb061eb56b80e9">Operators<p id="EN-US_TOPIC_0000001233510131__a936ac123a7124caabd52c1376db865e8"><a name="EN-US_TOPIC_0000001233510131__lbc1f68199e4b446a86eb061eb56b80e9"></a><a name="lbc1f68199e4b446a86eb061eb56b80e9"></a>SQL allows expressions with prefix and postfix unary (one-argument) operators, as well as binary (two-argument) operators. Like functions, operators can be overloaded, so the same problem of selecting the right operator exists.</p>
|
|
</li><li id="EN-US_TOPIC_0000001233510131__l380457e682884652893b809c6839d352">Value Storage<p id="EN-US_TOPIC_0000001233510131__a13a4c28d6de24dafb123b3be0837b690"><a name="EN-US_TOPIC_0000001233510131__l380457e682884652893b809c6839d352"></a><a name="l380457e682884652893b809c6839d352"></a>SQL <strong id="EN-US_TOPIC_0000001233510131__b842352706112739">INSERT</strong> and <strong id="EN-US_TOPIC_0000001233510131__b842352706112745">UPDATE</strong> statements place the results of expressions into a table. The expressions in the statement must be matched up with, and perhaps converted to, the types of the target columns.</p>
|
|
</li><li id="EN-US_TOPIC_0000001233510131__l7c0f60dd04ec42c68dddfe4ef5c94008"><strong id="EN-US_TOPIC_0000001233510131__b84235270611291">UNION</strong>, <strong id="EN-US_TOPIC_0000001233510131__b84235270611294">CASE</strong>, and related constructs<p id="EN-US_TOPIC_0000001233510131__a1808068808784da3a9e5475a40feb1bd">Since all query results from a unionized <strong id="EN-US_TOPIC_0000001233510131__b842352706112935">SELECT</strong> statement must appear in a single set of columns, the types of the results of each <strong id="EN-US_TOPIC_0000001233510131__b842352706112948">SELECT</strong> clause must be matched up and converted to a uniform set. Similarly, the result expressions of a <strong id="EN-US_TOPIC_0000001233510131__b842352706113022">CASE</strong> construct must be converted to a common type so that the <strong id="EN-US_TOPIC_0000001233510131__b842352706113031">CASE</strong> expression as a whole has a known output type. The same holds for <strong id="EN-US_TOPIC_0000001233510131__b842352706113051">ARRAY</strong> constructs, and for the <strong id="EN-US_TOPIC_0000001233510131__b842352706113057">GREATEST</strong> and <strong id="EN-US_TOPIC_0000001233510131__b84235270611310">LEAST</strong> functions.</p>
|
|
</li></ul>
|
|
<p id="EN-US_TOPIC_0000001233510131__abd91f6d473e5473fae186d7d1ea0e34b">The system catalog <strong id="EN-US_TOPIC_0000001233510131__b833061741016">pg_cast</strong> stores information about which conversions, or casts, exist between which data types, and how to perform those conversions.</p>
|
|
<p id="EN-US_TOPIC_0000001233510131__a64696ab4cf5a442d865615c96f7d7ca6">The return type and conversion behavior of an expression are determined during semantic analysis. Data types are divided into several basic type categories, including <strong id="EN-US_TOPIC_0000001233510131__b13177164316228"><span id="EN-US_TOPIC_0000001233510131__text214617390222">boolean</span></strong>, <strong id="EN-US_TOPIC_0000001233510131__b842352706114028">numeric</strong>, <strong id="EN-US_TOPIC_0000001233510131__b842352706114033">string</strong>, <strong id="EN-US_TOPIC_0000001233510131__b842352706114039">bitstring</strong>, <strong id="EN-US_TOPIC_0000001233510131__b842352706114045">datetime</strong>, <strong id="EN-US_TOPIC_0000001233510131__b842352706114050">timespan</strong>, <strong id="EN-US_TOPIC_0000001233510131__b842352706114056">geometric</strong>, and <strong id="EN-US_TOPIC_0000001233510131__b84235270611418">network</strong>. Within each category there can be one or more preferred types, which are preferred when there is a choice of possible types. With careful selection of preferred types and available implicit casts, it is possible to ensure that ambiguous expressions (those with multiple candidate parsing solutions) can be resolved in a useful way.</p>
|
|
<p id="EN-US_TOPIC_0000001233510131__a91f32802ab6640babd6a0208a8fbcff8">All type conversion rules must comply with the following basic principles:</p>
|
|
<ul id="EN-US_TOPIC_0000001233510131__u36bc257cda6848d4b9934fd2b4eb5ea9"><li id="EN-US_TOPIC_0000001233510131__lb872c224bce3427d99b73e09177a26b2">Implicit conversions should never have surprising or unpredictable outcomes.</li><li id="EN-US_TOPIC_0000001233510131__l65f38ee0bac54f0881775bad3125d429">There should be no extra overhead in the parser or executor if a query does not need implicit type conversion. That is, if a query is well-formed and the types already match, then the query should execute without spending extra time in the parser and without introducing unnecessary implicit conversion calls in the query.</li><li id="EN-US_TOPIC_0000001233510131__lc47a164c640f49b58a8009ed7fdfd47d">Additionally, if a query usually requires an implicit conversion for a function, and if then the user defines a new function with the correct argument types, the parser should use this new function.</li></ul>
|
|
</div>
|
|
<div class="section" id="EN-US_TOPIC_0000001233510131__s6c7bfb5b733c473bbdc05fb34f856021"><h4 class="sectiontitle">Converting Empty Strings to Numeric Values in TD-Compatible Mode</h4><ul id="EN-US_TOPIC_0000001233510131__uc41f3b659cfa40c59e8397fca7f459cf"><li id="EN-US_TOPIC_0000001233510131__l33cf764b60a04d6db5003c59bead82a8">Different from the Oracle database, which processes an empty string as NULL, Teradata database converts an empty string to <strong id="EN-US_TOPIC_0000001233510131__b9422104033613">0</strong> by default. Therefore, when an empty string is queried, value <strong id="EN-US_TOPIC_0000001233510131__b210611693713">0</strong> is found. Similarly, in TD-compatible mode, the empty string is converted to <strong id="EN-US_TOPIC_0000001233510131__b45431540185618">0</strong> of the corresponding numeric type by default. In addition, '-', '+', and ' ' are converted to <strong id="EN-US_TOPIC_0000001233510131__b964518428562">0</strong> by default in TD-compatible mode, but an error is reported for a decimal point string. Example:<div class="codecoloring" codetype="Sql" id="EN-US_TOPIC_0000001233510131__screen763119275010"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal"> 1</span>
|
|
<span class="normal"> 2</span>
|
|
<span class="normal"> 3</span>
|
|
<span class="normal"> 4</span>
|
|
<span class="normal"> 5</span>
|
|
<span class="normal"> 6</span>
|
|
<span class="normal"> 7</span>
|
|
<span class="normal"> 8</span>
|
|
<span class="normal"> 9</span>
|
|
<span class="normal">10</span>
|
|
<span class="normal">11</span>
|
|
<span class="normal">12</span>
|
|
<span class="normal">13</span>
|
|
<span class="normal">14</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">CREATE</span><span class="w"> </span><span class="k">TABLE</span><span class="w"> </span><span class="n">t1</span><span class="p">(</span><span class="k">no</span><span class="w"> </span><span class="nb">int</span><span class="p">,</span><span class="n">col</span><span class="w"> </span><span class="nb">varchar</span><span class="p">);</span>
|
|
<span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="k">values</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="s1">''</span><span class="p">);</span>
|
|
<span class="k">INSERT</span><span class="w"> </span><span class="k">INTO</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="k">values</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="k">null</span><span class="p">);</span>
|
|
<span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">col</span><span class="w"> </span><span class="k">is</span><span class="w"> </span><span class="k">null</span><span class="p">;</span>
|
|
<span class="w"> </span><span class="k">no</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">col</span>
|
|
<span class="c1">----+-----</span>
|
|
<span class="w"> </span><span class="mi">2</span><span class="w"> </span><span class="o">|</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
|
|
<span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">t1</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">col</span><span class="o">=</span><span class="s1">''</span><span class="p">;</span>
|
|
<span class="w"> </span><span class="k">no</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">col</span>
|
|
<span class="c1">----+-----</span>
|
|
<span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">|</span>
|
|
<span class="p">(</span><span class="mi">1</span><span class="w"> </span><span class="k">row</span><span class="p">)</span>
|
|
</pre></div></td></tr></table></div>
|
|
|
|
</div>
|
|
</li><li id="EN-US_TOPIC_0000001233510131__li585818535277">The method of converting an empty string into a numeric value in MySQL-compatible mode is the same as that in TD-compatible mode.</li></ul>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="dws_06_0075.html">Type Conversion</a></div>
|
|
</div>
|
|
</div>
|
|
|