blob: 77e9d1f9df078c6b902a80697cbb9cecf31ba703 [file] [log] [blame]
{"version":3,"sources":["webpack:///./src/pages/docs/installation/sql_templating.mdx"],"names":["_frontmatter","layoutProps","MDXLayout","DefaultLayout","MDXContent","components","props","mdxType","parentName","isMDXComponent"],"mappings":"wPAQaA,G,UAAe,S,gOAC5B,IAAMC,EAAc,CAClBD,gBAEIE,EAAYC,IACH,SAASC,EAAT,GAGZ,IAFDC,EAEC,EAFDA,WACGC,EACF,8BACD,OAAO,YAACJ,EAAD,eAAeD,EAAiBK,EAAhC,CAAuCD,WAAYA,EAAYE,QAAQ,cAG5E,iBAAQ,CACN,GAAM,kBADR,kBAGA,iBAAQ,CACN,GAAM,mBADR,mBAGA,yCAAwB,6BAAGC,WAAW,KAAQ,CAC1C,KAAQ,iDADY,oBAAxB,gKAImD,0BAAYA,WAAW,KAAvB,sBAJnD,oGAMA,uBAAK,gCAAMA,WAAW,OAAU,CAC5B,UAAa,oBADZ,wEAML,gIACE,0BAAYA,WAAW,KAAvB,8BADF,mTAIuC,0BAAYA,WAAW,KAAvB,KAJvC,2BAIgH,0BAAYA,WAAW,KAAvB,SAJhH,gCAMA,+CAA8B,0BAAYA,WAAW,KAAvB,8BAA9B,sGAEA,uBAAK,gCAAMA,WAAW,OAAU,CAC5B,UAAa,oBADZ,w+CA0CL,+PAGA,uBAAK,gCAAMA,WAAW,OAAU,CAC5B,UAAa,oBADZ,gHAQL,4EACF,6BAAGA,WAAW,KAAQ,CAChB,KAAQ,2EADd,kBADE,W,8NAQJJ,EAAWK,gBAAiB","file":"component---src-pages-docs-installation-sql-templating-mdx-d3e932d66f9ab0a3edb2.js","sourcesContent":["import * as React from 'react'\n /* @jsx mdx */\nimport { mdx } from '@mdx-js/react';\n/* @jsxRuntime classic */\n\n/* @jsx mdx */\n\nimport DefaultLayout from \"/Users/srinify/Documents/superset/docs/node_modules/gatsby-theme-docz/src/base/Layout.js\";\nexport const _frontmatter = {};\nconst layoutProps = {\n _frontmatter\n};\nconst MDXLayout = DefaultLayout;\nexport default function MDXContent({\n components,\n ...props\n}) {\n return <MDXLayout {...layoutProps} {...props} components={components} mdxType=\"MDXLayout\">\n\n\n <h2 {...{\n \"id\": \"sql-templating\"\n }}>{`SQL Templating`}</h2>\n <h3 {...{\n \"id\": \"jinja-templates\"\n }}>{`Jinja Templates`}</h3>\n <p>{`SQL Lab supports `}<a parentName=\"p\" {...{\n \"href\": \"https://jinja.palletsprojects.com/en/2.11.x/\"\n }}>{`Jinja templating`}</a>{` in queries. You'll\nneed to to overload the default Jinja context in your environment by defining the\nJINJA_CONTEXT_ADDONS in your superset configuration (`}<inlineCode parentName=\"p\">{`superset_config.py`}</inlineCode>{`). Objects referenced in\nthis dictionary are made available for users to use in their SQL code.`}</p>\n <pre><code parentName=\"pre\" {...{\n \"className\": \"language-python\"\n }}>{`JINJA_CONTEXT_ADDONS = {\n 'my_crazy_macro': lambda x: x*2,\n}\n`}</code></pre>\n <p>{`Besides default Jinja templating, SQL lab also supports self-defined template processor by setting\nthe `}<inlineCode parentName=\"p\">{`CUSTOM_TEMPLATE_PROCESSORS`}</inlineCode>{` in your superset configuration. The values in this dictionary\noverwrite the default Jinja template processors of the specified database engine. The example below\nconfigures a custom presto template processor which implements its own logic of processing macro\ntemplate with regex parsing. It uses the `}<inlineCode parentName=\"p\">{`$`}</inlineCode>{` style macro instead of `}<inlineCode parentName=\"p\">{`{{ }}`}</inlineCode>{` style in Jinja\ntemplating.`}</p>\n <p>{`By configuring it with `}<inlineCode parentName=\"p\">{`CUSTOM_TEMPLATE_PROCESSORS`}</inlineCode>{`, a SQL template on a presto database is\nprocessed by the custom one rather than the default one.`}</p>\n <pre><code parentName=\"pre\" {...{\n \"className\": \"language-python\"\n }}>{`def DATE(\n ts: datetime, day_offset: SupportsInt = 0, hour_offset: SupportsInt = 0\n) -> str:\n \"\"\"Current day as a string.\"\"\"\n day_offset, hour_offset = int(day_offset), int(hour_offset)\n offset_day = (ts + timedelta(days=day_offset, hours=hour_offset)).date()\n return str(offset_day)\n\nclass CustomPrestoTemplateProcessor(PrestoTemplateProcessor):\n \"\"\"A custom presto template processor.\"\"\"\n\n engine = \"presto\"\n\n def process_template(self, sql: str, **kwargs) -> str:\n \"\"\"Processes a sql template with $ style macro using regex.\"\"\"\n # Add custom macros functions.\n macros = {\n \"DATE\": partial(DATE, datetime.utcnow())\n } # type: Dict[str, Any]\n # Update with macros defined in context and kwargs.\n macros.update(self.context)\n macros.update(kwargs)\n\n def replacer(match):\n \"\"\"Expand $ style macros with corresponding function calls.\"\"\"\n macro_name, args_str = match.groups()\n args = [a.strip() for a in args_str.split(\",\")]\n if args == [\"\"]:\n args = []\n f = macros[macro_name[1:]]\n return f(*args)\n\n macro_names = [\"$\" + name for name in macros.keys()]\n pattern = r\"(%s)\\\\s*\\\\(([^()]*)\\\\)\" % \"|\".join(map(re.escape, macro_names))\n return re.sub(pattern, replacer, sql)\n\nCUSTOM_TEMPLATE_PROCESSORS = {\n CustomPrestoTemplateProcessor.engine: CustomPrestoTemplateProcessor\n}\n`}</code></pre>\n <p>{`SQL Lab also includes a live query validation feature with pluggable backends. You can configure\nwhich validation implementation is used with which database engine by adding a block like the\nfollowing to your configuration file:`}</p>\n <pre><code parentName=\"pre\" {...{\n \"className\": \"language-python\"\n }}>{`FEATURE_FLAGS = {\n 'SQL_VALIDATORS_BY_ENGINE': {\n 'presto': 'PrestoDBSQLValidator',\n }\n}\n`}</code></pre>\n <p>{`The available validators and names can be found in\n`}<a parentName=\"p\" {...{\n \"href\": \"https://github.com/apache/superset/tree/master/superset/sql_validators\"\n }}>{`sql_validators`}</a>{`.`}</p>\n\n </MDXLayout>;\n}\n;\nMDXContent.isMDXComponent = true;\n "],"sourceRoot":""}