blob: 3d148ac40e7cba945ca652b343e8e889e5d71b00 [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-scalar">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v2.4.1">
<title data-rh="true">SQL scalar functions | 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/latest/querying/sql-scalar"><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 scalar functions | 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/latest/querying/sql-scalar"><link data-rh="true" rel="alternate" href="https://druid.apache.org/docs/latest/querying/sql-scalar" hreflang="en"><link data-rh="true" rel="alternate" href="https://druid.apache.org/docs/latest/querying/sql-scalar" 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.dc5f839a.js" as="script">
<link rel="preload" href="/assets/js/main.a03dfc13.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/latest/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/latest/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/latest/tutorials/tutorial-msq-extern">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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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" tabindex="0" href="/docs/latest/querying/query-deep-storage">Query from deep storage</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/latest/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/latest/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 menu__link--active" aria-current="page" tabindex="0" href="/docs/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/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/latest/api-reference/">API reference</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/latest/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/latest/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/latest/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/latest/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">Scalar functions</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 scalar functions</h1></header><div class="theme-admonition theme-admonition-info alert alert--info admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>info</div><div class="admonitionContent_S0QG"><p> Apache Druid supports two query languages: Druid SQL and <a href="/docs/latest/querying/">native queries</a>.
This document describes the SQL language.</p></div></div><p><a href="/docs/latest/querying/sql">Druid SQL</a> includes scalar functions that include numeric and string functions, IP address functions, Sketch functions, and more, as described on this page. </p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="numeric-functions">Numeric functions<a href="#numeric-functions" class="hash-link" aria-label="Direct link to Numeric functions" title="Direct link to Numeric functions"></a></h2><p>For mathematical operations, Druid SQL will use integer math if all operands involved in an expression are integers.
Otherwise, Druid will switch to floating point math. You can force this to happen by casting one of your operands
to FLOAT. At runtime, Druid will widen 32-bit floats to 64-bit for most expressions.</p><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>PI</code></td><td>Constant Pi.</td></tr><tr><td><code>ABS(expr)</code></td><td>Absolute value.</td></tr><tr><td><code>CEIL(expr)</code></td><td>Ceiling.</td></tr><tr><td><code>EXP(expr)</code></td><td>e to the power of <code>expr</code>.</td></tr><tr><td><code>FLOOR(expr)</code></td><td>Floor.</td></tr><tr><td><code>LN(expr)</code></td><td>Logarithm (base e).</td></tr><tr><td><code>LOG10(expr)</code></td><td>Logarithm (base 10).</td></tr><tr><td><code>POWER(expr, power)</code></td><td><code>expr</code> raised to the power of <code>power</code>.</td></tr><tr><td><code>SQRT(expr)</code></td><td>Square root.</td></tr><tr><td><code>TRUNCATE(expr, [digits])</code></td><td>Truncate <code>expr</code> to a specific number of decimal digits. If digits is negative, then this truncates that many places to the left of the decimal point. Digits defaults to zero if not specified.</td></tr><tr><td><code>TRUNC(expr, [digits])</code></td><td>Alias for <code>TRUNCATE</code>.</td></tr><tr><td><code>ROUND(expr, [digits])</code></td><td><code>ROUND(x, y)</code> would return the value of the x rounded to the y decimal places. While x can be an integer or floating-point number, y must be an integer. The type of the return value is specified by that of x. y defaults to 0 if omitted. When y is negative, x is rounded on the left side of the y decimal points. If <code>expr</code> evaluates to either <code>NaN</code>, <code>expr</code> will be converted to 0. If <code>expr</code> is infinity, <code>expr</code> will be converted to the nearest finite double.</td></tr><tr><td><code>MOD(x, y)</code></td><td>Modulo (remainder of x divided by y).</td></tr><tr><td><code>SIN(expr)</code></td><td>Trigonometric sine of an angle <code>expr</code>.</td></tr><tr><td><code>COS(expr)</code></td><td>Trigonometric cosine of an angle <code>expr</code>.</td></tr><tr><td><code>TAN(expr)</code></td><td>Trigonometric tangent of an angle <code>expr</code>.</td></tr><tr><td><code>COT(expr)</code></td><td>Trigonometric cotangent of an angle <code>expr</code>.</td></tr><tr><td><code>ASIN(expr)</code></td><td>Arc sine of <code>expr</code>.</td></tr><tr><td><code>ACOS(expr)</code></td><td>Arc cosine of <code>expr</code>.</td></tr><tr><td><code>ATAN(expr)</code></td><td>Arc tangent of <code>expr</code>.</td></tr><tr><td><code>ATAN2(y, x)</code></td><td>Angle theta from the conversion of rectangular coordinates (x, y) to polar * coordinates (r, theta).</td></tr><tr><td><code>DEGREES(expr)</code></td><td>Converts an angle measured in radians to an approximately equivalent angle measured in degrees.</td></tr><tr><td><code>RADIANS(expr)</code></td><td>Converts an angle measured in degrees to an approximately equivalent angle measured in radians.</td></tr><tr><td><code>BITWISE_AND(expr1, expr2)</code></td><td>Returns the result of <code>expr1 &amp; expr2</code>. Double values will be implicitly cast to longs, use <code>BITWISE_CONVERT_DOUBLE_TO_LONG_BITS</code> to perform bitwise operations directly with doubles.</td></tr><tr><td><code>BITWISE_COMPLEMENT(expr)</code></td><td>Returns the result of <code>~expr</code>. Double values will be implicitly cast to longs, use <code>BITWISE_CONVERT_DOUBLE_TO_LONG_BITS</code> to perform bitwise operations directly with doubles.</td></tr><tr><td><code>BITWISE_CONVERT_DOUBLE_TO_LONG_BITS(expr)</code></td><td>Converts the bits of an IEEE 754 floating-point double value to a long. If the input is not a double, it is implicitly cast to a double prior to conversion.</td></tr><tr><td><code>BITWISE_CONVERT_LONG_BITS_TO_DOUBLE(expr)</code></td><td>Converts a long to the IEEE 754 floating-point double specified by the bits stored in the long. If the input is not a long, it is implicitly cast to a long prior to conversion.</td></tr><tr><td><code>BITWISE_OR(expr1, expr2)</code></td><td>Returns the result of <code>expr1 [PIPE] expr2</code>. Double values will be implicitly cast to longs, use <code>BITWISE_CONVERT_DOUBLE_TO_LONG_BITS</code> to perform bitwise operations directly with doubles.</td></tr><tr><td><code>BITWISE_SHIFT_LEFT(expr1, expr2)</code></td><td>Returns the result of <code>expr1 &lt;&lt; expr2</code>. Double values will be implicitly cast to longs, use <code>BITWISE_CONVERT_DOUBLE_TO_LONG_BITS</code> to perform bitwise operations directly with doubles.</td></tr><tr><td><code>BITWISE_SHIFT_RIGHT(expr1, expr2)</code></td><td>Returns the result of <code>expr1 &gt;&gt; expr2</code>. Double values will be implicitly cast to longs, use <code>BITWISE_CONVERT_DOUBLE_TO_LONG_BITS</code> to perform bitwise operations directly with doubles.</td></tr><tr><td><code>BITWISE_XOR(expr1, expr2)</code></td><td>Returns the result of <code>expr1 ^ expr2</code>. Double values will be implicitly cast to longs, use <code>BITWISE_CONVERT_DOUBLE_TO_LONG_BITS</code> to perform bitwise operations directly with doubles.</td></tr><tr><td><code>DIV(x, y)</code></td><td>Returns the result of integer division of x by y</td></tr><tr><td><code>HUMAN_READABLE_BINARY_BYTE_FORMAT(value, [precision])</code></td><td>Format a number in human-readable <a href="https://en.wikipedia.org/wiki/Binary_prefix" target="_blank" rel="noopener noreferrer">IEC</a> format. For example, HUMAN_READABLE_BINARY_BYTE_FORMAT(1048576) returns <code>1.00 MiB</code>. <code>precision</code> must be in the range of <code>[0, 3]</code> (default: 2).</td></tr><tr><td><code>HUMAN_READABLE_DECIMAL_BYTE_FORMAT(value, [precision])</code></td><td>Format a number in human-readable <a href="https://en.wikipedia.org/wiki/Binary_prefix" target="_blank" rel="noopener noreferrer">SI</a> format. HUMAN_READABLE_DECIMAL_BYTE_FORMAT(1048576) returns <code>1.04 MB</code>. <code>precision</code> must be in the range of <code>[0, 3]</code> (default: 2). <code>precision</code> must be in the range of <code>[0, 3]</code> (default: 2).</td></tr><tr><td><code>HUMAN_READABLE_DECIMAL_FORMAT(value, [precision])</code></td><td>Format a number in human-readable SI format. For example, HUMAN_READABLE_DECIMAL_FORMAT(1048576) returns <code>1.04 M</code>. <code>precision</code> must be in the range of <code>[0, 3]</code> (default: 2).</td></tr><tr><td><code>SAFE_DIVIDE(x, y)</code></td><td>Returns the division of x by y guarded on division by 0. In case y is 0 it returns 0, or <code>null</code> if <code>druid.generic.useDefaultValueForNull=false</code></td></tr></tbody></table><h2 class="anchor anchorWithStickyNavbar_LWe7" id="string-functions">String functions<a href="#string-functions" class="hash-link" aria-label="Direct link to String functions" title="Direct link to String functions"></a></h2><p>String functions accept strings, and return a type appropriate to the function.</p><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>CONCAT(expr, expr...)</code></td><td>Concats a list of expressions. Also see the <a href="/docs/latest/querying/sql-operators#concatenation-operator">concatenation operator</a>.</td></tr><tr><td><code>TEXTCAT(expr, expr)</code></td><td>Two argument version of <code>CONCAT</code>.</td></tr><tr><td><code>STRING_FORMAT(pattern, [args...])</code></td><td>Returns a string formatted in the manner of Java&#x27;s <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-" target="_blank" rel="noopener noreferrer">String.format</a>.</td></tr><tr><td><code>LENGTH(expr)</code></td><td>Length of <code>expr</code> in UTF-16 code units.</td></tr><tr><td><code>CHAR_LENGTH(expr)</code></td><td>Alias for <code>LENGTH</code>.</td></tr><tr><td><code>CHARACTER_LENGTH(expr)</code></td><td>Alias for <code>LENGTH</code>.</td></tr><tr><td><code>STRLEN(expr)</code></td><td>Alias for <code>LENGTH</code>.</td></tr><tr><td><code>LOOKUP(expr, lookupName)</code></td><td>Look up <code>expr</code> in a registered <a href="/docs/latest/querying/lookups">query-time lookup table</a>. Note that lookups can also be queried directly using the <a href="/docs/latest/querying/sql#from"><code>lookup</code> schema</a>.</td></tr><tr><td><code>LOWER(expr)</code></td><td>Returns <code>expr</code> in all lowercase.</td></tr><tr><td><code>UPPER(expr)</code></td><td>Returns <code>expr</code> in all uppercase.</td></tr><tr><td><code>PARSE_LONG(string, [radix])</code></td><td>Parses a string into a long (BIGINT) with the given radix, or 10 (decimal) if a radix is not provided.</td></tr><tr><td><code>POSITION(needle IN haystack [FROM fromIndex])</code></td><td>Returns the index of <code>needle</code> within <code>haystack</code>, with indexes starting from 1. The search will begin at <code>fromIndex</code>, or 1 if <code>fromIndex</code> is not specified. If <code>needle</code> is not found, returns 0.</td></tr><tr><td><code>REGEXP_EXTRACT(expr, pattern, [index])</code></td><td>Apply regular expression <code>pattern</code> to <code>expr</code> and extract a capture group, or <code>NULL</code> if there is no match. If index is unspecified or zero, returns the first substring that matched the pattern. The pattern may match anywhere inside <code>expr</code>; if you want to match the entire string instead, use the <code>^</code> and <code>$</code> markers at the start and end of your pattern. Note: when <code>druid.generic.useDefaultValueForNull = true</code>, it is not possible to differentiate an empty-string match from a non-match (both will return <code>NULL</code>).</td></tr><tr><td><code>REGEXP_LIKE(expr, pattern)</code></td><td>Returns whether <code>expr</code> matches regular expression <code>pattern</code>. The pattern may match anywhere inside <code>expr</code>; if you want to match the entire string instead, use the <code>^</code> and <code>$</code> markers at the start and end of your pattern. Similar to <a href="/docs/latest/querying/sql-operators#logical-operators"><code>LIKE</code></a>, but uses regexps instead of LIKE patterns. Especially useful in WHERE clauses.</td></tr><tr><td><code>REGEXP_REPLACE(expr, pattern, replacement)</code></td><td>Replaces all occurrences of regular expression <code>pattern</code> within <code>expr</code> with <code>replacement</code>. The replacement string may refer to capture groups using <code>$1</code>, <code>$2</code>, etc. The pattern may match anywhere inside <code>expr</code>; if you want to match the entire string instead, use the <code>^</code> and <code>$</code> markers at the start and end of your pattern.</td></tr><tr><td><code>CONTAINS_STRING(expr, str)</code></td><td>Returns true if the <code>str</code> is a substring of <code>expr</code>.</td></tr><tr><td><code>ICONTAINS_STRING(expr, str)</code></td><td>Returns true if the <code>str</code> is a substring of <code>expr</code>. The match is case-insensitive.</td></tr><tr><td><code>REPLACE(expr, pattern, replacement)</code></td><td>Replaces pattern with replacement in <code>expr</code>, and returns the result.</td></tr><tr><td><code>STRPOS(haystack, needle)</code></td><td>Returns the index of <code>needle</code> within <code>haystack</code>, with indexes starting from 1. If <code>needle</code> is not found, returns 0.</td></tr><tr><td><code>SUBSTRING(expr, index, [length])</code></td><td>Returns a substring of <code>expr</code> starting at index, with a max length, both measured in UTF-16 code units.</td></tr><tr><td><code>RIGHT(expr, [length])</code></td><td>Returns the rightmost length characters from <code>expr</code>.</td></tr><tr><td><code>LEFT(expr, [length])</code></td><td>Returns the leftmost length characters from <code>expr</code>.</td></tr><tr><td><code>SUBSTR(expr, index, [length])</code></td><td>Alias for <code>SUBSTRING</code>.</td></tr><tr><td><code>TRIM([BOTH </code><code>|</code><code>LEADING</code><code>|</code><code> TRAILING] [chars FROM] expr)</code></td><td>Returns <code>expr</code> with characters removed from the leading, trailing, or both ends of &quot;expr&quot; if they are in &quot;chars&quot;. If &quot;chars&quot; is not provided, it defaults to &quot; &quot; (a space). If the directional argument is not provided, it defaults to &quot;BOTH&quot;.</td></tr><tr><td><code>BTRIM(expr, [chars])</code></td><td>Alternate form of <code>TRIM(BOTH chars FROM expr)</code>.</td></tr><tr><td><code>LTRIM(expr, [chars])</code></td><td>Alternate form of <code>TRIM(LEADING chars FROM expr)</code>.</td></tr><tr><td><code>RTRIM(expr, [chars])</code></td><td>Alternate form of <code>TRIM(TRAILING chars FROM expr)</code>.</td></tr><tr><td><code>REVERSE(expr)</code></td><td>Reverses <code>expr</code>.</td></tr><tr><td><code>REPEAT(expr, [N])</code></td><td>Repeats <code>expr</code> N times</td></tr><tr><td><code>LPAD(expr, length, [chars])</code></td><td>Returns a string of <code>length</code> from <code>expr</code> left-padded with <code>chars</code>. If <code>length</code> is shorter than the length of <code>expr</code>, the result is <code>expr</code> which is truncated to <code>length</code>. The result will be null if either <code>expr</code> or <code>chars</code> is null. If <code>chars</code> is an empty string, no padding is added, however <code>expr</code> may be trimmed if necessary.</td></tr><tr><td><code>RPAD(expr, length, [chars])</code></td><td>Returns a string of <code>length</code> from <code>expr</code> right-padded with <code>chars</code>. If <code>length</code> is shorter than the length of <code>expr</code>, the result is <code>expr</code> which is truncated to <code>length</code>. The result will be null if either <code>expr</code> or <code>chars</code> is null. If <code>chars</code> is an empty string, no padding is added, however <code>expr</code> may be trimmed if necessary.</td></tr></tbody></table><h2 class="anchor anchorWithStickyNavbar_LWe7" id="date-and-time-functions">Date and time functions<a href="#date-and-time-functions" class="hash-link" aria-label="Direct link to Date and time functions" title="Direct link to Date and time functions"></a></h2><p>Time functions can be used with:</p><ul><li>Druid&#x27;s primary timestamp column, <code>__time</code>;</li><li>Numeric values representing milliseconds since the epoch, through the MILLIS_TO_TIMESTAMP function; and</li><li>String timestamps, through the TIME_PARSE function.</li></ul><p>By default, time operations use the UTC time zone. You can change the time zone by setting the connection
context parameter <code>sqlTimeZone</code> to the name of another time zone, like <code>America/Los_Angeles</code>, or to an offset like
<code>-08:00</code>. If you need to mix multiple time zones in the same query, or if you need to use a time zone other than
the connection time zone, some functions also accept time zones as parameters. These parameters always take precedence
over the connection time zone.</p><p>Literal timestamps in the connection time zone can be written using <code>TIMESTAMP &#x27;2000-01-01 00:00:00&#x27;</code> syntax. The
simplest way to write literal timestamps in other time zones is to use TIME_PARSE, like
<code>TIME_PARSE(&#x27;2000-02-01 00:00:00&#x27;, NULL, &#x27;America/Los_Angeles&#x27;)</code>.</p><p>The best ways to filter based on time are by using ISO8601 intervals, like
<code>TIME_IN_INTERVAL(__time, &#x27;2000-01-01/2000-02-01&#x27;)</code>, or by using literal timestamps with the <code>&gt;=</code> and <code>&lt;</code> operators, like
<code>__time &gt;= TIMESTAMP &#x27;2000-01-01 00:00:00&#x27; AND __time &lt; TIMESTAMP &#x27;2000-02-01 00:00:00&#x27;</code>.</p><p>Druid supports the standard SQL BETWEEN operator, but we recommend avoiding it for time filters. BETWEEN is inclusive
of its upper bound, which makes it awkward to write time filters correctly. For example, the equivalent of
<code>TIME_IN_INTERVAL(__time, &#x27;2000-01-01/2000-02-01&#x27;)</code> is
<code>__time BETWEEN TIMESTAMP &#x27;2000-01-01 00:00:00&#x27; AND TIMESTAMP &#x27;2000-01-31 23:59:59.999&#x27;</code>.</p><p>Druid processes timestamps internally as longs (64-bit integers) representing milliseconds since the epoch. Therefore,
time functions perform best when used with the primary timestamp column, or with timestamps stored in long columns as
milliseconds and accessed with MILLIS_TO_TIMESTAMP. Other timestamp representations, include string timestamps and
POSIX timestamps (seconds since the epoch) require query-time conversion to Druid&#x27;s internal form, which adds additional
overhead.</p><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>CURRENT_TIMESTAMP</code></td><td>Current timestamp in the connection&#x27;s time zone.</td></tr><tr><td><code>CURRENT_DATE</code></td><td>Current date in the connection&#x27;s time zone.</td></tr><tr><td><code>DATE_TRUNC(unit, timestamp_expr)</code></td><td>Rounds down a timestamp, returning it as a new timestamp. Unit can be &#x27;milliseconds&#x27;, &#x27;second&#x27;, &#x27;minute&#x27;, &#x27;hour&#x27;, &#x27;day&#x27;, &#x27;week&#x27;, &#x27;month&#x27;, &#x27;quarter&#x27;, &#x27;year&#x27;, &#x27;decade&#x27;, &#x27;century&#x27;, or &#x27;millennium&#x27;.</td></tr><tr><td><code>TIME_CEIL(timestamp_expr, period, [origin, [timezone]])</code></td><td>Rounds up a timestamp, returning it as a new timestamp. Period can be any ISO8601 period, like P3M (quarters) or PT12H (half-days). Specify <code>origin</code> as a timestamp to set the reference time for rounding. For example, <code>TIME_CEIL(__time, &#x27;PT1H&#x27;, TIMESTAMP &#x27;2016-06-27 00:30:00&#x27;)</code> measures an hourly period from 00:30-01:30 instead of 00:00-01:00. See <a href="/docs/latest/querying/granularities">Period granularities</a> for details on the default starting boundaries. The time zone, if provided, should be a time zone name like &quot;America/Los_Angeles&quot; or offset like &quot;-08:00&quot;. This function is similar to <code>CEIL</code> but is more flexible.</td></tr><tr><td><code>TIME_FLOOR(timestamp_expr, period, [origin, [timezone]])</code></td><td>Rounds down a timestamp, returning it as a new timestamp. Period can be any ISO8601 period, like P3M (quarters) or PT12H (half-days). Specify <code>origin</code> as a timestamp to set the reference time for rounding. For example, <code>TIME_FLOOR(__time, &#x27;PT1H&#x27;, TIMESTAMP &#x27;2016-06-27 00:30:00&#x27;)</code> measures an hourly period from 00:30-01:30 instead of 00:00-01:00. See <a href="/docs/latest/querying/granularities">Period granularities</a> for details on the default starting boundaries. The time zone, if provided, should be a time zone name like &quot;America/Los_Angeles&quot; or offset like &quot;-08:00&quot;. This function is similar to <code>FLOOR</code> but is more flexible.</td></tr><tr><td><code>TIME_SHIFT(timestamp_expr, period, step, [timezone])</code></td><td>Shifts a timestamp by a period (step times), returning it as a new timestamp. Period can be any ISO8601 period. Step may be negative. The time zone, if provided, should be a time zone name like &quot;America/Los_Angeles&quot; or offset like &quot;-08:00&quot;.</td></tr><tr><td><code>TIME_EXTRACT(timestamp_expr, [unit, [timezone]])</code></td><td>Extracts a time part from <code>expr</code>, returning it as a number. Unit can be EPOCH, SECOND, MINUTE, HOUR, DAY (day of month), DOW (day of week), DOY (day of year), WEEK (week of <a href="https://en.wikipedia.org/wiki/ISO_week_date" target="_blank" rel="noopener noreferrer">week year</a>), MONTH (1 through 12), QUARTER (1 through 4), or YEAR. The time zone, if provided, should be a time zone name like &quot;America/Los_Angeles&quot; or offset like &quot;-08:00&quot;. This function is similar to <code>EXTRACT</code> but is more flexible. Unit and time zone must be literals, and must be provided quoted, like <code>TIME_EXTRACT(__time, &#x27;HOUR&#x27;)</code> or <code>TIME_EXTRACT(__time, &#x27;HOUR&#x27;, &#x27;America/Los_Angeles&#x27;)</code>.</td></tr><tr><td><code>TIME_PARSE(string_expr, [pattern, [timezone]])</code></td><td>Parses a string into a timestamp using a given <a href="http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html" target="_blank" rel="noopener noreferrer">Joda DateTimeFormat pattern</a>, or ISO8601 (e.g. <code>2000-01-02T03:04:05Z</code>) if the pattern is not provided. The time zone, if provided, should be a time zone name like &quot;America/Los_Angeles&quot; or offset like &quot;-08:00&quot;, and will be used as the time zone for strings that do not include a time zone offset. Pattern and time zone must be literals. Strings that cannot be parsed as timestamps will be returned as NULL.</td></tr><tr><td><code>TIME_FORMAT(timestamp_expr, [pattern, [timezone]])</code></td><td>Formats a timestamp as a string with a given <a href="http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html" target="_blank" rel="noopener noreferrer">Joda DateTimeFormat pattern</a>, or ISO8601 (e.g. <code>2000-01-02T03:04:05Z</code>) if the pattern is not provided. The time zone, if provided, should be a time zone name like &quot;America/Los_Angeles&quot; or offset like &quot;-08:00&quot;. Pattern and time zone must be literals.</td></tr><tr><td><code>TIME_IN_INTERVAL(timestamp_expr, interval)</code></td><td>Returns whether a timestamp is contained within a particular interval. The interval must be a literal string containing any ISO8601 interval, such as <code>&#x27;2001-01-01/P1D&#x27;</code> or <code>&#x27;2001-01-01T01:00:00/2001-01-02T01:00:00&#x27;</code>. The start instant of the interval is inclusive and the end instant is exclusive.</td></tr><tr><td><code>MILLIS_TO_TIMESTAMP(millis_expr)</code></td><td>Converts a number of milliseconds since the epoch (1970-01-01 00:00:00 UTC) into a timestamp.</td></tr><tr><td><code>TIMESTAMP_TO_MILLIS(timestamp_expr)</code></td><td>Converts a timestamp into a number of milliseconds since the epoch.</td></tr><tr><td><code>EXTRACT(unit FROM timestamp_expr)</code></td><td>Extracts a time part from <code>expr</code>, returning it as a number. Unit can be EPOCH, MICROSECOND, MILLISECOND, SECOND, MINUTE, HOUR, DAY (day of month), DOW (day of week), ISODOW (ISO day of week), DOY (day of year), WEEK (week of year), MONTH, QUARTER, YEAR, ISOYEAR, DECADE, CENTURY or MILLENNIUM. Units must be provided unquoted, like <code>EXTRACT(HOUR FROM __time)</code>.</td></tr><tr><td><code>FLOOR(timestamp_expr TO unit)</code></td><td>Rounds down a timestamp, returning it as a new timestamp. Unit can be SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.</td></tr><tr><td><code>CEIL(timestamp_expr TO unit)</code></td><td>Rounds up a timestamp, returning it as a new timestamp. Unit can be SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.</td></tr><tr><td><code>TIMESTAMPADD(unit, count, timestamp)</code></td><td>Equivalent to <code>timestamp + count * INTERVAL &#x27;1&#x27; UNIT</code>.</td></tr><tr><td><code>TIMESTAMPDIFF(unit, timestamp1, timestamp2)</code></td><td>Returns the (signed) number of <code>unit</code> between <code>timestamp1</code> and <code>timestamp2</code>. Unit can be SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.</td></tr></tbody></table><h2 class="anchor anchorWithStickyNavbar_LWe7" id="reduction-functions">Reduction functions<a href="#reduction-functions" class="hash-link" aria-label="Direct link to Reduction functions" title="Direct link to Reduction functions"></a></h2><p>Reduction functions operate on zero or more expressions and return a single expression. If no expressions are passed as
arguments, then the result is <code>NULL</code>. The expressions must all be convertible to a common data type, which will be the
type of the result:</p><ul><li>If all argument are <code>NULL</code>, the result is <code>NULL</code>. Otherwise, <code>NULL</code> arguments are ignored.</li><li>If the arguments comprise a mix of numbers and strings, the arguments are interpreted as strings.</li><li>If all arguments are integer numbers, the arguments are interpreted as longs.</li><li>If all arguments are numbers and at least one argument is a double, the arguments are interpreted as doubles. </li></ul><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>GREATEST([expr1, ...])</code></td><td>Evaluates zero or more expressions and returns the maximum value based on comparisons as described above.</td></tr><tr><td><code>LEAST([expr1, ...])</code></td><td>Evaluates zero or more expressions and returns the minimum value based on comparisons as described above.</td></tr></tbody></table><h2 class="anchor anchorWithStickyNavbar_LWe7" id="ip-address-functions">IP address functions<a href="#ip-address-functions" class="hash-link" aria-label="Direct link to IP address functions" title="Direct link to IP address functions"></a></h2><p>For the IPv4 address functions, the <code>address</code> argument can either be an IPv4 dotted-decimal string
(e.g., &quot;192.168.0.1&quot;) or an IP address represented as an integer (e.g., 3232235521). The <code>subnet</code>
argument should be a string formatted as an IPv4 address subnet in CIDR notation (e.g., &quot;192.168.0.0/16&quot;).</p><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>IPV4_MATCH(address, subnet)</code></td><td>Returns true if the <code>address</code> belongs to the <code>subnet</code> literal, else false. If <code>address</code> is not a valid IPv4 address, then false is returned. This function is more efficient if <code>address</code> is an integer instead of a string.</td></tr><tr><td><code>IPV4_PARSE(address)</code></td><td>Parses <code>address</code> into an IPv4 address stored as an integer . If <code>address</code> is an integer that is a valid IPv4 address, then it is passed through. Returns null if <code>address</code> cannot be represented as an IPv4 address.</td></tr><tr><td><code>IPV4_STRINGIFY(address)</code></td><td>Converts <code>address</code> into an IPv4 address dotted-decimal string. If <code>address</code> is a string that is a valid IPv4 address, then it is passed through. Returns null if <code>address</code> cannot be represented as an IPv4 address.</td></tr></tbody></table><h2 class="anchor anchorWithStickyNavbar_LWe7" id="sketch-functions">Sketch functions<a href="#sketch-functions" class="hash-link" aria-label="Direct link to Sketch functions" title="Direct link to Sketch functions"></a></h2><p>These functions operate on expressions or columns that return sketch objects.
To create sketch objects, see the <a href="/docs/latest/querying/sql-aggregations#sketch-functions">DataSketches aggregators</a>.</p><h3 class="anchor anchorWithStickyNavbar_LWe7" id="hll-sketch-functions">HLL sketch functions<a href="#hll-sketch-functions" class="hash-link" aria-label="Direct link to HLL sketch functions" title="Direct link to HLL sketch functions"></a></h3><p>The following functions operate on <a href="/docs/latest/development/extensions-core/datasketches-hll">DataSketches HLL sketches</a>.
The <a href="/docs/latest/development/extensions-core/datasketches-extension">DataSketches extension</a> must be loaded to use the following functions.</p><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>HLL_SKETCH_ESTIMATE(expr, [round])</code></td><td>Returns the distinct count estimate from an HLL sketch. <code>expr</code> must return an HLL sketch. The optional <code>round</code> boolean parameter will round the estimate if set to <code>true</code>, with a default of <code>false</code>.</td></tr><tr><td><code>HLL_SKETCH_ESTIMATE_WITH_ERROR_BOUNDS(expr, [numStdDev])</code></td><td>Returns the distinct count estimate and error bounds from an HLL sketch. <code>expr</code> must return an HLL sketch. An optional <code>numStdDev</code> argument can be provided.</td></tr><tr><td><code>HLL_SKETCH_UNION([lgK, tgtHllType], expr0, expr1, ...)</code></td><td>Returns a union of HLL sketches, where each input expression must return an HLL sketch. The <code>lgK</code> and <code>tgtHllType</code> can be optionally specified as the first parameter; if provided, both optional parameters must be specified.</td></tr><tr><td><code>HLL_SKETCH_TO_STRING(expr)</code></td><td>Returns a human-readable string representation of an HLL sketch for debugging. <code>expr</code> must return an HLL sketch.</td></tr></tbody></table><h3 class="anchor anchorWithStickyNavbar_LWe7" id="theta-sketch-functions">Theta sketch functions<a href="#theta-sketch-functions" class="hash-link" aria-label="Direct link to Theta sketch functions" title="Direct link to Theta sketch functions"></a></h3><p>The following functions operate on <a href="/docs/latest/development/extensions-core/datasketches-theta">theta sketches</a>.
The <a href="/docs/latest/development/extensions-core/datasketches-extension">DataSketches extension</a> must be loaded to use the following functions.</p><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>THETA_SKETCH_ESTIMATE(expr)</code></td><td>Returns the distinct count estimate from a theta sketch. <code>expr</code> must return a theta sketch.</td></tr><tr><td><code>THETA_SKETCH_ESTIMATE_WITH_ERROR_BOUNDS(expr, errorBoundsStdDev)</code></td><td>Returns the distinct count estimate and error bounds from a theta sketch. <code>expr</code> must return a theta sketch.</td></tr><tr><td><code>THETA_SKETCH_UNION([size], expr0, expr1, ...)</code></td><td>Returns a union of theta sketches, where each input expression must return a theta sketch. The <code>size</code> can be optionally specified as the first parameter.</td></tr><tr><td><code>THETA_SKETCH_INTERSECT([size], expr0, expr1, ...)</code></td><td>Returns an intersection of theta sketches, where each input expression must return a theta sketch. The <code>size</code> can be optionally specified as the first parameter.</td></tr><tr><td><code>THETA_SKETCH_NOT([size], expr0, expr1, ...)</code></td><td>Returns a set difference of theta sketches, where each input expression must return a theta sketch. The <code>size</code> can be optionally specified as the first parameter.</td></tr></tbody></table><h3 class="anchor anchorWithStickyNavbar_LWe7" id="quantiles-sketch-functions">Quantiles sketch functions<a href="#quantiles-sketch-functions" class="hash-link" aria-label="Direct link to Quantiles sketch functions" title="Direct link to Quantiles sketch functions"></a></h3><p>The following functions operate on <a href="/docs/latest/development/extensions-core/datasketches-quantiles">quantiles sketches</a>.
The <a href="/docs/latest/development/extensions-core/datasketches-extension">DataSketches extension</a> must be loaded to use the following functions.</p><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>DS_GET_QUANTILE(expr, fraction)</code></td><td>Returns the quantile estimate corresponding to <code>fraction</code> from a quantiles sketch. <code>expr</code> must return a quantiles sketch.</td></tr><tr><td><code>DS_GET_QUANTILES(expr, fraction0, fraction1, ...)</code></td><td>Returns a string representing an array of quantile estimates corresponding to a list of fractions from a quantiles sketch. <code>expr</code> must return a quantiles sketch.</td></tr><tr><td><code>DS_HISTOGRAM(expr, splitPoint0, splitPoint1, ...)</code></td><td>Returns a string representing an approximation to the histogram given a list of split points that define the histogram bins from a quantiles sketch. <code>expr</code> must return a quantiles sketch.</td></tr><tr><td><code>DS_CDF(expr, splitPoint0, splitPoint1, ...)</code></td><td>Returns a string representing approximation to the Cumulative Distribution Function given a list of split points that define the edges of the bins from a quantiles sketch. <code>expr</code> must return a quantiles sketch.</td></tr><tr><td><code>DS_RANK(expr, value)</code></td><td>Returns an approximation to the rank of a given value that is the fraction of the distribution less than that value from a quantiles sketch. <code>expr</code> must return a quantiles sketch.</td></tr><tr><td><code>DS_QUANTILE_SUMMARY(expr)</code></td><td>Returns a string summary of a quantiles sketch, useful for debugging. <code>expr</code> must return a quantiles sketch.</td></tr></tbody></table><h3 class="anchor anchorWithStickyNavbar_LWe7" id="tuple-sketch-functions">Tuple sketch functions<a href="#tuple-sketch-functions" class="hash-link" aria-label="Direct link to Tuple sketch functions" title="Direct link to Tuple sketch functions"></a></h3><p>The following functions operate on <a href="/docs/latest/development/extensions-core/datasketches-tuple">tuple sketches</a>.
The <a href="/docs/latest/development/extensions-core/datasketches-extension">DataSketches extension</a> must be loaded to use the following functions.</p><table><thead><tr><th>Function</th><th>Notes</th><th>Default</th></tr></thead><tbody><tr><td><code>DS_TUPLE_DOUBLES_METRICS_SUM_ESTIMATE(expr)</code></td><td>Computes approximate sums of the values contained within a <a href="/docs/latest/development/extensions-core/datasketches-tuple#estimated-metrics-values-for-each-column-of-arrayofdoublessketch">Tuple sketch</a> column which contains an array of double values as its Summary Object.</td><td></td></tr><tr><td><code>DS_TUPLE_DOUBLES_INTERSECT(expr, ..., [nominalEntries])</code></td><td>Returns an intersection of tuple sketches, where each input expression must return a tuple sketch which contains an array of double values as its Summary Object. The values contained in the Summary Objects are summed when combined. If the last value of the array is a numeric literal, Druid assumes that the value is an override parameter for <a href="/docs/latest/development/extensions-core/datasketches-tuple">nominal entries</a>.</td><td></td></tr><tr><td><code>DS_TUPLE_DOUBLES_NOT(expr, ..., [nominalEntries])</code></td><td>Returns a set difference of tuple sketches, where each input expression must return a tuple sketch which contains an array of double values as its Summary Object. The values contained in the Summary Object are preserved as is. If the last value of the array is a numeric literal, Druid assumes that the value is an override parameter for <a href="/docs/latest/development/extensions-core/datasketches-tuple">nominal entries</a>.</td><td></td></tr><tr><td><code>DS_TUPLE_DOUBLES_UNION(expr, ..., [nominalEntries])</code></td><td>Returns a union of tuple sketches, where each input expression must return a tuple sketch which contains an array of double values as its Summary Object. The values contained in the Summary Objects are summed when combined. If the last value of the array is a numeric literal, Druid assumes that the value is an override parameter for <a href="/docs/latest/development/extensions-core/datasketches-tuple">nominal entries</a>.</td><td></td></tr></tbody></table><h2 class="anchor anchorWithStickyNavbar_LWe7" id="other-scalar-functions">Other scalar functions<a href="#other-scalar-functions" class="hash-link" aria-label="Direct link to Other scalar functions" title="Direct link to Other scalar functions"></a></h2><table><thead><tr><th>Function</th><th>Notes</th></tr></thead><tbody><tr><td><code>CAST(value AS TYPE)</code></td><td>Cast value to another type. See <a href="/docs/latest/querying/sql-data-types">Data types</a> for details about how Druid SQL handles CAST.</td></tr><tr><td><code>CASE expr WHEN value1 THEN result1 \[ WHEN value2 THEN result2 ... \] \[ ELSE resultN \] END</code></td><td>Simple CASE.</td></tr><tr><td><code>CASE WHEN boolean_expr1 THEN result1 \[ WHEN boolean_expr2 THEN result2 ... \] \[ ELSE resultN \] END</code></td><td>Searched CASE.</td></tr><tr><td><code>NULLIF(value1, value2)</code></td><td>Returns NULL if value1 and value2 match, else returns value1.</td></tr><tr><td><code>COALESCE(value1, value2, ...)</code></td><td>Returns the first value that is neither NULL nor empty string.</td></tr><tr><td><code>NVL(value1, value2)</code></td><td>Returns <code>value1</code> if <code>value1</code> is not null, otherwise <code>value2</code>.</td></tr><tr><td><code>BLOOM_FILTER_TEST(expr, serialized-filter)</code></td><td>Returns true if the value of <code>expr</code> is contained in the Base64-serialized Bloom filter. See the <a href="/docs/latest/development/extensions-core/bloom-filter">Bloom filter extension</a> documentation for additional details. See the <a href="/docs/latest/querying/sql-aggregations"><code>BLOOM_FILTER</code> function</a> for computing Bloom filters.</td></tr></tbody></table></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages"><a class="pagination-nav__link pagination-nav__link--prev" href="/docs/latest/querying/sql-operators"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">Operators</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/docs/latest/querying/sql-aggregations"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">Aggregation functions</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="#numeric-functions" class="table-of-contents__link toc-highlight">Numeric functions</a></li><li><a href="#string-functions" class="table-of-contents__link toc-highlight">String functions</a></li><li><a href="#date-and-time-functions" class="table-of-contents__link toc-highlight">Date and time functions</a></li><li><a href="#reduction-functions" class="table-of-contents__link toc-highlight">Reduction functions</a></li><li><a href="#ip-address-functions" class="table-of-contents__link toc-highlight">IP address functions</a></li><li><a href="#sketch-functions" class="table-of-contents__link toc-highlight">Sketch functions</a><ul><li><a href="#hll-sketch-functions" class="table-of-contents__link toc-highlight">HLL sketch functions</a></li><li><a href="#theta-sketch-functions" class="table-of-contents__link toc-highlight">Theta sketch functions</a></li><li><a href="#quantiles-sketch-functions" class="table-of-contents__link toc-highlight">Quantiles sketch functions</a></li><li><a href="#tuple-sketch-functions" class="table-of-contents__link toc-highlight">Tuple sketch functions</a></li></ul></li><li><a href="#other-scalar-functions" class="table-of-contents__link toc-highlight">Other scalar functions</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.dc5f839a.js"></script>
<script src="/assets/js/main.a03dfc13.js"></script>
</body>
</html>