Java Code Examples for org.apache.calcite.sql.validate.SqlValidatorUtil#uniquify()

The following examples show how to use org.apache.calcite.sql.validate.SqlValidatorUtil#uniquify() . 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: RexUtil.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a record type with specified field names.
 *
 * <p>The array of field names may be null, or any of the names within it
 * can be null. We recommend using explicit names where possible, because it
 * makes it much easier to figure out the intent of fields when looking at
 * planner output.
 *
 * @param typeFactory Type factory
 * @param exprs       Expressions
 * @param names       Field names, may be null, or elements may be null
 * @param suggester   Generates alternative names if {@code names} is not
 *                    null and its elements are not unique
 * @return Record type
 */
public static RelDataType createStructType(RelDataTypeFactory typeFactory, final List<? extends RexNode> exprs,
        List<String> names, SqlValidatorUtil.Suggester suggester) {
    if (names != null && suggester != null) {
        names = SqlValidatorUtil.uniquify(names, suggester, typeFactory.getTypeSystem().isSchemaCaseSensitive());
    }
    final RelDataTypeFactory.Builder builder = typeFactory.builder();
    for (int i = 0; i < exprs.size(); i++) {
        String name;
        if (names == null || (name = names.get(i)) == null) {
            name = "$f" + i;
        }
        builder.add(name, exprs.get(i).getType());
    }
    return builder.build();
}
 
Example 2
Source File: RexUtil.java    From calcite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a record type with specified field names.
 *
 * <p>The array of field names may be null, or any of the names within it
 * can be null. We recommend using explicit names where possible, because it
 * makes it much easier to figure out the intent of fields when looking at
 * planner output.
 *
 * @param typeFactory Type factory
 * @param exprs       Expressions
 * @param names       Field names, may be null, or elements may be null
 * @param suggester   Generates alternative names if {@code names} is not
 *                    null and its elements are not unique
 * @return Record type
 */
public static RelDataType createStructType(
    RelDataTypeFactory typeFactory,
    final List<? extends RexNode> exprs,
    List<String> names,
    SqlValidatorUtil.Suggester suggester) {
  if (names != null && suggester != null) {
    names = SqlValidatorUtil.uniquify(names, suggester,
        typeFactory.getTypeSystem().isSchemaCaseSensitive());
  }
  final RelDataTypeFactory.Builder builder = typeFactory.builder();
  for (int i = 0; i < exprs.size(); i++) {
    String name;
    if (names == null || (name = names.get(i)) == null) {
      name = "$f" + i;
    }
    builder.add(name, exprs.get(i).getType());
  }
  return builder.build();
}
 
Example 3
Source File: TopProjectVisitor.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Adds top project to ensure final output field names are preserved.
 * In case of duplicated column names, will rename duplicates.
 * Top project will be added only if top project is non-trivial and
 * child physical relational node is not project.
 *
 * @param prel physical relational node
 * @param validatedRowType final output row type
 * @return physical relational node with top project if necessary
 */
private Prel addTopProjectPrel(Prel prel, RelDataType validatedRowType) {
  RelDataType rowType = prel.getRowType();
  if (rowType.getFieldCount() != validatedRowType.getFieldCount()) {
    return prel;
  }

  RexBuilder rexBuilder = prel.getCluster().getRexBuilder();
  List<RexNode> projections = new ArrayList<>();
  int projectCount = rowType.getFieldList().size();

  for (int i = 0; i < projectCount; i++) {
    projections.add(rexBuilder.makeInputRef(prel, i));
  }

  List<String> fieldNames = SqlValidatorUtil.uniquify(
      validatedRowType.getFieldNames(),
      SqlValidatorUtil.EXPR_SUGGESTER,
      prel.getCluster().getTypeFactory().getTypeSystem().isSchemaCaseSensitive());

  RelDataType newRowType = RexUtil.createStructType(prel.getCluster().getTypeFactory(), projections, fieldNames, null);
  ProjectPrel topProject = new ProjectPrel(prel.getCluster(),
      prel.getTraitSet(),
      prel,
      projections,
      newRowType,
      true);  //outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.

  if (prel instanceof Project && DrillRelOptUtil.isTrivialProject(topProject, true)) {
    return new ProjectPrel(prel.getCluster(),
        prel.getTraitSet(),
        ((Project) prel).getInput(),
        ((Project) prel).getProjects(),
        prel.getRowType(),
        true); //outputProj = true : NONE -> OK_NEW_SCHEMA, also handle expression with NULL type.
  } else {
    return topProject;
  }
}
 
