Java Code Examples for org.apache.calcite.util.Pair#zip()

The following examples show how to use org.apache.calcite.util.Pair#zip() . 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: JoinPrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Build the list of join conditions for this join.
 * A join condition is built only for equality and IS NOT DISTINCT FROM comparisons. The difference is:
 * null == null is FALSE whereas null IS NOT DISTINCT FROM null is TRUE
 * For a use case of the IS NOT DISTINCT FROM comparison, see
 * {@link org.apache.calcite.rel.rules.RemoveDistinctAggregateRule}
 * @param conditions populated list of join conditions
 * @param leftFields join fields from the left input
 * @param rightFields join fields from the right input
 */
protected void buildJoinConditions(List<JoinCondition> conditions,
    List<String> leftFields,
    List<String> rightFields,
    List<Integer> leftKeys,
    List<Integer> rightKeys) {
  List<RexNode> conjuncts = RelOptUtil.conjunctions(this.getCondition());
  short i=0;

  for (Pair<Integer, Integer> pair : Pair.zip(leftKeys, rightKeys)) {
    final RexNode conditionExpr = conjuncts.get(i++);
    final SqlKind kind  = conditionExpr.getKind();
    if (kind != SqlKind.EQUALS && kind != SqlKind.IS_NOT_DISTINCT_FROM) {
      throw UserException.unsupportedError()
          .message("Unsupported comparator in join condition %s", conditionExpr)
          .build(logger);
    }

    conditions.add(new JoinCondition(kind.toString(),
        FieldReference.getWithQuotedRef(leftFields.get(pair.left)),
        FieldReference.getWithQuotedRef(rightFields.get(pair.right))));
  }
}
 
Example 2
Source File: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a cast for a row type.
 *
 * @param rexBuilder RexBuilder to use for constructing casts
 * @param lhsRowType target row type
 * @param rhsExps    expressions to be cast
 * @return cast expressions
 */
public static List<RexNode> generateCastExpressions(RexBuilder rexBuilder, RelDataType lhsRowType,
        List<RexNode> rhsExps) {
    List<RelDataTypeField> lhsFields = lhsRowType.getFieldList();
    List<RexNode> castExps = new ArrayList<>();
    for (Pair<RelDataTypeField, RexNode> pair : Pair.zip(lhsFields, rhsExps, true)) {
        RelDataTypeField lhsField = pair.left;
        RelDataType lhsType = lhsField.getType();
        final RexNode rhsExp = pair.right;
        RelDataType rhsType = rhsExp.getType();
        if (lhsType.equals(rhsType)) {
            castExps.add(rhsExp);
        } else {
            castExps.add(rexBuilder.makeCast(lhsType, rhsExp));
        }
    }
    return castExps;
}
 
Example 3
Source File: WithItemNamespace.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override protected RelDataType validateImpl(RelDataType targetRowType) {
  final SqlValidatorNamespace childNs =
      validator.getNamespace(withItem.query);
  final RelDataType rowType = childNs.getRowTypeSansSystemColumns();
  if (withItem.columnList == null) {
    return rowType;
  }
  final RelDataTypeFactory.Builder builder =
      validator.getTypeFactory().builder();
  for (Pair<SqlNode, RelDataTypeField> pair
      : Pair.zip(withItem.columnList, rowType.getFieldList())) {
    builder.add(((SqlIdentifier) pair.left).getSimple(),
        pair.right.getType());
  }
  return builder.build();
}
 
Example 4
Source File: SqlTypeUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether two types are scalar types of the same family, or struct types whose fields
 * are pairwise of the same family.
 *
 * @param type1 First type
 * @param type2 Second type
 * @return Whether types have the same family
 */
private static boolean isSameFamily(RelDataType type1, RelDataType type2) {
  if (type1.isStruct() != type2.isStruct()) {
    return false;
  }

  if (type1.isStruct()) {
    int n = type1.getFieldCount();
    if (n != type2.getFieldCount()) {
      return false;
    }
    for (Pair<RelDataTypeField, RelDataTypeField> pair
        : Pair.zip(type1.getFieldList(), type2.getFieldList())) {
      if (!isSameFamily(pair.left.getType(), pair.right.getType())) {
        return false;
      }
    }
    return true;
  }

  final RelDataTypeFamily family1 = family(type1);
  final RelDataTypeFamily family2 = family(type2);
  return family1 == family2;
}
 
