Github user ravipesala commented on a diff in the pull request:

    https://github.com/apache/carbondata/pull/1433#discussion_r146761132
  
    --- Diff: 
integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/preaaggregate/PreAggregateUtil.scala
 ---
    @@ -0,0 +1,435 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You 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 org.apache.spark.sql.execution.command.preaaggregate
    +
    +import scala.collection.mutable.ListBuffer
    +
    +import org.apache.spark.SparkConf
    +import org.apache.spark.sql.{CarbonDatasourceHadoopRelation, CarbonEnv, 
SparkSession}
    +import org.apache.spark.sql.catalyst.TableIdentifier
    +import org.apache.spark.sql.catalyst.expressions.{Alias, 
AttributeReference, Cast}
    +import org.apache.spark.sql.catalyst.expressions.aggregate._
    +import org.apache.spark.sql.catalyst.plans.logical._
    +import org.apache.spark.sql.execution.command.{ColumnTableRelation, Field}
    +import org.apache.spark.sql.execution.datasources.LogicalRelation
    +import org.apache.spark.sql.hive.{CarbonRelation, CarbonSessionState}
    +import 
org.apache.spark.sql.hive.HiveExternalCatalog.{DATASOURCE_SCHEMA_NUMPARTS, 
DATASOURCE_SCHEMA_PART_PREFIX}
    +import org.apache.spark.sql.types.DataType
    +
    +import org.apache.carbondata.common.logging.{LogService, LogServiceFactory}
    +import org.apache.carbondata.core.constants.CarbonCommonConstants
    +import org.apache.carbondata.core.locks.{CarbonLockUtil, ICarbonLock, 
LockUsage}
    +import 
org.apache.carbondata.core.metadata.converter.ThriftWrapperSchemaConverterImpl
    +import org.apache.carbondata.core.metadata.schema.table.{CarbonTable, 
DataMapSchema}
    +import org.apache.carbondata.core.util.path.CarbonStorePath
    +import org.apache.carbondata.format.TableInfo
    +import 
