blob: 8611f37b8de5d71a223e740c0748f7eb99ebdb42 [file] [log] [blame]
"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[4139],{3905:(e,t,n)=>{n.d(t,{Zo:()=>u,kt:()=>c});var a=n(67294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function l(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},o=Object.keys(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var p=a.createContext({}),s=function(e){var t=a.useContext(p),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},u=function(e){var t=s(e.components);return a.createElement(p.Provider,{value:t},e.children)},d="mdxType",k={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},m=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,o=e.originalType,p=e.parentName,u=l(e,["components","mdxType","originalType","parentName"]),d=s(n),m=r,c=d["".concat(p,".").concat(m)]||d[m]||k[m]||o;return n?a.createElement(c,i(i({ref:t},u),{},{components:n})):a.createElement(c,i({ref:t},u))}));function c(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var o=n.length,i=new Array(o);i[0]=m;var l={};for(var p in t)hasOwnProperty.call(t,p)&&(l[p]=t[p]);l.originalType=e,l[d]="string"==typeof e?e:r,i[1]=l;for(var s=2;s<o;s++)i[s]=n[s];return a.createElement.apply(null,i)}return a.createElement.apply(null,n)}m.displayName="MDXCreateElement"},3692:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>u,contentTitle:()=>p,default:()=>c,frontMatter:()=>l,metadata:()=>s,toc:()=>d});var a=n(87462),r=n(63366),o=(n(67294),n(3905)),i=["components"],l={id:"lookups",title:"Lookups"},p=void 0,s={unversionedId:"querying/lookups",id:"querying/lookups",title:"Lookups",description:"\x3c!--",source:"@site/docs/latest/querying/lookups.md",sourceDirName:"querying",slug:"/querying/lookups",permalink:"/docs/latest/querying/lookups",draft:!1,tags:[],version:"current",frontMatter:{id:"lookups",title:"Lookups"},sidebar:"docs",previous:{title:"Joins",permalink:"/docs/latest/querying/joins"},next:{title:"Multi-value dimensions",permalink:"/docs/latest/querying/multi-value-dimensions"}},u={},d=[{value:"Query Syntax",id:"query-syntax",level:2},{value:"Query Rewrites",id:"query-rewrites",level:2},{value:"Reverse lookup",id:"reverse-lookup",level:3},{value:"Pull up",id:"pull-up",level:3},{value:"Injective lookups",id:"injective-lookups",level:3},{value:"Dynamic Configuration",id:"dynamic-configuration",level:2},{value:"Configuration propagation behavior",id:"configuration-propagation-behavior",level:2},{value:"API",id:"api",level:2},{value:"Configuration",id:"configuration",level:2},{value:"Saving configuration across restarts",id:"saving-configuration-across-restarts",level:2},{value:"Introspect a Lookup",id:"introspect-a-lookup",level:2}],k={toc:d},m="wrapper";function c(e){var t=e.components,n=(0,r.Z)(e,i);return(0,o.kt)(m,(0,a.Z)({},k,n,{components:t,mdxType:"MDXLayout"}),(0,o.kt)("p",null,"Lookups are a concept in Apache Druid where dimension values are (optionally) replaced with new values, allowing join-like\nfunctionality. Applying lookups in Druid is similar to joining a dimension table in a data warehouse. See\n",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/dimensionspecs"},"dimension specs"),' for more information. For the purpose of these documents, a "key"\nrefers to a dimension value to match, and a "value" refers to its replacement. So if you wanted to map\n',(0,o.kt)("inlineCode",{parentName:"p"},"appid-12345")," to ",(0,o.kt)("inlineCode",{parentName:"p"},"Super Mega Awesome App")," then the key would be ",(0,o.kt)("inlineCode",{parentName:"p"},"appid-12345")," and the value would be\n",(0,o.kt)("inlineCode",{parentName:"p"},"Super Mega Awesome App"),"."),(0,o.kt)("p",null,"It is worth noting that lookups support not just use cases where keys map one-to-one to unique values, such as country\ncode and country name, but also support use cases where multiple IDs map to the same value, e.g. multiple app-ids\nmapping to a single account manager. When lookups are one-to-one, Druid is able to apply additional\n",(0,o.kt)("a",{parentName:"p",href:"#query-rewrites"},"query rewrites"),"; see below for more details."),(0,o.kt)("p",null,"Lookups do not have history. They always use the current data. This means that if the chief account manager for a\nparticular app-id changes, and you issue a query with a lookup to store the app-id to account manager relationship,\nit will return the current account manager for that app-id REGARDLESS of the time range over which you query."),(0,o.kt)("p",null,"If you require data time range sensitive lookups, such a use case is not currently supported dynamically at query time,\nand such data belongs in the raw denormalized data for use in Druid."),(0,o.kt)("p",null,'Lookups are generally preloaded in-memory on all servers. But very small lookups (on the order of a few dozen to a few\nhundred entries) can also be passed inline in native queries time using the "map" lookup type. Refer to the\n',(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/dimensionspecs"},"dimension specs")," documentation for details."),(0,o.kt)("p",null,"Other lookup types are available as extensions, including:"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},"Globally cached lookups from local files, remote URIs, or JDBC through ",(0,o.kt)("a",{parentName:"li",href:"/docs/latest/development/extensions-core/lookups-cached-global"},"lookups-cached-global"),"."),(0,o.kt)("li",{parentName:"ul"},"Globally cached lookups from a Kafka topic through ",(0,o.kt)("a",{parentName:"li",href:"/docs/latest/development/extensions-core/kafka-extraction-namespace"},"kafka-extraction-namespace"),".")),(0,o.kt)("h2",{id:"query-syntax"},"Query Syntax"),(0,o.kt)("p",null,"In ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql"},"Druid SQL"),", lookups can be queried using the ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-scalar#string-functions"},(0,o.kt)("inlineCode",{parentName:"a"},"LOOKUP")," function"),", for example:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n LOOKUP(store, 'store_to_country') AS country,\n SUM(revenue)\nFROM sales\nGROUP BY 1\n")),(0,o.kt)("p",null,"The ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP")," function also accepts a third argument called ",(0,o.kt)("inlineCode",{parentName:"p"},"replaceMissingValueWith")," as a constant string. If the lookup\ndoes not contain a value for the provided key, then the ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP")," function returns this ",(0,o.kt)("inlineCode",{parentName:"p"},"replaceMissingValueWith")," value\nrather than ",(0,o.kt)("inlineCode",{parentName:"p"},"NULL"),", just like ",(0,o.kt)("inlineCode",{parentName:"p"},"COALESCE"),". For example, ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP(store, 'store_to_country', 'NA')")," is equivalent to\n",(0,o.kt)("inlineCode",{parentName:"p"},"COALESCE(LOOKUP(store, 'store_to_country'), 'NA')"),"."),(0,o.kt)("p",null,"Lookups can be queried using the ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/datasource#join"},"JOIN operator"),":"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n store_to_country.v AS country,\n SUM(sales.revenue) AS country_revenue\nFROM\n sales\n INNER JOIN lookup.store_to_country ON sales.store = store_to_country.k\nGROUP BY 1\n")),(0,o.kt)("admonition",{type:"info"},(0,o.kt)("p",{parentName:"admonition"},"The ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP")," function has automatic ",(0,o.kt)("a",{parentName:"p",href:"#query-rewrites"},"query rewrites")," available that the ",(0,o.kt)("inlineCode",{parentName:"p"},"JOIN")," approach does not,\nincluding ",(0,o.kt)("a",{parentName:"p",href:"#reverse-lookup"},"reverse lookups")," and ",(0,o.kt)("a",{parentName:"p",href:"#pull-up"},"pulling up through ",(0,o.kt)("inlineCode",{parentName:"a"},"GROUP BY")),". If these rewrites are\nimportant for you, consider using the ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP")," function instead of ",(0,o.kt)("inlineCode",{parentName:"p"},"JOIN"),".")),(0,o.kt)("p",null,"In native queries, lookups can be queried with ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/dimensionspecs"},"dimension specs or extraction functions"),"."),(0,o.kt)("h2",{id:"query-rewrites"},"Query Rewrites"),(0,o.kt)("p",null,"Druid can perform two automatic query rewrites when using the ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP")," function: ",(0,o.kt)("a",{parentName:"p",href:"#reverse-lookup"},"reverse lookups")," and\n",(0,o.kt)("a",{parentName:"p",href:"#pull-up"},"pulling up through ",(0,o.kt)("inlineCode",{parentName:"a"},"GROUP BY")),". These rewrites and their requirements are described in the following\nsections."),(0,o.kt)("h3",{id:"reverse-lookup"},"Reverse lookup"),(0,o.kt)("p",null,"When ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP")," function calls appear in the ",(0,o.kt)("inlineCode",{parentName:"p"},"WHERE")," clause of a query, Druid reverses them ",(0,o.kt)("a",{parentName:"p",href:"#table"},"when possible"),".\nFor example, if the lookup table ",(0,o.kt)("inlineCode",{parentName:"p"},"sku_to_name")," contains the mapping ",(0,o.kt)("inlineCode",{parentName:"p"},"'WB00013' => 'WhizBang Sprocket'"),", then Druid\nautomatically rewrites this query:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n LOOKUP(sku, 'sku_to_name') AS name,\n SUM(revenue)\nFROM sales\nWHERE LOOKUP(sku, 'sku_to_name') = 'WhizBang Sprocket'\nGROUP BY LOOKUP(sku, 'sku_to_name')\n")),(0,o.kt)("p",null,"Into this:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n LOOKUP(sku, 'sku_to_name') AS name,\n SUM(revenue)\nFROM sales\nWHERE sku = 'WB00013'\nGROUP BY LOOKUP(sku, 'sku_to_name')\n")),(0,o.kt)("p",null,"The difference is that in the latter case, data servers do not need to apply the ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP")," function while filtering, and\ncan make more efficient use of indexes for ",(0,o.kt)("inlineCode",{parentName:"p"},"sku"),"."),(0,o.kt)("a",{name:"table"},"The following table")," contains examples of when it is possible to reverse calls to `LOOKUP` while in Druid's default null handling mode. The list of examples is illustrative, albeit not exhaustive.",(0,o.kt)("table",null,(0,o.kt)("thead",{parentName:"table"},(0,o.kt)("tr",{parentName:"thead"},(0,o.kt)("th",{parentName:"tr",align:null},"SQL"),(0,o.kt)("th",{parentName:"tr",align:null},"Reversible?"))),(0,o.kt)("tbody",{parentName:"table"},(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') = 'WhizBang Sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') IS NOT DISTINCT FROM 'WhizBang Sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes, for non-null literals")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') <> 'WhizBang Sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"No, unless ",(0,o.kt)("inlineCode",{parentName:"td"},"sku_to_name")," is ",(0,o.kt)("a",{parentName:"td",href:"#injective-lookups"},"injective"))),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') IS DISTINCT FROM 'WhizBang Sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes, for non-null literals")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') = 'WhizBang Sprocket' IS NOT TRUE")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') IN ('WhizBang Sprocket', 'WhizBang Chain')")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') NOT IN ('WhizBang Sprocket', 'WhizBang Chain')")),(0,o.kt)("td",{parentName:"tr",align:null},"No, unless ",(0,o.kt)("inlineCode",{parentName:"td"},"sku_to_name")," is ",(0,o.kt)("a",{parentName:"td",href:"#injective-lookups"},"injective"))),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') IN ('WhizBang Sprocket', 'WhizBang Chain') IS NOT TRUE")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') IS NULL")),(0,o.kt)("td",{parentName:"tr",align:null},"No")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(sku, 'sku_to_name') IS NOT NULL")),(0,o.kt)("td",{parentName:"tr",align:null},"No")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOOKUP(UPPER(sku), 'sku_to_name') = 'WhizBang Sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes, to ",(0,o.kt)("inlineCode",{parentName:"td"},"UPPER(sku) = [key for 'WhizBang Sprocket']")," (the ",(0,o.kt)("inlineCode",{parentName:"td"},"UPPER")," function remains)")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE(LOOKUP(sku, 'sku_to_name'), 'N/A') = 'WhizBang Sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes, but see next item for ",(0,o.kt)("inlineCode",{parentName:"td"},"= 'N/A'"))),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE(LOOKUP(sku, 'sku_to_name'), 'N/A') = 'N/A'")),(0,o.kt)("td",{parentName:"tr",align:null},"No, unless ",(0,o.kt)("inlineCode",{parentName:"td"},"sku_to_name")," is ",(0,o.kt)("a",{parentName:"td",href:"#injective-lookups"},"injective"),", which allows Druid to ignore the ",(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE"))),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE(LOOKUP(sku, 'sku_to_name'), 'N/A') = 'WhizBang Sprocket' IS NOT TRUE")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE(LOOKUP(sku, 'sku_to_name'), 'N/A') <> 'WhizBang Sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes, but see next item for ",(0,o.kt)("inlineCode",{parentName:"td"},"<> 'N/A'"))),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE(LOOKUP(sku, 'sku_to_name'), 'N/A') <> 'N/A'")),(0,o.kt)("td",{parentName:"tr",align:null},"No, unless ",(0,o.kt)("inlineCode",{parentName:"td"},"sku_to_name")," is ",(0,o.kt)("a",{parentName:"td",href:"#injective-lookups"},"injective"),", which allows Druid to ignore the ",(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE"))),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE(LOOKUP(sku, 'sku_to_name'), sku) = 'WhizBang Sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"No, ",(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE")," is only reversible when the second argument is a constant")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"LOWER(LOOKUP(sku, 'sku_to_name')) = 'whizbang sprocket'")),(0,o.kt)("td",{parentName:"tr",align:null},"No, functions other than ",(0,o.kt)("inlineCode",{parentName:"td"},"COALESCE")," are not reversible")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"MV_CONTAINS(LOOKUP(sku, 'sku_to_name'), 'WhizBang Sprocket')")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"NOT MV_CONTAINS(LOOKUP(sku, 'sku_to_name'), 'WhizBang Sprocket')")),(0,o.kt)("td",{parentName:"tr",align:null},"No, unless ",(0,o.kt)("inlineCode",{parentName:"td"},"sku_to_name")," is ",(0,o.kt)("a",{parentName:"td",href:"#injective-lookups"},"injective"))),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"MV_OVERLAP(LOOKUP(sku, 'sku_to_name'), ARRAY['WhizBang Sprocket'])")),(0,o.kt)("td",{parentName:"tr",align:null},"Yes")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"NOT MV_OVERLAP(LOOKUP(sku, 'sku_to_name'), ARRAY['WhizBang Sprocket'])")),(0,o.kt)("td",{parentName:"tr",align:null},"No, unless ",(0,o.kt)("inlineCode",{parentName:"td"},"sku_to_name")," is ",(0,o.kt)("a",{parentName:"td",href:"#injective-lookups"},"injective"))))),(0,o.kt)("p",null,"You can see the difference in the native query that is generated during SQL planning, which you\ncan retrieve with ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql#explain-plan"},(0,o.kt)("inlineCode",{parentName:"a"},"EXPLAIN PLAN FOR")),". When a lookup is reversed in this way, the ",(0,o.kt)("inlineCode",{parentName:"p"},"lookup"),"\nfunction disappears and is replaced by a simpler filter, typically of type ",(0,o.kt)("inlineCode",{parentName:"p"},"equals")," or ",(0,o.kt)("inlineCode",{parentName:"p"},"in"),"."),(0,o.kt)("p",null,"Lookups are not reversed if the number of matching keys exceeds the ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-query-context"},(0,o.kt)("inlineCode",{parentName:"a"},"sqlReverseLookupThreshold")),"\nor ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-query-context"},(0,o.kt)("inlineCode",{parentName:"a"},"inSubQueryThreshold"))," for the query."),(0,o.kt)("p",null,"This rewrite adds some planning time that may become noticeable for larger lookups, especially if many keys map to the\nsame value. You can see the impact on planning time in the ",(0,o.kt)("inlineCode",{parentName:"p"},"sqlQuery/planningTimeMs")," metric. You can also measure the\ntime taken by ",(0,o.kt)("inlineCode",{parentName:"p"},"EXPLAIN PLAN FOR"),", which plans the query but does not execute it."),(0,o.kt)("p",null,"This rewrite can be disabled by setting ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-query-context"},(0,o.kt)("inlineCode",{parentName:"a"},"sqlReverseLookup: false"))," in your query context."),(0,o.kt)("h3",{id:"pull-up"},"Pull up"),(0,o.kt)("p",null,"Lookups marked as ",(0,o.kt)("a",{parentName:"p",href:"#injective-lookups"},(0,o.kt)("em",{parentName:"a"},"injective"))," can be pulled up through a ",(0,o.kt)("inlineCode",{parentName:"p"},"GROUP BY"),". For example, if the lookup\n",(0,o.kt)("inlineCode",{parentName:"p"},"sku_to_name")," is injective, Druid automatically rewrites this query:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n LOOKUP(sku, 'sku_to_name') AS name,\n SUM(revenue)\nFROM sales\nGROUP BY LOOKUP(sku, 'sku_to_name')\n")),(0,o.kt)("p",null,"Into this:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n LOOKUP(sku, 'sku_to_name') AS name,\n SUM(revenue)\nFROM sales\nGROUP BY sku\n")),(0,o.kt)("p",null,"The difference is that the ",(0,o.kt)("inlineCode",{parentName:"p"},"LOOKUP")," function is not applied until after the ",(0,o.kt)("inlineCode",{parentName:"p"},"GROUP BY")," is finished, which speeds up\nthe ",(0,o.kt)("inlineCode",{parentName:"p"},"GROUP BY"),"."),(0,o.kt)("p",null,"You can see the difference in the native query that is generated during SQL planning, which you\ncan retrieve with ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql#explain-plan"},(0,o.kt)("inlineCode",{parentName:"a"},"EXPLAIN PLAN FOR")),". When a lookup is pulled up in this way, the ",(0,o.kt)("inlineCode",{parentName:"p"},"lookup"),"\nfunction call typically moves from the ",(0,o.kt)("inlineCode",{parentName:"p"},"virtualColumns")," or ",(0,o.kt)("inlineCode",{parentName:"p"},"dimensions")," section of a native query into the\n",(0,o.kt)("inlineCode",{parentName:"p"},"postAggregations"),"."),(0,o.kt)("p",null,"This rewrite can be disabled by setting ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-query-context"},(0,o.kt)("inlineCode",{parentName:"a"},"sqlPullUpLookup: false"))," in your query context."),(0,o.kt)("h3",{id:"injective-lookups"},"Injective lookups"),(0,o.kt)("p",null,'Injective lookups are eligible for the largest set of query rewrites. Injective lookups must satisfy the following\n"one-to-one lookup" properties:'),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},"All values in the lookup table must be unique. That is, no two keys can map to the same value."),(0,o.kt)("li",{parentName:"ul"},"The lookup table must have a key-value pair defined for every input that the ",(0,o.kt)("inlineCode",{parentName:"li"},"LOOKUP")," function call may\nencounter. For example, when calling ",(0,o.kt)("inlineCode",{parentName:"li"},"LOOKUP(sku, 'sku_to_name')"),", the ",(0,o.kt)("inlineCode",{parentName:"li"},"sku_to_name")," lookup table must have a key\nfor all possible ",(0,o.kt)("inlineCode",{parentName:"li"},"sku"),"."),(0,o.kt)("li",{parentName:"ul"},"In SQL-compatible null handling mode (when ",(0,o.kt)("inlineCode",{parentName:"li"},"druid.generic.useDefaultValueForNull = false"),", the default) injective\nlookup tables are not required to have keys for ",(0,o.kt)("inlineCode",{parentName:"li"},"null"),", since ",(0,o.kt)("inlineCode",{parentName:"li"},"LOOKUP")," of ",(0,o.kt)("inlineCode",{parentName:"li"},"null")," is always ",(0,o.kt)("inlineCode",{parentName:"li"},"null")," itself."),(0,o.kt)("li",{parentName:"ul"},"When ",(0,o.kt)("inlineCode",{parentName:"li"},"druid.generic.useDefaultValueForNull = true"),", a ",(0,o.kt)("inlineCode",{parentName:"li"},"LOOKUP")," of ",(0,o.kt)("inlineCode",{parentName:"li"},"null")," retrieves the value mapped to the\nempty-string key (",(0,o.kt)("inlineCode",{parentName:"li"},'""'),"). In this mode, injective lookup tables must have an empty-string key if the ",(0,o.kt)("inlineCode",{parentName:"li"},"LOOKUP"),"\nfunction may encounter null input values.")),(0,o.kt)("p",null,"To determine whether a lookup is injective, Druid relies on an ",(0,o.kt)("inlineCode",{parentName:"p"},"injective")," property that you can set in the\n",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/development/extensions-core/lookups-cached-global"},"lookup definition"),". In general, you should set\n",(0,o.kt)("inlineCode",{parentName:"p"},"injective: true")," for any lookup that satisfies the required properties, to allow Druid to run your queries as fast as\npossible."),(0,o.kt)("p",null,"Druid does not verify whether lookups satisfy these required properties. Druid may return incorrect query results\nif you set ",(0,o.kt)("inlineCode",{parentName:"p"},"injective: true")," for a lookup table that is not actually a one-to-one lookup."),(0,o.kt)("h2",{id:"dynamic-configuration"},"Dynamic Configuration"),(0,o.kt)("p",null,'The following documents the behavior of the cluster-wide config which is accessible through the Coordinator.\nThe configuration is propagated through the concept of "tier" of servers.\nA "tier" is defined as a group of services which should receive a set of lookups.\nFor example, you might have all Historicals be part of ',(0,o.kt)("inlineCode",{parentName:"p"},"__default"),", and Peons be part of individual tiers for the datasources they are tasked with.\nThe tiers for lookups are completely independent of Historical tiers."),(0,o.kt)("p",null,"These configs are accessed using JSON through the following URI template"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre"},"http://<COORDINATOR_IP>:<PORT>/druid/coordinator/v1/lookups/config/{tier}/{id}\n")),(0,o.kt)("p",null,"All URIs below are assumed to have ",(0,o.kt)("inlineCode",{parentName:"p"},"http://<COORDINATOR_IP>:<PORT>")," prepended."),(0,o.kt)("p",null,"If you have NEVER configured lookups before, you MUST post an empty json object ",(0,o.kt)("inlineCode",{parentName:"p"},"{}")," to ",(0,o.kt)("inlineCode",{parentName:"p"},"/druid/coordinator/v1/lookups/config")," to initialize the configuration."),(0,o.kt)("p",null,"These endpoints will return one of the following results:"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},"404 if the resource is not found"),(0,o.kt)("li",{parentName:"ul"},"400 if there is a problem in the formatting of the request"),(0,o.kt)("li",{parentName:"ul"},"202 if the request was accepted asynchronously (",(0,o.kt)("inlineCode",{parentName:"li"},"POST")," and ",(0,o.kt)("inlineCode",{parentName:"li"},"DELETE"),")"),(0,o.kt)("li",{parentName:"ul"},"200 if the request succeeded (",(0,o.kt)("inlineCode",{parentName:"li"},"GET")," only)")),(0,o.kt)("h2",{id:"configuration-propagation-behavior"},"Configuration propagation behavior"),(0,o.kt)("p",null,"The configuration is propagated to the query serving processes (Broker / Router / Peon / Historical) by the Coordinator.\nThe query serving processes have an internal API for managing lookups on the process and those are used by the Coordinator.\nThe Coordinator periodically checks if any of the processes need to load/drop lookups and updates them appropriately."),(0,o.kt)("p",null,"Please note that only 2 simultaneous lookup configuration propagation requests can be concurrently handled by a single query serving process. This limit is applied to prevent lookup handling from consuming too many server HTTP connections."),(0,o.kt)("h2",{id:"api"},"API"),(0,o.kt)("p",null,"See ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/api-reference/lookups-api"},"Lookups API")," for reference on configuring lookups and lookup status. "),(0,o.kt)("h2",{id:"configuration"},"Configuration"),(0,o.kt)("p",null,"See ",(0,o.kt)("a",{parentName:"p",href:"/docs/latest/configuration/#lookups-dynamic-configuration"},"Lookups Dynamic Configuration")," for Coordinator configuration."),(0,o.kt)("p",null,"To configure a Broker / Router / Historical / Peon to announce itself as part of a lookup tier, use following properties."),(0,o.kt)("table",null,(0,o.kt)("thead",{parentName:"table"},(0,o.kt)("tr",{parentName:"thead"},(0,o.kt)("th",{parentName:"tr",align:null},"Property"),(0,o.kt)("th",{parentName:"tr",align:null},"Description"),(0,o.kt)("th",{parentName:"tr",align:null},"Default"))),(0,o.kt)("tbody",{parentName:"table"},(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.lookupTier")),(0,o.kt)("td",{parentName:"tr",align:null},"The tier for ",(0,o.kt)("strong",{parentName:"td"},"lookups")," for this process. This is independent of other tiers."),(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"__default"))),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.lookupTierIsDatasource")),(0,o.kt)("td",{parentName:"tr",align:null},"For some things like indexing service tasks, the datasource is passed in the runtime properties of a task. This option fetches the tierName from the same value as the datasource for the task. It is suggested to only use this as Peon options for the indexing service, if at all. If true, ",(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.lookupTier")," MUST NOT be specified"),(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},'"false"'))))),(0,o.kt)("p",null,"To configure the behavior of the dynamic configuration manager, use the following properties on the Coordinator:"),(0,o.kt)("table",null,(0,o.kt)("thead",{parentName:"table"},(0,o.kt)("tr",{parentName:"thead"},(0,o.kt)("th",{parentName:"tr",align:null},"Property"),(0,o.kt)("th",{parentName:"tr",align:null},"Description"),(0,o.kt)("th",{parentName:"tr",align:null},"Default"))),(0,o.kt)("tbody",{parentName:"table"},(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.manager.lookups.hostTimeout")),(0,o.kt)("td",{parentName:"tr",align:null},"Timeout (in ms) PER HOST for processing request"),(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"2000"),"(2 seconds)")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.manager.lookups.allHostTimeout")),(0,o.kt)("td",{parentName:"tr",align:null},"Timeout (in ms) to finish lookup management on all the processes."),(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"900000"),"(15 mins)")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.manager.lookups.period")),(0,o.kt)("td",{parentName:"tr",align:null},"How long to pause between management cycles"),(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"120000"),"(2 mins)")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.manager.lookups.threadPoolSize")),(0,o.kt)("td",{parentName:"tr",align:null},"Number of service processes that can be managed concurrently"),(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"10"))))),(0,o.kt)("h2",{id:"saving-configuration-across-restarts"},"Saving configuration across restarts"),(0,o.kt)("p",null,"It is possible to save the configuration across restarts such that a process will not have to wait for Coordinator action to re-populate its lookups. To do this the following property is set:"),(0,o.kt)("table",null,(0,o.kt)("thead",{parentName:"table"},(0,o.kt)("tr",{parentName:"thead"},(0,o.kt)("th",{parentName:"tr",align:null},"Property"),(0,o.kt)("th",{parentName:"tr",align:null},"Description"),(0,o.kt)("th",{parentName:"tr",align:null},"Default"))),(0,o.kt)("tbody",{parentName:"table"},(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.snapshotWorkingDir")),(0,o.kt)("td",{parentName:"tr",align:null},"Working path used to store snapshot of current lookup configuration, leaving this property null will disable snapshot/bootstrap utility"),(0,o.kt)("td",{parentName:"tr",align:null},"null")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.enableLookupSyncOnStartup")),(0,o.kt)("td",{parentName:"tr",align:null},"Enable the lookup synchronization process with Coordinator on startup. The queryable processes will fetch and load the lookups from the Coordinator instead of waiting for the Coordinator to load the lookups for them. Users may opt to disable this option if there are no lookups configured in the cluster."),(0,o.kt)("td",{parentName:"tr",align:null},"true")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.numLookupLoadingThreads")),(0,o.kt)("td",{parentName:"tr",align:null},"Number of threads for loading the lookups in parallel on startup. This thread pool is destroyed once startup is done. It is not kept during the lifetime of the JVM"),(0,o.kt)("td",{parentName:"tr",align:null},"Available Processors / 2")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.coordinatorFetchRetries")),(0,o.kt)("td",{parentName:"tr",align:null},"How many times to retry to fetch the lookup bean list from Coordinator, during the sync on startup."),(0,o.kt)("td",{parentName:"tr",align:null},"3")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.lookupStartRetries")),(0,o.kt)("td",{parentName:"tr",align:null},"How many times to retry to start each lookup, either during the sync on startup, or during the runtime."),(0,o.kt)("td",{parentName:"tr",align:null},"3")),(0,o.kt)("tr",{parentName:"tbody"},(0,o.kt)("td",{parentName:"tr",align:null},(0,o.kt)("inlineCode",{parentName:"td"},"druid.lookup.coordinatorRetryDelay")),(0,o.kt)("td",{parentName:"tr",align:null},"How long to delay (in millis) between retries to fetch lookup list from the Coordinator during the sync on startup."),(0,o.kt)("td",{parentName:"tr",align:null},"60_000")))),(0,o.kt)("h2",{id:"introspect-a-lookup"},"Introspect a Lookup"),(0,o.kt)("p",null,"The Broker provides an API for lookup introspection if the lookup type implements a ",(0,o.kt)("inlineCode",{parentName:"p"},"LookupIntrospectHandler"),"."),(0,o.kt)("p",null,"A ",(0,o.kt)("inlineCode",{parentName:"p"},"GET")," request to ",(0,o.kt)("inlineCode",{parentName:"p"},"/druid/v1/lookups/introspect/{lookupId}")," will return the map of complete values."),(0,o.kt)("p",null,"ex: ",(0,o.kt)("inlineCode",{parentName:"p"},"GET /druid/v1/lookups/introspect/nato-phonetic")),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre"},'{\n "A": "Alfa",\n "B": "Bravo",\n "C": "Charlie",\n ...\n "Y": "Yankee",\n "Z": "Zulu",\n "-": "Dash"\n}\n\n')),(0,o.kt)("p",null,"The list of keys can be retrieved via ",(0,o.kt)("inlineCode",{parentName:"p"},"GET")," to ",(0,o.kt)("inlineCode",{parentName:"p"},'/druid/v1/lookups/introspect/{lookupId}/keys"')),(0,o.kt)("p",null,"ex: ",(0,o.kt)("inlineCode",{parentName:"p"},"GET /druid/v1/lookups/introspect/nato-phonetic/keys")),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre"},'[\n "A",\n "B",\n "C",\n ...\n "Y",\n "Z",\n "-"\n]\n')),(0,o.kt)("p",null,"A ",(0,o.kt)("inlineCode",{parentName:"p"},"GET")," request to ",(0,o.kt)("inlineCode",{parentName:"p"},'/druid/v1/lookups/introspect/{lookupId}/values"')," will return the list of values."),(0,o.kt)("p",null,"ex: ",(0,o.kt)("inlineCode",{parentName:"p"},"GET /druid/v1/lookups/introspect/nato-phonetic/values")),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre"},'[\n "Alfa",\n "Bravo",\n "Charlie",\n ...\n "Yankee",\n "Zulu",\n "Dash"\n]\n')))}c.isMDXComponent=!0}}]);