Java Code Examples for org.apache.calcite.rel.RelNode#getRowType()

The following examples show how to use org.apache.calcite.rel.RelNode#getRowType() . 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: RuntimeFilterVisitor.java    From Bats with Apache License 2.0 6 votes vote down vote up
/**
 * Find a join condition's left input source scan Prel. If we can't find a target scan Prel then this
 * RuntimeFilter can not pushed down to a probe side scan Prel.
 *
 * @param fieldName   left join condition field Name
 * @param leftRelNode left RelNode of a BiRel or the SingleRel
 * @return a left scan Prel which contains the left join condition name or null
 */
private ScanPrel findLeftScanPrel(String fieldName, RelNode leftRelNode) {
  if (leftRelNode instanceof ScanPrel) {
    RelDataType scanRowType = leftRelNode.getRowType();
    RelDataTypeField field = scanRowType.getField(fieldName, true, true);
    if (field != null) {
      //found
      return (ScanPrel) leftRelNode;
    } else {
      return null;
    }
  } else if (leftRelNode instanceof RelSubset) {
    RelNode bestNode = ((RelSubset) leftRelNode).getBest();
    if (bestNode != null) {
      return findLeftScanPrel(fieldName, bestNode);
    } else {
      return null;
    }
  } else {
    List<RelNode> relNodes = leftRelNode.getInputs();
    RelNode leftNode = relNodes.get(0);
    return findLeftScanPrel(fieldName, leftNode);
  }
}
 
Example 2
Source File: WindowPrel.java    From dremio-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Derive rowType for the copied WindowPrel based on input.
 * When copy() is called, the input might be different from the current one's input.
 * We have to use the new input's field in the copied WindowPrel.
 */
private RelDataType deriveCopiedRowTypeFromInput(final RelNode input) {
  final RelDataType inputRowType = input.getRowType();
  final RelDataType windowRowType = this.getRowType();

  final List<RelDataTypeField> fieldList = new ArrayList<>(inputRowType.getFieldList());
  final int inputFieldCount = inputRowType.getFieldCount();
  final int windowFieldCount = windowRowType.getFieldCount();

  for (int i = inputFieldCount; i < windowFieldCount; i++) {
    fieldList.add(windowRowType.getFieldList().get(i));
  }

  final RelDataType rowType = this.getCluster().getRexBuilder().getTypeFactory().createStructType(fieldList);

  return rowType;
}
 
Example 3
Source File: EnumerableFilterToCalcRule.java    From calcite with Apache License 2.0 6 votes vote down vote up
public void onMatch(RelOptRuleCall call) {
  final EnumerableFilter filter = call.rel(0);
  final RelNode input = filter.getInput();

  // Create a program containing a filter.
  final RexBuilder rexBuilder = filter.getCluster().getRexBuilder();
  final RelDataType inputRowType = input.getRowType();
  final RexProgramBuilder programBuilder =
      new RexProgramBuilder(inputRowType, rexBuilder);
  programBuilder.addIdentity();
  programBuilder.addCondition(filter.getCondition());
  final RexProgram program = programBuilder.getProgram();

  final EnumerableCalc calc = EnumerableCalc.create(input, program);
  call.transformTo(calc);
}
 
Example 4
Source File: RelMdColumnOrigins.java    From Bats with Apache License 2.0 5 votes vote down vote up
public Set<RelColumnOrigin> getColumnOrigins(RelNode rel,
    RelMetadataQuery mq, int iOutputColumn) {
  // NOTE jvs 28-Mar-2006: We may get this wrong for a physical table
  // expression which supports projections.  In that case,
  // it's up to the plugin writer to override with the
  // correct information.

  if (rel.getInputs().size() > 0) {
    // No generic logic available for non-leaf rels.
    return null;
  }

  final Set<RelColumnOrigin> set = new HashSet<>();

  RelOptTable table = rel.getTable();
  if (table == null) {
    // Somebody is making column values up out of thin air, like a
    // VALUES clause, so we return an empty set.
    return set;
  }

  // Detect the case where a physical table expression is performing
  // projection, and say we don't know instead of making any assumptions.
  // (Theoretically we could try to map the projection using column
  // names.)  This detection assumes the table expression doesn't handle
  // rename as well.
  if (table.getRowType() != rel.getRowType()) {
    return null;
  }

  set.add(new RelColumnOrigin(table, iOutputColumn, false));
  return set;
}
 
