<commit_before>package mimir.sql;

import java.sql._

import mimir.Database
import mimir.algebra._
import mimir.util._
import net.sf.jsqlparser.expression.operators.arithmetic._
import net.sf.jsqlparser.expression.operators.conditional._
import net.sf.jsqlparser.expression.operators.relational._
import net.sf.jsqlparser.expression.{BinaryExpression, DateValue, DoubleValue, Function, LongValue, NullValue, Parenthesis, StringValue, WhenClause}
import net.sf.jsqlparser.schema.Column
import net.sf.jsqlparser.statement.create.table._
import net.sf.jsqlparser.statement.select.{AllColumns, AllTableColumns, FromItem, PlainSelect, SelectBody, SelectExpressionItem, SubJoin, SubSelect}
import org.joda.time.LocalDate

import scala.collection.JavaConversions._;

class SqlToRA(db: Database) 
{
  def unhandled(feature : String) = {
    println("ERROR: Unhandled Feature: " + feature)
    throw new SQLException("Unhandled Feature: "+feature)
  }
  
  def convert(t : ColDataType): Type.T = {
    t.getDataType.toUpperCase match { 
      case "INT" => Type.TInt
      case "NUMBER" => Type.TInt
      case "CHAR" => Type.TString
    }
  }
  
  def convert(s : net.sf.jsqlparser.statement.select.Select) : Operator = convert(s, null)._1
  def convert(s : net.sf.jsqlparser.statement.select.Select, alias: String) : (Operator, Map[String, String]) = {
    convert(s.getSelectBody(), alias)
  }
  
  def convert(sb : SelectBody) : Operator = 
    convert(sb, null)._1;
  
