diff --git a/core/src/main/scala/com/yahoo/maha/core/fact/Fact.scala b/core/src/main/scala/com/yahoo/maha/core/fact/Fact.scala index a08b6a978..f59e182d6 100644 --- a/core/src/main/scala/com/yahoo/maha/core/fact/Fact.scala +++ b/core/src/main/scala/com/yahoo/maha/core/fact/Fact.scala @@ -843,6 +843,23 @@ case class FactBuilder private[fact](private val baseFact: Fact, private var tab mutableDiscardingSet.toSet } + private[this] def remapMultiplier( + costMultiplierMap: Map[RequestType, CostMultiplier] + , costMultiplier: Option[BigDecimal] = None): Map[RequestType, CostMultiplier] = { + costMultiplierMap.map(rTypeMult => + rTypeMult._1 -> { + val adjustedLRL: LongRangeLookup[BigDecimal] = + LongRangeLookup( + rTypeMult._2.rows.list.map( + row => (row._1, row._2 * costMultiplier.getOrElse(1)) + ) + ) + CostMultiplier(adjustedLRL) + } + + ) + } + def withNewGrain(name: String , from: String , grain: Grain @@ -1269,7 +1286,8 @@ case class FactBuilder private[fact](private val baseFact: Fact, private var tab , forceFilters: Set[ForceFilter] = Set.empty , resetAliasIfNotPresent: Boolean = false , availableOnwardsDate : Option[String] = None - , underlyingTableName: Option[String] = None) : FactBuilder = { + , underlyingTableName: Option[String] = None + , costMultiplier: Option[BigDecimal] = None) : FactBuilder = { require(tableMap.nonEmpty, "no table to create subset from") require(tableMap.contains(from), s"from table not valid $from") require(!tableMap.contains(name), s"table $name already exists") @@ -1329,6 +1347,9 @@ case class FactBuilder private[fact](private val baseFact: Fact, private var tab case _ => ddlAnnotation } + + val remappedMultiplier: Map[RequestType, CostMultiplier] = remapMultiplier(fromTable.costMultiplierMap, costMultiplier) + tableMap = tableMap + (name -> new FactTable( name @@ -1341,7 +1362,7 @@ case class FactBuilder private[fact](private val baseFact: Fact, private var tab , Option(fromTable) , fromTable.annotations , newDDLAnnotation - , fromTable.costMultiplierMap + , remappedMultiplier , newForceFilters , fromTable.defaultCardinality , fromTable.defaultRowCount @@ -1442,6 +1463,7 @@ case class FactBuilder private[fact](private val baseFact: Fact, private var tab val newGrain = grain.getOrElse(fromTable.grain) val newForceFilters = if(forceFilters.isEmpty) fromTable.forceFilters else forceFilters + val remappedMultiplier: Map[RequestType, CostMultiplier] = remapMultiplier(fromTable.costMultiplierMap, costMultiplier) tableMap = tableMap + (name -> new FactTable( @@ -1455,7 +1477,7 @@ case class FactBuilder private[fact](private val baseFact: Fact, private var tab , Option(fromTable) , fromTable.annotations ++ overrideAnnotations , ddlAnnotations - , fromTable.costMultiplierMap + , remappedMultiplier , newForceFilters , fromTable.defaultCardinality , fromTable.defaultRowCount diff --git a/core/src/test/scala/com/yahoo/maha/core/ColumnTest.scala b/core/src/test/scala/com/yahoo/maha/core/ColumnTest.scala index d78002360..a8deede06 100644 --- a/core/src/test/scala/com/yahoo/maha/core/ColumnTest.scala +++ b/core/src/test/scala/com/yahoo/maha/core/ColumnTest.scala @@ -3,7 +3,7 @@ package com.yahoo.maha.core import com.yahoo.maha.core.dimension.{ConstDimCol, DimCol, HivePartDimCol} -import com.yahoo.maha.core.fact.{ConstFactCol, DruidConstDerFactCol, HiveDerFactCol, NoopRollup} +import com.yahoo.maha.core.fact.{ConstFactCol, DruidConstDerFactCol, HiveDerFactCol, NoopRollup, PostgresDerFactCol} import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers @@ -69,4 +69,20 @@ class ColumnTest extends AnyFunSuite with Matchers { } } } + + test("PostgresDerFactCol test") { + ColumnContext.withColumnContext { implicit cc: ColumnContext => + import PostgresExpression._ + DimCol("dimCol", IntType()) + val col = PostgresDerFactCol("postgres_ctr_copywith_test", DecType(), "{clicks}" /- "{impressions}" * "1000") + ColumnContext.withColumnContext { + implicit cc:ColumnContext=> + col.copyWith(cc, Map.empty, true) + } + ColumnContext.withColumnContext { + implicit cc:ColumnContext=> + col.copyWith(cc, Map.empty, false) + } + } + } } diff --git a/core/src/test/scala/com/yahoo/maha/core/fact/NewRollupFactTest.scala b/core/src/test/scala/com/yahoo/maha/core/fact/NewRollupFactTest.scala index 86385e7ad..3aec99510 100644 --- a/core/src/test/scala/com/yahoo/maha/core/fact/NewRollupFactTest.scala +++ b/core/src/test/scala/com/yahoo/maha/core/fact/NewRollupFactTest.scala @@ -7,7 +7,7 @@ import com.yahoo.maha.core.NoopSchema.NoopSchema import com.yahoo.maha.core._ import com.yahoo.maha.core.ddl.HiveDDLAnnotation import com.yahoo.maha.core.dimension.DimCol -import com.yahoo.maha.core.request.{RequestType, SyncRequest} +import com.yahoo.maha.core.request.{AsyncRequest, RequestType, SyncRequest} /** * Created by jians on 10/20/15. @@ -295,6 +295,18 @@ class NewRollupFactTest extends BaseFactTest { require(bcOption.isDefined, "Failed to get candidates!") assert(bcOption.get.facts.values.exists( f => f.fact.name == "fact2") === true) } + + test("Create a new rollup with a different cost basis") { + val fact = fact1 + fact.newRollUp("fact2", "fact1", Set("ad_group_id"), schemas = Set(AdvertiserSchema, ResellerSchema), availableOnwardsDate = Some(toDate), costMultiplier = Some(0.5)) + + val costMultMap1 = publicFact(fact).facts("fact1").costMultiplierMap + val costMultMap2 = publicFact(fact).facts("fact2").costMultiplierMap + assert(costMultMap1(SyncRequest).rows.list.head._2 == 1 && costMultMap2(SyncRequest).rows.list.head._2 == 0.5) + assert(costMultMap1(AsyncRequest).rows.list.head._2 == 1 && costMultMap2(AsyncRequest).rows.list.head._2 == 0.5) + } + + } diff --git a/core/src/test/scala/com/yahoo/maha/core/fact/createSubsetTest.scala b/core/src/test/scala/com/yahoo/maha/core/fact/createSubsetTest.scala index d2c6a04ac..4241ebd02 100644 --- a/core/src/test/scala/com/yahoo/maha/core/fact/createSubsetTest.scala +++ b/core/src/test/scala/com/yahoo/maha/core/fact/createSubsetTest.scala @@ -6,7 +6,7 @@ import com.yahoo.maha.core.CoreSchema._ import com.yahoo.maha.core.NoopSchema.NoopSchema import com.yahoo.maha.core.ddl.HiveDDLAnnotation import com.yahoo.maha.core.dimension.DimCol -import com.yahoo.maha.core.request.SyncRequest +import com.yahoo.maha.core.request.{AsyncRequest, SyncRequest} import com.yahoo.maha.core.{ColumnContext, DailyGrain, InFilter, InFilterOperation, IntType, OracleEngine, PrimaryKey, StrType} /** @@ -165,4 +165,14 @@ class createSubsetTest extends BaseFactTest { require(bcOption.isDefined, "Failed to get candidates!") assert(bcOption.get.facts.keys.exists(_ == "fact2") === true, "create subset failed") } + + test("Create a subset with a different cost basis") { + val fact = fact1 + fact.createSubset("fact2", "fact1", Set("clicks"), schemas = Set(AdvertiserSchema, ResellerSchema), availableOnwardsDate = Some(toDate), costMultiplier = Some(0.5)) + + val costMultMap1 = publicFact(fact).facts("fact1").costMultiplierMap + val costMultMap2 = publicFact(fact).facts("fact2").costMultiplierMap + assert(costMultMap1(SyncRequest).rows.list.head._2 == 1 && costMultMap2(SyncRequest).rows.list.head._2 == 0.5) + assert(costMultMap1(AsyncRequest).rows.list.head._2 == 1 && costMultMap2(AsyncRequest).rows.list.head._2 == 0.5) + } }