<commit_before>/*
 * Copyright (C) 2012 The Regents of The University California.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package shark.memstore2

import java.sql.Timestamp
import java.nio.ByteBuffer
import java.nio.ByteOrder

import org.apache.hadoop.hive.serde2.io.ByteWritable
import org.apache.hadoop.hive.serde2.io.DoubleWritable
import org.apache.hadoop.hive.serde2.io.ShortWritable
import org.apache.hadoop.hive.serde2.`lazy`.ByteArrayRef
import org.apache.hadoop.hive.serde2.`lazy`.LazyBinary
import org.apache.hadoop.hive.serde2.`lazy`.LazyFactory
import org.apache.hadoop.hive.serde2.`lazy`.objectinspector.primitive.LazyPrimitiveObjectInspectorFactory
import org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryBinary
import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector
import org.apache.hadoop.hive.serde2.objectinspector.primitive._
import org.apache.hadoop.io._

import org.scalatest.FunSuite
import collection.mutable.{Set, HashSet}

import shark.memstore2.buffer.ByteBufferReader
import shark.memstore2.column._


class ColumnIteratorSuite extends FunSuite {

  val PARALLEL_MODE = true

  test("void column") {
    val builder = new VoidColumnBuilder
    builder.initialize(5)
    builder.append(null, null)
    builder.appendNull()
    builder.append(null, null)
    val buf = builder.build

    val bufreader = ByteBufferReader.createUnsafeReader(buf)
    val columnType = bufreader.getLong().toInt

    val factory = ColumnIterator.getFactory(columnType)
    assert(factory.createIterator(bufreader).getClass === classOf[VoidColumnIterator.Default])


    val iter = new VoidColumnIterator.Default(ByteBufferReader.createUnsafeReader(buf))
    iter.next()
    assert(iter.current == NullWritable.get())
    iter.next()
    assert(iter.current == NullWritable.get())
    iter.next()
    assert(iter.current == NullWritable.get())
  }

  test("boolean column") {
    var builder = new BooleanColumnBuilder
    testColumn(
      Array[java.lang.Boolean](true, false, true, true, true),
      builder,
      PrimitiveObjectInspectorFactory.writableBooleanObjectInspector,
      classOf[BooleanColumnIterator.Default])
    assert(builder.stats.min === false)
    assert(builder.stats.max === true)

    testColumn(
      Array[java.lang.Boolean](null, false, null, true, true),
      builder,
      PrimitiveObjectInspectorFactory.writableBooleanObjectInspector,
      classOf[BooleanColumnIterator.Default],
      true)
    assert(builder.stats.min === false)
    assert(builder.stats.max === true)
  }

  test("byte column") {
    val builder = new ByteColumnBuilder
    testColumn(
      Array[java.lang.Byte](1.toByte, 2.toByte, 15.toByte, 55.toByte, 0.toByte, 40.toByte),
      builder,
      PrimitiveObjectInspectorFactory.writableByteObjectInspector,
      classOf[ByteColumnIterator.Default])
    assert(builder.stats.min === 0.toByte)
    assert(builder.stats.max === 55.toByte)

    testColumn(
      Array[java.lang.Byte](null, 2.toByte, 15.toByte, null, 0.toByte, null),
      builder,
      PrimitiveObjectInspectorFactory.writableByteObjectInspector,
      classOf[ByteColumnIterator.Default],
      true)
    assert(builder.stats.min === 0.toByte)
    assert(builder.stats.max === 15.toByte)
  }

  test("short column") {
    val builder = new ShortColumnBuilder
    testColumn(
      Array[java.lang.Short](1.toShort, 2.toShort, -15.toShort, 355.toShort, 0.toShort, 40.toShort),
      builder,
      PrimitiveObjectInspectorFactory.writableShortObjectInspector,
      classOf[ShortColumnIterator.Default])
    assert(builder.stats.min === -15.toShort)
    assert(builder.stats.max === 355.toShort)

    testColumn(
      Array[java.lang.Short](1.toShort, 2.toShort, -15.toShort, null, 0.toShort, null),
      builder,
      PrimitiveObjectInspectorFactory.writableShortObjectInspector,
      classOf[ShortColumnIterator.Default],
      true)
    assert(builder.stats.min === -15.toShort)
    assert(builder.stats.max === 2.toShort)
  }

  test("dictionary encoding Int") {
    val l = List[Int](1,22,30,4)
    val d = new Dictionary(l)

    assert(d.get(0) == 1)

    val buf = ByteBuffer.allocate(2048)
    buf.order(ByteOrder.nativeOrder())
    buf.putInt(5);
    buf.rewind
    assert(5 == buf.getInt())
    buf.rewind

    DictionarySerializer.writeToBuffer(buf, d)
    buf.rewind

    val bbr = ByteBufferReader.createUnsafeReader(buf)

    val newd = DictionarySerializer.readFromBuffer(bbr)

    assert(d.get(0) == newd.get(0))
    assert(d.get(3) == newd.get(3))
  }

  test("int column") {
    val builder = new IntColumnBuilder
    testColumn(
      Array[java.lang.Integer](0, 1, 2, 5, 134, -12, 1, 0, 99, 1),
      builder,
      PrimitiveObjectInspectorFactory.writableIntObjectInspector,
      classOf[DictionaryEncodedIntColumnIterator.Default])
    assert(builder.stats.min === -12)
    assert(builder.stats.max === 134)

    testColumn(
      Array[java.lang.Integer](null, 1, 2, 5, 134, -12, null, 0, 99, 1),
      builder,
      PrimitiveObjectInspectorFactory.writableIntObjectInspector,
      classOf[DictionaryEncodedIntColumnIterator.Default],
      true)
    assert(builder.stats.min === -12)
    assert(builder.stats.max === 134)
    assert(builder.isCompressed == true)

<commit_msg>one more test to check if compression is being used<commit_after>/*
 * Copyright (C) 2012 The Regents of The University California.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package shark.memstore2

import java.sql.Timestamp
import java.nio.ByteBuffer
import java.nio.ByteOrder

import org.apache.hadoop.hive.serde2.io.ByteWritable
import org.apache.hadoop.hive.serde2.io.DoubleWritable
import org.apache.hadoop.hive.serde2.io.ShortWritable
import org.apache.hadoop.hive.serde2.`lazy`.ByteArrayRef
import org.apache.hadoop.hive.serde2.`lazy`.LazyBinary
import org.apache.hadoop.hive.serde2.`lazy`.LazyFactory
import org.apache.hadoop.hive.serde2.`lazy`.objectinspector.primitive.LazyPrimitiveObjectInspectorFactory
import org.apache.hadoop.hive.serde2.lazybinary.LazyBinaryBinary
import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector
import org.apache.hadoop.hive.serde2.objectinspector.primitive._
import org.apache.hadoop.io._

import org.scalatest.FunSuite
import collection.mutable.{Set, HashSet}

import shark.memstore2.buffer.ByteBufferReader
import shark.memstore2.column._


class ColumnIteratorSuite extends FunSuite {

  val PARALLEL_MODE = true

  test("void column") {
    val builder = new VoidColumnBuilder
    builder.initialize(5)
    builder.append(null, null)
    builder.appendNull()
    builder.append(null, null)
    val buf = builder.build

    val bufreader = ByteBufferReader.createUnsafeReader(buf)
    val columnType = bufreader.getLong().toInt

    val factory = ColumnIterator.getFactory(columnType)
    assert(factory.createIterator(bufreader).getClass === classOf[VoidColumnIterator.Default])


    val iter = new VoidColumnIterator.Default(ByteBufferReader.createUnsafeReader(buf))
    iter.next()
    assert(iter.current == NullWritable.get())
    iter.next()
    assert(iter.current == NullWritable.get())
    iter.next()
    assert(iter.current == NullWritable.get())
  }

  test("boolean column") {
    var builder = new BooleanColumnBuilder
    testColumn(
      Array[java.lang.Boolean](true, false, true, true, true),
      builder,
      PrimitiveObjectInspectorFactory.writableBooleanObjectInspector,
      classOf[BooleanColumnIterator.Default])
    assert(builder.stats.min === false)
    assert(builder.stats.max === true)

    testColumn(
      Array[java.lang.Boolean](null, false, null, true, true),
      builder,
      PrimitiveObjectInspectorFactory.writableBooleanObjectInspector,
      classOf[BooleanColumnIterator.Default],
      true)
    assert(builder.stats.min === false)
    assert(builder.stats.max === true)
  }

  test("byte column") {
    val builder = new ByteColumnBuilder
    testColumn(
      Array[java.lang.Byte](1.toByte, 2.toByte, 15.toByte, 55.toByte, 0.toByte, 40.toByte),
      builder,
      PrimitiveObjectInspectorFactory.writableByteObjectInspector,
      classOf[ByteColumnIterator.Default])
    assert(builder.stats.min === 0.toByte)
    assert(builder.stats.max === 55.toByte)

    testColumn(
      Array[java.lang.Byte](null, 2.toByte, 15.toByte, null, 0.toByte, null),
      builder,
      PrimitiveObjectInspectorFactory.writableByteObjectInspector,
      classOf[ByteColumnIterator.Default],
      true)
    assert(builder.stats.min === 0.toByte)
    assert(builder.stats.max === 15.toByte)
  }

  test("short column") {
    val builder = new ShortColumnBuilder
    testColumn(
      Array[java.lang.Short](1.toShort, 2.toShort, -15.toShort, 355.toShort, 0.toShort, 40.toShort),
      builder,
      PrimitiveObjectInspectorFactory.writableShortObjectInspector,
      classOf[ShortColumnIterator.Default])
    assert(builder.stats.min === -15.toShort)
    assert(builder.stats.max === 355.toShort)

    testColumn(
      Array[java.lang.Short](1.toShort, 2.toShort, -15.toShort, null, 0.toShort, null),
      builder,
      PrimitiveObjectInspectorFactory.writableShortObjectInspector,
      classOf[ShortColumnIterator.Default],
      true)
    assert(builder.stats.min === -15.toShort)
    assert(builder.stats.max === 2.toShort)
  }

  test("dictionary encoding Int") {
    val l = List[Int](1,22,30,4)
    val d = new Dictionary(l)

    assert(d.get(0) == 1)

    val buf = ByteBuffer.allocate(2048)
    buf.order(ByteOrder.nativeOrder())
    buf.putInt(5);
    buf.rewind
    assert(5 == buf.getInt())
    buf.rewind

    DictionarySerializer.writeToBuffer(buf, d)
    buf.rewind

    val bbr = ByteBufferReader.createUnsafeReader(buf)

    val newd = DictionarySerializer.readFromBuffer(bbr)

    assert(d.get(0) == newd.get(0))
    assert(d.get(3) == newd.get(3))
  }

  test("int column") {
    val builder = new IntColumnBuilder
    testColumn(
      Array[java.lang.Integer](0, 1, 2, 5, 134, -12, 1, 0, 99, 1),
      builder,
      PrimitiveObjectInspectorFactory.writableIntObjectInspector,
      classOf[DictionaryEncodedIntColumnIterator.Default])
    assert(builder.stats.min === -12)
    assert(builder.stats.max === 134)
    assert(builder.isCompressed == true)

    testColumn(
      Array[java.lang.Integer](null, 1, 2, 5, 134, -12, null, 0, 99, 1),
      builder,
      PrimitiveObjectInspectorFactory.writableIntObjectInspector,
      classOf[DictionaryEncodedIntColumnIterator.Default],
      true)
    assert(builder.stats.min === -12)
    assert(builder.stats.max === 134)
    assert(builder.isCompressed == true)


{'fraction_non_alphanumeric': 0.11371208885091841, 'fraction_numerical': 0.02016232379325075, 'mean_word_length': 3.849212924606462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5560230', 'n_tokens_mistral': 3780, 'n_tokens_neox': 3592, 'n_words': 727}