Example 5
Source File: JoinPrel.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Build the list of join conditions for this join.
 * A join condition is built only for equality and IS NOT DISTINCT FROM comparisons. The difference is:
 * null == null is FALSE whereas null IS NOT DISTINCT FROM null is TRUE
 * For a use case of the IS NOT DISTINCT FROM comparison, see
 * {@link org.apache.calcite.rel.rules.AggregateRemoveRule}
 * @param conditions populated list of join conditions
 * @param leftFields join fields from the left input
 * @param rightFields join fields from the right input
 */
protected void buildJoinConditions(List<JoinCondition> conditions,
    List<String> leftFields,
    List<String> rightFields,
    List<Integer> leftKeys,
    List<Integer> rightKeys) {
  List<RexNode> conjuncts = RelOptUtil.conjunctions(this.getCondition());
  short i=0;

  for (Pair<Integer, Integer> pair : Pair.zip(leftKeys, rightKeys)) {
    final RexNode conditionExpr = conjuncts.get(i++);
    final SqlKind kind  = conditionExpr.getKind();
    if (kind != SqlKind.EQUALS && kind != SqlKind.IS_NOT_DISTINCT_FROM) {
      throw UserException.unsupportedError()
          .message("Unsupported comparator in join condition %s", conditionExpr)
          .build(logger);
    }

    conditions.add(new JoinCondition(kind.toString(),
        FieldReference.getWithQuotedRef(leftFields.get(pair.left)),
        FieldReference.getWithQuotedRef(rightFields.get(pair.right))));
  }
}
 
Example 6
Source File: HiveTableSqlFunction.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * This method is copied from calcite, and modify it to not rely on Function.
 * TODO FlinkTableFunction need implement getElementType.
 */
private static Object[] convertArguments(
		List<RelDataType> operandTypes,
		List<SqlNode> operandList) {
	List<Object> arguments = new ArrayList<>(operandList.size());
	// Construct a list of arguments, if they are all constants.
	for (Pair<RelDataType, SqlNode> pair
			: Pair.zip(operandTypes, operandList)) {
		try {
			final Object o = getValue(pair.right);
			final Object o2 = coerce(o, pair.left);
			arguments.add(o2);
		} catch (NonLiteralException e) {
			arguments.add(null);
		}
	}
	return arguments.toArray();
}
 
Example 7
Source File: RelRoot.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a simple RelRoot. */
public static RelRoot of(RelNode rel, RelDataType rowType, SqlKind kind) {
  final ImmutableIntList refs =
      ImmutableIntList.identity(rowType.getFieldCount());
  final List<String> names = rowType.getFieldNames();
  return new RelRoot(rel, rowType, kind, Pair.zip(refs, names),
      RelCollations.EMPTY);
}
 
Example 8
Source File: RexBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
private static List<Integer> nullableArgs(List<Integer> list0,
    List<RelDataType> types) {
  final List<Integer> list = new ArrayList<>();
  for (Pair<Integer, RelDataType> pair : Pair.zip(list0, types)) {
    if (pair.right.isNullable()) {
      list.add(pair.left);
    }
  }
  return list;
}
 
Example 9
Source File: Handler.java    From calcite with Apache License 2.0 5 votes vote down vote up
private ImmutableList<RexLiteral> tuple(List<Ast.Node> nodeList,
    RelDataType rowType) {
  final ImmutableList.Builder<RexLiteral> listBuilder =
      ImmutableList.builder();
  for (Pair<Ast.Node, RelDataTypeField> pair
      : Pair.zip(nodeList, rowType.getFieldList())) {
    final Ast.Node node = pair.left;
    final RelDataType type = pair.right.getType();
    listBuilder.add(item(node, type));
  }
  return listBuilder.build();
}
 
Example 10
Source File: RexToLixTranslator.java    From calcite with Apache License 2.0 5 votes vote down vote up
public List<Expression> translateList(
    List<RexNode> operandList,
    RexImpTable.NullAs nullAs,
    List<? extends Type> storageTypes) {
  final List<Expression> list = new ArrayList<>();
  for (Pair<RexNode, ? extends Type> e : Pair.zip(operandList, storageTypes)) {
    list.add(translate(e.left, nullAs, e.right));
  }
  return list;
}
 
