org.apache.calcite.sql.SqlMatchRecognize Java Examples

The following examples show how to use org.apache.calcite.sql.SqlMatchRecognize. 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: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
private void registerMatchRecognize(
	SqlValidatorScope parentScope,
	SqlValidatorScope usingScope,
	SqlMatchRecognize call,
	SqlNode enclosingNode,
	String alias,
	boolean forceNullable) {

	final MatchRecognizeNamespace matchRecognizeNamespace =
		createMatchRecognizeNameSpace(call, enclosingNode);
	registerNamespace(usingScope, alias, matchRecognizeNamespace, forceNullable);

	final MatchRecognizeScope matchRecognizeScope =
		new MatchRecognizeScope(parentScope, call);
	scopes.put(call, matchRecognizeScope);

	// parse input query
	SqlNode expr = call.getTableRef();
	SqlNode newExpr = registerFrom(usingScope, matchRecognizeScope, true, expr,
		expr, null, null, forceNullable, false);
	if (expr != newExpr) {
		call.setOperand(0, newExpr);
	}
}
 
Example #2
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
private void registerMatchRecognize(
	SqlValidatorScope parentScope,
	SqlValidatorScope usingScope,
	SqlMatchRecognize call,
	SqlNode enclosingNode,
	String alias,
	boolean forceNullable) {

	final MatchRecognizeNamespace matchRecognizeNamespace =
		createMatchRecognizeNameSpace(call, enclosingNode);
	registerNamespace(usingScope, alias, matchRecognizeNamespace, forceNullable);

	final MatchRecognizeScope matchRecognizeScope =
		new MatchRecognizeScope(parentScope, call);
	scopes.put(call, matchRecognizeScope);

	// parse input query
	SqlNode expr = call.getTableRef();
	SqlNode newExpr = registerFrom(usingScope, matchRecognizeScope, true, expr,
		expr, null, null, forceNullable, false);
	if (expr != newExpr) {
		call.setOperand(0, newExpr);
	}
}
 
Example #3
Source File: MatchRecognizeScope.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a MatchRecognizeScope. */
public MatchRecognizeScope(SqlValidatorScope parent,
    SqlMatchRecognize matchRecognize) {
  super(parent);
  this.matchRecognize = matchRecognize;
  patternVars = validator.getCatalogReader().nameMatcher().createSet();
  patternVars.add(STAR);
}
 
Example #4
Source File: MatchRecognizeScope.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a MatchRecognizeScope. */
public MatchRecognizeScope(SqlValidatorScope parent,
    SqlMatchRecognize matchRecognize) {
  super(parent);
  this.matchRecognize = matchRecognize;
  patternVars = validator.getCatalogReader().nameMatcher().createSet();
  patternVars.add(STAR);
}
 
Example #5
Source File: MatchRecognizeNamespace.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Creates a MatchRecognizeNamespace. */
protected MatchRecognizeNamespace(SqlValidatorImpl validator,
    SqlMatchRecognize matchRecognize,
    SqlNode enclosingNode) {
  super(validator, enclosingNode);
  this.matchRecognize = matchRecognize;
}
 
Example #6
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private List<Map.Entry<String, RelDataType>> validateMeasure(SqlMatchRecognize mr,
	MatchRecognizeScope scope, boolean allRows) {
	final List<String> aliases = new ArrayList<>();
	final List<SqlNode> sqlNodes = new ArrayList<>();
	final SqlNodeList measures = mr.getMeasureList();
	final List<Map.Entry<String, RelDataType>> fields = new ArrayList<>();

	for (SqlNode measure : measures) {
		assert measure instanceof SqlCall;
		final String alias = deriveAlias(measure, aliases.size());
		aliases.add(alias);

		SqlNode expand = expand(measure, scope);
		expand = navigationInMeasure(expand, allRows);
		setOriginal(expand, measure);

		inferUnknownTypes(unknownType, scope, expand);
		final RelDataType type = deriveType(scope, expand);
		setValidatedNodeType(measure, type);

		fields.add(Pair.of(alias, type));
		sqlNodes.add(
			SqlStdOperatorTable.AS.createCall(SqlParserPos.ZERO, expand,
				new SqlIdentifier(alias, SqlParserPos.ZERO)));
	}

	SqlNodeList list = new SqlNodeList(sqlNodes, measures.getParserPosition());
	inferUnknownTypes(unknownType, scope, list);

	for (SqlNode node : list) {
		validateExpr(node, scope);
	}

	mr.setOperand(SqlMatchRecognize.OPERAND_MEASURES, list);

	return fields;
}
 
