[ 
https://issues.apache.org/jira/browse/DRILL-5419?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15987722#comment-15987722
 ] 

ASF GitHub Bot commented on DRILL-5419:
---------------------------------------

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

    https://github.com/apache/drill/pull/819#discussion_r113795987
  
    --- Diff: 
exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/DrillSubstringFuncHolder.java
 ---
    @@ -0,0 +1,94 @@
    +/*
    + * 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.expr.fn;
    +
    +import org.apache.drill.common.expression.LogicalExpression;
    +import org.apache.drill.common.expression.ValueExpressions;
    +import org.apache.drill.common.types.TypeProtos;
    +import org.apache.drill.common.types.Types;
    +import org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers;
    +
    +import java.util.List;
    +
    +/**
    + * Function holder for functions with function scope set as
    + * {@link 
org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope#SUBSTRING}.
    + */
    +public class DrillSubstringFuncHolder extends DrillSimpleFuncHolder {
    +
    +  public DrillSubstringFuncHolder(FunctionAttributes functionAttributes, 
FunctionInitializer initializer) {
    +    super(functionAttributes, initializer);
    +  }
    +
    +  /**
    +   * Defines function return type and calculates output precision.
    +   *
    +   * <b>substring(source, regexp)<b/>
    +   * <ul/>If input precision is known, output precision is max varchar 
value {@link Types#MAX_VARCHAR_LENGTH}.<ul/>
    +   *
    +   * <b>substring(source, offset)<b/>
    +   * <ul>If input precision is unknown then output precision is max 
varchar value {@link Types#MAX_VARCHAR_LENGTH}.<ul/>
    +   * <ul>If input precision is known, output precision is input precision 
minus offset plus 1
    +   * since offset starts from 1.<ul/>
    +   * <ul>If offset value is greater than input precision or offset value 
is corrupted (less then equals zero),
    +   * output precision is zero.<ul/>
    +   *
    +   * <b>substring(source, offset, length)<b/>
    +   * <ul>If offset value is greater than input precision or offset or 
length values are corrupted (less then equals zero),
    +   * output precision is zero.<ul/>
    +   * <ul>If source length (including offset) is less than substring 
length, output precision is source length (including offset).<ul/>
    +   * <ul>If source length (including offset) is greater than substring 
length, output precision is substring length.<ul/>
    +   *
    +   * @param logicalExpressions logical expressions
    +   * @return return type
    +   */
    +  @Override
    +  public TypeProtos.MajorType getReturnType(List<LogicalExpression> 
logicalExpressions) {
    +    TypeProtos.MajorType.Builder builder = 
TypeProtos.MajorType.newBuilder()
    +        .setMinorType(getReturnType().getMinorType())
    +        .setMode(getReturnTypeDataMode(logicalExpressions));
    +
    +    int sourceLength = 
logicalExpressions.get(0).getMajorType().hasPrecision() ?
    +        logicalExpressions.get(0).getMajorType().getPrecision() : 
Types.MAX_VARCHAR_LENGTH;
    --- End diff --
    
    Seems there are two cases, for precision == MAX_VARCHAR_LENGTH:
    1) precision is not set for a varchar column. Hence we may return 
MAX_VARCHAR_LENGTH, if it's asked for the maximum length of any value in the 
column.
    2) precision is explicitly set to MAX_VARCHAR_LENGTH, because of certain 
calculation.
    
    Do we have a way to differentiate the above two cases? The code here seems 
to set "sourceLength = MAX_VARCHAR_LENGTH" if source does not have precision, 
then sourceLength could be set to the return type. In other words, from a 
varchar not knowning it's length ==> a varchar with MAX_VARCHAR_LENGTH.  
    
    I think it might make sense to differentiate if we want to have better 
control of memory allocation for varchar column in the future: we may use 
different way to estimate the memory size if we do not have any idea about 
varchar lenght, from the case where we know the varchar's max length is 
MAX_VARCHAR_LENGTH.
    



> Calculate return string length for literals & some string functions
> -------------------------------------------------------------------
>
>                 Key: DRILL-5419
>                 URL: https://issues.apache.org/jira/browse/DRILL-5419
>             Project: Apache Drill
>          Issue Type: Bug
>    Affects Versions: 1.9.0
>            Reporter: Arina Ielchiieva
>            Assignee: Arina Ielchiieva
>         Attachments: version_with_cast.JPG
>
>
> Though Drill is schema-less and cannot determine in advance what the length 
> of the column should be but if query has an explicit type/length specified, 
> Drill should return correct column length.
> For example, JDBC / ODBC Driver is ALWAYS returning 64K as the length of a 
> varchar or char even if casts are applied.
> Changes:
> *LITERALS*
> String literals length is the same as actual literal length.
> Example: for 'aaa' return length is 3.
> *CAST*
> Return length is the one indicated in cast expression. This also applies when 
> user has created view where each string columns was casted to varchar with 
> some specific length.
> This length will be returned to the user without need to apply cast one more 
> time. Below mentioned functions can take leverage of underlying varchar 
> length and calculate return length.
> *LOWER, UPPER, INITCAP, REVERSE, FIRST_VALUE, LAST_VALUE* 
> Return length is underlying column length, i.e. if column is known, the same 
> length will be returned.
> Example:
> lower(cast(col as varchar(30))) will return 30.
> lower(col) will return max varchar length, since we don't know actual column 
> length.
> *LAG, LEAD*
> Return length is underlying column length but column type will be nullable.
> *LPAD, RPAD*
> Pads the string to the length specified. Return length is this specified 
> length. 
> *CONCAT, CONCAT OPERATOR (||)*
> Return length is sum of underlying columns length. If length is greater then 
> varchar max length,  varchar max length is returned.
> *SUBSTR, SUBSTRING, LEFT, RIGHT*
> Calculates return length according to each function substring rules, for 
> example, taking into account how many char should be substracted.
> *IF EXPRESSIONS (CASE STATEMENT, COALESCE), UNION OPERATOR*
> When combining string columns with different length, return length is max 
> from source columns.



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

Reply via email to