Hacking MySQL: making TRUNCATE behaviour more intuitive

Posted by Jorge Bernal June 12, 2007

This is my second article about hacking MySQL. If you are interested in this topic, you may want to read my previous Hacking MySQL: SIGNAL support (I)

The problem

If I tell you there is a function called TRUNCATE, what do you think it does? which are its arguments?

For me, the obvious behaviour would be something like:

mysql> SELECT TRUNCATE(123.45);
+--------------------+
| TRUNCATE(123.45)   |
+--------------------+
|                123 |
+--------------------+
1 row in set (0.08 sec)

But the actual behaviour is this:

mysql> SELECT TRUNCATE(123.45);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

This is because TRUNCATE requires a second argument. The syntax, according to the manual, is:

TRUNCATE(X,D)

Returns the number X, truncated to D decimal places. If D is 0, the result has no decimal point or fractional part. D can be negative to cause D digits left of the decimal point of the value X to become zero.

Wouldn’t be cool if the default value for D were 0 if we don’t specify otherwise?

The solution

This time we only have to touch the parser (sql/sql_yacc.yy). The code for this is quite simple, although you might find some problems on type conversion.

--- mysql-5.1/sql/sql_yacc.yy	2007-05-08 12:25:51.000000000 +0200
+++ mysql-5.1-truncate/sql/sql_yacc.yy	2007-05-08 15:55:42.000000000 +0200
@@ -6741,6 +6741,13 @@
 	  { $$= new (YYTHD->mem_root) Item_func_replace($3,$5,$7); }
 	| TRUNCATE_SYM '(' expr ',' expr ')'
 	  { $$= new (YYTHD->mem_root) Item_func_round($3,$5,1); }
