Java Code Examples for org.apache.calcite.sql.SqlOperator#getName()

The following examples show how to use org.apache.calcite.sql.SqlOperator#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocumentationTest.java    From calcite with Apache License 2.0 6 votes vote down vote up
private void addOperators(Map<String, PatternOp> map, String prefix,
    List<SqlOperator> operatorList) {
  for (SqlOperator op : operatorList) {
    final String name = op.getName().equals("TRANSLATE3") ? "TRANSLATE"
        : op.getName();
    if (op instanceof SqlSpecialOperator
        || !name.matches("^[a-zA-Z][a-zA-Z0-9_]*$")) {
      continue;
    }
    final String regex;
    if (op instanceof SqlOverlapsOperator) {
      regex = "[ ]*<td>period1 " + name + " period2</td>";
    } else if (op instanceof SqlFunction
        && (op.getOperandTypeChecker() == null
            || op.getOperandTypeChecker().getOperandCountRange().getMin()
                != 0)) {
      regex = prefix + "\\| .*" + name + "\\(.*";
    } else {
      regex = prefix + "\\| .*" + name + ".*";
    }
    map.put(regex, new PatternOp(Pattern.compile(regex), name));
  }
}
 
Example 2
Source File: DrillCalciteSqlOperatorWrapper.java    From Bats with Apache License 2.0 5 votes vote down vote up
public DrillCalciteSqlOperatorWrapper(SqlOperator operator, final String rename, final List<DrillFuncHolder> functions) {
  super(
      operator.getName(),
      operator.getKind(),
      operator.getLeftPrec(),
      operator.getRightPrec(),
      TypeInferenceUtils.getDrillSqlReturnTypeInference(
          rename,
          functions),
      operator.getOperandTypeInference(),
      Checker.ANY_CHECKER);
  this.operator = operator;
}
 
Example 3
Source File: DrillOperatorTable.java    From Bats with Apache License 2.0 5 votes vote down vote up
private void populateWrappedCalciteOperators() {
  for (SqlOperator calciteOperator : inner.getOperatorList()) {
    final SqlOperator wrapper;
    if (calciteOperator instanceof SqlAggFunction) {
      wrapper = new DrillCalciteSqlAggFunctionWrapper((SqlAggFunction) calciteOperator,
          getFunctionListWithInference(calciteOperator.getName()));
    } else if (calciteOperator instanceof SqlFunction) {
      wrapper = new DrillCalciteSqlFunctionWrapper((SqlFunction) calciteOperator,
          getFunctionListWithInference(calciteOperator.getName()));
    } else if (calciteOperator instanceof SqlBetweenOperator) {
      // During the procedure of converting to RexNode,
      // StandardConvertletTable.convertBetween expects the SqlOperator to be a subclass of SqlBetweenOperator
      final SqlBetweenOperator sqlBetweenOperator = (SqlBetweenOperator) calciteOperator;
      wrapper = new DrillCalciteSqlBetweenOperatorWrapper(sqlBetweenOperator);
    } else {
      final String drillOpName;
      // For UNARY_MINUS (-) or UNARY_PLUS (+), we do not rename them as function_add or function_subtract.
      // Otherwise, Calcite will mix them up with binary operator subtract (-) or add (+)
      if (calciteOperator == SqlStdOperatorTable.UNARY_MINUS || calciteOperator == SqlStdOperatorTable.UNARY_PLUS) {
        drillOpName = calciteOperator.getName();
      } else {
        drillOpName = FunctionCallFactory.replaceOpWithFuncName(calciteOperator.getName());
      }

      final List<DrillFuncHolder> drillFuncHolders = getFunctionListWithInference(drillOpName);
      if (drillFuncHolders.isEmpty()) {
        continue;
      }

      wrapper = new DrillCalciteSqlOperatorWrapper(calciteOperator, drillOpName, drillFuncHolders);
    }
    calciteToWrapper.put(calciteOperator, wrapper);
  }
}
 
