Return-Path: X-Original-To: apmail-drill-dev-archive@www.apache.org Delivered-To: apmail-drill-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id F1DB31885B for ; Wed, 2 Mar 2016 15:58:27 +0000 (UTC) Received: (qmail 69459 invoked by uid 500); 2 Mar 2016 15:58:27 -0000 Delivered-To: apmail-drill-dev-archive@drill.apache.org Received: (qmail 69404 invoked by uid 500); 2 Mar 2016 15:58:27 -0000 Mailing-List: contact dev-help@drill.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@drill.apache.org Delivered-To: mailing list dev@drill.apache.org Received: (qmail 69393 invoked by uid 99); 2 Mar 2016 15:58:27 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 02 Mar 2016 15:58:27 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 6236FDFDE2; Wed, 2 Mar 2016 15:58:27 +0000 (UTC) From: hsuanyi To: dev@drill.apache.org Reply-To: dev@drill.apache.org References: In-Reply-To: Subject: [GitHub] drill pull request: Drill 4372 review Content-Type: text/plain Message-Id: <20160302155827.6236FDFDE2@git1-us-west.apache.org> Date: Wed, 2 Mar 2016 15:58:27 +0000 (UTC) Github user hsuanyi commented on a diff in the pull request: https://github.com/apache/drill/pull/397#discussion_r54743281 --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java --- @@ -0,0 +1,571 @@ +/** + * 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.drill.exec.planner.sql; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; + +import org.apache.calcite.avatica.util.TimeUnit; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.sql.SqlCallBinding; +import org.apache.calcite.sql.SqlCharStringLiteral; +import org.apache.calcite.sql.SqlDynamicParam; +import org.apache.calcite.sql.SqlLiteral; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.SqlOperatorBinding; +import org.apache.calcite.sql.type.SqlReturnTypeInference; +import org.apache.calcite.sql.type.SqlTypeName; + +import org.apache.drill.common.expression.ExpressionPosition; +import org.apache.drill.common.expression.FunctionCall; +import org.apache.drill.common.expression.LogicalExpression; +import org.apache.drill.common.expression.MajorTypeInLogicalExpression; +import org.apache.drill.common.exceptions.UserException; +import org.apache.drill.common.types.TypeProtos; +import org.apache.drill.common.types.Types; +import org.apache.drill.exec.expr.TypeHelper; +import org.apache.drill.exec.expr.fn.DrillFuncHolder; +import org.apache.drill.exec.planner.logical.DrillConstExecutor; +import org.apache.drill.exec.resolver.FunctionResolver; +import org.apache.drill.exec.resolver.FunctionResolverFactory; + +import java.util.List; + +public class TypeInferenceUtils { + private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TypeInferenceUtils.class); + + public static final TypeProtos.MajorType UNKNOWN_TYPE = TypeProtos.MajorType.getDefaultInstance(); + private static ImmutableMap DRILL_TO_CALCITE_TYPE_MAPPING = + ImmutableMap. builder() + .put(TypeProtos.MinorType.INT, SqlTypeName.INTEGER) + .put(TypeProtos.MinorType.BIGINT, SqlTypeName.BIGINT) + .put(TypeProtos.MinorType.FLOAT4, SqlTypeName.FLOAT) + .put(TypeProtos.MinorType.FLOAT8, SqlTypeName.DOUBLE) + .put(TypeProtos.MinorType.VARCHAR, SqlTypeName.VARCHAR) + .put(TypeProtos.MinorType.BIT, SqlTypeName.BOOLEAN) + .put(TypeProtos.MinorType.DATE, SqlTypeName.DATE) + .put(TypeProtos.MinorType.DECIMAL9, SqlTypeName.DECIMAL) + .put(TypeProtos.MinorType.DECIMAL18, SqlTypeName.DECIMAL) + .put(TypeProtos.MinorType.DECIMAL28SPARSE, SqlTypeName.DECIMAL) + .put(TypeProtos.MinorType.DECIMAL38SPARSE, SqlTypeName.DECIMAL) + .put(TypeProtos.MinorType.TIME, SqlTypeName.TIME) + .put(TypeProtos.MinorType.TIMESTAMP, SqlTypeName.TIMESTAMP) + .put(TypeProtos.MinorType.VARBINARY, SqlTypeName.VARBINARY) + .put(TypeProtos.MinorType.INTERVALYEAR, SqlTypeName.INTERVAL_YEAR_MONTH) + .put(TypeProtos.MinorType.INTERVALDAY, SqlTypeName.INTERVAL_DAY_TIME) + .put(TypeProtos.MinorType.MAP, SqlTypeName.MAP) + .put(TypeProtos.MinorType.LIST, SqlTypeName.ARRAY) + .put(TypeProtos.MinorType.LATE, SqlTypeName.ANY) + // These are defined in the Drill type system but have been turned off for now + // .put(TypeProtos.MinorType.TINYINT, SqlTypeName.TINYINT) + // .put(TypeProtos.MinorType.SMALLINT, SqlTypeName.SMALLINT) + // Calcite types currently not supported by Drill, nor defined in the Drill type list: + // - CHAR, SYMBOL, MULTISET, DISTINCT, STRUCTURED, ROW, OTHER, CURSOR, COLUMN_LIST + .build(); + + private static ImmutableMap CALCITE_TO_DRILL_MAPPING = + ImmutableMap. builder() + .put(SqlTypeName.INTEGER, TypeProtos.MinorType.INT) + .put(SqlTypeName.BIGINT, TypeProtos.MinorType.BIGINT) + .put(SqlTypeName.FLOAT, TypeProtos.MinorType.FLOAT4) + .put(SqlTypeName.DOUBLE, TypeProtos.MinorType.FLOAT8) + .put(SqlTypeName.VARCHAR, TypeProtos.MinorType.VARCHAR) + .put(SqlTypeName.BOOLEAN, TypeProtos.MinorType.BIT) + .put(SqlTypeName.DATE, TypeProtos.MinorType.DATE) + .put(SqlTypeName.TIME, TypeProtos.MinorType.TIME) + .put(SqlTypeName.TIMESTAMP, TypeProtos.MinorType.TIMESTAMP) + .put(SqlTypeName.VARBINARY, TypeProtos.MinorType.VARBINARY) + .put(SqlTypeName.INTERVAL_YEAR_MONTH, TypeProtos.MinorType.INTERVALYEAR) + .put(SqlTypeName.INTERVAL_DAY_TIME, TypeProtos.MinorType.INTERVALDAY) + .put(SqlTypeName.CHAR, TypeProtos.MinorType.VARCHAR) + + // The following types are not added due to a variety of reasons: + // (1) Disabling decimal type + //.put(SqlTypeName.DECIMAL, TypeProtos.MinorType.DECIMAL9) + //.put(SqlTypeName.DECIMAL, TypeProtos.MinorType.DECIMAL18) + //.put(SqlTypeName.DECIMAL, TypeProtos.MinorType.DECIMAL28SPARSE) + //.put(SqlTypeName.DECIMAL, TypeProtos.MinorType.DECIMAL38SPARSE) + + // (2) These 2 types are defined in the Drill type system but have been turned off for now + // .put(SqlTypeName.TINYINT, TypeProtos.MinorType.TINYINT) + // .put(SqlTypeName.SMALLINT, TypeProtos.MinorType.SMALLINT) + + // (3) Calcite types currently not supported by Drill, nor defined in the Drill type list: + // - SYMBOL, MULTISET, DISTINCT, STRUCTURED, ROW, OTHER, CURSOR, COLUMN_LIST + // .put(SqlTypeName.MAP, TypeProtos.MinorType.MAP) + // .put(SqlTypeName.ARRAY, TypeProtos.MinorType.LIST) + .build(); + + /** + * Given a Drill's TypeProtos.MinorType, return a Calcite's corresponding SqlTypeName + */ + public static SqlTypeName getCalciteTypeFromDrillType(final TypeProtos.MinorType type) { + return DRILL_TO_CALCITE_TYPE_MAPPING.get(type); + } + + /** + * Given a Calcite's RelDataType, return a Drill's corresponding TypeProtos.MinorType + */ + public static TypeProtos.MinorType getDrillTypeFromCalciteType(final RelDataType relDataType) { + final SqlTypeName sqlTypeName = relDataType.getSqlTypeName(); + TypeProtos.MinorType minorType = CALCITE_TO_DRILL_MAPPING.get(sqlTypeName); + if(minorType == null) { + minorType = TypeProtos.MinorType.LATE; + } + return minorType; + } + + /** + * Give the name and DrillFuncHolder list, return the inference mechanism. + */ + public static SqlReturnTypeInference getDrillSqlReturnTypeInference( + final String name, + final List functions) { + switch(name.toUpperCase()) { + case "DATE_PART": + return DrillDatePartSqlReturnTypeInference.INSTANCE; --- End diff -- Right, most of them are singleton, except for the default one, which needs a DrillFuncHolder list to be associated with them. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---