Example 11
Source File: RelOptUtil.java    From calcite with Apache License 2.0 5 votes vote down vote up
public static boolean areRowTypesEqual(
    RelDataType rowType1,
    RelDataType rowType2,
    boolean compareNames) {
  if (rowType1 == rowType2) {
    return true;
  }
  if (compareNames) {
    // if types are not identity-equal, then either the names or
    // the types must be different
    return false;
  }
  if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
    return false;
  }
  final List<RelDataTypeField> f1 = rowType1.getFieldList();
  final List<RelDataTypeField> f2 = rowType2.getFieldList();
  for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
    final RelDataType type1 = pair.left.getType();
    final RelDataType type2 = pair.right.getType();
    // If one of the types is ANY comparison should succeed
    if (type1.getSqlTypeName() == SqlTypeName.ANY
        || type2.getSqlTypeName() == SqlTypeName.ANY) {
      continue;
    }
    if (!type1.equals(type2)) {
      return false;
    }
  }
  return true;
}
 
Example 12
Source File: SqlUserDefinedTableMacro.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Converts arguments from {@link org.apache.calcite.sql.SqlNode} to
 * java object format.
 *
 * @param typeFactory type factory used to convert the arguments
 * @param operandList input arguments
 * @param function target function to get parameter types from
 * @param opName name of the operator to use in error message
 * @param failOnNonLiteral true when conversion should fail on non-literal
 * @return converted list of arguments
 */
public static List<Object> convertArguments(RelDataTypeFactory typeFactory,
    List<SqlNode> operandList, Function function,
    SqlIdentifier opName,
    boolean failOnNonLiteral) {
  List<Object> arguments = new ArrayList<>(operandList.size());
  // Construct a list of arguments, if they are all constants.
  for (Pair<FunctionParameter, SqlNode> pair
      : Pair.zip(function.getParameters(), operandList)) {
    try {
      final Object o = getValue(pair.right);
      final Object o2 = coerce(o, pair.left.getType(typeFactory));
      arguments.add(o2);
    } catch (NonLiteralException e) {
      if (failOnNonLiteral) {
        throw new IllegalArgumentException("All arguments of call to macro "
            + opName + " should be literal. Actual argument #"
            + pair.left.getOrdinal() + " (" + pair.left.getName()
            + ") is not literal: " + pair.right);
      }
      final RelDataType type = pair.left.getType(typeFactory);
      final Object value;
      if (type.isNullable()) {
        value = null;
      } else {
        value = 0L;
      }
      arguments.add(value);
    }
  }
  return arguments;
}
 
Example 13
Source File: SqlAlterQuarkDataSource.java    From quark with Apache License 2.0 5 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  writer.keyword("ALTER");
  writer.keyword("DATASOURCE");
  identifier.unparse(writer, leftPrec, rightPrec);
  writer.keyword("SET");
  for (Pair<SqlNode, SqlNode> pair
      : Pair.zip(getTargetColumnList(), getSourceExpressionList())) {
    writer.sep(",");
    SqlIdentifier id = (SqlIdentifier) pair.left;
    id.unparse(writer, leftPrec, rightPrec);
    writer.keyword("=");
    SqlNode sourceExp = pair.right;
    sourceExp.unparse(writer, leftPrec, rightPrec);
  }
}
 
Example 14
Source File: CalciteAssert.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Finds a non-static method based on its target, name and arguments.
 * Throws if not found. */
static Method method(Object o, String methodName, Object[] args) {
  for (Class<?> aClass = o.getClass();;) {
  loop:
    for (Method method1 : aClass.getMethods()) {
      if (method1.getName().equals(methodName)
          && method1.getParameterTypes().length == args.length
          && Modifier.isPublic(method1.getDeclaringClass().getModifiers())) {
        for (Pair<Object, Class> pair
            : Pair.zip(args, (Class[]) method1.getParameterTypes())) {
          if (!pair.right.isInstance(pair.left)) {
            continue loop;
          }
        }
        return method1;
      }
    }
    if (aClass.getSuperclass() != null
        && aClass.getSuperclass() != Object.class) {
      aClass = aClass.getSuperclass();
    } else {
      final Class<?>[] interfaces = aClass.getInterfaces();
      if (interfaces.length > 0) {
        aClass = interfaces[0];
      } else {
        break;
      }
    }
  }
  throw new AssertionError("method " + methodName + " not found");
}
 
