blob: 91e4abaae7167f695a434561e0a1de20e83cdeef [file] [log] [blame]
{"version":3,"sources":["webpack:///./src/pages/docs/installation/sql_templating.mdx"],"names":["_frontmatter","MDXLayout","DefaultLayout","MDXContent","components","props","mdxType","isMDXComponent"],"mappings":"wPAMaA,G,UAAe,S,gOAE5B,IAKMC,EAAYC,IACH,SAASC,EAAT,GAGZ,IAFDC,EAEC,EAFDA,WACGC,EACF,8BACD,OAAO,YAACJ,EAAD,eAAeI,EAAf,CAAsBD,WAAYA,EAAYE,QAAQ,cAC3D,iBAAQ,CACN,GAAM,kBADR,kBAGA,iBAAQ,CACN,GAAM,mBADR,mBAGA,yCACE,gBAAO,CACL,KAAQ,+CACR,WAAc,KAFhB,oBADF,gKAQE,yBAAgB,CACd,WAAc,KADhB,sBARF,oGAaA,uBAAK,mBAAU,CACX,UAAa,kBACb,WAAc,OAFb,wEAOL,gIAEE,yBAAgB,CACd,WAAc,KADhB,8BAFF,mTASE,yBAAgB,CACd,WAAc,KADhB,KATF,2BAaE,yBAAgB,CACd,WAAc,KADhB,SAbF,gCAkBA,+CACE,yBAAgB,CACd,WAAc,KADhB,8BADF,sGAMA,uBAAK,mBAAU,CACX,UAAa,kBACb,WAAc,OAFb,w+CA2CL,+PAGA,uBAAK,mBAAU,CACX,UAAa,kBACb,WAAc,OAFb,gHASL,4EAEE,gBAAO,CACL,KAAQ,mFACR,WAAc,KAFhB,kBAFF,W,8NAWJH,EAAWI,gBAAiB","file":"component---src-pages-docs-installation-sql-templating-mdx-22b66f52df5cbbcf0816.js","sourcesContent":["import * as React from 'react'\n /* @jsx mdx */\nimport { mdx } from '@mdx-js/react';\n/* @jsx mdx */\n\nimport DefaultLayout from \"/Users/max/code/superset/docs/node_modules/gatsby-theme-docz/src/base/Layout.js\";\nexport const _frontmatter = {};\n\nconst makeShortcode = name => function MDXDefaultShortcode(props) {\n console.warn(\"Component \" + name + \" was not imported, exported, or provided by MDXProvider as global scope\");\n return <div {...props} />;\n};\n\nconst MDXLayout = DefaultLayout;\nexport default function MDXContent({\n components,\n ...props\n}) {\n return <MDXLayout {...props} components={components} mdxType=\"MDXLayout\">\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 `}\n <a {...{\n \"href\": \"https://jinja.palletsprojects.com/en/2.11.x/\",\n \"parentName\": \"p\"\n }}>{`Jinja templating`}</a>\n {` 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 (`}\n <inlineCode {...{\n \"parentName\": \"p\"\n }}>{`superset_config.py`}</inlineCode>\n {`). Objects referenced in\nthis dictionary are made available for users to use in their SQL code.`}</p>\n <pre><code {...{\n \"className\": \"language-python\",\n \"parentName\": \"pre\"\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 `}\n <inlineCode {...{\n \"parentName\": \"p\"\n }}>{`CUSTOM_TEMPLATE_PROCESSORS`}</inlineCode>\n {` 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 `}\n <inlineCode {...{\n \"parentName\": \"p\"\n }}>{`$`}</inlineCode>\n {` style macro instead of `}\n <inlineCode {...{\n \"parentName\": \"p\"\n }}>{`{{ }}`}</inlineCode>\n {` style in Jinja\ntemplating.`}</p>\n <p>{`By configuring it with `}\n <inlineCode {...{\n \"parentName\": \"p\"\n }}>{`CUSTOM_TEMPLATE_PROCESSORS`}</inlineCode>\n {`, a SQL template on a presto database is\nprocessed by the custom one rather than the default one.`}</p>\n <pre><code {...{\n \"className\": \"language-python\",\n \"parentName\": \"pre\"\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 {...{\n \"className\": \"language-python\",\n \"parentName\": \"pre\"\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`}\n <a {...{\n \"href\": \"https://github.com/apache/incubator-superset/tree/master/superset/sql_validators\",\n \"parentName\": \"p\"\n }}>{`sql_validators`}</a>\n {`.`}</p>\n\n </MDXLayout>;\n}\n;\nMDXContent.isMDXComponent = true;\n "],"sourceRoot":""}