Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Threaded View
Drill, mail # dev - abs() function implementation


Copy link to this message
-
abs() function implementation
Tanujit Ghosh 2013-08-22, 04:21
Hi,

I'm trying to implement the abs() function within drill.

Now the abs() function can work with int,bigint as well as other numeric
types.

so i have done this, but this is failing as we cannot register twice. How
do we handle this situation where the corresponding function implementation
is called based on the input parameter data type.

I'm bit lost here.

@FunctionTemplate(name = "absolute_int",
    scope = FunctionTemplate.FunctionScope.SIMPLE,
    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
  public static class AbsoluteInt implements DrillFunc {

    @Param IntHolder input;
    @Output IntHolder out;

    public void setup(RecordBatch b){}

    public void eval(){
      out.value = Math.abs(input.value);
    }

    public static class Provider implements CallProvider {

      @Override
      public FunctionDefinition[] getFunctionDefintions() {
        return new FunctionDefinition[] {
          FunctionDefinition.simple("absolute_int",
            new BasicArgumentValidator(new Arg(Types.required(
TypeProtos.MinorType.INT))),
            new OutputTypeDeterminer.SameAsFirstInput(),
            "abs")
        };
      }
    }
  }

  @FunctionTemplate(name = "absolute_bigint",
    scope = FunctionTemplate.FunctionScope.SIMPLE,
    nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
  public static class AbsoluteBigInt implements DrillFunc {

    @Param BigIntHolder input;
    @Output BigIntHolder out;

    public void setup(RecordBatch b){}

    public void eval(){
      out.value = Math.abs(input.value);
    }

    public static class Provider implements CallProvider {

      @Override
      public FunctionDefinition[] getFunctionDefintions() {
        return new FunctionDefinition[] {
          FunctionDefinition.simple("absolute_bigint",
            new BasicArgumentValidator(new
Arg(Types.required(TypeProtos.MinorType.BIGINT))),
            new OutputTypeDeterminer.SameAsFirstInput(),
            "abs")
        };
      }
    }
  }
Thanks and Regards,
Tanujit