Example 15
Source File: MoreRelOptUtil.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
private static boolean checkRowTypesCompatiblity(
    RelDataType rowType1,
    RelDataType rowType2,
    boolean compareNames,
    boolean allowSubstring,
    boolean insertOp) {
  if (rowType1 == rowType2) {
    return true;
  }
  if (compareNames) {
    // if types are not identity-equal, then either the names or
    // the types must be different
    return false;
  }
  if (rowType2.getFieldCount() != rowType1.getFieldCount()) {
    return false;
  }
  final List<RelDataTypeField> f1 = rowType1.getFieldList();
  final List<RelDataTypeField> f2 = rowType2.getFieldList();
  for (Pair<RelDataTypeField, RelDataTypeField> pair : Pair.zip(f1, f2)) {
    final RelDataType type1 = pair.left.getType();
    final RelDataType type2 = pair.right.getType();
    // If one of the types is ANY comparison should succeed
    if (type1.getSqlTypeName() == SqlTypeName.ANY
      || type2.getSqlTypeName() == SqlTypeName.ANY) {
      continue;
    }
    if (!(type1.toString().equals(type2.toString()))) {
      if (allowSubstring
          && (type1.getSqlTypeName() == SqlTypeName.CHAR && type2.getSqlTypeName() == SqlTypeName.CHAR)
          && (type1.getPrecision() <= type2.getPrecision())) {
        continue;
      }

      // Check if Dremio implicit casting can resolve the incompatibility
      List<TypeProtos.MinorType> types = Lists.newArrayListWithCapacity(2);
      TypeProtos.MinorType minorType1 = Types.getMinorTypeFromName(type1.getSqlTypeName().getName());
      TypeProtos.MinorType minorType2 = Types.getMinorTypeFromName(type2.getSqlTypeName().getName());
      types.add(minorType1);
      types.add(minorType2);
      if (insertOp) {
        // Insert is more strict than normal select in terms of implicit casts
        // Return false if TypeCastRules do not allow implicit cast
        if (TypeCastRules.isCastable(minorType1, minorType2, true) &&
          TypeCastRules.getLeastRestrictiveTypeForInsert(types) != null) {
          if (TypeCastRules.isCastSafeFromDataTruncation(type1, type2)) {
            continue;
          }
        }
      } else {
        if (TypeCastRules.getLeastRestrictiveType(types) != null) {
          continue;
        }
      }

      return false;
    }
  }
  return true;
}
 
Example 16
Source File: MutableProject.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Returns a list of (expression, name) pairs. */
public final List<Pair<RexNode, String>> getNamedProjects() {
  return Pair.zip(projects, rowType.getFieldNames());
}
 
Example 17
Source File: CalcitePrepareImpl.java    From calcite with Apache License 2.0 4 votes vote down vote up
private PreparedResult prepare_(Supplier<RelNode> fn,
    RelDataType resultType) {
  Class runtimeContextClass = Object.class;
  init(runtimeContextClass);

  final RelNode rel = fn.get();
  final RelDataType rowType = rel.getRowType();
  final List<Pair<Integer, String>> fields =
      Pair.zip(ImmutableIntList.identity(rowType.getFieldCount()),
          rowType.getFieldNames());
  final RelCollation collation =
      rel instanceof Sort
          ? ((Sort) rel).collation
          : RelCollations.EMPTY;
  RelRoot root = new RelRoot(rel, resultType, SqlKind.SELECT, fields,
      collation, new ArrayList<>());

  if (timingTracer != null) {
    timingTracer.traceTime("end sql2rel");
  }

  final RelDataType jdbcType =
      makeStruct(rexBuilder.getTypeFactory(), resultType);
  fieldOrigins = Collections.nCopies(jdbcType.getFieldCount(), null);
  parameterRowType = rexBuilder.getTypeFactory().builder().build();

  // Structured type flattening, view expansion, and plugging in
  // physical storage.
  root = root.withRel(flattenTypes(root.rel, true));

  // Trim unused fields.
  root = trimUnusedFields(root);

  final List<Materialization> materializations = ImmutableList.of();
  final List<CalciteSchema.LatticeEntry> lattices = ImmutableList.of();
  root = optimize(root, materializations, lattices);

  if (timingTracer != null) {
    timingTracer.traceTime("end optimization");
  }

  return implement(root);
}
 
