cost-based optimization for magic : algebra and implementation praveen seshadri , joseph m. hellerstein , hamid pirahesh , t. y. cliff leung , raghu ramakrishnan , divesh srivastava , peter j. stuckey , s. sudarshan