<commit_before>package slamdata.engine

import slamdata.engine.analysis.fixplate._
import slamdata.engine.analysis._
import slamdata.engine.sql.SQLParser
import slamdata.engine.std._
import scalaz._
import org.specs2.mutable._
import org.specs2.matcher.{Matcher, Expectable}
import org.specs2.execute.PendingUntilFixed

@org.junit.runner.RunWith(classOf[org.specs2.runner.JUnitRunner])
class CompilerSpec extends Specification with CompilerHelpers {
  import StdLib._
  import structural._
  import agg._
  import math._
  import relations._
  import set._
  import string._
  import LogicalPlan._
  import SemanticAnalysis._

  "compiler" should {
    "compile simple constant example 1" in {
      testLogicalPlanCompile(
        "select 1",
        makeObj(
          "0" -> constant(Data.Int(1))
        )
      )
    }

    "compile simple constant example 2" in {
      testLogicalPlanCompile(
        "select 1 * 1",
        makeObj(
          "0" -> Multiply(constant(Data.Int(1)), constant(Data.Int(1)))
        )
      )
    }

    "compile simple constant with multiple named projections" in {
      testLogicalPlanCompile(
        "select 1.0 as a, 'abc' as b",
        makeObj(
          "a" -> constant(Data.Dec(1.0)),
          "b" -> constant(Data.Str("abc"))
        )
      )
    }
    

    "compile simple select *" in {
      testLogicalPlanCompile(
        "select * from foo",
        letOne('tmp0,
          read("foo"),
          letOne('tmp1,  // OK, this one is pretty silly
            free('tmp0),
            free('tmp1)
          )
        )
      )
    }
    
    "compile qualified select *" in {
      testLogicalPlanCompile(
        "select foo.* from foo",
        letOne('tmp0,
          read("foo"),
          letOne('tmp1,  // OK, this one is pretty silly
            free('tmp0),
            free('tmp1)
          )
        )
      )
    }.pendingUntilFixed  // parser bug?

    "compile simple select with unnamed projection which is just an identifier" in {
      testLogicalPlanCompile(
        "select name from city",
        letOne('tmp0,
          read("city"),
          letOne('tmp1,
            makeObj(
              "name" ->
                ObjectProject(
                  free('tmp0),
                  constant(Data.Str("name"))
                )
            ),
            free('tmp1)
          )
        )
      )
    }

    "compile simple 1-table projection when root identifier is also a projection" in {
      // 'foo' must be interpreted as a projection because only this interpretation is possible
      testLogicalPlanCompile(
        "select foo.bar from baz",        
        letOne('tmp0,
          read("baz"),
          letOne('tmp1,
            makeObj(
              "bar" ->
                ObjectProject(
                  ObjectProject(free('tmp0), constant(Data.Str("foo"))),
                  constant(Data.Str("bar"))
                )
            ),
            free('tmp1)
          )
        )
      )
    }

    "compile simple 1-table projection when root identifier is also a table ref" in {
      // 'foo' must be interpreted as a table reference because this interpretation is possible
      // and consistent with ANSI SQL.
      testLogicalPlanCompile(
        "select foo.bar from foo",
        letOne('tmp0,
          read("foo"),
          letOne('tmp1,
            makeObj(
              "bar" ->
                ObjectProject(free('tmp0), constant(Data.Str("bar")))
            ),
            free('tmp1)
          )
        )
      )
    }

    "compile two term addition from one table" in {
      testLogicalPlanCompile(
        "select foo + bar from baz",
        letOne('tmp0,
          read("baz"),
          letOne('tmp1,
            makeObj(
              "0" ->
                Add(
                  ObjectProject(free('tmp0), constant(Data.Str("foo"))),
                  ObjectProject(free('tmp0), constant(Data.Str("bar")))
                )
            ),
            free('tmp1)
          )
        )
      )
    }
    
    "compile negate" in {
      testLogicalPlanCompile(
        "select -foo from bar",
        letOne('tmp0,
          read("bar"),
          letOne('tmp1,
            makeObj(
              "0" ->
                Multiply(
                  constant(Data.Int(-1)),
                  ObjectProject(free('tmp0), constant(Data.Str("foo")))
                )
            ),
            free('tmp1)
          )
        )
      )
    }
    
    "compile concat" in {
      testLogicalPlanCompile(
        "select concat(foo, concat(' ', bar)) from baz",
        letOne('tmp0,
          read("baz"),
          letOne('tmp1,
            makeObj(
              "0" ->
                Concat(
                  ObjectProject(free('tmp0), constant(Data.Str("foo"))),
                  Concat(
                    constant(Data.Str(" ")),
                    ObjectProject(free('tmp0), constant(Data.Str("bar")))
                  )
                )
            ),
            free('tmp1)
          )
        )
      )
    }
    
    "compile like" in {
      testLogicalPlanCompile(
        "select * from foo where bar like 'a%'",
        letOne('tmp0,
          read("foo"),
          letOne('tmp1,
            Filter(
              free('tmp0),
              Like(
                ObjectProject(free('tmp0), constant(Data.Str("bar"))),
                constant(Data.Str("a%"))
              )
            ),
            letOne('tmp2,
              free('tmp1),
              free('tmp2)
            )
          )
        )
      )
    }
    
    "compile complex expression" in {
      testLogicalPlanCompile(
        "select avgTemp*9/5 + 32 from cities",
        letOne('tmp0,
          read("cities"),
          letOne('tmp1,
            makeObj(
              "0" ->
                Add(
                  Divide(
                    Multiply(
                      ObjectProject(free('tmp0), constant(Data.Str("avgTemp"))),
                      constant(Data.Int(9))
                    ),
                    constant(Data.Int(5))
                  ),
                  constant(Data.Int(32))
                )
            ),
            free('tmp1)
          )
        )
      )
    }
    
    "compile two term multiplication from two tables" in {
      testLogicalPlanCompile(<commit_msg>Add failing test for issue #14.<commit_after>package slamdata.engine

import slamdata.engine.analysis.fixplate._
import slamdata.engine.analysis._
import slamdata.engine.sql.SQLParser
import slamdata.engine.std._
import scalaz._
import org.specs2.mutable._
import org.specs2.matcher.{Matcher, Expectable}
import org.specs2.execute.PendingUntilFixed

@org.junit.runner.RunWith(classOf[org.specs2.runner.JUnitRunner])
class CompilerSpec extends Specification with CompilerHelpers {
  import StdLib._
  import structural._
  import agg._
  import math._
  import relations._
  import set._
  import string._
  import LogicalPlan._
  import SemanticAnalysis._

  "compiler" should {
    "compile simple constant example 1" in {
      testLogicalPlanCompile(
        "select 1",
        makeObj(
          "0" -> constant(Data.Int(1))
        )
      )
    }

    "compile simple constant example 2" in {
      testLogicalPlanCompile(
        "select 1 * 1",
        makeObj(
          "0" -> Multiply(constant(Data.Int(1)), constant(Data.Int(1)))
        )
      )
    }

    "compile simple constant with multiple named projections" in {
      testLogicalPlanCompile(
        "select 1.0 as a, 'abc' as b",
        makeObj(
          "a" -> constant(Data.Dec(1.0)),
          "b" -> constant(Data.Str("abc"))
        )
      )
    }
    

    "compile simple select *" in {
      testLogicalPlanCompile(
        "select * from foo",
        letOne('tmp0,
          read("foo"),
          letOne('tmp1,  // OK, this one is pretty silly
            free('tmp0),
            free('tmp1)
          )
        )
      )
    }
    
    "compile qualified select *" in {
      testLogicalPlanCompile(
        "select foo.* from foo",
        letOne('tmp0,
          read("foo"),
          letOne('tmp1,  // OK, this one is pretty silly
            free('tmp0),
            free('tmp1)
          )
        )
      )
    }.pendingUntilFixed  // parser bug?

    "compile simple select with unnamed projection which is just an identifier" in {
      testLogicalPlanCompile(
        "select name from city",
        letOne('tmp0,
          read("city"),
          letOne('tmp1,
            makeObj(
              "name" ->
                ObjectProject(
                  free('tmp0),
                  constant(Data.Str("name"))
                )
            ),
            free('tmp1)
          )
        )
      )
    }

    "compile simple 1-table projection when root identifier is also a projection" in {
      // 'foo' must be interpreted as a projection because only this interpretation is possible
      testLogicalPlanCompile(
        "select foo.bar from baz",        
        letOne('tmp0,
          read("baz"),
          letOne('tmp1,
            makeObj(
              "bar" ->
                ObjectProject(
                  ObjectProject(free('tmp0), constant(Data.Str("foo"))),
                  constant(Data.Str("bar"))
                )
            ),
            free('tmp1)
          )
        )
      )
    }

    "compile simple 1-table projection when root identifier is also a table ref" in {
      // 'foo' must be interpreted as a table reference because this interpretation is possible
      // and consistent with ANSI SQL.
      testLogicalPlanCompile(
        "select foo.bar from foo",
        letOne('tmp0,
          read("foo"),
          letOne('tmp1,
            makeObj(
              "bar" ->
                ObjectProject(free('tmp0), constant(Data.Str("bar")))
            ),
            free('tmp1)
          )
        )
      )
    }

    "compile two term addition from one table" in {
      testLogicalPlanCompile(
        "select foo + bar from baz",
        letOne('tmp0,
          read("baz"),
          letOne('tmp1,
            makeObj(
              "0" ->
                Add(
                  ObjectProject(free('tmp0), constant(Data.Str("foo"))),
                  ObjectProject(free('tmp0), constant(Data.Str("bar")))
                )
            ),
            free('tmp1)
          )
        )
      )
    }
    
    "compile negate" in {
      testLogicalPlanCompile(
        "select -foo from bar",
        letOne('tmp0,
          read("bar"),
          letOne('tmp1,
            makeObj(
              "0" ->
                Multiply(
                  constant(Data.Int(-1)),
                  ObjectProject(free('tmp0), constant(Data.Str("foo")))
                )
            ),
            free('tmp1)
          )
        )
      )
    }
    
    "compile concat" in {
      testLogicalPlanCompile(
        "select concat(foo, concat(' ', bar)) from baz",
        letOne('tmp0,
          read("baz"),
          letOne('tmp1,
            makeObj(
              "0" ->
                Concat(
                  ObjectProject(free('tmp0), constant(Data.Str("foo"))),
                  Concat(
                    constant(Data.Str(" ")),
                    ObjectProject(free('tmp0), constant(Data.Str("bar")))
                  )
                )
            ),
            free('tmp1)
          )
        )
      )
    }
    
    "compile like" in {
      testLogicalPlanCompile(
        "select * from foo where bar like 'a%'",
        letOne('tmp0,
          read("foo"),
          letOne('tmp1,
            Filter(
              free('tmp0),
              Like(
                ObjectProject(free('tmp0), constant(Data.Str("bar"))),
                constant(Data.Str("a%"))
              )
            ),
            letOne('tmp2,
              free('tmp1),
              free('tmp2)
            )
          )
        )
      )
    }
    
    "compile complex expression" in {
      testLogicalPlanCompile(
        "select avgTemp*9/5 + 32 from cities",
        letOne('tmp0,
          read("cities"),
          letOne('tmp1,
            makeObj(
              "0" ->
                Add(
                  Divide(
                    Multiply(
                      ObjectProject(free('tmp0), constant(Data.Str("avgTemp"))),
                      constant(Data.Int(9))
                    ),
                    constant(Data.Int(5))
                  ),
                  constant(Data.Int(32))
                )
            ),
            free('tmp1)
          )
        )
      )
    }

    "compile cross select *" in {
      testLogicalPlanCompile(
        "select * from person, car",
        letOne('tmp0, Cross(read("person"), read("car")),
          letOne('tmp1,
                 ObjectConcat(
                   ObjectProject(free('tmp0), constant(Data.Str("left"))),
                   ObjectProject(free('tmp0), constant(Data.Str("right")))
                 ),
            free('tmp1)
          )
        )
      )
    }

    "compile two term multiplication from two tables" in {
      testLogicalPlanCompile(
{'fraction_non_alphanumeric': 0.11394337174692819, 'fraction_numerical': 0.012439899259711517, 'mean_word_length': 1.315603463509454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, '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': '7311771', 'n_tokens_mistral': 3733, 'n_tokens_neox': 3555, 'n_words': 757}