Java Code Examples for org.apache.calcite.sql.SqlMatchRecognize

The following examples show how to use org.apache.calcite.sql.SqlMatchRecognize. These examples are extracted from open source projects. 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 Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: 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 Project: flink   Source File: SqlValidatorImpl.java    License: 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 Project: Bats   Source File: MatchRecognizeNamespace.java    License: 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 4
Source Project: Bats   Source File: MatchRecognizeScope.java    License: 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 Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: 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 6
Source Project: flink   Source File: SqlValidatorImpl.java    License: 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 Project: calcite   Source File: MatchRecognizeNamespace.java    License: 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 Project: calcite   Source File: MatchRecognizeScope.java    License: 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 9
Source Project: Bats   Source File: MatchRecognizeNamespace.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlMatchRecognize getNode() {
  return matchRecognize;
}
 
Example 10
Source Project: Bats   Source File: MatchRecognizeScope.java    License: Apache License 2.0 4 votes vote down vote up
public SqlMatchRecognize getMatchRecognize() {
  return matchRecognize;
}
 
Example 11
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
public SqlValidatorScope getMatchRecognizeScope(SqlMatchRecognize node) {
	return scopes.get(node);
}
 
Example 12
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
protected MatchRecognizeNamespace createMatchRecognizeNameSpace(
	SqlMatchRecognize call,
	SqlNode enclosingNode) {
	return new MatchRecognizeNamespace(this, call, enclosingNode);
}
 
Example 13
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
public SqlValidatorScope getMatchRecognizeScope(SqlMatchRecognize node) {
	return scopes.get(node);
}
 
Example 14
Source Project: flink   Source File: SqlValidatorImpl.java    License: 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 Project: calcite   Source File: MatchRecognizeNamespace.java    License: Apache License 2.0 4 votes vote down vote up
@Override public SqlMatchRecognize getNode() {
  return matchRecognize;
}
 
Example 16
Source Project: calcite   Source File: MatchRecognizeScope.java    License: Apache License 2.0 4 votes vote down vote up
public SqlMatchRecognize getMatchRecognize() {
  return matchRecognize;
}
 
Example 17
Source Project: calcite   Source File: RelBuilderTest.java    License: 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 18
Source Project: Bats   Source File: SqlValidator.java    License: 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 Project: calcite   Source File: SqlValidator.java    License: 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);