  def convert(sb : SelectBody, tableAlias: String) : (Operator, Map[String, String]) = {
    if(sb.isInstanceOf[PlainSelect]){
      val ps = sb.asInstanceOf[PlainSelect]
      
      val bindings = scala.collection.mutable.Map[String, String]()
      val reverseBindings = scala.collection.mutable.Map[String, String]()
      val sources = scala.collection.mutable.Map[String, List[String]]()
      
      var (ret, currBindings, sourceAlias) = convert(ps.getFromItem)
      sources.put(sourceAlias, currBindings.values.toList);
      bindings.putAll(currBindings)
      reverseBindings.putAll(
        currBindings.map( _ match { case (x,y) => (y,x) } )
      )

      var joinCond = null;
      
      if(ps.getJoins() != null) { 
        ret = ps.getJoins().foldLeft(ret)(
          (a, j) => {
            val (source, currBindings, sourceAlias) = convert(j.getRightItem())
            sources.put(sourceAlias, currBindings.values.toList);
            bindings.putAll(currBindings)
            reverseBindings.putAll(
              currBindings.map( _ match { case (x,y) => (y,x) } )
            )
            var r: Operator = Join(a, source)
            if(j.getOnExpression() != null) { 
              r = Select(convert(j.getOnExpression, bindings.toMap), r)
            }
            r
          }
        );
      }

      if(ps.getWhere != null) {
        ret = Select(convert(ps.getWhere, bindings.toMap), ret)
      }

      var exprId = 0;
      var includeAll = false;
      var needProject = (tableAlias != null);
      
      val defaultTargetsForTable = (name: String) => {
        sources.get(name).get.map(
          (x) => {
            val baseX = reverseBindings.get(x).get
            ( baseX, 
              ProjectArg(
                if(tableAlias != null){ tableAlias+"_"+baseX }
                  else { baseX },
                Var(x)
              ) 
            )
          }).filter( _._1 != "ROWID" )
      }
      
      val target: List[(String, ProjectArg)] = 
        ps.getSelectItems().map( (si) => {
          if(si.isInstanceOf[SelectExpressionItem]) {<commit_msg>Bugfix for nested query naming.  Closes #36<commit_after>package mimir.sql;

import java.sql._

import mimir.Database
import mimir.algebra._
import mimir.util._
import net.sf.jsqlparser.expression.operators.arithmetic._
import net.sf.jsqlparser.expression.operators.conditional._
import net.sf.jsqlparser.expression.operators.relational._
import net.sf.jsqlparser.expression.{BinaryExpression, DateValue, DoubleValue, Function, LongValue, NullValue, Parenthesis, StringValue, WhenClause}
import net.sf.jsqlparser.schema.Column
import net.sf.jsqlparser.statement.create.table._
import net.sf.jsqlparser.statement.select.{AllColumns, AllTableColumns, FromItem, PlainSelect, SelectBody, SelectExpressionItem, SubJoin, SubSelect}
import org.joda.time.LocalDate

import scala.collection.JavaConversions._;

class SqlToRA(db: Database) 
{
  def unhandled(feature : String) = {
    println("ERROR: Unhandled Feature: " + feature)
    throw new SQLException("Unhandled Feature: "+feature)
  }
  
  def convert(t : ColDataType): Type.T = {
    t.getDataType.toUpperCase match { 
      case "INT" => Type.TInt
      case "NUMBER" => Type.TInt
      case "CHAR" => Type.TString
    }
  }
  
  def convert(s : net.sf.jsqlparser.statement.select.Select) : Operator = convert(s, null)._1
  def convert(s : net.sf.jsqlparser.statement.select.Select, alias: String) : (Operator, Map[String, String]) = {
    convert(s.getSelectBody(), alias)
  }
  
  def convert(sb : SelectBody) : Operator = 
    convert(sb, null)._1;
  
  def convert(sb : SelectBody, tableAlias: String) : (Operator, Map[String, String]) = {
    if(sb.isInstanceOf[PlainSelect]){
      val ps = sb.asInstanceOf[PlainSelect]
      
      val bindings = scala.collection.mutable.Map[String, String]()
      val reverseBindings = scala.collection.mutable.Map[String, String]()
      val sources = scala.collection.mutable.Map[String, List[String]]()
      
      var (ret, currBindings, sourceAlias) = convert(ps.getFromItem)
      sources.put(sourceAlias, currBindings.values.toList);
      bindings.putAll(currBindings)
      reverseBindings.putAll(
        currBindings.map( _ match { case (x,y) => (y,x) } )
      )

      var joinCond = null;
      
      if(ps.getJoins() != null) { 
        ret = ps.getJoins().foldLeft(ret)(
          (a, j) => {
            val (source, currBindings, sourceAlias) = convert(j.getRightItem())
            sources.put(sourceAlias, currBindings.values.toList);
            bindings.putAll(currBindings)
            reverseBindings.putAll(
              currBindings.map( _ match { case (x,y) => (y,x) } )
            )
            var r: Operator = Join(a, source)
            if(j.getOnExpression() != null) { 
              r = Select(convert(j.getOnExpression, bindings.toMap), r)
            }
            r
          }
        );
      }

      if(ps.getWhere != null) {
        ret = Select(convert(ps.getWhere, bindings.toMap), ret)
      }

      var exprId = 0;
      var includeAll = false;
      var needProject = (tableAlias != null);
      
      val defaultTargetsForTable = (name: String) => {
        sources.get(name).get.map(
          (x) => {
            val baseX = reverseBindings.get(x).get
            ( baseX, 
              ProjectArg(
                if(tableAlias != null){ tableAlias.toUpperCase+"_"+baseX }
                  else { baseX },
                Var(x)
              ) 
            )
          }).filter( _._1 != "ROWID" )
      }
      
      val target: List[(String, ProjectArg)] = 
        ps.getSelectItems().map( (si) => {
          if(si.isInstanceOf[SelectExpressionItem]) {
{'fraction_non_alphanumeric': 0.1262176454216532, 'fraction_numerical': 0.0013915947676036739, 'mean_word_length': 2.543885601577909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5004871', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2247, 'n_words': 428}