Example 18
Source File: StarColumnConverter.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public Prel visitProject(ProjectPrel prel, Void value) throws RuntimeException {
  ProjectPrel proj = prel;

  // Require prefix rename : there exists other expression, in addition to a star column.
  if (!prefixedForStar  // not set yet.
      && StarColumnHelper.containsStarColumnInProject(prel.getInput().getRowType(), proj.getProjects())
      && prel.getRowType().getFieldNames().size() > 1) {
    prefixedForStar = true;
  }

  // For project, we need make sure that the project's field name is same as the input,
  // when the project expression is RexInPutRef, since we may insert a PAS which will
  // rename the projected fields.

  RelNode child = ((Prel) prel.getInput(0)).accept(this, null);

  List<String> fieldNames = Lists.newArrayList();

  for (Pair<String, RexNode> pair : Pair.zip(prel.getRowType().getFieldNames(), proj.getProjects())) {
    if (pair.right instanceof RexInputRef) {
      String name = child.getRowType().getFieldNames().get(((RexInputRef) pair.right).getIndex());
      fieldNames.add(name);
    } else {
      fieldNames.add(pair.left);
    }
  }

  // Make sure the field names are unique : no allow of duplicate field names in a rowType.
  fieldNames = makeUniqueNames(fieldNames);

  RelDataType rowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), proj.getProjects(), fieldNames);

  ProjectPrel newProj = (ProjectPrel) proj.copy(proj.getTraitSet(), child, proj.getProjects(), rowType);

  if (ProjectRemoveRule.isTrivial(newProj)) {
    return (Prel) child;
  } else {
    return newProj;
  }
}
 
Example 19
Source File: SqlMerge.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) {
  final SqlWriter.Frame frame =
      writer.startList(SqlWriter.FrameTypeEnum.SELECT, "MERGE INTO", "");
  final int opLeft = getOperator().getLeftPrec();
  final int opRight = getOperator().getRightPrec();
  targetTable.unparse(writer, opLeft, opRight);
  if (alias != null) {
    writer.keyword("AS");
    alias.unparse(writer, opLeft, opRight);
  }

  writer.newlineAndIndent();
  writer.keyword("USING");
  source.unparse(writer, opLeft, opRight);

  writer.newlineAndIndent();
  writer.keyword("ON");
  condition.unparse(writer, opLeft, opRight);

  if (updateCall != null) {
    writer.newlineAndIndent();
    writer.keyword("WHEN MATCHED THEN UPDATE");
    final SqlWriter.Frame setFrame =
        writer.startList(
            SqlWriter.FrameTypeEnum.UPDATE_SET_LIST,
            "SET",
            "");

    for (Pair<SqlNode, SqlNode> pair : Pair.zip(
        updateCall.targetColumnList, updateCall.sourceExpressionList)) {
      writer.sep(",");
      SqlIdentifier id = (SqlIdentifier) pair.left;
      id.unparse(writer, opLeft, opRight);
      writer.keyword("=");
      SqlNode sourceExp = pair.right;
      sourceExp.unparse(writer, opLeft, opRight);
    }
    writer.endList(setFrame);
  }

  if (insertCall != null) {
    writer.newlineAndIndent();
    writer.keyword("WHEN NOT MATCHED THEN INSERT");
    if (insertCall.getTargetColumnList() != null) {
      insertCall.getTargetColumnList().unparse(writer, opLeft, opRight);
    }
    insertCall.getSource().unparse(writer, opLeft, opRight);

    writer.endList(frame);
  }
}
 
Example 20
Source File: DrillProjectRelBase.java    From Bats with Apache License 2.0 4 votes vote down vote up
private List<Pair<RexNode, String>> projects() {
  return Pair.zip(exps, getRowType().getFieldNames());
}