Пример #1
0
 private SqlOperator toOp(SqlIdentifier name, Function function) {
   List<RelDataType> argTypes = new ArrayList<RelDataType>();
   List<SqlTypeFamily> typeFamilies = new ArrayList<SqlTypeFamily>();
   for (FunctionParameter o : function.getParameters()) {
     final RelDataType type = o.getType(typeFactory);
     argTypes.add(type);
     typeFamilies.add(Util.first(type.getSqlTypeName().getFamily(), SqlTypeFamily.ANY));
   }
   final RelDataType returnType;
   if (function instanceof ScalarFunction) {
     return new SqlUserDefinedFunction(
         name,
         ReturnTypes.explicit(Schemas.proto((ScalarFunction) function)),
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         toSql(argTypes),
         function);
   } else if (function instanceof AggregateFunction) {
     returnType = ((AggregateFunction) function).getReturnType(typeFactory);
     return new SqlUserDefinedAggFunction(
         name,
         ReturnTypes.explicit(returnType),
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         (AggregateFunction) function);
   } else if (function instanceof TableMacro) {
     return new SqlUserDefinedTableMacro(
         name,
         ReturnTypes.CURSOR,
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         (TableMacro) function);
   } else if (function instanceof TableFunction) {
     return new SqlUserDefinedTableFunction(
         name,
         ReturnTypes.CURSOR,
         InferTypes.explicit(argTypes),
         OperandTypes.family(typeFamilies),
         toSql(argTypes),
         (TableFunction) function);
   } else {
     throw new AssertionError("unknown function type " + function);
   }
 }
Пример #2
0
/** The <code>OVERLAY</code> function. */
public class SqlOverlayFunction extends SqlFunction {
  // ~ Static fields/initializers ---------------------------------------------

  private static final SqlOperandTypeChecker OTC_CUSTOM =
      OperandTypes.or(
          OperandTypes.STRING_STRING_INTEGER, OperandTypes.STRING_STRING_INTEGER_INTEGER);

  // ~ Constructors -----------------------------------------------------------

  public SqlOverlayFunction() {
    super(
        "OVERLAY",
        SqlKind.OTHER_FUNCTION,
        ReturnTypes.DYADIC_STRING_SUM_PRECISION_NULLABLE_VARYING,
        null,
        OTC_CUSTOM,
        SqlFunctionCategory.STRING);
  }

  // ~ Methods ----------------------------------------------------------------

  public void unparse(SqlWriter writer, SqlNode[] operands, int leftPrec, int rightPrec) {
    final SqlWriter.Frame frame = writer.startFunCall(getName());
    operands[0].unparse(writer, leftPrec, rightPrec);
    writer.sep("PLACING");
    operands[1].unparse(writer, leftPrec, rightPrec);
    writer.sep("FROM");
    operands[2].unparse(writer, leftPrec, rightPrec);
    if (4 == operands.length) {
      writer.sep("FOR");
      operands[3].unparse(writer, leftPrec, rightPrec);
    }
    writer.endFunCall(frame);
  }

  public String getSignatureTemplate(final int operandsCount) {
    switch (operandsCount) {
      case 3:
        return "{0}({1} PLACING {2} FROM {3})";
      case 4:
        return "{0}({1} PLACING {2} FROM {3} FOR {4})";
    }
    assert false;
    return null;
  }
}