{"id":1187,"date":"2023-09-06T06:36:07","date_gmt":"2023-09-06T06:36:07","guid":{"rendered":"https:\/\/craftcookcode.com\/?p=1187"},"modified":"2024-07-10T02:02:58","modified_gmt":"2024-07-10T02:02:58","slug":"qlik-replicate-expressions-wait-thats-illegal","status":"publish","type":"post","link":"https:\/\/craftcookcode.com\/?p=1187","title":{"rendered":"Qlik Replicate Expressions &#8211; Wait.  That&#8217;s Illegal"},"content":{"rendered":"\n<p>A new team will be taking over for out of hours oncall for Qlik Replicate and I tasked with training up a new team to diagnose and resolve problems with QR.<\/p>\n\n\n\n<p>I thought it was a good idea to create some practice QR tasks in our development environment; intentionally put some problems in the tasks and let the new team diagnose the problem in a safe and no pressure scenarios.<\/p>\n\n\n\n<p>I did the obvious ones; like put an incorrect password in an end point, put a typo in a filter criteria.<\/p>\n\n\n\n<p>Looking for another scenario I thought \u2013 \u201cNothing better than a divide by zero error.\u201d<style>\/*! elementor - v3.13.3 - 28-05-2023 *\/&amp;lt;br \/>&lt;br>.elementor-heading-title{padding:0;margin:0;line-height:1}.elementor-widget-heading .elementor-heading-title[class*=elementor-size-]>a{color:inherit;font-size:inherit;line-height:inherit}.elementor-widget-heading .elementor-heading-title.elementor-size-small{font-size:15px}.elementor-widget-heading .elementor-heading-title.elementor-size-medium{font-size:19px}.elementor-widget-heading .elementor-heading-title.elementor-size-large{font-size:29px}.elementor-widget-heading .elementor-heading-title.elementor-size-xl{font-size:39px}.elementor-widget-heading .elementor-heading-title.elementor-size-xxl{font-size:59px}<\/style><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wait. That&#8217;s Illegal &#8211; Part 1<\/h2>\n\n\n\n<p>I created a simple table; with a field of CURR_BAL = 1000 and ASSET_VALUE = 0 and added a new field call LVR with the expression of:<style>\/*! elementor - v3.13.3 - 28-05-2023 *\/&amp;lt;br \/>&lt;br>.elementor-widget-image{text-align:center}.elementor-widget-image a{display:inline-block}.elementor-widget-image a img[src$=\".svg\"]{width:48px}.elementor-widget-image img{vertical-align:middle;display:inline-block}<\/style><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$CURR_BALANCE \/ $ASSET_VALUE\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/09\/Code_CDC_Expression.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Ran a full load expecting lots of nice and nasty error messages for the new team to investigate.<\/p>\n\n\n\n<p>Hmm \u2013 no errors.<\/p>\n\n\n\n<p>That\u2019s not what I expected.<\/p>\n\n\n\n<p>All the rows were transferred across and there was no errors or warnings in the log file, no data errors or anything.<\/p>\n\n\n\n<p>I bumped up all the logging to \u201cTrace\u201d and reran the full load.&nbsp; Once again, no error messages.<\/p>\n\n\n\n<p>Redirecting the output from the task to a kafka topic; I notice the records that had an intentional div\/0 scenario was coming through as null.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n  &quot;magic&quot;: &quot;atMSG&quot;,\n  &quot;type&quot;: &quot;DT&quot;,\n  &quot;headers&quot;: null,\n  &quot;messageSchemaId&quot;: null,\n  &quot;messageSchema&quot;: null,\n  &quot;message&quot;: {\n    &quot;data&quot;: {\n      &quot;ACCOUNT_ID&quot;: 5,\n      &quot;ACCOUNT_NAME&quot;: &quot;Elissa&quot;,\n      &quot;CURR_BALANCE&quot;: &quot;10000.00&quot;,\n      &quot;ASSET_VALUE&quot;: &quot;0.00&quot;,\n      &quot;LVR&quot;: null\n    },\n    &quot;beforeData&quot;: null,\n    &quot;headers&quot;: {\n      &quot;operation&quot;: &quot;REFRESH&quot;,\n      &quot;changeSequence&quot;: &quot;&quot;,\n      &quot;timestamp&quot;: &quot;&quot;,\n      &quot;streamPosition&quot;: &quot;&quot;,\n      &quot;transactionId&quot;: &quot;&quot;,\n      &quot;changeMask&quot;: null,\n      &quot;columnMask&quot;: null,\n      &quot;transactionEventCounter&quot;: null,\n      &quot;transactionLastEvent&quot;: null\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>Then I remembered \u2013 CDC expressions are built on sqlite syntax.&nbsp; Looking up how sqlite handles div\/0 scenarios; by default sqlite will return a null instead of failing the query on a div\/0.<\/p>\n\n\n\n<p>Quoting from <a href=\"https:\/\/www.sqlite.org\/sqllogictest\/wiki?name=Differences+Between+Engines\">Sqlite &#8211; Difference Between engines<\/a><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>SQLite does not raise arithmetic exceptions (eg. divide by zero, 1\/0). SQLite returns a NULL value for 1\/0.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Wait. That&#8217;s Illegal &#8211; Part 2<\/h2>\n\n\n\n<p>I got past my div\/0 conundrum after the explanation of what was happening and had a look at a \u201cvalid\u201d record.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n    &quot;magic&quot;: &quot;atMSG&quot;,\n    &quot;type&quot;: &quot;DT&quot;,\n    &quot;headers&quot;: null,\n    &quot;messageSchemaId&quot;: null,\n    &quot;messageSchema&quot;: null,\n    &quot;message&quot;: {\n        &quot;data&quot;: {\n            &quot;ACCOUNT_ID&quot;: 1,\n            &quot;ACCOUNT_NAME&quot;: &quot;Alice&quot;,\n            &quot;CURR_BALANCE&quot;: &quot;12345.00&quot;,\n            &quot;ASSET_VALUE&quot;: &quot;500000.00&quot;,\n            &quot;LVR&quot;: 0        &amp;lt;------------- Huh?\n        },\n        &quot;beforeData&quot;: null,\n        &quot;headers&quot;: {\n            &quot;operation&quot;: &quot;REFRESH&quot;,\n            &quot;changeSequence&quot;: &quot;&quot;,\n            &quot;timestamp&quot;: &quot;&quot;,\n            &quot;streamPosition&quot;: &quot;&quot;,\n            &quot;transactionId&quot;: &quot;&quot;,\n            &quot;changeMask&quot;: null,\n            &quot;columnMask&quot;: null,\n            &quot;transactionEventCounter&quot;: null,\n            &quot;transactionLastEvent&quot;: null\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p>Again our LVR value was wrong.<\/p>\n\n\n\n<p>12345.00 \/ 500000.00 should be 0.02469; instead of 0<\/p>\n\n\n\n<p>A couple of things popped to mind.<\/p>\n\n\n\n<p>It looked like the classic integer division issue that always catches out new (and experienced) programmer.<\/p>\n\n\n\n<p>But that didn\u2019t make any sense since the source fields were both numeric(18,2).<\/p>\n\n\n\n<p>I checked the destination field that I added.<\/p>\n\n\n\n<p>It was set to numeric(18,2), which isn\u2019t precise enough for the LVR value but we still should have got 0.2 instead 0.<\/p>\n\n\n\n<p>OK &#8211; QR must not like&nbsp;the \u201ccustom\u201d precision of numeric(18,2) for the LVR field.&nbsp;<\/p>\n\n\n\n<p>I tried forcing the LVR field data type to REAL(4).<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/craftcookcode.com\/wp-content\/uploads\/2023\/09\/Code_CDC_Expression_2-1024x560.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Still got the incorrect value for LVR<\/p>\n\n\n\n<p>Maybe for some reason QR \/sqlite treats the values coming in as an integer; even though the source data type is numeric(18,2) on the database.<\/p>\n\n\n\n<p>Let&#8217;s give this a go:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCAST($CURR_BALANCE AS NUMERIC) \/ $ASSET_VALUE\n<\/pre><\/div>\n\n\n<p>^^ Nope<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCAST($CURR_BALANCE AS NUMERIC) \/ CAST($ASSET_VALUE AS NUMERIC)\u00a0\n<\/pre><\/div>\n\n\n<p>^^&nbsp;Long shot but nope<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCAST($CURR_BALANCE AS REAL) \/ $ASSET_VALUE\u00a0\n<\/pre><\/div>\n\n\n<p>^^&nbsp;At last success<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n{\n    &quot;magic&quot;: &quot;atMSG&quot;,\n    &quot;type&quot;: &quot;DT&quot;,\n    &quot;headers&quot;: null,\n    &quot;messageSchemaId&quot;: null,\n    &quot;messageSchema&quot;: null,\n    &quot;message&quot;: {\n        &quot;data&quot;: {\n            &quot;ACCOUNT_ID&quot;: 1,\n            &quot;ACCOUNT_NAME&quot;: &quot;Alice&quot;,\n            &quot;CURR_BALANCE&quot;: &quot;12345.00&quot;,\n            &quot;ASSET_VALUE&quot;: &quot;500000.00&quot;,\n            &quot;LVR&quot;: &quot;0.0246&quot;  &amp;lt;------------- Finally!\n        },\n        &quot;beforeData&quot;: null,\n        &quot;headers&quot;: {\n            &quot;operation&quot;: &quot;REFRESH&quot;,\n            &quot;changeSequence&quot;: &quot;&quot;,\n            &quot;timestamp&quot;: &quot;&quot;,\n            &quot;streamPosition&quot;: &quot;&quot;,\n            &quot;transactionId&quot;: &quot;&quot;,\n            &quot;changeMask&quot;: null,\n            &quot;columnMask&quot;: null,\n            &quot;transactionEventCounter&quot;: null,\n            &quot;transactionLastEvent&quot;: null\n        }\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>It works &#8211; but forcing a precise of numeric value to a floating point data type REAL leaves me uneasy; especially in a finance world were precision is needed.<\/p>\n\n\n\n<p>I haven&#8217;t researched or tested whether this is a risk of generating an imprecise number through a division or not; something I will need to put on my todo list.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>My design principle for Qlik Replicate tasks is to keep them as simple as possible.<\/p>\n\n\n\n<p>Grab the data off the source and get it to the destination as quickly as possible.<\/p>\n\n\n\n<p>By design principle I am against putting expressions in QR tasks:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>It is not as feature rich as other programming languages\u00a0<\/li>\n\n\n\n<li>It hides business rules that adds a black box and complexity to the pipeline<\/li>\n<\/ol>\n\n\n\n<p>Point 2 was the big one for me.&nbsp; I didn&#8217;t want downstream users messaging me every time there was an abnormality in the data.&nbsp; With no expressions in the QR tasks &#8211; I could rightfully say, &#8220;QR just grabs what it finds on the source database &#8211; go and talk to them&#8221;<\/p>\n\n\n\n<p>Now with the discovery of these little &#8220;features&#8221; that could come from expressions; and more importantly no error or warnings about these &#8220;features&#8221;; it gives me even more incentive not to program in exceptions.<\/p>\n\n\n\n<p>QR in my eyes is not an &#8220;ETL&#8221; tool &#8211; it is just a &#8220;EL&#8221; tool.<\/p>\n\n\n\n<p>If your designers want complex expressions and transformations programmed into their data pipelines &#8211; it should be the downstream developers task.<\/p>\n\n\n\n<p>That&#8217;s why they get paid the big bucks.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>A new team will be taking over for out of hours oncall for Qlik Replicate and I tasked with training up a new team to diagnose&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1076,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1187","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-qlik-replicate"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/1187","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1187"}],"version-history":[{"count":5,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/1187\/revisions"}],"predecessor-version":[{"id":1442,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/posts\/1187\/revisions\/1442"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=\/wp\/v2\/media\/1076"}],"wp:attachment":[{"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftcookcode.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}