Example 4
Source File: DefaultSqlHandler.java    From Bats with Apache License 2.0 5 votes vote down vote up
protected DrillRel addRenamedProject(DrillRel rel, RelDataType validatedRowType) {
  RelDataType t = rel.getRowType();

  RexBuilder b = rel.getCluster().getRexBuilder();
  List<RexNode> projections = Lists.newArrayList();
  int projectCount = t.getFieldList().size();

  for (int i =0; i < projectCount; i++) {
    projections.add(b.makeInputRef(rel, i));
  }

  final List<String> fieldNames2 = SqlValidatorUtil.uniquify(
      validatedRowType.getFieldNames(),
      SqlValidatorUtil.EXPR_SUGGESTER,
      rel.getCluster().getTypeFactory().getTypeSystem().isSchemaCaseSensitive());

  RelDataType newRowType = RexUtil.createStructType(rel.getCluster().getTypeFactory(),
      projections, fieldNames2, null);

  DrillProjectRel topProj = DrillProjectRel.create(rel.getCluster(), rel.getTraitSet(), rel, projections, newRowType);

  // Add a final non-trivial Project to get the validatedRowType, if child is not project.
  if (rel instanceof Project && DrillRelOptUtil.isTrivialProject(topProj, true)) {
    return rel;
  } else{
    return topProj;
  }
}
 
Example 5
Source File: ElasticsearchRules.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public static List<String> elasticsearchFieldNames(final RelDataType rowType) {
    return SqlValidatorUtil.uniquify(
            new AbstractList<String>() {
                @Override
                public String get(int index) {
                    final String name = rowType.getFieldList().get(index).getName();
                    return name.startsWith("$") ? "_" + name.substring(2) : name;
                }

                @Override
                public int size() {
                    return rowType.getFieldCount();
                }
            });
}
 
Example 6
Source File: ElasticsearchRules.java    From dk-fitting with Apache License 2.0 5 votes vote down vote up
public static List<String> elasticsearchFieldNames(final RelDataType rowType) {
    return SqlValidatorUtil.uniquify(
            new AbstractList<String>() {
                @Override
                public String get(int index) {
                    final String name = rowType.getFieldList().get(index).getName();
                    return name.startsWith("$") ? "_" + name.substring(2) : name;
                }

                @Override
                public int size() {
                    return rowType.getFieldCount();
                }
            });
}
 
Example 7
Source File: PrelTransformer.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
public static Rel addRenamedProject(SqlHandlerConfig config, Rel rel, RelDataType validatedRowType) {
  RelDataType t = rel.getRowType();

  RexBuilder b = rel.getCluster().getRexBuilder();
  List<RexNode> projections = Lists.newArrayList();
  int projectCount = t.getFieldList().size();

  for (int i =0; i < projectCount; i++) {
    projections.add(b.makeInputRef(rel, i));
  }

  final List<String> fieldNames2 = SqlValidatorUtil.uniquify(
          validatedRowType.getFieldNames(),
          SqlValidatorUtil.F_SUGGESTER,
          rel.getCluster().getTypeFactory().getTypeSystem().isSchemaCaseSensitive());

  RelDataType newRowType = RexUtil.createStructType(rel.getCluster().getTypeFactory(), projections, fieldNames2);

  ProjectRel topProj = ProjectRel.create(rel.getCluster(), rel.getTraitSet(), rel, projections, newRowType);

  final boolean hasAnyType = Iterables.find(
      validatedRowType.getFieldList(),
      new Predicate<RelDataTypeField>() {
        @Override
        public boolean apply(@Nullable RelDataTypeField input) {
          return input.getType().getSqlTypeName() == SqlTypeName.ANY;
        }
      },
      null
  ) != null;

  // Add a final non-trivial Project to get the validatedRowType, if child is not project or the input row type
  // contains at least one field of type ANY
  if (rel instanceof Project && MoreRelOptUtil.isTrivialProject(topProj, true) && !hasAnyType) {
    return rel;
  }

  return topProj;
}
 
Example 8
Source File: SolrRules.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static List<String> solrFieldNames(final RelDataType rowType) {
  return SqlValidatorUtil.uniquify(
      new AbstractList<String>() {
        @Override
        public String get(int index) {
          return rowType.getFieldList().get(index).getName();
        }

        @Override
        public int size() {
          return rowType.getFieldCount();
        }
      }, true);
}
 
Example 9
Source File: RelToSqlConverter.java    From quark with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a result based on a single relational expression.
 */