Example 5
Source File: PythonCorrelateSplitRule.java    From flink with Apache License 2.0 5 votes vote down vote up
private FlinkLogicalCalc createNewLeftCalc(
	RelNode left,
	RexBuilder rexBuilder,
	ArrayBuffer<RexNode> extractedRexNodes,
	FlinkLogicalCorrelate correlate) {
	// add the fields of the primitive left input.
	List<RexNode> leftCalcProjects = new LinkedList<>();
	RelDataType leftRowType = left.getRowType();
	List<String> leftCalcCalcFieldNames = createNewFieldNames(
		leftRowType,
		rexBuilder,
		leftRowType.getFieldCount(),
		extractedRexNodes,
		leftCalcProjects);

	// create a new calc
	return new FlinkLogicalCalc(
		correlate.getCluster(),
		correlate.getTraitSet(),
		left,
		RexProgram.create(
			leftRowType,
			leftCalcProjects,
			null,
			leftCalcCalcFieldNames,
			rexBuilder));
}
 
Example 6
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
private RexVisitorImpl<Void> handleSubQuery(final RelNode rel) {
	return new RexVisitorImpl<Void>(true) {

		@Override
		public Void visitSubQuery(RexSubQuery subQuery) {
			RelNode newRel = subQuery.rel;
			if (subQuery.getKind() == SqlKind.IN) {
				newRel = addProjectionForIn(subQuery.rel);
			}
			final Frame frame = decorrelator.getInvoke(newRel);
			if (frame != null && frame.c != null) {

				Frame target = frame;
				if (subQuery.getKind() == SqlKind.EXISTS) {
					target = addProjectionForExists(frame);
				}

				final DecorrelateRexShuttle shuttle = new DecorrelateRexShuttle(
						rel.getRowType(),
						target.r.getRowType(),
						rel.getVariablesSet());

				final RexNode newCondition = target.c.accept(shuttle);
				Pair<RelNode, RexNode> newNodeAndCondition = new Pair<>(target.r, newCondition);
				subQueryMap.put(subQuery, newNodeAndCondition);
			}
			return null;
		}
	};
}
 
Example 7
Source File: SubQueryDecorrelator.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Adds Projection to choose the fields used by join condition.
 */
private Frame addProjectionForExists(Frame frame) {
	final List<Integer> corIndices = new ArrayList<>(frame.getCorInputRefIndices());
	final RelNode rel = frame.r;
	final RelDataType rowType = rel.getRowType();
	if (corIndices.size() == rowType.getFieldCount()) {
		// no need projection
		return frame;
	}

	final List<RexNode> projects = new ArrayList<>();
	final Map<Integer, Integer> mapInputToOutput = new HashMap<>();

	Collections.sort(corIndices);
	int newPos = 0;
	for (int index : corIndices) {
		projects.add(RexInputRef.of(index, rowType));
		mapInputToOutput.put(index, newPos++);
	}

	relBuilder.clear();
	relBuilder.push(frame.r);
	relBuilder.project(projects);
	final RelNode newProject = relBuilder.build();
	final RexNode newCondition = adjustInputRefs(frame.c, mapInputToOutput, newProject.getRowType());

	// There is no old RelNode corresponding to newProject, so oldToNewOutputs is empty.
	return new Frame(rel, newProject, newCondition, new HashMap<>());
}
 
