<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Experimental Thoughts &#187; Ruby</title>
	<atom:link href="http://thoughts.j-davis.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://thoughts.j-davis.com</link>
	<description>Ideas on Databases, Logic, and Language by Jeff Davis</description>
	<lastBuildDate>Thu, 06 May 2010 19:29:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>None, nil, Nothing, undef, NA, and SQL NULL</title>
		<link>http://thoughts.j-davis.com/2008/08/13/none-nil-nothing-undef-na-and-sql-null/</link>
		<comments>http://thoughts.j-davis.com/2008/08/13/none-nil-nothing-undef-na-and-sql-null/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 18:00:02 +0000</pubDate>
		<dc:creator>Jeff Davis</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[NULL]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://davisjeff.wordpress.com/?p=14</guid>
		<description><![CDATA[In my last post, Why DBMSs are so complex, I raised the issue of type mismatches between the application language and the DBMS. Type matching between the DBMS and the application is as important as types themselves for successful application development. If a type behaves one way in the DBMS, and a &#8220;similar&#8221; type behaves [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post, <a title="Why DBMSs are so complex" href="http://thoughts.j-davis.com/2008/08/03/why-dbmss-are-so-complex/">Why DBMSs are so complex</a>, I raised the issue of type mismatches between the application language and the DBMS.</p>
<p>Type matching between the DBMS and the application is as important as types themselves for successful application development. If a type behaves one way in the DBMS, and a &#8220;similar&#8221; type behaves slightly differently in the application, that can only cause confusion. And it&#8217;s a source of unnecessary awkwardness: you already need to define the types that suit your business best in one place, why do you need to redefine them somewhere else, based on a different basic type system?</p>
<p><span id="more-14"></span></p>
<p>At least we&#8217;re using PostgreSQL, the most extensible database available, where you can define sophisticated types and make them perform like native features.</p>
<p>But there are still problems. Most notably, it&#8217;s a non-trivial challenge to find an appropriate way to model NULLs in the application language. You can&#8217;t <strong>not</strong> use them in the DBMS, because the SQL spec generates them from oblivion, e.g. from an outer join or an aggregate function, even when you have no NULLs in your database. So the only way to model the same semantics in your application is to somehow make your application language understand NULL semantics.</p>
<div>Here&#8217;s how SQL NULL behaves:</p>
<pre>
=&gt; -- aggregate with one NULL input
=&gt; select sum(column1) from (values(NULL::int)) t;
sum
-----

(1 row)

=&gt; -- aggregate with two inputs, one of them NULL
=&gt; select sum(column1) from (values(1),(NULL)) t;
sum
-----
1
(1 row)

=&gt; -- aggregate with no input
=&gt; select sum(column1) from (values(1),(NULL)) t where false;
sum
-----

(1 row)

=&gt; -- + operator
=&gt; select 1 + NULL;
?column?
----------

(1 row)
</pre>
<p>I&#8217;ll divide the &#8220;NULL-ish&#8221; values of various languages into two broad categories:</p>
<ol>
<li>Separate type, few operators defined, error early, no 3VL &#8212; Python, Ruby and Haskell fall into this category, because their &#8220;NULL-ish&#8221; types (<tt>None</tt>, <tt><strong>nil</strong></tt>, and <tt><strong>Nothing</strong></tt>, respectively) usually result in an immediate exception, unless the operator to which the NULLish value is passed handles it as a special case. Few built-in operators are defined for arguments of these types. These fail to behave like SQL NULL, because they employ no three-valued logic (3VL) at all, and thus fail in the forth portion of the SQL example.</li>
<li>Member of all types, every operator defined &#8212; Perl and R fall into this category. Perl&#8217;s <strong>undef</strong> can be passed through many built-in operators (like +), but doesn&#8217;t ever use 3VL, so fails the forth portion of the SQL example. R uses a kind of 3VL for it&#8217;s <tt><strong>NA</strong></tt> value, but it uses it everywhere, so <tt>sum(c(1,<strong>NA</strong>))</tt> results in <tt><strong>NA</strong></tt> (thus failing the second portion of the SQL example). In R, you can omit <tt>NA</tt>s from the sum explicitly (not a very good solution, by the way), but then it will fail the first portion of the SQL example.</li>
</ol>
<p>As far as I can tell (correct me if I&#8217;m mistaken), none of these languages support the third portion of the SQL example: the sum of an empty list in SQL is NULL. The languages that I tested with a built-in <tt>sum</tt> operator (Python, R, Haskell) all return <tt>0</tt> when passed an empty list.</p>
<p>Languages from the first category appear safer, because you will catch the errors earlier rather than later. However, transforming SQL NULLs in these languages to <tt>None</tt>, <tt><strong>nil</strong></tt>, or <tt><strong>Nothing</strong></tt> is actually quite dangerous, because a change in the data you store in your database (inserting NULLs or deleting records that may be aggregated) or even a change in a query (outer join, or an aggregate that may have no input) can produce NULLs, and therefore produce exceptions, that can evade even rigorous testing procedures and sneak into production.</p>
<p>Languages from the second category tend to pass the &#8220;<strong>undef</strong>&#8221; or &#8220;<strong>NA</strong>&#8221; along deeper into the application, which can cause unintuitive and difficult-to-trace problems. Perhaps worse, something will always happen, and usually the result will take the form of the correct answer even if it is wrong.</p>
<p>So where does that leave us? I think the blame here rests entirely on the SQL standard&#8217;s definition of NULL, and the inconsistency between &#8220;not a value at all&#8221; and &#8220;the third logical value&#8221; (both of which can be used to describe NULL in different contexts). Not much can be done about that, so I think the best strategy is to try to interpret and remove NULLs as early as possible. They can be removed from result sets before returning to the client by using COALESCE, and they can be removed after they reach the client with client code. Passing them along as some kind of special value is only useful if your application already must be thoroughly aware of that special value.</p>
<p>Note: Microsoft has defined some kind of &#8220;DBNull&#8221; value, and from browsing the docs, it appears a substantial amount of work went into making them behave as SQL NULLs. This includes a special set of SQL types and operators. Microsoft appears to be making a lot of progress matching DBMS and application types more closely, but I think the definition of SQL NULLs is a lost cause.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://thoughts.j-davis.com/2008/08/13/none-nil-nothing-undef-na-and-sql-null/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ruby-pg is now the official postgres ruby gem</title>
		<link>http://thoughts.j-davis.com/2007/12/14/ruby-pg-is-now-the-official-postgres-ruby-gem/</link>
		<comments>http://thoughts.j-davis.com/2007/12/14/ruby-pg-is-now-the-official-postgres-ruby-gem/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 18:00:10 +0000</pubDate>
		<dc:creator>Jeff Davis</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Language]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://davisjeff.wordpress.com/?p=18</guid>
		<description><![CDATA[ruby-pg is now the official rubyforge project for the &#8220;postgres&#8221; ruby gem. See the project here: http://www.rubyforge.org/projects/ruby-pg or install the gem directly: # gem install &#8211;remote postgres The previous project has gone unmaintained for a long time, which lead to the fork. This gem includes some important fixes, most notably the ability to compile against [...]]]></description>
			<content:encoded><![CDATA[<div>ruby-pg is now the official rubyforge project for the &#8220;postgres&#8221; ruby<br />
gem. See the project here:</p>
<p><a href="http://www.rubyforge.org/projects/ruby-pg">http://www.rubyforge.org/projects/ruby-pg</a></p>
<p>or install the gem directly:</p>
<p># gem install &#8211;remote postgres</p>
<p><span id="more-18"></span></p>
<p>The previous project has gone unmaintained for a long time, which lead<br />
to the fork.</p>
<p>This gem includes some important fixes, most notably the ability to<br />
compile against PostgreSQL 8.3.</p>
<div>The gem contains two modules:</p>
<ul>
<li>&#8216;postgres&#8217; &#8212; require this module as before, you can use it without<br />
making any changes to your application. This is essentially just a fork<br />
from version 0.7.1.2006.04.06, but contains some important fixes,<br />
including the ability to build against 8.3.</li>
<li>&#8216;pg&#8217; &#8212; a new interface, designed to offer every feature available in<br />
libpq to Ruby, with a better API. This module is simpler, cleaner, and<br />
more portable. It is still unstable, so test before using.</li>
</ul>
<p>PostgreSQL+Ruby users: please test and report any problems. I&#8217;d like to<br />
make sure this is as stable as possible, and builds on all necessary<br />
platforms.</p></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://thoughts.j-davis.com/2007/12/14/ruby-pg-is-now-the-official-postgres-ruby-gem/feed/</wfw:commentRss>
		<slash:comments>616</slash:comments>
		</item>
	</channel>
</rss>
