<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gustavo Diel on Medium]]></title>
        <description><![CDATA[Stories by Gustavo Diel on Medium]]></description>
        <link>https://medium.com/@gustavo.diel?source=rss-33f9281bad5b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*JvoVnvJlCEdaqHGl</url>
            <title>Stories by Gustavo Diel on Medium</title>
            <link>https://medium.com/@gustavo.diel?source=rss-33f9281bad5b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 05:06:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gustavo.diel/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Swift extensions  for easier Array manipulation]]></title>
            <link>https://medium.com/magrathealabs/swift-extensions-for-easier-array-manipulation-f5bb72167544?source=rss-33f9281bad5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5bb72167544</guid>
            <category><![CDATA[extension]]></category>
            <category><![CDATA[english]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <dc:creator><![CDATA[Gustavo Diel]]></dc:creator>
            <pubDate>Wed, 08 Apr 2020 12:49:15 GMT</pubDate>
            <atom:updated>2020-08-03T19:48:59.329Z</atom:updated>
            <content:encoded><![CDATA[<h3>Swift<strong> extensions for easier Array manipulation</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6b25FC-FmHxYLNflieTgDw.png" /></figure><p>Swift’s extension capabilities are one of the most powerful features in the language. It allows developers to add new methods or calculated variables into anything. In this post I am going to show a really interesting way of using this feature with arrays.</p><p>The following example is about an app that shows a list of users, all of which have some attributes and may or may not be active. Let’s implement it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/418af3b87f5a40bb2782f006942180d3/href">https://medium.com/media/418af3b87f5a40bb2782f006942180d3/href</a></iframe><p>Now, we’d like to offer options to our customers. As such, this app allows the users to choose between listing all of the users or just the active ones. So you may have this on your <strong><em>TableViewController</em></strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce65683185e2fc13c096022a2179ad65/href">https://medium.com/media/ce65683185e2fc13c096022a2179ad65/href</a></iframe><p>And that works ok. But if we were to have the same logic in more than one controller, this becomes repetitive quickly. How can we improve this?</p><p>Let’s start with a simple extension:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/324126b7fd8a476f8229168402429cde/href">https://medium.com/media/324126b7fd8a476f8229168402429cde/href</a></iframe><p>Now, we update our controller, like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52a3f77c9192b5f1139e9928bf1df196/href">https://medium.com/media/52a3f77c9192b5f1139e9928bf1df196/href</a></iframe><p>It&#39;s getting better. But I am still not satisfied with it.</p><p>Due to the fact that <strong><em>users</em></strong> is an optional, and we know the business logic, we know for sure that if <strong><em>users</em></strong> is <strong><em>nil</em></strong>, <strong><em>usersToShow</em></strong> should be an empty array. So how can we achieve this without using guard statements of default values everywhere? More extensions!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/596fcaa59d6f564cd121fe5d048164b4/href">https://medium.com/media/596fcaa59d6f564cd121fe5d048164b4/href</a></iframe><p>Now we may update our controller with a cleaner code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc203bb3c4a032c7addc5b03c76b6d08/href">https://medium.com/media/dc203bb3c4a032c7addc5b03c76b6d08/href</a></iframe><h3>Conclusion</h3><p>Using Swift’s extension, we are literally extending the capabilities of the current code. In this example, we’ve managed to extend the implementation of the default type <strong><em>Array</em></strong> when specifically storing <strong><em>User</em></strong> objects to inject some logic from our product. This way, we have this logic in a single place and we may use it anywhere in our code. Later on, we injected code to an optional array of users. This allowed us to easily define the rule that if there are no users (aka the <strong><em>users</em></strong> variable is <strong><em>nil</em></strong>) there will be no users to be shown, nor active.</p><p>We are now able to clean our code and contain the logic in a single place. Also, if we extend <strong><em>User</em></strong> to another class (let’s say <strong><em>Admin</em></strong>), we only need to create a <strong><em>var admin: [Admin]?</em></strong> and the logic will also be extended to this variable.</p><p>I do hope that this has been helpful and maybe teach you one thing or two. Swift has a lot of tips and tricks that I’d love to share with you in the future!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5bb72167544" width="1" height="1" alt=""><hr><p><a href="https://medium.com/magrathealabs/swift-extensions-for-easier-array-manipulation-f5bb72167544">Swift extensions  for easier Array manipulation</a> was originally published in <a href="https://medium.com/magrathealabs">Magrathea</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Advanced BigQuery queries with JavaScript and JSON]]></title>
            <link>https://medium.com/magrathealabs/advanced-bigquery-queries-with-javascript-and-json-e02fb58f38cb?source=rss-33f9281bad5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e02fb58f38cb</guid>
            <category><![CDATA[json]]></category>
            <category><![CDATA[english]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[bigquery]]></category>
            <category><![CDATA[query]]></category>
            <dc:creator><![CDATA[Gustavo Diel]]></dc:creator>
            <pubDate>Thu, 19 Mar 2020 14:39:20 GMT</pubDate>
            <atom:updated>2020-08-03T19:49:15.994Z</atom:updated>
            <content:encoded><![CDATA[<p>BigQuery has a lot of options and capabilities, with its handful of built-in methods at our disposal. In this post, I’d like to show you how we may take advantage on JSON parsing and JavaScript inside queries!</p><p>First of all, lets define some data, shall we? The table name is <strong><em>my_table. </em></strong>Let&#39;s have a look at it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46d5169f246bb9218c739ad4f342d140/href">https://medium.com/media/46d5169f246bb9218c739ad4f342d140/href</a></iframe><p>This is a really simple data structure, but it resembles something that I&#39;ve experienced in the past. This log came from a Ruby application, and if you know Ruby, you’ll probably notice that <strong><em>params </em></strong>inside <strong><em>payload </em></strong>is a Ruby Hash. Not ideal, but it happens.</p><h3><strong>Queries with JSON</strong></h3><p>Now, our first task is to simply show all deletion logs. Easy right?</p><pre>SELECT id, payload<br>FROM my_table<br>WHERE payload LIKE &#39;%delete%&#39;</pre><p>And it works! Well... kinda. If any param had <strong><em>delete</em></strong> anywhere else, we would’ve fetched some wrong rows. We must make sure to look inside the <strong><em>action</em></strong> key only. Let’s do this:</p><pre>SELECT id, payload<br>FROM my_table<br>WHERE JSON_EXTRACT_SCALAR(payload, &#39;$.action&#39;) = &#39;delete&#39;</pre><p>Now we are talking! BigQuery has a built-in JSON parse for us to use and abuse. Let’s say we want to return only the params of each create action, like this:</p><pre>SELECT id, JSON_EXTRACT_SCALAR(payload, &#39;$.params&#39;) AS params<br>FROM my_table<br>WHERE JSON_EXTRACT_SCALAR(payload, &#39;$.action&#39;) = &#39;create&#39;</pre><p>and it works perfectly.</p><h3><strong>Using JavaScript inside queries</strong></h3><p>Now let’s get to the more advanced stuff. The first one is: fetch the params of every create action, but in JSON format instead of Ruby&#39;s hash.</p><p>For this problem, we must parse the <strong><em>params</em></strong>, and BigQuery has no pre-built methods for that. So, let&#39;s create our own!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/707c50fc8814e83de5fd00bbf740806a/href">https://medium.com/media/707c50fc8814e83de5fd00bbf740806a/href</a></iframe><p>To complicate things a bit further, for each attribute inside <strong><em>params</em></strong>, we want to output each key and value along with it&#39;s row’s ID. So the output would be like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac9ded68dc1c6fd183a493f3fd617e65/href">https://medium.com/media/ac9ded68dc1c6fd183a493f3fd617e65/href</a></iframe><p>My solution is as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/069b232d85d90059028efe01f85403b9/href">https://medium.com/media/069b232d85d90059028efe01f85403b9/href</a></iframe><h3>Conclusion</h3><p>As shown above, BigQuery can be really flexible with just a few lines of code. The JSON parser is pretty sweet, and when you need more advanced stuff, the JavaScript is very handy. Should it be used everywhere? No. It does come with a performance cost.</p><p>But when you have a ginormous data and time is not of the essence, but have to be sure to return only what you absolutely need or you need to execute some logic to parse some data, then a JS function in your query may be a good solution.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e02fb58f38cb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/magrathealabs/advanced-bigquery-queries-with-javascript-and-json-e02fb58f38cb">Advanced BigQuery queries with JavaScript and JSON</a> was originally published in <a href="https://medium.com/magrathealabs">Magrathea</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fetching millions of rows from PostgreSQL with Rails]]></title>
            <link>https://medium.com/magrathealabs/fetching-millions-of-rows-from-postgresql-with-rails-70c0cec1b6f5?source=rss-33f9281bad5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/70c0cec1b6f5</guid>
            <category><![CDATA[english]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[stream]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[postgres]]></category>
            <dc:creator><![CDATA[Gustavo Diel]]></dc:creator>
            <pubDate>Wed, 10 Jul 2019 20:20:16 GMT</pubDate>
            <atom:updated>2020-03-10T17:14:24.155Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H3nRGjtBpj7s17dNJQi6og.jpeg" /></figure><p>Let’s say you have a simple Rails application, and you want to process a large quantity of rows from a PostgreSQL instance. For instance, you want to iterate over every post the user has, so you may do something like this:</p><pre>Post.where(user_id: 16)<br>==&gt; SELECT &quot;posts&quot;.* FROM &quot;posts&quot; where &quot;posts&quot;.&quot;user_id&quot; = 16</pre><p>This SQL query might take anything from some milliseconds to a couple of seconds on a regular sized application. But what if you have millions of users, and each of them have millions of posts and each post has lots of data? This is where that simple query might fail: the DB will try to process and return all the results at once, which may overload the connection, the DB machine and even your own Rails console.</p><p>You could try to paginate the query with something like</p><pre>Posts.where(user_id: 16).limit(MAX_PAGE_SIZE).offset(offset)<br>==&gt; SELECT &quot;posts&quot;.* FROM &quot;posts&quot; WHERE &quot;posts&quot;.&quot;user_id&quot; = 16 LIMIT MAX_PAGE_SIZE OFFSET offset</pre><p>For the first results it will work, but after some thousands of millions of rows, it’s going to start hanging, or even timeout! <a href="https://stackoverflow.com/questions/26625614/select-query-with-offset-limit-is-too-much-slow/27169302#27169302">This happens because PG handles offset queries using a <em>full-scan</em> strategy</a>.</p><h3>The alternative: Streaming!</h3><p>It is possible to use a raw PG connection in order to stream the results directly from the database, like this:</p><pre>connection = ActiveRecord::Base.connection.raw_connection</pre><pre># Feeds the DB with the query it will run<br># But it does NOT execute it yet!<br>connection.send_query(&quot;SELECT title FROM &quot;posts&quot; WHERE &quot;posts&quot;.&quot;user_id&quot; = #{user_id}&quot;)</pre><pre># This line alone would solve our problems, as it sets DB’s mode<br># as single line, which instead of sending the results all at once,<br># it sends them line by line, as requested by the application.<br>connection.set_single_row_mode</pre><pre># This will return the next result from DB, and if you use stream_each<br># right after it, it will stream the results one by one until there<br># are no more results to fetch.<br>connection.get_result.stream_each do |row|<br>  puts row[&#39;title&#39;]<br>end</pre><h3>Conclusion</h3><p>We now have a really simple but powerful script that was able to query billions of lines of data without slowing down anybody’s experience, and it only took half a day! We hope that streaming single rows from DB with ActiveRecord may help you in your future endeavours!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=70c0cec1b6f5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/magrathealabs/fetching-millions-of-rows-from-postgresql-with-rails-70c0cec1b6f5">Fetching millions of rows from PostgreSQL with Rails</a> was originally published in <a href="https://medium.com/magrathealabs">Magrathea</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>