+	| TRUNCATE_SYM '(' expr ')'
+	  {
+            THD *thd= YYTHD;
+            Item *i1= new (thd->mem_root) Item_int((char*) "0",(int32) 0,1);
+
+      	    $$= new (thd->mem_root) Item_func_round($3,i1,1);
+	  }
 	| WEEK_SYM '(' expr ')'
 	  {
             THD *thd= YYTHD;

Get the patch: [28304-truncate_default_value.diff]

Like I said, the hardest part was to go through sql/item*.h trying to guess the expected type for Item_func_round and how to cast that type correctly. I should learn how to better debug the MySQL server.

Conclusion

Unfortunately, the bug (#28304) has been marked as to be fixed later but, at least, this helped me on my way to understand MySQL internals.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

My top 10 commands

Posted by Jorge Bernal June 11, 2007

It seems I’m a ruby guy after all:

$ history | awk '{print $2}' | awk 'BEGIN {FS="|"} {print $1}'|sort|uniq -c | sort -rn | head -10
111 svn
61 cd
44 rake
36 l
32 hg
18 ./script/console
14 rm
12 screen
12 history
12 ./script/server

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

New lens: Nikkor 50mm 1.8D AF

Posted by Jorge Bernal June 06, 2007

It’s been almost a year and a half since I got my first DSLR camera, a Nikon D50. It’s a nice camera and I’ve managed to take a bunch of cool pictures with it, but the kit lens (Nikkor 18-55 3.5/5.6G) is sometimes not enough. Since I take most of my pictures indoors at conferences, parties, concerts,… I needed a more appropriate lens for this.

So, after my last trip I went to eBay and got this awesome lens:

Nikkor 50mm lens

Some good features:

  • It’s really fast focusing
  • It’s very sharp (see example)
  • It’s small and lightweight, perfect for travelling

Also, the fact that it’s not a zoom forces you to move around subjects and experiment, helping you to improve your creativity.

You can check my 50mm test set on flickr.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

RedHat’s SLA: simpler is better

Posted by Jorge Bernal June 05, 2007

I know this is a bit old, but I’ve been trying to catch up with all the new stories after a conference, vacation, broken laptop and loads of work.

RedHat Enterprise Linux 5 (RHEL 5) was launched some months ago. I’ve never been a big fan of RedHat in terms of technology. I guess is quite good now, but RPMs scared me years ago and I’m not over it yet.

One of the things I liked is that RedHat proved that the KISS principle doesn’t only apply to software development, but to marketing and sales too. This is their new SLA (service level agreement). Can’t be simpler.

RedHat’s new SLA

That reminds me of MySQL and their all-you-can-eat support package (MySQL Enterprise Unlimited), easy to understand and with a catchy slogan:

For the price of a single CPU of Oracle Enterprise Edition ($40,000 per CPU), you can deploy an unlimited number of MySQL Enterprise Servers, with full 24×7 production support

It seems open source businesses are not only innovating on the technical side, but also disrupting the industry.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Improving your finances the European way

Posted by Jorge Bernal May 28, 2007

I’ve been thinking about this for a long time and talked to some American friends about this, so I decided to share with you some insights into the personal finances in Europe (actually it might be only Spain, or just me).

Debt

The original idea for the title was “What’s wrong with American personal finance”, but I guess a positive thinking could help more than just criticism. I usually read some productivity and lifehacks blogs like LifeHacker or zenhabits and once in a time, a money article pops up. The two most common pieces of advice in this field are:

  1. Avoid late fees by using Quicken/MS Money to pay your bills
  2. Get rid of your debt

Those two common issues amongst American people were really bizarre for me the first time, and still today I can’t fully understand were the problem comes from.

Avoiding late fees

Simply put, I’ve never paid a single late fee in my life (maybe renting videos long time ago). The most common payment method in Spain for recurrent bills like Internet, rent, phone,… is called domicilación bancaria. I’ve been told there’s something like that in the US, and I’ve found similar concepts on the Wikipedia: Direct debit and standing order.

That is, if I get a new DSL contract I give my bank account number to the service provider and they charge me every month directly in my account. The only chance to get a late fee there is if there are no funds in that account.

So, my piece of advice here is: ask your bank about this possibility and get your bills paid with direct debit

Get rid of your debt

This in fact hit me long time ago, in an episode of The fresh prince of Bel-Air, when Will gets his first credit card and starts spending way more than he can pay. I thought that was a silly joke, but as I discovered later, it seems like the sad beginning for a lot of people.

I have one credit card and I only use it to buy online and when I travel outside Spain. My other cards are debit cards: some Visa Electron and some Maestro. In fact, most of the time my credit card stays at home.

I don’t even know if there are debit cards in the US, but if they exist, you should get one.

I found this cool parody some time ago about getting out of debt:

Conclusion

My final basic trick is that my expenses are much lower than my income, so I can pay them every month.

I think the above video is my conclusion to this: don’t buy stuff you can’t afford and I should add find the financial tools you need to stay away from credits.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

The beginnings of business decadence

Posted by Jorge Bernal May 25, 2007

The pursuit of happiness

Business was originated to produce happiness, not to pile up millions. Too many so-called “successful” men are making business an end and aim in itself. They regard the multiplying of their millions and the extension of their works as the be-all and end-all of life. Such men are sometimes happy in a feverish, hustling sort of way, much as a fly placed in a tube of oxygen is furiously happy until its life burns out. But they have no time for the tranquil, finer, deeper joys of living. They are so obsessed with the material that they cannot enjoy the immaterial, the intangible, the ideal, the spiritual&emdash;quiet thought, self-communion, reflection, poise, inward happiness, domestic felicity. What profiteth it a man to gain uncounted riches if he thereby sacrifices his better self, his nobler qualities of manhood? Mere getting is not living.

Forbes Magazine, September 15, 1917

This was the first Forbes editorial, the first US business magazine.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Classic riddle

Posted by Jorge Bernal May 22, 2007

Labyrinth puzzle

From the always great xkcd

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Why to teach?

Posted by Jorge Bernal May 22, 2007

My decision about 1 year ago to start doing training was a strange move for me. It was totally new and scary field for me, but like I said at the moment, according to my experience all change has been for good. I think I can trust my intuition.

Like Seth Godin said (more than) once:
Safe is risky

I found a short article about How we learn which summarizes my point of view after 5 courses.

10% of what we READ

20% of what we HEAR

30% of what we SEE

50% of what we SEE and HEAR

70% of what is DISCUSSED with OTHERS

80% of what is EXPERIENCED PERSONALLY

95% of what we TEACH TO SOMEONE ELSE

William Glasser

You can never imagine the kind of questions you are going to get from so many different mindsets, so you have to find a lot of answers that you’d never find otherwise.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Fair price

Posted by Jorge Bernal May 20, 2007

Much has been said about music prices nowadays, I remember reading somewhere that usually the 10 most sold albums in Amazon every week were below $10. That’s not a bad price, but let’s call it sensible pricing.

Sensible pricing is sometimes not enough. Some albums are so good you’d feel confortable paying $20 for them, and some of those $9.99 albums have only one half-good song. I found this 37signals’ article today: Jane Siberry’s “you decide what feels right” pricing detailing how some small record labels are letting consumers (I don’t think that word applies anymore, but still) decide which is the fair price for a CD. At this time, 14% paid above suggested. See it on Sheeba Catalogue

The Canadian folk-pop singer Jane Siberry has a clever system: she has a “pay what you can� policy with her downloadable songs, so fans can download them free — but her site also shows the average price her customers have paid for each track. This subtly creates a community standard, a generalized awareness of how much people think each track is really worth. The result? The average price is as much as $1.30 a track, more than her fans would pay at iTunes

This is not new, magnatune has been doing that for about 4 years. They let you listen the full disc, then download it paying what you consider a fair price

Magnatune pricing

And to help this cool ideas, if you like piano music, let me recommend you Rob Costlow (blog). It’s a great album to stop and relax enjoying the beautiful sound of a piano. And he could be called a piano hacker according to his biography:

By the time he was twelve Rob Costlow was annoying his piano instructor by adding unwritten endings to songs during rehearsals and recitals.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Male Restroom Etiquette

Posted by Jorge Bernal May 16, 2007

I just found this video on Youtube by chance. It wasn’t being too funny but, after a while, when trying to explain why you shouldn’t talk in a public restroom, it starts with an example:

They strike up a conversation about Linux. [...] So there they are, discussing the advantages of open source development…

That really got me. They story gets really complicated and in the end… well you best watch it yourself. And remember: never ever say a single word within a men’s room

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

« Older blog posts • Newer blog posts »