org.apache.carbondata.spark.exception.MalformedCarbonCommandException
    +import org.apache.carbondata.spark.util.CommonUtil
    +
    +/**
    + * Utility class for keeping all the utility method for pre-aggregate
    + */
    +object PreAggregateUtil {
    +
    +  private val LOGGER = 
LogServiceFactory.getLogService(this.getClass.getCanonicalName)
    +
    +  def getParentCarbonTable(plan: LogicalPlan): CarbonTable = {
    +    plan match {
    +      case Aggregate(_, aExp, SubqueryAlias(_, l: LogicalRelation, _))
    +        if l.relation.isInstanceOf[CarbonDatasourceHadoopRelation] =>
    +        
l.relation.asInstanceOf[CarbonDatasourceHadoopRelation].carbonRelation.metaData.carbonTable
    +      case _ => throw new MalformedCarbonCommandException("table does not 
exist")
    +    }
    +  }
    +
    +  /**
    +   * Below method will be used to validate the select plan
    +   * and get the required fields from select plan
    +   * Currently only aggregate query is support any other type of query will
    +   * fail
    +   * @param plan
    +   * @param selectStmt
    +   * @return list of fields
    +   */
    +  def validateActualSelectPlanAndGetAttrubites(plan: LogicalPlan,
    +      selectStmt: String): Seq[Field] = {
    +    val list = scala.collection.mutable.LinkedHashSet.empty[Field]
    +    plan match {
    +      case Aggregate(_, aExp, SubqueryAlias(_, l: LogicalRelation, _))
    +        if l.relation.isInstanceOf[CarbonDatasourceHadoopRelation] =>
    +        val carbonTable = 
l.relation.asInstanceOf[CarbonDatasourceHadoopRelation].carbonRelation
    +          .metaData.carbonTable
    +        val parentTableName = 
carbonTable.getAbsoluteTableIdentifier.getCarbonTableIdentifier
    +          .getTableName
    +        val parentDatabaseName = 
carbonTable.getAbsoluteTableIdentifier.getCarbonTableIdentifier
    +          .getDatabaseName
    +        val parentTableId = 
carbonTable.getAbsoluteTableIdentifier.getCarbonTableIdentifier
    +          .getTableId
    +        if 
(!carbonTable.getTableInfo.getParentRelationIdentifiers.isEmpty) {
    +          throw new MalformedCarbonCommandException(
    +            "Pre Aggregation is not supported on Pre-Aggregated Table")
    +        }
    +        aExp.map {
    +          case Alias(attr: AggregateExpression, _) =>
    +            if (attr.isDistinct) {
    +              throw new MalformedCarbonCommandException(
    +                "Distinct is not supported On Pre Aggregation")
    +            }
    +            list ++= validateAggregateFunctionAndGetFields(carbonTable,
    +              attr.aggregateFunction,
    +              parentTableName,
    +              parentDatabaseName,
    +              parentTableId)
    +          case attr: AttributeReference =>
    +            list += getField(attr.name,
    +              attr.dataType,
    +              parentColumnId = 
carbonTable.getColumnByName(parentTableName, attr.name).getColumnId,
    +              parentTableName = parentTableName,
    +              parentDatabaseName = parentDatabaseName, parentTableId = 
parentTableId)
    +          case Alias(attr: AttributeReference, _) =>
    +            list += getField(attr.name,
    +              attr.dataType,
    +              parentColumnId = 
carbonTable.getColumnByName(parentTableName, attr.name).getColumnId,
    +              parentTableName = parentTableName,
    +              parentDatabaseName = parentDatabaseName, parentTableId = 
parentTableId)
    +          case _ =>
    +            throw new MalformedCarbonCommandException(s"Unsupported Select 
Statement:${
    +              selectStmt } ")
    +        }
    +        Some(carbonTable)
    +      case _ =>
    +        throw new MalformedCarbonCommandException(s"Unsupported Select 
Statement:${ selectStmt } ")
    +    }
    +    list.toSeq
    +  }
    +
    +  /**
    +   * Below method will be used to validate about the aggregate function
    +   * which is applied on select query.
    +   * Currently sum, max, min, count, avg is supported
    +   * in case of any other aggregate function it will throw error
    +   * In case of avg it will return two fields one for count
    +   * and other of sum of that column to support rollup
    +   * @param carbonTable
    +   * @param aggFunctions
    +   * @param parentTableName
    +   * @param parentDatabaseName
    +   * @param parentTableId
    +   * @return list of fields
    +   */
    +  def validateAggregateFunctionAndGetFields(carbonTable: CarbonTable,
    +      aggFunctions: AggregateFunction,
    +      parentTableName: String,
    +      parentDatabaseName: String,
    +      parentTableId: String) : scala.collection.mutable.ListBuffer[Field] 
= {
    +    val list = scala.collection.mutable.ListBuffer.empty[Field]
    +    aggFunctions match {
    +      case sum@Sum(attr: AttributeReference) =>
    +        list += getField(attr.name,
    +          attr.dataType,
    +          sum.prettyName,
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +      case sum@Sum(Cast(attr: AttributeReference, changeDataType: 
DataType)) =>
    +        list += getField(attr.name,
    +          changeDataType,
    +          sum.prettyName,
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +      case count@Count(Seq(attr: AttributeReference)) =>
    +        list += getField(attr.name,
    +          attr.dataType,
    +          count.prettyName,
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +      case min@Min(attr: AttributeReference) =>
    +        list += getField(attr.name,
    +          attr.dataType,
    +          min.prettyName,
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +      case min@Min(Cast(attr: AttributeReference, changeDataType: 
DataType)) =>
    +        list += getField(attr.name,
    +          changeDataType,
    +          min.prettyName,
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +      case max@Max(attr: AttributeReference) =>
    +        list += getField(attr.name,
    +          attr.dataType,
    +          max.prettyName,
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +      case max@Max(Cast(attr: AttributeReference, changeDataType: 
DataType)) =>
    +        list += getField(attr.name,
    +          changeDataType,
    +          max.prettyName,
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +      case Average(attr: AttributeReference) =>
    +        list += getField(attr.name,
    +          attr.dataType,
    +          "sum",
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +        list += getField(attr.name,
    +          attr.dataType,
    +          "count",
    +          carbonTable.getColumnByName(parentTableName, 
attr.name).getColumnId,
    +          parentTableName,
    +          parentDatabaseName, parentTableId = parentTableId)
    +      case Average(Cast(attr: AttributeReference, changeDataType: 
DataType)) =>
    --- End diff --
    
    I think it would be issue if it is used the changedDataType. Even while 
querying also we should need to check the datatype. Better don't take this type 
of aggfunction  in first phase.


---

Reply via email to