Example 8
Source File: ViewHandler.java    From Bats with Apache License 2.0 5 votes vote down vote up
@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
  SqlCreateView createView = unwrap(sqlNode, SqlCreateView.class);

  final String newViewName = FileSelection.removeLeadingSlash(createView.getName());

  // Disallow temporary tables usage in view definition
  config.getConverter().disallowTemporaryTables();
  // Store the viewSql as view def SqlNode is modified as part of the resolving the new table definition below.
  final String viewSql = createView.getQuery().toString();
  final ConvertedRelNode convertedRelNode = validateAndConvert(createView.getQuery());
  final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
  final RelNode queryRelNode = convertedRelNode.getConvertedNode();

  final RelNode newViewRelNode = SqlHandlerUtil.resolveNewTableRel(true, createView.getFieldNames(), validatedRowType, queryRelNode);

  final SchemaPlus defaultSchema = context.getNewDefaultSchema();
  final AbstractSchema drillSchema = SchemaUtilites.resolveToMutableDrillSchema(defaultSchema, createView.getSchemaPath());

  final View view = new View(newViewName, viewSql, newViewRelNode.getRowType(),
      SchemaUtilites.getSchemaPathAsList(defaultSchema));
  final String schemaPath = drillSchema.getFullSchemaName();

  // check view creation possibility
  if(!checkViewCreationPossibility(drillSchema, createView, context)) {
    return DirectPlan
      .createDirectPlan(context, false, String.format("A table or view with given name [%s] already exists in schema [%s]", view.getName(), schemaPath));
  }

  final boolean replaced = drillSchema.createView(view);
  final String summary = String.format("View '%s' %s successfully in '%s' schema",
      newViewName, replaced ? "replaced" : "created", drillSchema.getFullSchemaName());

  return DirectPlan.createDirectPlan(context, true, summary);
}
 
Example 9
Source File: RexShuttleTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-3165">[CALCITE-3165]
 * Project#accept(RexShuttle shuttle) does not update rowType</a>. */
@Test void testProjectUpdatesRowType() {
  final RelBuilder builder = RelBuilder.create(RelBuilderTest.config().build());

  // Equivalent SQL: SELECT deptno, sal FROM emp
  final RelNode root =
      builder
          .scan("EMP")
          .project(
              builder.field("DEPTNO"),
              builder.field("SAL"))
          .build();

  // Equivalent SQL: SELECT CAST(deptno AS VARCHAR), CAST(sal AS VARCHAR) FROM emp
  final RelNode rootWithCast =
      builder
          .scan("EMP")
          .project(
              builder.cast(builder.field("DEPTNO"), SqlTypeName.VARCHAR),
              builder.cast(builder.field("SAL"), SqlTypeName.VARCHAR))
          .build();
  final RelDataType type = rootWithCast.getRowType();

  // Transform the first expression into the second one, by using a RexShuttle
  // that converts every RexInputRef into a 'CAST(RexInputRef AS VARCHAR)'
  final RelNode rootWithCastViaRexShuttle = root.accept(new RexShuttle() {
    @Override public RexNode visitInputRef(RexInputRef inputRef) {
      return  builder.cast(inputRef, SqlTypeName.VARCHAR);
    }
  });
  final RelDataType type2 = rootWithCastViaRexShuttle.getRowType();

  assertThat(type, is(type2));
}
 
Example 10
Source File: RelBuilder.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Returns references to the fields of a given input. */
public ImmutableList<RexNode> fields(int inputCount, int inputOrdinal) {
    final RelNode input = peek(inputCount, inputOrdinal);
    final RelDataType rowType = input.getRowType();
    final ImmutableList.Builder<RexNode> nodes = ImmutableList.builder();
    for (int fieldOrdinal : Util.range(rowType.getFieldCount())) {
        nodes.add(field(inputCount, inputOrdinal, fieldOrdinal));
    }
    return nodes.build();
}
 
Example 11
Source File: SqlImplementor.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Returns the row type of {@code rel}, adjusting the field names if
 * {@code node} is "(query) as tableAlias (fieldAlias, ...)". */
