Friday, November 30, 2007

SQL Query - Order of Execution

When a query is submitted to the database, it is executed in the following order:
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause
ORDER BY clause

So why is it important to understand this?
When a query is executed,
First all the tables and their join conditions are executed filtering out invalid references between them.
Then the WHERE clause is applied which again filters the records based on the condition given.
Now you have handful of records which are GROUP-ed
And HAVING clause is applied on the result.
As soon as it is completed, the columns mentioned are selected from the corresponding tables.
And finally sorted using ORDER BY clause.
So when a query is written it should be verified based on this order, otherwise it will lead wrong resultsets.

Example:
Recently I came across the following problem.

The original query was
SELECT DISTINCT so.departure_airport, so.airport_name
FROM special_offers so, special_offers_groups_join sogj
WHERE so.resort_code = '000170' AND so.accomodation_code = '015397'
AND so.departure_date = '2008-01-13'
AND so.nights = '7'
AND sogj.group_id = '1'
AND sogj.special_offer_id = so.special_offer_id
ORDER BY price ASC

Table data:
departure_airport airport_name price
'MAN' 'Manchester Airport' 344
'MAN' 'Manchester Airport' 288
'MAN' 'Manchester Airport' 316
'BRS' 'Bristol' 289
'BRS' 'Bristol' 345
'BRS' 'Bristol' 317
'BHX' 'Birmingham Airport' 343
'BHX' 'Birmingham Airport' 287
'BHX' 'Birmingham Airport' 315

Here the query is executed as follows:
All the records from both the tables are taken.
Conditions mentioned in the WHERE clause is applied fetching say 9 records.
Now the distinct of departure_airport and airport_name alone is taken since price is not in SELECT clause. This gives you three rows randomly selected (mostly in the order of insertion to the table!)
And the result is sorted based on price (incorrect price column due to distinct keyword).
So the output is not ordered properly based on price!

So we need to rewrite the query as follows:
All the records from both the tables should be taken.
Conditions mentioned in the WHERE clause needs to applied on the resulting resultset.
Group the resultset based on departure_airport and airport_name to get the minimum price in each group.
Departure_airport airport_name minprice
'BHX' 'Birmingham Airport' 287
'BRS' 'Bristol' 289
'MAN' 'Manchester Airport' 288

Select the columns departure_airport, airport_name and min(so.price) from the resultset.
And finally apply the ORDER By clause which orders the resultset based on the column min(so.price).
So the query should be:

SELECT so.departure_airport, so.airport_name, MIN(so.price) AS minprice
FROM special_offers so, special_offers_groups_join sogj
WHERE so.resort_code = '000170'
AND so.accomodation_code = '015397'
AND so.departure_date = '2008-01-13'
AND so.nights = '7'
AND sogj.group_id = '1'
AND sogj.special_offer_id = so.special_offer_id
GROUP BY so.departure_airport,so.airport_name
ORDER BY minprice;

Thursday, November 29, 2007

Custom Condition in ANT

Here is an example of custom ant task.

The custom condition validates the current execution time with the time interval specified.


package com.package.ant.customTask;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.condition.Condition;
import org.joda.time.DateTime;
import org.joda.time.MutableDateTime;

/**
* This class checks whether the current time lies between the specified time
* range.
*
* @author Srikanth NT
*
*/
public class BetweenTimeRangeCondition implements Condition {
private String startTimePeriod = "00:01";
private String endTimePeriod = "06:30";

/**
* @see org.apache.tools.ant.Task#execute()
*/
public boolean eval() throws BuildException {
DateTime startsAt = getValidTime(startTimePeriod);
DateTime endsAt = getValidTime(endTimePeriod);

if (startsAt.isAfter(endsAt)) {
throw new BuildException("Start time period cannot be greater than End time period.");
}

return (startsAt.isBeforeNow() && endsAt.isAfterNow());
}

/**
* Validates the given time.
* @param timePeriod
* @return
* @throws BuildException
*/
private DateTime getValidTime(String timePeriod) throws BuildException {
MutableDateTime testCalendar = new MutableDateTime();
if (timePeriod.indexOf(':') != 2 && timePeriod.lastIndexOf(':') != 2) {
throw new BuildException("Time period '" + timePeriod + "' should be in the format 'hh:mm' (24 Hour).");
}

try {
testCalendar.setHourOfDay(Integer.parseInt(timePeriod.split(":")[0]));
testCalendar.setMinuteOfHour(Integer.parseInt(timePeriod.split(":")[1]));
testCalendar.setSecondOfMinute(0);
} catch (Exception e) {
throw new BuildException("Time period '" + timePeriod + "' should be in the format 'hh:mm' (24 Hour).");
}
return testCalendar.toDateTime();
}

/**
* @param startTimePeriod
*/
public void setStartTimePeriod(String startTimePeriod) {
this.startTimePeriod = startTimePeriod;
}

/**
* @param endTimePeriod
*/
public void setEndTimePeriod(String endTimePeriod) {
this.endTimePeriod = endTimePeriod;
}

}


And in the build.xml include the following lines.

<typedef name="betweentime" classname="com.package.ant.customTask.BetweenTimeRangeCondition" classpath="BetweenTimeRangeCondition.class" />

<fail message="Failing the build.">
<condition>
<not>
<betweentime startTimePeriod="00:01" endTimePeriod="06:30" />
</not>
</condition>
</fail>

Wednesday, November 28, 2007

Today I had a chance to create a custom task in ant.
I created a new class extending Task and had few instance variables to suit my business needs. Then I overridden the execute method to validate a condition and based on the output, a property in build.xml was set to either true or false. Tommorrow I will post the sample code!

I also worked on Apache redirection using regular expression rules. It worked very well. Hopefuly I would post the procedure tommorrow.

Bye for now....

Monday, November 26, 2007

I started a dull day doing lot of mistakes! But managed to code something on Python for file processing. I tried to traverse through the directory getting all the input files and adding it to the list.

def prepareListOfFiles(dir):
for f in os.listdir(dir):
if os.path.isdir(dir + f) == True:
prepareListOfFiles(dir + f + '\\')
else:
listOfFiles.append(dir+f)

And to delete a list of files of the given pattern, I wrote:

def cleanOldOutputFiles(dir):
for f in os.listdir(dir):
if os.path.isdir(dir + f) == True:
cleanOldOutputFiles(dir + f + '\\')
else:
if f.endswith('.output'):
os.remove(dir+f)

Remember I am new to Python !

Sunday, November 25, 2007

Hi,
this is my first blog in my life.
I live in London working as a Java developer.
I wanna use this space to share my experiences with everyone.