public Result result(SqlNode node, Collection<Clause> clauses, RelNode rel) {
  final String alias2 = SqlValidatorUtil.getAlias(node, -1);
  final String alias3 = alias2 != null ? alias2 : "t";
  final String alias4 =
      SqlValidatorUtil.uniquify(
          alias3, aliasSet, SqlValidatorUtil.EXPR_SUGGESTER);
  final String alias5 = alias2 == null || !alias2.equals(alias4) ? alias4
      : null;
  return new Result(node, clauses, alias5,
      Collections.singletonList(Pair.of(alias4, rel.getRowType())));
}
 
Example 10
Source File: PythonCorrelateSplitRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private List<String> createNewFieldNames(
	RelDataType rowType,
	RexBuilder rexBuilder,
	int primitiveFieldCount,
	ArrayBuffer<RexNode> extractedRexNodes,
	List<RexNode> calcProjects) {
	for (int i = 0; i < primitiveFieldCount; i++) {
		calcProjects.add(RexInputRef.of(i, rowType));
	}
	// add the fields of the extracted rex calls.
	Iterator<RexNode> iterator = extractedRexNodes.iterator();
	while (iterator.hasNext()) {
		calcProjects.add(iterator.next());
	}

	List<String> nameList = new LinkedList<>();
	for (int i = 0; i < primitiveFieldCount; i++) {
		nameList.add(rowType.getFieldNames().get(i));
	}
	Iterator<Object> indicesIterator = extractedRexNodes.indices().iterator();
	while (indicesIterator.hasNext()) {
		nameList.add("f" + indicesIterator.next());
	}
	return SqlValidatorUtil.uniquify(
		nameList,
		rexBuilder.getTypeFactory().getTypeSystem().isSchemaCaseSensitive());
}
 
Example 11
Source File: PythonCorrelateSplitRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private List<String> createNewFieldNames(
	RelDataType rowType,
	RexBuilder rexBuilder,
	int primitiveFieldCount,
	ArrayBuffer<RexNode> extractedRexNodes,
	List<RexNode> calcProjects) {
	for (int i = 0; i < primitiveFieldCount; i++) {
		calcProjects.add(RexInputRef.of(i, rowType));
	}
	// add the fields of the extracted rex calls.
	Iterator<RexNode> iterator = extractedRexNodes.iterator();
	while (iterator.hasNext()) {
		calcProjects.add(iterator.next());
	}

	List<String> nameList = new LinkedList<>();
	for (int i = 0; i < primitiveFieldCount; i++) {
		nameList.add(rowType.getFieldNames().get(i));
	}
	Iterator<Object> indicesIterator = extractedRexNodes.indices().iterator();
	while (indicesIterator.hasNext()) {
		nameList.add("f" + indicesIterator.next());
	}
	return SqlValidatorUtil.uniquify(
		nameList,
		rexBuilder.getTypeFactory().getTypeSystem().isSchemaCaseSensitive());
}
 
Example 12
Source File: ElasticsearchRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
static List<String> elasticsearchFieldNames(final RelDataType rowType) {
  return SqlValidatorUtil.uniquify(
      new AbstractList<String>() {
        @Override public String get(int index) {
          final String name = rowType.getFieldList().get(index).getName();
          return name.startsWith("$") ? "_" + name.substring(2) : name;
        }

        @Override public int size() {
          return rowType.getFieldCount();
        }
      },
      SqlValidatorUtil.EXPR_SUGGESTER, true);
}
 
Example 13
Source File: MongoRules.java    From calcite with Apache License 2.0 5 votes vote down vote up
static List<String> mongoFieldNames(final RelDataType rowType) {
  return SqlValidatorUtil.uniquify(
      new AbstractList<String>() {
        @Override public String get(int index) {
          final String name = rowType.getFieldList().get(index).getName();
          return name.startsWith("$") ? "_" + name.substring(2) : name;
        }

        @Override public int size() {
          return rowType.getFieldCount();
        }
      },
      SqlValidatorUtil.EXPR_SUGGESTER, true);
}
 
Example 14
Source File: RelDataTypeFactory.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that field names are unique.
 */
public Builder uniquify() {
  final List<String> uniqueNames = SqlValidatorUtil.uniquify(names,
      typeFactory.getTypeSystem().isSchemaCaseSensitive());
  if (uniqueNames != names) {
    names.clear();
    names.addAll(uniqueNames);
  }
  return this;
}
 
Example 15
Source File: RelDataTypeFactory.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * Makes sure that field names are unique.
 */
public Builder uniquify() {
  final List<String> uniqueNames = SqlValidatorUtil.uniquify(names,
      typeFactory.getTypeSystem().isSchemaCaseSensitive());
  if (uniqueNames != names) {
    names.clear();
    names.addAll(uniqueNames);
  }
  return this;
}
 