private RelDataType adjustedRowType(RelNode rel, SqlNode node) {
  final RelDataType rowType = rel.getRowType();
  final RelDataTypeFactory.Builder builder;
  switch (node.getKind()) {
  case UNION:
  case INTERSECT:
  case EXCEPT:
    return adjustedRowType(rel, ((SqlCall) node).getOperandList().get(0));

  case SELECT:
    final SqlNodeList selectList = ((SqlSelect) node).getSelectList();
    if (selectList == null) {
      return rowType;
    }
    builder = rel.getCluster().getTypeFactory().builder();
    Pair.forEach(selectList,
        rowType.getFieldList(),
        (selectItem, field) ->
            builder.add(
                Util.first(SqlValidatorUtil.getAlias(selectItem, -1),
                    field.getName()),
                field.getType()));
    return builder.build();

  case AS:
    final List<SqlNode> operandList = ((SqlCall) node).getOperandList();
    if (operandList.size() <= 2) {
      return rowType;
    }
    builder = rel.getCluster().getTypeFactory().builder();
    Pair.forEach(Util.skip(operandList, 2),
        rowType.getFieldList(),
        (operand, field) ->
            builder.add(operand.toString(), field.getType()));
    return builder.build();

  default:
    return rowType;
  }
}
 
Example 12
Source File: FindFiltersForCollation.java    From Bats with Apache License 2.0 4 votes vote down vote up
public FindFiltersForCollation(RelNode input) {
  super(true);
  inputRowType = input.getRowType();
  init();
}
 
Example 13
Source File: MutableLeafRel.java    From calcite with Apache License 2.0 4 votes vote down vote up
protected MutableLeafRel(MutableRelType type, RelNode rel) {
  super(rel.getCluster(), rel.getRowType(), type);
  this.rel = rel;
}
 
Example 14
Source File: CalcitePlanner.java    From herddb with Apache License 2.0 4 votes vote down vote up
private PlannerResult runPlanner(String defaultTableSpace, String query) throws RelConversionException,
        SqlParseException, ValidationException, MetadataStorageManagerException, StatementExecutionException {
    SchemaPlus subSchema = getSchemaForTableSpace(defaultTableSpace);
    if (subSchema == null) {
        clearCache();
        throw new StatementExecutionException("tablespace " + defaultTableSpace + " is not available");
    }
    Properties props = new Properties();
    props.put(CalciteConnectionProperty.TIME_ZONE.camelName(), TimeZone.getDefault().getID());
    props.put(CalciteConnectionProperty.LOCALE.camelName(), Locale.ROOT.toString());
    final CalciteConnectionConfigImpl calciteRuntimeContextConfig = new CalciteConnectionConfigImpl(props);

    final FrameworkConfig config = Frameworks.newConfigBuilder()
            .parserConfig(SQL_PARSER_CONFIG)
            .defaultSchema(subSchema)
            .traitDefs(TRAITS)
            .context(new Context() {
                @Override
                public <C> C unwrap(Class<C> aClass) {
                    if (aClass == CalciteConnectionConfigImpl.class
                            || aClass == CalciteConnectionConfig.class) {
                        return (C) calciteRuntimeContextConfig;
                    }
                    return null;
                }
            })
            // define the rules you want to apply
            .programs(Programs.ofRules(Programs.RULE_SET))
            .build();
    Planner planner = Frameworks.getPlanner(config);
    if (LOG.isLoggable(Level.FINER)) {
        LOG.log(Level.FINER, "Query: {0}", query);
    }
    try {
        SqlNode n = planner.parse(query);
        n = planner.validate(n);
        RelNode logicalPlan = planner.rel(n).project();
        if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
            LOG.log(DUMP_QUERY_LEVEL, "Query: {0} {1}", new Object[]{query,
                RelOptUtil.dumpPlan("-- Logical Plan", logicalPlan, SqlExplainFormat.TEXT,
                SqlExplainLevel.ALL_ATTRIBUTES)});
        }
        RelDataType originalRowType = logicalPlan.getRowType();
        RelOptCluster cluster = logicalPlan.getCluster();
        final RelOptPlanner optPlanner = cluster.getPlanner();

        optPlanner.addRule(ReduceExpressionsRule.FILTER_INSTANCE);
        RelTraitSet desiredTraits =
                cluster.traitSet()
                        .replace(EnumerableConvention.INSTANCE);
        final RelCollation collation =
                logicalPlan instanceof Sort
                        ? ((Sort) logicalPlan).collation
                        : null;
        if (collation != null) {
            desiredTraits = desiredTraits.replace(collation);
        }
        final RelNode newRoot = optPlanner.changeTraits(logicalPlan, desiredTraits);
        optPlanner.setRoot(newRoot);
        RelNode bestExp = optPlanner.findBestExp();
        if (LOG.isLoggable(DUMP_QUERY_LEVEL)) {
            LOG.log(DUMP_QUERY_LEVEL, "Query: {0} {1}", new Object[]{query,
                RelOptUtil.dumpPlan("-- Best  Plan", bestExp, SqlExplainFormat.TEXT,
                SqlExplainLevel.ALL_ATTRIBUTES)});
        }

        return new PlannerResult(bestExp, originalRowType, logicalPlan, n);
    } catch (AssertionError err) {
        throw new StatementExecutionException("Internal Calcite error " + err, err);
    }
}
 
