blob: eeb05ce0f4ef7bacd521edbd6fd13eae19ec277f [file] [log] [blame]
<!doctype html>
<html lang="en" dir="ltr" class="docs-wrapper docs-doc-page docs-version-current plugin-docs plugin-id-default docs-doc-id-querying/sql-data-types">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v2.4.1">
<title data-rh="true">SQL data types | Apache® Druid</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://druid.apache.org/img/druid_nav.png"><meta data-rh="true" name="twitter:image" content="https://druid.apache.org/img/druid_nav.png"><meta data-rh="true" property="og:url" content="https://druid.apache.org/docs/26.0.0/querying/sql-data-types"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="SQL data types | Apache® Druid"><meta data-rh="true" name="description" content="&lt;!--"><meta data-rh="true" property="og:description" content="&lt;!--"><link data-rh="true" rel="icon" href="/img/favicon.png"><link data-rh="true" rel="canonical" href="https://druid.apache.org/docs/26.0.0/querying/sql-data-types"><link data-rh="true" rel="alternate" href="https://druid.apache.org/docs/26.0.0/querying/sql-data-types" hreflang="en"><link data-rh="true" rel="alternate" href="https://druid.apache.org/docs/26.0.0/querying/sql-data-types" hreflang="x-default"><link rel="preconnect" href="https://www.google-analytics.com">
<link rel="preconnect" href="https://www.googletagmanager.com">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-131010415-1"></script>
<script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","UA-131010415-1",{})</script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script><link rel="stylesheet" href="/assets/css/styles.f80751b3.css">
<link rel="preload" href="/assets/js/runtime~main.38900cbf.js" as="script">
<link rel="preload" href="/assets/js/main.5e106d68.js" as="script">
</head>
<body class="navigation-with-keyboard">
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
<div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="navbar navbar--fixed-top navbar--dark"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="/img/druid_nav.png" alt="Apache® Druid" class="themedImage_ToTc themedImage--light_HNdA"><img src="/img/druid_nav.png" alt="Apache® Druid" class="themedImage_ToTc themedImage--dark_i4oU"></div></a></div><div class="navbar__items navbar__items--right"><a class="navbar__item navbar__link" href="/technology">Technology</a><a class="navbar__item navbar__link" href="/use-cases">Use Cases</a><a class="navbar__item navbar__link" href="/druid-powered">Powered By</a><a class="navbar__item navbar__link" href="/docs/26.0.0/design/">Docs</a><a class="navbar__item navbar__link" href="/community/">Community</a><div class="navbar__item dropdown dropdown--hoverable dropdown--right"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">Apache®</a><ul class="dropdown__menu"><li><a href="https://www.apache.org/" target="_blank" rel="noopener noreferrer" class="dropdown__link">Foundation<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://apachecon.com/?ref=druid.apache.org" target="_blank" rel="noopener noreferrer" class="dropdown__link">Events<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://www.apache.org/licenses/" target="_blank" rel="noopener noreferrer" class="dropdown__link">License<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://www.apache.org/foundation/thanks.html" target="_blank" rel="noopener noreferrer" class="dropdown__link">Thanks<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://www.apache.org/security/" target="_blank" rel="noopener noreferrer" class="dropdown__link">Security<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://www.apache.org/foundation/sponsorship.html" target="_blank" rel="noopener noreferrer" class="dropdown__link">Sponsorship<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div><a class="navbar__item navbar__link" href="/downloads/">Download</a><div class="searchBox_ZlJk"><div class="navbar__search"><span aria-label="expand searchbar" role="button" class="search-icon" tabindex="0"></span><input type="search" id="search_input_react" placeholder="Loading..." aria-label="Search" class="navbar__search-input search-bar" disabled=""></div></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div id="__docusaurus_skipToContent_fallback" class="main-wrapper mainWrapper_z2l0 docsWrapper_BCFX"><button aria-label="Scroll back to top" class="clean-btn theme-back-to-top-button backToTopButton_sjWU" type="button"></button><div class="docPage__5DB"><aside class="theme-doc-sidebar-container docSidebarContainer_b6E3"><div class="sidebarViewport_Xe31"><div class="sidebar_njMd"><nav aria-label="Docs sidebar" class="menu thin-scrollbar menu_SIkG"><ul class="theme-doc-sidebar-menu menu__list"><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/design/">Getting started</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/tutorials/tutorial-batch">Tutorials</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/design/architecture">Design</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/ingestion/">Ingestion</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/data-management/">Data management</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret menu__link--active" aria-expanded="true" href="/docs/26.0.0/querying/sql">Querying</a></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret menu__link--active" aria-expanded="true" tabindex="0" href="/docs/26.0.0/querying/sql">Druid SQL</a></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql">Overview and syntax</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/docs/26.0.0/querying/sql-data-types">SQL data types</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-operators">Operators</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-scalar">Scalar functions</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-aggregations">Aggregation functions</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-array-functions">Array functions</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-multivalue-string-functions">Multi-value string functions</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-json-functions">JSON functions</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-functions">All functions</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-api">Druid SQL API</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-jdbc">JDBC driver API</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-query-context">SQL query context</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-metadata-tables">SQL metadata tables</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/sql-translation">SQL query translation</a></li></ul></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/">Native queries</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/query-execution">Query execution</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/26.0.0/querying/troubleshooting">Troubleshooting</a></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" tabindex="0" href="/docs/26.0.0/querying/datasource">Concepts</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" tabindex="0" href="/docs/26.0.0/querying/timeseriesquery">Native query types</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" tabindex="0" href="/docs/26.0.0/querying/filters">Native query components</a></div></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/configuration/">Configuration</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/operations/web-console">Operations</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/development/overview">Development</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/26.0.0/misc/papers-and-talks">Misc</a></div></li></ul></nav></div></div></aside><main class="docMainContainer_gTbr"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_VOVn"><div class="docItemContainer_Djhp"><article><nav class="theme-doc-breadcrumbs breadcrumbsContainer_Z_bl" aria-label="Breadcrumbs"><ul class="breadcrumbs" itemscope="" itemtype="https://schema.org/BreadcrumbList"><li class="breadcrumbs__item"><a aria-label="Home page" class="breadcrumbs__link" href="/"><svg viewBox="0 0 24 24" class="breadcrumbHomeIcon_YNFT"><path d="M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z" fill="currentColor"></path></svg></a></li><li class="breadcrumbs__item"><span class="breadcrumbs__link">Querying</span><meta itemprop="position" content="1"></li><li class="breadcrumbs__item"><span class="breadcrumbs__link">Druid SQL</span><meta itemprop="position" content="2"></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item breadcrumbs__item--active"><span class="breadcrumbs__link" itemprop="name">SQL data types</span><meta itemprop="position" content="3"></li></ul></nav><div class="tocCollapsible_ETCw theme-doc-toc-mobile tocMobile_ITEo"><button type="button" class="clean-btn tocCollapsibleButton_TO0P">On this page</button></div><div class="theme-doc-markdown markdown"><header><h1>SQL data types</h1></header><blockquote><p>Apache Druid supports two query languages: Druid SQL and <a href="/docs/26.0.0/querying/">native queries</a>.
This document describes the SQL language.</p></blockquote><p>Columns in Druid are associated with a specific data type. This topic describes supported data types in <a href="/docs/26.0.0/querying/sql">Druid SQL</a>. </p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="standard-types">Standard types<a href="#standard-types" class="hash-link" aria-label="Direct link to Standard types" title="Direct link to Standard types"></a></h2><p>Druid natively supports the following basic column types:</p><ul><li>LONG: (64 bit signed int)</li><li>FLOAT (32 bit float)</li><li>DOUBLE: (64 bit float)</li><li>STRING: (UTF-8 encoded strings and string arrays)</li><li>COMPLEX: non-standard data types, such as nested JSON, hyperUnique and approxHistogram, and DataSketches</li><li>ARRAY: arrays composed of any of these types</li></ul><p>Timestamps (including the <code>__time</code> column) are treated by Druid as longs, with the value being the number of
milliseconds since 1970-01-01 00:00:00 UTC, not counting leap seconds. Therefore, timestamps in Druid do not carry any
timezone information, but only carry information about the exact moment in time they represent. See the
<a href="/docs/26.0.0/querying/sql-scalar#date-and-time-functions">Time functions</a> section for more information about timestamp handling.</p><p>Casts between two SQL types with the same Druid runtime type (see below table) have no effect, other than exceptions
noted in the table. Casts between two SQL types that have different Druid runtime types generate a runtime cast in
Druid. If a value cannot be cast to the target type, as in <code>CAST(&#x27;foo&#x27; AS BIGINT)</code>, Druid either substitutes a default
value (when <code>druid.generic.useDefaultValueForNull = true</code>, the default mode), or substitutes <a href="#null-values">NULL</a> (when
<code>druid.generic.useDefaultValueForNull = false</code>). NULL values cast to non-nullable types are also substituted with a
default value. For example, if <code>druid.generic.useDefaultValueForNull = true</code>, a null VARCHAR cast to BIGINT is converted
to a zero.</p><p>The following table describes how Druid maps SQL types onto native types when running queries.</p><table><thead><tr><th>SQL type</th><th>Druid runtime type</th><th>Default value<sup>*</sup></th><th>Notes</th></tr></thead><tbody><tr><td>CHAR</td><td>STRING</td><td><code>&#x27;&#x27;</code></td><td></td></tr><tr><td>VARCHAR</td><td>STRING</td><td><code>&#x27;&#x27;</code></td><td>Druid STRING columns are reported as VARCHAR. Can include <a href="#multi-value-strings">multi-value strings</a> as well.</td></tr><tr><td>DECIMAL</td><td>DOUBLE</td><td><code>0.0</code></td><td>DECIMAL uses floating point, not fixed point math</td></tr><tr><td>FLOAT</td><td>FLOAT</td><td><code>0.0</code></td><td>Druid FLOAT columns are reported as FLOAT</td></tr><tr><td>REAL</td><td>DOUBLE</td><td><code>0.0</code></td><td></td></tr><tr><td>DOUBLE</td><td>DOUBLE</td><td><code>0.0</code></td><td>Druid DOUBLE columns are reported as DOUBLE</td></tr><tr><td>BOOLEAN</td><td>LONG</td><td><code>false</code></td><td></td></tr><tr><td>TINYINT</td><td>LONG</td><td><code>0</code></td><td></td></tr><tr><td>SMALLINT</td><td>LONG</td><td><code>0</code></td><td></td></tr><tr><td>INTEGER</td><td>LONG</td><td><code>0</code></td><td></td></tr><tr><td>BIGINT</td><td>LONG</td><td><code>0</code></td><td>Druid LONG columns (except <code>__time</code>) are reported as BIGINT</td></tr><tr><td>TIMESTAMP</td><td>LONG</td><td><code>0</code>, meaning 1970-01-01 00:00:00 UTC</td><td>Druid&#x27;s <code>__time</code> column is reported as TIMESTAMP. Casts between string and timestamp types assume standard SQL formatting, e.g. <code>2000-01-02 03:04:05</code>, <em>not</em> ISO8601 formatting. For handling other formats, use one of the <a href="/docs/26.0.0/querying/sql-scalar#date-and-time-functions">time functions</a>.</td></tr><tr><td>DATE</td><td>LONG</td><td><code>0</code>, meaning 1970-01-01</td><td>Casting TIMESTAMP to DATE rounds down the timestamp to the nearest day. Casts between string and date types assume standard SQL formatting, e.g. <code>2000-01-02</code>. For handling other formats, use one of the <a href="/docs/26.0.0/querying/sql-scalar#date-and-time-functions">time functions</a>.</td></tr><tr><td>ARRAY</td><td>ARRAY</td><td><code>NULL</code></td><td>Druid native array types work as SQL arrays, and multi-value strings can be converted to arrays. See the <a href="#arrays"><code>ARRAY</code> details</a>.</td></tr><tr><td>OTHER</td><td>COMPLEX</td><td>none</td><td>May represent various Druid column types such as hyperUnique, approxHistogram, etc.</td></tr></tbody></table><sup>*</sup> Default value applies if `druid.generic.useDefaultValueForNull = true` (the default mode). Otherwise, the default value is `NULL` for all types.<h2 class="anchor anchorWithStickyNavbar_LWe7" id="multi-value-strings">Multi-value strings<a href="#multi-value-strings" class="hash-link" aria-label="Direct link to Multi-value strings" title="Direct link to Multi-value strings"></a></h2><p>Druid&#x27;s native type system allows strings to potentially have multiple values. These
<a href="/docs/26.0.0/querying/multi-value-dimensions">multi-value string dimensions</a> are reported in SQL as <code>VARCHAR</code> typed, and can be
syntactically used like any other <code>VARCHAR</code>. Regular string functions that refer to multi-value string dimensions are
applied to all values for each row individually. Multi-value string dimensions can also be treated as arrays via special
<a href="/docs/26.0.0/querying/sql-multivalue-string-functions">multi-value string functions</a>, which can perform powerful array-aware operations, but retain
their <code>VARCHAR</code> typing and behavior.</p><p>Grouping by a multi-value expression observes the native Druid multi-value aggregation behavior, which is similar to
an implicit SQL <code>UNNEST</code>. Refer to the documentation on <a href="/docs/26.0.0/querying/multi-value-dimensions">multi-value string dimensions</a>
for additional details.</p><blockquote><p>Because multi-value dimensions are treated by the SQL planner as <code>VARCHAR</code>, there are some inconsistencies between how
they are handled in Druid SQL and in native queries. For example, expressions involving multi-value dimensions may be
incorrectly optimized by the Druid SQL planner: <code>multi_val_dim = &#x27;a&#x27; AND multi_val_dim = &#x27;b&#x27;</code> is optimized to
<code>false</code>, even though it is possible for a single row to have both &quot;a&quot; and &quot;b&quot; as values for <code>multi_val_dim</code>. The
SQL behavior of multi-value dimensions may change in a future release to more closely align with their behavior
in native queries, but the <a href="/docs/26.0.0/querying/sql-multivalue-string-functions">multi-value string functions</a> should be able to provide
nearly all possible native functionality.</p></blockquote><h2 class="anchor anchorWithStickyNavbar_LWe7" id="arrays">Arrays<a href="#arrays" class="hash-link" aria-label="Direct link to Arrays" title="Direct link to Arrays"></a></h2><p>Druid supports <code>ARRAY</code> types constructed at query time, though it currently lacks the ability to store them in
segments. <code>ARRAY</code> types behave as standard SQL arrays, where results are grouped by matching entire arrays. This is in
contrast to the implicit <code>UNNEST</code> that occurs when grouping on multi-value dimensions directly or when used with the
multi-value functions. You can convert multi-value dimensions to standard SQL arrays either by explicitly by converting
them with <code>MV_TO_ARRAY</code> or implicitly when used within the <a href="/docs/26.0.0/querying/sql-array-functions">array functions</a>. Arrays may
also be constructed from multiple columns using the array functions.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="multi-value-strings-behavior">Multi-value strings behavior<a href="#multi-value-strings-behavior" class="hash-link" aria-label="Direct link to Multi-value strings behavior" title="Direct link to Multi-value strings behavior"></a></h2><p>The behavior of Druid <a href="/docs/26.0.0/querying/multi-value-dimensions">multi-value string dimensions</a> varies depending on the context of
their usage.</p><p>When used with standard <code>VARCHAR</code> functions which expect a single input value per row, such as <code>CONCAT</code>, Druid will map
the function across all values in the row. If the row is null or empty, the function receives <code>NULL</code> as its input.</p><p>When used with the explicit <a href="/docs/26.0.0/querying/sql-multivalue-string-functions">multi-value string functions</a>, Druid processes the
row values as if they were <code>ARRAY</code> typed. Any operations which produce null and empty rows are distinguished as
separate values (unlike implicit mapping behavior). These multi-value string functions, typically denoted with an <code>MV_</code>
prefix, retain their <code>VARCHAR</code> type after the computation is complete. Note that Druid multi-value columns do <em>not</em>
distinguish between empty and null rows. An empty row will never appear natively as input to a multi-valued function,
but any multi-value function which manipulates the array form of the value may produce an empty array, which is handled
separately while processing.</p><blockquote><p>Do not mix the usage of multi-value functions and normal scalar functions within the same expression, as the planner will be unable
to determine how to properly process the value given its ambiguous usage. A multi-value string must be treated consistently within
an expression.</p></blockquote><p>When converted to <code>ARRAY</code> or used with <a href="/docs/26.0.0/querying/sql-array-functions">array functions</a>, multi-value strings behave as standard SQL arrays and can no longer
be manipulated with non-array functions.</p><p>Druid serializes multi-value <code>VARCHAR</code> results as a JSON string of the array, if grouping was not applied on the value.
If the value was grouped, due to the implicit <code>UNNEST</code> behavior, all results will always be standard single value
<code>VARCHAR</code>. <code>ARRAY</code> typed results will be serialized into stringified JSON arrays if the context parameter
<code>sqlStringifyArrays</code> is set, otherwise they remain in their array format.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="null-values">NULL values<a href="#null-values" class="hash-link" aria-label="Direct link to NULL values" title="Direct link to NULL values"></a></h2><p>The <a href="/docs/26.0.0/configuration/#sql-compatible-null-handling"><code>druid.generic.useDefaultValueForNull</code></a>
runtime property controls Druid&#x27;s NULL handling mode. For the most SQL compliant behavior, set this to <code>false</code>.</p><p>When <code>druid.generic.useDefaultValueForNull = true</code> (the default mode), Druid treats NULLs and empty strings
interchangeably, rather than according to the SQL standard. In this mode Druid SQL only has partial support for NULLs.
For example, the expressions <code>col IS NULL</code> and <code>col = &#x27;&#x27;</code> are equivalent, and both evaluate to true if <code>col</code>
contains an empty string. Similarly, the expression <code>COALESCE(col1, col2)</code> returns <code>col2</code> if <code>col1</code> is an empty
string. While the <code>COUNT(*)</code> aggregator counts all rows, the <code>COUNT(expr)</code> aggregator counts the number of rows
where <code>expr</code> is neither null nor the empty string. Numeric columns in this mode are not nullable; any null or missing
values are treated as zeroes.</p><p>When <code>druid.generic.useDefaultValueForNull = false</code>, NULLs are treated more closely to the SQL standard. In this mode,
numeric NULL is permitted, and NULLs and empty strings are no longer treated as interchangeable. This property
affects both storage and querying, and must be set on all Druid service types to be available at both ingestion time
and query time. There is some overhead associated with the ability to handle NULLs; see
the <a href="/docs/26.0.0/design/segments#handling-null-values">segment internals</a> documentation for more details.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="boolean-logic">Boolean logic<a href="#boolean-logic" class="hash-link" aria-label="Direct link to Boolean logic" title="Direct link to Boolean logic"></a></h2><p>The <a href="/docs/26.0.0/configuration/#expression-processing-configurations"><code>druid.expressions.useStrictBooleans</code></a>
runtime property controls Druid&#x27;s boolean logic mode. For the most SQL compliant behavior, set this to <code>true</code>.</p><p>When <code>druid.expressions.useStrictBooleans = false</code> (the default mode), Druid uses two-valued logic.</p><p>When <code>druid.expressions.useStrictBooleans = true</code>, Druid uses three-valued logic for
<a href="/docs/26.0.0/misc/math-expr">expressions</a> evaluation, such as <code>expression</code> virtual columns or <code>expression</code> filters.
However, even in this mode, Druid uses two-valued logic for filter types other than <code>expression</code>.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="nested-columns">Nested columns<a href="#nested-columns" class="hash-link" aria-label="Direct link to Nested columns" title="Direct link to Nested columns"></a></h2><p>Druid supports storing nested data structures in segments using the native <code>COMPLEX&lt;json&gt;</code> type. See <a href="/docs/26.0.0/querying/nested-columns">Nested columns</a> for more information.</p><p>You can interact with nested data using <a href="/docs/26.0.0/querying/sql-json-functions">JSON functions</a>, which can extract nested values, parse from string, serialize to string, and create new <code>COMPLEX&lt;json&gt;</code> structures.</p><p><code>COMPLEX</code> types have limited functionality outside the specialized functions that use them, so their behavior is undefined when:</p><ul><li>Grouping on complex values.</li><li>Filtering directly on complex values, such as <code>WHERE json is NULL</code>.</li><li>Used as inputs to aggregators without specialized handling for a specific complex type.</li></ul><p>In many cases, functions are provided to translate <code>COMPLEX</code> value types to <code>STRING</code>, which serves as a workaround solution until <code>COMPLEX</code> type functionality can be improved.</p></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages"><a class="pagination-nav__link pagination-nav__link--prev" href="/docs/26.0.0/querying/sql"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">Overview and syntax</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/docs/26.0.0/querying/sql-operators"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">Operators</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#standard-types" class="table-of-contents__link toc-highlight">Standard types</a></li><li><a href="#multi-value-strings" class="table-of-contents__link toc-highlight">Multi-value strings</a></li><li><a href="#arrays" class="table-of-contents__link toc-highlight">Arrays</a></li><li><a href="#multi-value-strings-behavior" class="table-of-contents__link toc-highlight">Multi-value strings behavior</a></li><li><a href="#null-values" class="table-of-contents__link toc-highlight">NULL values</a></li><li><a href="#boolean-logic" class="table-of-contents__link toc-highlight">Boolean logic</a></li><li><a href="#nested-columns" class="table-of-contents__link toc-highlight">Nested columns</a></li></ul></div></div></div></div></main></div></div><footer class="footer"><div class="container container-fluid"><div class="footer__bottom text--center"><div class="margin-bottom--sm"><img src="/img/favicon.png" class="themedImage_ToTc themedImage--light_HNdA footer__logo"><img src="/img/favicon.png" class="themedImage_ToTc themedImage--dark_i4oU footer__logo"></div><div class="footer__copyright">Copyright © 2023 Apache Software Foundation. Except where otherwise noted, licensed under CC BY-SA 4.0. Apache Druid, Druid, and the Druid logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.</div></div></div></footer></div>
<script src="/assets/js/runtime~main.38900cbf.js"></script>
<script src="/assets/js/main.5e106d68.js"></script>
</body>
</html>