Example #7
Source File: MatchRecognizeNamespace.java    From Bats with Apache License 2.0 5 votes vote down vote up
/** Creates a MatchRecognizeNamespace. */
protected MatchRecognizeNamespace(SqlValidatorImpl validator,
    SqlMatchRecognize matchRecognize,
    SqlNode enclosingNode) {
  super(validator, enclosingNode);
  this.matchRecognize = matchRecognize;
}
 
Example #8
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 5 votes vote down vote up
private List<Map.Entry<String, RelDataType>> validateMeasure(SqlMatchRecognize mr,
	MatchRecognizeScope scope, boolean allRows) {
	final List<String> aliases = new ArrayList<>();
	final List<SqlNode> sqlNodes = new ArrayList<>();
	final SqlNodeList measures = mr.getMeasureList();
	final List<Map.Entry<String, RelDataType>> fields = new ArrayList<>();

	for (SqlNode measure : measures) {
		assert measure instanceof SqlCall;
		final String alias = deriveAlias(measure, aliases.size());
		aliases.add(alias);

		SqlNode expand = expand(measure, scope);
		expand = navigationInMeasure(expand, allRows);
		setOriginal(expand, measure);

		inferUnknownTypes(unknownType, scope, expand);
		final RelDataType type = deriveType(scope, expand);
		setValidatedNodeType(measure, type);

		fields.add(Pair.of(alias, type));
		sqlNodes.add(
			SqlStdOperatorTable.AS.createCall(SqlParserPos.ZERO, expand,
				new SqlIdentifier(alias, SqlParserPos.ZERO)));
	}

	SqlNodeList list = new SqlNodeList(sqlNodes, measures.getParserPosition());
	inferUnknownTypes(unknownType, scope, list);

	for (SqlNode node : list) {
		validateExpr(node, scope);
	}

	mr.setOperand(SqlMatchRecognize.OPERAND_MEASURES, list);

	return fields;
}
 
Example #9
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
public SqlValidatorScope getMatchRecognizeScope(SqlMatchRecognize node) {
	return scopes.get(node);
}
 