Example 15
Source File: MycatRelBuilder.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
public  RelNode makeTransientSQLScan(String targetName, RelNode input,boolean forUpdate) {
    RelDataType rowType = input.getRowType();
    MycatConvention convention = getConvertion(targetName);
    return makeBySql(targetName,rowType,MycatCalciteSupport.INSTANCE.convertToSql(input, convention.dialect,forUpdate));
}
 
Example 16
Source File: RelStructuredTypeFlattener.java    From calcite with Apache License 2.0 4 votes vote down vote up
public void rewriteRel(LogicalCalc rel) {
  // Translate the child.
  final RelNode newInput = getNewForOldRel(rel.getInput());

  final RelOptCluster cluster = rel.getCluster();
  RexProgramBuilder programBuilder =
      new RexProgramBuilder(
          newInput.getRowType(),
          cluster.getRexBuilder());

  // Convert the common expressions.
  final RexProgram program = rel.getProgram();
  final RewriteRexShuttle shuttle = new RewriteRexShuttle();
  for (RexNode expr : program.getExprList()) {
    programBuilder.registerInput(expr.accept(shuttle));
  }

  // Convert the projections.
  final List<Pair<RexNode, String>> flattenedExpList = new ArrayList<>();
  List<String> fieldNames = rel.getRowType().getFieldNames();
  flattenProjections(new RewriteRexShuttle(),
      program.getProjectList(),
      fieldNames,
      "",
      flattenedExpList);

  // Register each of the new projections.
  for (Pair<RexNode, String> flattenedExp : flattenedExpList) {
    programBuilder.addProject(flattenedExp.left, flattenedExp.right);
  }

  // Translate the condition.
  final RexLocalRef conditionRef = program.getCondition();
  if (conditionRef != null) {
    final Ord<RelDataType> newField =
        getNewFieldForOldInput(conditionRef.getIndex());
    programBuilder.addCondition(new RexLocalRef(newField.i, newField.e));
  }

  RexProgram newProgram = programBuilder.getProgram();

  // Create a new calc relational expression.
  LogicalCalc newRel = LogicalCalc.create(newInput, newProgram);
  setNewForOldRel(rel, newRel);
}
 