Example 4
Source File: HiveSqlUDAFReturnTypeInference.java    From marble with Apache License 2.0 5 votes vote down vote up
@Override public RelDataType inferReturnType(
    final SqlOperatorBinding opBinding) {
  try {
    RelDataTypeFactory factory = opBinding.getTypeFactory();
    SqlOperator sqlOperator = opBinding.getOperator();
    String opName = sqlOperator.getName();
    Class hiveUDAFClass = HiveSqlOperatorTable.instance()
        .getHiveUDAFClass(opName);
    List<RelDataTypeHolder> argsType = new ArrayList<>();

    for (int i = 0; i < opBinding.getOperandCount(); i++) {
      RelDataTypeHolder relDataTypeHolder;
      if (TypeInferenceUtil.isOperandConstantForHiveUDAF(hiveUDAFClass, i)) {
        //we use a pre-defined fake value here to getGenericUDAFReturnType
        Object constantValue = TypeInferenceUtil
            .HIVE_UDAF_CONSTANT_OBJECT_INSPECT_CONTEXT_MAP
            .get(hiveUDAFClass).get(i);
        relDataTypeHolder = new RelDataTypeHolder(opBinding.getOperandType(i),
            true, constantValue);
      } else {
        relDataTypeHolder = new RelDataTypeHolder(
            opBinding.getOperandType(i));
      }
      argsType.add(relDataTypeHolder);
    }
    RelDataType resultType = getGenericUDAFReturnType(
        hiveUDAFClass,
        argsType.toArray(new RelDataTypeHolder[0]), factory);
    return resultType;
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: ExpressionGenerator.java    From streamline with Apache License 2.0 5 votes vote down vote up
@Override
public Expression visit(SqlCall call) {
    SqlOperator sqlOperator = call.getOperator();
    if (sqlOperator instanceof SqlBinaryOperator) {
        return visitBinaryOperator((SqlBinaryOperator) sqlOperator, call.getOperandList().get(0),
                call.getOperandList().get(1));
    } else if (sqlOperator instanceof SqlSpecialOperator) {
        return visitSqlSpecialOperator((SqlSpecialOperator) sqlOperator, call.getOperandList());
    } else if (sqlOperator instanceof SqlFunction) {
        SqlFunction sqlFunction = (SqlFunction) sqlOperator;
        if (sqlFunction instanceof SqlAggFunction) {
            return visitAggregateFunction(sqlFunction.getName(), call.getOperandList());
        } else if (sqlFunction instanceof SqlUnresolvedFunction) {
            String udfName = sqlFunction.getName().toUpperCase();
            if (catalogUdfs.containsKey(udfName)) {
                Udf udfInfo = catalogUdfs.get(udfName);
                if (udfInfo.isAggregate()) {
                    return visitUserDefinedAggregateFunction(udfInfo, call.getOperandList());
                } else {
                    return visitUserDefinedFunction(udfInfo, call.getOperandList());
                }
            } else {
                throw new UnsupportedOperationException("Unknown built-in or User defined function '" + udfName + "'");
            }
        } else {
            return visitFunction(sqlFunction.getName(), call.getOperandList());
        }
    } else {
        throw new UnsupportedOperationException("Operator " + sqlOperator.getName() + " is not supported");
    }
}
 
Example 6
Source File: RelJson.java    From Bats with Apache License 2.0 4 votes vote down vote up
private String toJson(SqlOperator operator) {
  // User-defined operators are not yet handled.
  return operator.getName();
}
 
Example 7
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private static void findAllValidFunctionNames(
	List<String> names,
	SqlValidator validator,
	Collection<SqlMoniker> result,
	SqlParserPos pos) {
	// a function name can only be 1 part
	if (names.size() > 1) {
		return;
	}
	for (SqlOperator op : validator.getOperatorTable().getOperatorList()) {
		SqlIdentifier curOpId =
			new SqlIdentifier(
				op.getName(),
				pos);

		final SqlCall call =
			SqlUtil.makeCall(
				validator.getOperatorTable(),
				curOpId);
		if (call != null) {
			result.add(
				new SqlMonikerImpl(
					op.getName(),
					SqlMonikerType.FUNCTION));
		} else {
			if ((op.getSyntax() == SqlSyntax.FUNCTION)
				|| (op.getSyntax() == SqlSyntax.PREFIX)) {
				if (op.getOperandTypeChecker() != null) {
					String sig = op.getAllowedSignatures();
					sig = sig.replaceAll("'", "");
					result.add(
						new SqlMonikerImpl(
							sig,
							SqlMonikerType.FUNCTION));
					continue;
				}
				result.add(
					new SqlMonikerImpl(
						op.getName(),
						SqlMonikerType.FUNCTION));
			}
		}
	}
}
 
Example 8
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void findAllValidFunctionNames(
	List<String> names,
	SqlValidator validator,
	Collection<SqlMoniker> result,
	SqlParserPos pos) {
	// a function name can only be 1 part
	if (names.size() > 1) {
		return;
	}
	for (SqlOperator op : validator.getOperatorTable().getOperatorList()) {
		SqlIdentifier curOpId =
			new SqlIdentifier(
				op.getName(),
				pos);

		final SqlCall call = validator.makeNullaryCall(curOpId);
		if (call != null) {
			result.add(
				new SqlMonikerImpl(
					op.getName(),
					SqlMonikerType.FUNCTION));
		} else {
			if ((op.getSyntax() == SqlSyntax.FUNCTION)
				|| (op.getSyntax() == SqlSyntax.PREFIX)) {
				if (op.getOperandTypeChecker() != null) {
					String sig = op.getAllowedSignatures();
					sig = sig.replaceAll("'", "");
					result.add(
						new SqlMonikerImpl(
							sig,
							SqlMonikerType.FUNCTION));
					continue;
				}
				result.add(
					new SqlMonikerImpl(
						op.getName(),
						SqlMonikerType.FUNCTION));
			}
		}
	}
}
 
Example 9
Source File: HiveSqlUDFReturnTypeInference.java    From marble with Apache License 2.0 4 votes vote down vote up
@Override public RelDataType inferReturnType(
    final SqlOperatorBinding opBinding) {
  try {
    SqlOperator sqlOperator = opBinding.getOperator();
    List<RelDataTypeHolder> argsType = new ArrayList<>();
    for (int i = 0; i < opBinding.getOperandCount(); i++) {
      RelDataTypeHolder relDataTypeHolder;
      boolean isSqlCallBinding = opBinding instanceof SqlCallBinding;
      if (isSqlCallBinding) {
        List<SqlNode> operands = ((SqlCallBinding) opBinding).operands();
        if (operands.get(i) instanceof SqlLiteral) {
          relDataTypeHolder = new RelDataTypeHolder(
              opBinding.getOperandType(i), true,
              ((SqlLiteral) operands.get(i)).getValue());
        } else {
          relDataTypeHolder = new RelDataTypeHolder(
              opBinding.getOperandType(i));
        }

      } else {
        relDataTypeHolder = new RelDataTypeHolder(
            opBinding.getOperandType(i));
      }
      argsType.add(relDataTypeHolder);
    }
    String opName = sqlOperator.getName();
    GenericUDF udfInstance = HiveUDFImplementor.newGenericUDF(
        opName, sqlOperator.getSyntax());
    ObjectInspector[] inputObjectInspector =
        TypeInferenceUtil.getObjectInspector(
            argsType.toArray(new RelDataTypeHolder[0]));
    ObjectInspector outputObjectInspector = udfInstance.initialize(
        inputObjectInspector);
    RelDataType resultType = TypeInferenceUtil.getRelDataType(
        outputObjectInspector,
        opBinding.getTypeFactory());
    return resultType;

  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}