Github user arina-ielchiieva commented on a diff in the pull request:

    https://github.com/apache/drill/pull/819#discussion_r113912681
  
    --- 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 --
    
    For limit 0 queries I can determine if we know precision or not. But for 
regular queries, it's not always possible.
    Example: `select substring(cast(id as int), 1, 5) from t`
    Before length calculation, Drill implicitly casts int to varchar using max 
varchar length [1].
    Thus when it comes to substring length calculation, I receive varchar with 
max varchar precision and I don't if this is real precision or just the one 
used in implicit cast. I have treated unknown precision as max varchar length 
to sync return type output between limit 0 and regular queries.
    
    With this change we might not have good control of memory allocation for 
varchar column, so I have excluded substring / substr / left / right functions 
length calculation from this PR. Jira DRILL-5476 is created to address length 
calculation logic for these functions later.
    
    [1] 
https://github.com/apache/drill/blob/master/exec/java-exec/src/main/java/org/apache/drill/exec/expr/ExpressionTreeMaterializer.java#L219


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to