Example 17
Source File: CreateViewHandler.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
public List<SimpleCommandResult> toResult(String sql, SqlNode sqlNode) throws Exception {
    SqlCreateView createView = SqlNodeUtil.unwrap(sqlNode, SqlCreateView.class);
    final String newViewName = createView.getName();

    // Store the viewSql as view def SqlNode is modified as part of the resolving the new table definition below.
    final String viewSql = createView.getQuery().toSqlString(new SqlDialect(SqlDialect.CALCITE.getDatabaseProduct(), SqlDialect.CALCITE.getDatabaseProduct().name(), ParserConfig.QUOTING.string)).getSql();
    final ConvertedRelNode convertedRelNode = PrelTransformer.validateAndConvert(config, createView.getQuery());
    final RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
    final RelNode queryRelNode = convertedRelNode.getConvertedNode();
    final RelNode newViewRelNode = SqlHandlerUtil.resolveNewTableRel(true, createView.getFieldNames(), validatedRowType, queryRelNode);
    Catalog catalog = config.getContext().getCatalog();
    NamespaceKey viewPath = catalog.resolveSingle(createView.getPath());
    NamespaceKey defaultSchema = catalog.getDefaultSchema();

    final DremioTable existingTable = config.getContext().getCatalog().getTableNoResolve(viewPath);
    List<String> viewContext = defaultSchema == null ? null : defaultSchema.getPathComponents();

    final View view = new View(newViewName, viewSql, newViewRelNode.getRowType(), createView.getFieldNames(), viewContext);

    boolean replaced = false;

    if (existingTable != null) {
      if (existingTable.getJdbcTableType() != Schema.TableType.VIEW) {
        // existing table is not a view
        throw UserException.validationError()
            .message("A non-view table with given name [%s] already exists in schema [%s]",
                newViewName, viewPath.getParent()
            )
            .build(logger);
      }

      if (existingTable.getJdbcTableType() == Schema.TableType.VIEW && !createView.getReplace()) {
        // existing table is a view and create view has no "REPLACE" clause
        throw UserException.validationError()
            .message("A view with given name [%s] already exists in schema [%s]",
                newViewName, viewPath.getParent()
            )
            .build(logger);
      }

      config.getContext().getCatalog().updateView(viewPath, view);
      replaced = true;
    } else {
      config.getContext().getCatalog().createView(viewPath, view);
    }

    return Collections.singletonList(SimpleCommandResult.successful("View '%s' %s successfully",
        viewPath, replaced ? "replaced" : "created"));
}
 
Example 18
Source File: MutableLeafRel.java    From Bats with Apache License 2.0 4 votes vote down vote up
protected MutableLeafRel(MutableRelType type, RelNode rel) {
  super(rel.getCluster(), rel.getRowType(), type);
  this.rel = rel;
}
 
Example 19
Source File: PigRelBuilder.java    From calcite with Apache License 2.0 3 votes vote down vote up
/**
 * Projects a specific row type out of a relation algebra operator.
 * For any field in output type, if there is no matching input field, we project
 * null value of the corresponding output field type.
 *
 * <p>For example, given:
 * <ul>
 * <li>Input rel {@code A} with {@code A_type(X: int, Y: varchar)}
 * <li>Output type {@code B_type(X: int, Y: varchar, Z: boolean, W: double)}
 * </ul>
 *
 * <p>{@code project(A, B_type)} gives new relation
 * {@code C(X: int, Y: varchar, null, null)}.
 *
 * @param input The relation algebra operator to be projected
 * @param outputType The data type for the projected relation algebra operator
 * @return The projected relation algebra operator
 */
public RelNode project(RelNode input, RelDataType outputType) {
  final RelDataType inputType = input.getRowType();
  if (compatibleType(inputType, outputType)
      && inputType.getFieldNames().equals(outputType.getFieldNames())) {
    // Same data type, simply returns the input rel
    return input;
  }

  // Now build the projection expressions on top of the input rel.
  push(input);
  project(projects(inputType, outputType), outputType.getFieldNames(), true);
  return build();
}
 
Example 20
Source File: StrippingFactory.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Return a strip result with no stripping applied.
 * @param node
 * @return
 */
public static StripResult noStrip(RelNode node) {
  return new StripResult(ExpansionNode.removeFromTree(node), new StripLeaf(node.getCluster(), node.getCluster().traitSet(), node.getRowType()));
}