Example 16
Source File: GeodeRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
static List<String> geodeFieldNames(final RelDataType rowType) {
  return SqlValidatorUtil.uniquify(rowType.getFieldNames(), true);
}
 
Example 17
Source File: CassandraRules.java    From calcite with Apache License 2.0 4 votes vote down vote up
static List<String> cassandraFieldNames(final RelDataType rowType) {
  return SqlValidatorUtil.uniquify(rowType.getFieldNames(),
      SqlValidatorUtil.EXPR_SUGGESTER, true);
}
 
Example 18
Source File: DruidQuery.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Computes the project group set.
 *
 * @param projectNode Project under the Aggregates if any
 * @param groupSet Ids of grouping keys as they are listed in {@code projects} list
 * @param inputRowType Input row type under the project
 * @param druidQuery Druid Query
 *
 * @return A list of {@link DimensionSpec} containing the group by dimensions,
 * and a list of {@link VirtualColumn} containing Druid virtual column
 * projections; or null, if translation is not possible.
 * Note that the size of lists can be different.
 */
@Nullable
protected static Pair<List<DimensionSpec>, List<VirtualColumn>> computeProjectGroupSet(
    @Nullable Project projectNode, ImmutableBitSet groupSet,
    RelDataType inputRowType, DruidQuery druidQuery) {
  final List<DimensionSpec> dimensionSpecList = new ArrayList<>();
  final List<VirtualColumn> virtualColumnList = new ArrayList<>();
  final Set<String> usedFieldNames = new HashSet<>();
  for (int groupKey : groupSet) {
    final DimensionSpec dimensionSpec;
    final RexNode project;
    if (projectNode == null) {
      project =  RexInputRef.of(groupKey, inputRowType);
    } else {
      project = projectNode.getProjects().get(groupKey);
    }

    Pair<String, ExtractionFunction> druidColumn =
        toDruidColumn(project, inputRowType, druidQuery);
    if (druidColumn.left != null && druidColumn.right == null) {
      // SIMPLE INPUT REF
      dimensionSpec = new DefaultDimensionSpec(druidColumn.left, druidColumn.left,
          DruidExpressions.EXPRESSION_TYPES.get(project.getType().getSqlTypeName()));
      usedFieldNames.add(druidColumn.left);
    } else if (druidColumn.left != null && druidColumn.right != null) {
     // CASE it is an extraction Dimension
      final String columnPrefix;
      //@TODO Remove it! if else statement is not really needed it is here to make tests pass.
      if (project.getKind() == SqlKind.EXTRACT) {
        columnPrefix =
            EXTRACT_COLUMN_NAME_PREFIX + "_" + Objects
                .requireNonNull(DruidDateTimeUtils
                    .extractGranularity(project, druidQuery.getConnectionConfig().timeZone())
                    .getType().lowerName);
      } else if (project.getKind() == SqlKind.FLOOR) {
        columnPrefix =
            FLOOR_COLUMN_NAME_PREFIX + "_" + Objects
                .requireNonNull(DruidDateTimeUtils
                    .extractGranularity(project, druidQuery.getConnectionConfig().timeZone())
                    .getType().lowerName);
      } else {
        columnPrefix = "extract";
      }
      final String uniqueExtractColumnName = SqlValidatorUtil
          .uniquify(columnPrefix, usedFieldNames,
              SqlValidatorUtil.EXPR_SUGGESTER);
      dimensionSpec = new ExtractionDimensionSpec(druidColumn.left,
          druidColumn.right, uniqueExtractColumnName);
      usedFieldNames.add(uniqueExtractColumnName);
    } else {
      // CASE it is Expression
      final String expression = DruidExpressions
          .toDruidExpression(project, inputRowType, druidQuery);
      if (Strings.isNullOrEmpty(expression)) {
        return null;
      }
      final String name = SqlValidatorUtil
          .uniquify("vc", usedFieldNames,
              SqlValidatorUtil.EXPR_SUGGESTER);
      VirtualColumn vc = new VirtualColumn(name, expression,
          DruidExpressions.EXPRESSION_TYPES.get(project.getType().getSqlTypeName()));
      virtualColumnList.add(vc);
      dimensionSpec = new DefaultDimensionSpec(name, name,
          DruidExpressions.EXPRESSION_TYPES.get(project.getType().getSqlTypeName()));
      usedFieldNames.add(name);

    }

    dimensionSpecList.add(dimensionSpec);
  }
  return Pair.of(dimensionSpecList, virtualColumnList);
}