Example #10
Source File: RelBuilderTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Test void testMatchRecognize() {
  // Equivalent SQL:
  //   SELECT *
  //   FROM emp
  //   MATCH_RECOGNIZE (
  //     PARTITION BY deptno
  //     ORDER BY empno asc
  //     MEASURES
  //       STRT.mgr as start_nw,
  //       LAST(DOWN.mgr) as bottom_nw,
  //     PATTERN (STRT DOWN+ UP+) WITHIN INTERVAL '5' SECOND
  //     DEFINE
  //       DOWN as DOWN.mgr < PREV(DOWN.mgr),
  //       UP as UP.mgr > PREV(UP.mgr)
  //   )
  final RelBuilder builder = RelBuilder.create(config().build()).scan("EMP");
  final RelDataTypeFactory typeFactory = builder.getTypeFactory();
  final RelDataType intType = typeFactory.createSqlType(SqlTypeName.INTEGER);

  RexNode pattern = builder.patternConcat(
      builder.literal("STRT"),
      builder.patternQuantify(builder.literal("DOWN"), builder.literal(1),
          builder.literal(-1), builder.literal(false)),
      builder.patternQuantify(builder.literal("UP"), builder.literal(1),
          builder.literal(-1), builder.literal(false)));

  ImmutableMap.Builder<String, RexNode> pdBuilder = new ImmutableMap.Builder<>();
  RexNode downDefinition = builder.call(SqlStdOperatorTable.LESS_THAN,
      builder.call(SqlStdOperatorTable.PREV,
          builder.patternField("DOWN", intType, 3),
          builder.literal(0)),
      builder.call(SqlStdOperatorTable.PREV,
          builder.patternField("DOWN", intType, 3),
          builder.literal(1)));
  pdBuilder.put("DOWN", downDefinition);
  RexNode upDefinition = builder.call(SqlStdOperatorTable.GREATER_THAN,
      builder.call(SqlStdOperatorTable.PREV,
          builder.patternField("UP", intType, 3),
          builder.literal(0)),
      builder.call(SqlStdOperatorTable.PREV,
          builder.patternField("UP", intType, 3),
          builder.literal(1)));
  pdBuilder.put("UP", upDefinition);

  ImmutableList.Builder<RexNode> measuresBuilder = new ImmutableList.Builder<>();
  measuresBuilder.add(
      builder.alias(builder.patternField("STRT", intType, 3),
          "start_nw"));
  measuresBuilder.add(
      builder.alias(
          builder.call(SqlStdOperatorTable.LAST,
              builder.patternField("DOWN", intType, 3),
              builder.literal(0)),
          "bottom_nw"));

  RexNode after = builder.getRexBuilder().makeFlag(
      SqlMatchRecognize.AfterOption.SKIP_TO_NEXT_ROW);

  ImmutableList.Builder<RexNode> partitionKeysBuilder = new ImmutableList.Builder<>();
  partitionKeysBuilder.add(builder.field("DEPTNO"));

  ImmutableList.Builder<RexNode> orderKeysBuilder = new ImmutableList.Builder<>();
  orderKeysBuilder.add(builder.field("EMPNO"));

  RexNode interval = builder.literal("INTERVAL '5' SECOND");

  final ImmutableMap<String, TreeSet<String>> subsets = ImmutableMap.of();
  final RelNode root = builder
      .match(pattern, false, false, pdBuilder.build(),
          measuresBuilder.build(), after, subsets, false,
          partitionKeysBuilder.build(), orderKeysBuilder.build(), interval)
      .build();
  final String expected = "LogicalMatch(partition=[[7]], order=[[0]], "
      + "outputFields=[[$7, 'start_nw', 'bottom_nw']], allRows=[false], "
      + "after=[FLAG(SKIP TO NEXT ROW)], pattern=[(('STRT', "
      + "PATTERN_QUANTIFIER('DOWN', 1, -1, false)), "
      + "PATTERN_QUANTIFIER('UP', 1, -1, false))], "
      + "isStrictStarts=[false], isStrictEnds=[false], "
      + "interval=['INTERVAL ''5'' SECOND'], subsets=[[]], "
      + "patternDefinitions=[[<(PREV(DOWN.$3, 0), PREV(DOWN.$3, 1)), "
      + ">(PREV(UP.$3, 0), PREV(UP.$3, 1))]], "
      + "inputFields=[[EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO]])\n"
      + "  LogicalTableScan(table=[[scott, EMP]])\n";
  assertThat(root, hasTree(expected));
}
 
Example #11
Source File: MatchRecognizeScope.java    From calcite with Apache License 2.0 4 votes vote down vote up
public SqlMatchRecognize getMatchRecognize() {
  return matchRecognize;
}
 
Example #12
Source File: MatchRecognizeNamespace.java    From calcite with Apache License 2.0 4 votes vote down vote up
@Override public SqlMatchRecognize getNode() {
  return matchRecognize;
}
 
Example #13
Source File: SqlValidatorImpl.java    From flink with Apache License 2.0 4 votes vote down vote up
protected MatchRecognizeNamespace createMatchRecognizeNameSpace(
	SqlMatchRecognize call,
	SqlNode enclosingNode) {
	return new MatchRecognizeNamespace(this, call, enclosingNode);
}
 
Example #14
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
protected MatchRecognizeNamespace createMatchRecognizeNameSpace(
	SqlMatchRecognize call,
	SqlNode enclosingNode) {
	return new MatchRecognizeNamespace(this, call, enclosingNode);
}
 
Example #15
Source File: SqlValidatorImpl.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public SqlValidatorScope getMatchRecognizeScope(SqlMatchRecognize node) {
	return scopes.get(node);
}
 
Example #16
Source File: MatchRecognizeScope.java    From Bats with Apache License 2.0 4 votes vote down vote up
public SqlMatchRecognize getMatchRecognize() {
  return matchRecognize;
}
 
Example #17
Source File: MatchRecognizeNamespace.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override public SqlMatchRecognize getNode() {
  return matchRecognize;
}
 
Example #18
Source File: SqlValidator.java    From calcite with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a scope match recognize clause.
 *
 * @param node Match recognize
 * @return naming scope for Match recognize clause
 */
SqlValidatorScope getMatchRecognizeScope(SqlMatchRecognize node);
 
Example #19
Source File: SqlValidator.java    From Bats with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a scope match recognize clause.
 *
 * @param node Match recognize
 * @return naming scope for Match recognize clause
 */
SqlValidatorScope getMatchRecognizeScope(SqlMatchRecognize node);