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

The following examples show how to use org.apache.calcite.sql.SqlInsert. 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
protected RelDataType getLogicalTargetRowType(
	RelDataType targetRowType,
	SqlInsert insert) {
	if (insert.getTargetColumnList() == null
		&& conformance.isInsertSubsetColumnsAllowed()) {
		// Target an implicit subset of columns.
		final SqlNode source = insert.getSource();
		final RelDataType sourceRowType = getNamespace(source).getRowType();
		final RelDataType logicalSourceRowType =
			getLogicalSourceRowType(sourceRowType, insert);
		final RelDataType implicitTargetRowType =
			typeFactory.createStructType(
				targetRowType.getFieldList()
					.subList(0, logicalSourceRowType.getFieldCount()));
		final SqlValidatorNamespace targetNamespace = getNamespace(insert);
		validateNamespace(targetNamespace, implicitTargetRowType);
		return implicitTargetRowType;
	} else {
		// Either the set of columns are explicitly targeted, or target the full
		// set of columns.
		return targetRowType;
	}
}
 
Example 2
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 6 votes vote down vote up
protected RelDataType getLogicalTargetRowType(
	RelDataType targetRowType,
	SqlInsert insert) {
	if (insert.getTargetColumnList() == null
		&& conformance.isInsertSubsetColumnsAllowed()) {
		// Target an implicit subset of columns.
		final SqlNode source = insert.getSource();
		final RelDataType sourceRowType = getNamespace(source).getRowType();
		final RelDataType logicalSourceRowType =
			getLogicalSourceRowType(sourceRowType, insert);
		final RelDataType implicitTargetRowType =
			typeFactory.createStructType(
				targetRowType.getFieldList()
					.subList(0, logicalSourceRowType.getFieldCount()));
		final SqlValidatorNamespace targetNamespace = getNamespace(insert);
		validateNamespace(targetNamespace, implicitTargetRowType);
		return implicitTargetRowType;
	} else {
		// Either the set of columns are explicitly targeted, or target the full
		// set of columns.
		return targetRowType;
	}
}
 
Example 3
Source Project: alchemy   Source File: SideParser.java    License: Apache License 2.0 6 votes vote down vote up
public static void parse(SqlNode sqlNode, Deque<SqlNode> deque) {
    deque.offer(sqlNode);
    SqlKind sqlKind = sqlNode.getKind();
    switch (sqlKind) {
        case INSERT:
            SqlNode sqlSource = ((SqlInsert)sqlNode).getSource();
            parse(sqlSource, deque);
            break;
        case SELECT:
            SqlNode sqlFrom = ((SqlSelect)sqlNode).getFrom();
            parse(sqlFrom, deque);
            break;
        case JOIN:
            SqlNode sqlLeft = ((SqlJoin)sqlNode).getLeft();
            SqlNode sqlRight = ((SqlJoin)sqlNode).getRight();
            parse(sqlLeft, deque);
            parse(sqlRight, deque);
            break;
        case AS:
            SqlNode sqlAs = ((SqlBasicCall)sqlNode).getOperands()[0];
            parse(sqlAs, deque);
            break;
        default:
            return;
    }
}
 
Example 4
Source Project: alchemy   Source File: SideParser.java    License: Apache License 2.0 6 votes vote down vote up
public static void rewrite(SqlNode sqlNode, SqlSelect sqlSelect) {
    SqlKind sqlKind = sqlNode.getKind();
    switch (sqlKind) {
        case INSERT:
            SqlInsert sqlInsert = ((SqlInsert)sqlNode);
            sqlInsert.setSource(sqlSelect);
            break;
        case SELECT:
            SqlSelect select = (SqlSelect)sqlNode;
            select.setFrom(sqlSelect);
            break;
        case AS:
            SqlBasicCall basicCall = (SqlBasicCall)sqlNode;
            basicCall.setOperand(0, sqlSelect);
            break;
        default:
            throw new UnsupportedOperationException(sqlKind + "目前不支持维表操作");
    }
}
 
Example 5
Source Project: alchemy   Source File: SqlParseUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void parse(List<String> sqls, List<String> sources, List<String> udfs, List<String> sinks)
    throws SqlParseException {
    for (String sql : sqls) {
        SqlParser sqlParser = SqlParser.create(sql, CONFIG);
        SqlNode sqlNode = sqlParser.parseStmt();
        SqlKind kind = sqlNode.getKind();
        switch (kind){
            case INSERT:
                SqlInsert sqlInsert = (SqlInsert)sqlNode;
                addSink(sinks, findSinkName(sqlInsert));
                SqlSelect source = (SqlSelect) sqlInsert.getSource();
                parseSource(source, sources, udfs);
                break;
            case SELECT:
                parseSource((SqlSelect) sqlNode, sources, udfs);
                break;
            default:
                throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql);
        }
    }
}
 
Example 6
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Locates the n'th expression in an INSERT or UPDATE query.
 *
 * @param query       Query
 * @param ordinal     Ordinal of expression
 * @param sourceCount Number of expressions
 * @return Ordinal'th expression, never null
 */
private SqlNode getNthExpr(SqlNode query, int ordinal, int sourceCount) {
	if (query instanceof SqlInsert) {
		SqlInsert insert = (SqlInsert) query;
		if (insert.getTargetColumnList() != null) {
			return insert.getTargetColumnList().get(ordinal);
		} else {
			return getNthExpr(
				insert.getSource(),
				ordinal,
				sourceCount);
		}
	} else if (query instanceof SqlUpdate) {
		SqlUpdate update = (SqlUpdate) query;
		if (update.getTargetColumnList() != null) {
			return update.getTargetColumnList().get(ordinal);
		} else if (update.getSourceExpressionList() != null) {
			return update.getSourceExpressionList().get(ordinal);
		} else {
			return getNthExpr(
				update.getSourceSelect(),
				ordinal,
				sourceCount);
		}
	} else if (query instanceof SqlSelect) {
		SqlSelect select = (SqlSelect) query;
		if (select.getSelectList().size() == sourceCount) {
			return select.getSelectList().get(ordinal);
		} else {
			return query; // give up
		}
	} else {
		return query; // give up
	}
}
 
Example 7
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Locates the n'th expression in an INSERT or UPDATE query.
 *
 * @param query       Query
 * @param ordinal     Ordinal of expression
 * @param sourceCount Number of expressions
 * @return Ordinal'th expression, never null
 */
private SqlNode getNthExpr(SqlNode query, int ordinal, int sourceCount) {
	if (query instanceof SqlInsert) {
		SqlInsert insert = (SqlInsert) query;
		if (insert.getTargetColumnList() != null) {
			return insert.getTargetColumnList().get(ordinal);
		} else {
			return getNthExpr(
				insert.getSource(),
				ordinal,
				sourceCount);
		}
	} else if (query instanceof SqlUpdate) {
		SqlUpdate update = (SqlUpdate) query;
		if (update.getTargetColumnList() != null) {
			return update.getTargetColumnList().get(ordinal);
		} else if (update.getSourceExpressionList() != null) {
			return update.getSourceExpressionList().get(ordinal);
		} else {
			return getNthExpr(
				update.getSourceSelect(),
				ordinal,
				sourceCount);
		}
	} else if (query instanceof SqlSelect) {
		SqlSelect select = (SqlSelect) query;
		if (select.getSelectList().size() == sourceCount) {
			return select.getSelectList().get(ordinal);
		} else {
			return query; // give up
		}
	} else {
		return query; // give up
	}
}
 
Example 8
Source Project: alchemy   Source File: SqlParseUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static List<String> findQuerySql(List<String> sqls)
    throws SqlParseException {
    List<String> newSqls = new ArrayList<>(sqls.size());
    for (String sql : sqls) {
        SqlParser sqlParser = SqlParser.create(sql, CONFIG);
        SqlNode sqlNode = sqlParser.parseStmt();
        if (sqlNode.getKind() != SqlKind.INSERT) {
            throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql);
        }
        SqlInsert sqlInsert = (SqlInsert)sqlNode;
        newSqls.add(sqlInsert.getSource().toString());
    }
    return newSqls;
}
 
Example 9
Source Project: alchemy   Source File: SqlParseUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static String parseSinkName(String sql) throws SqlParseException {
    SqlParser sqlParser = SqlParser.create(sql, CONFIG);
    SqlNode sqlNode = sqlParser.parseStmt();
    SqlKind sqlKind = sqlNode.getKind();
    if (sqlKind != SqlKind.INSERT) {
        throw new IllegalArgumentException("It must be an insert SQL, sql:" + sql);
    }
    return findSinkName((SqlInsert)sqlNode);
}
 
Example 10
Source Project: alchemy   Source File: SqlParseUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static String findSinkName(SqlInsert sqlInsert) {
    SqlNode target = sqlInsert.getTargetTable();
    SqlKind targetKind = target.getKind();
    if (targetKind != SqlKind.IDENTIFIER) {
        throw new IllegalArgumentException("invalid insert SQL, sql:" + sqlInsert.toString());
    }
    SqlIdentifier identifier = (SqlIdentifier)target;
    return identifier.getSimple();
}
 
Example 11
Source Project: herddb   Source File: CalcitePlanner.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean detectUpsert(PlannerResult res) {
    if (res.sql instanceof SqlInsert) {
        SqlInsert si = (SqlInsert) res.sql;
        return si.isUpsert();
    }
    return false;
}
 
Example 12
Source Project: calcite   Source File: TypeCoercionImpl.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Coerces the field expression at index {@code columnIndex} of source
 * in an INSERT or UPDATE query to target type.
 *
 * @param sourceScope  Query source scope
 * @param query        Query
 * @param columnIndex  Source column index to coerce type
 * @param targetType   Target type
 */
private boolean coerceSourceRowType(
    SqlValidatorScope sourceScope,
    SqlNode query,
    int columnIndex,
    RelDataType targetType) {
  switch (query.getKind()) {
  case INSERT:
    SqlInsert insert = (SqlInsert) query;
    return coerceSourceRowType(sourceScope,
        insert.getSource(),
        columnIndex,
        targetType);
  case UPDATE:
    SqlUpdate update = (SqlUpdate) query;
    if (update.getSourceExpressionList() != null) {
      final SqlNodeList sourceExpressionList = update.getSourceExpressionList();
      return coerceColumnType(sourceScope, sourceExpressionList, columnIndex, targetType);
    } else {
      return coerceSourceRowType(sourceScope,
          update.getSourceSelect(),
          columnIndex,
          targetType);
    }
  default:
    return rowTypeCoercion(sourceScope, query, columnIndex, targetType);
  }
}
 
Example 13
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
public void validateInsert(SqlInsert insert) {
	final SqlValidatorNamespace targetNamespace = getNamespace(insert);
	validateNamespace(targetNamespace, unknownType);
	final RelOptTable relOptTable = SqlValidatorUtil.getRelOptTable(
		targetNamespace, catalogReader.unwrap(Prepare.CatalogReader.class), null, null);
	final SqlValidatorTable table = relOptTable == null
		? targetNamespace.getTable()
		: relOptTable.unwrap(SqlValidatorTable.class);

	// INSERT has an optional column name list.  If present then
	// reduce the rowtype to the columns specified.  If not present
	// then the entire target rowtype is used.
	final RelDataType targetRowType =
		createTargetRowType(
			table,
			insert.getTargetColumnList(),
			false);

	final SqlNode source = insert.getSource();
	if (source instanceof SqlSelect) {
		final SqlSelect sqlSelect = (SqlSelect) source;
		validateSelect(sqlSelect, targetRowType);
	} else {
		final SqlValidatorScope scope = scopes.get(source);
		validateQuery(source, scope, targetRowType);
	}

	// REVIEW jvs 4-Dec-2008: In FRG-365, this namespace row type is
	// discarding the type inferred by inferUnknownTypes (which was invoked
	// from validateSelect above).  It would be better if that information
	// were used here so that we never saw any untyped nulls during
	// checkTypeAssignment.
	final RelDataType sourceRowType = getNamespace(source).getRowType();
	final RelDataType logicalTargetRowType =
		getLogicalTargetRowType(targetRowType, insert);
	setValidatedNodeType(insert, logicalTargetRowType);
	final RelDataType logicalSourceRowType =
		getLogicalSourceRowType(sourceRowType, insert);

	checkFieldCount(insert.getTargetTable(), table, source,
		logicalSourceRowType, logicalTargetRowType);

	checkTypeAssignment(logicalSourceRowType, logicalTargetRowType, insert);

	checkConstraint(table, source, logicalTargetRowType);

	validateAccess(insert.getTargetTable(), table, SqlAccessEnum.INSERT);
}
 
Example 14
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
protected RelDataType getLogicalSourceRowType(
	RelDataType sourceRowType,
	SqlInsert insert) {
	return sourceRowType;
}
 
Example 15
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
InsertNamespace(SqlValidatorImpl validator, SqlInsert node,
	SqlNode enclosingNode, SqlValidatorScope parentScope) {
	super(validator, node.getTargetTable(), enclosingNode, parentScope);
	this.node = Objects.requireNonNull(node);
}
 
Example 16
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
public SqlInsert getNode() {
	return node;
}
 
Example 17
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
public void validateInsert(SqlInsert insert) {
	final SqlValidatorNamespace targetNamespace = getNamespace(insert);
	validateNamespace(targetNamespace, unknownType);
	final RelOptTable relOptTable = SqlValidatorUtil.getRelOptTable(
		targetNamespace, catalogReader.unwrap(Prepare.CatalogReader.class), null, null);
	final SqlValidatorTable table = relOptTable == null
		? targetNamespace.getTable()
		: relOptTable.unwrap(SqlValidatorTable.class);

	// INSERT has an optional column name list.  If present then
	// reduce the rowtype to the columns specified.  If not present
	// then the entire target rowtype is used.
	final RelDataType targetRowType =
		createTargetRowType(
			table,
			insert.getTargetColumnList(),
			false);

	final SqlNode source = insert.getSource();
	if (source instanceof SqlSelect) {
		final SqlSelect sqlSelect = (SqlSelect) source;
		validateSelect(sqlSelect, targetRowType);
	} else {
		final SqlValidatorScope scope = scopes.get(source);
		validateQuery(source, scope, targetRowType);
	}

	// REVIEW jvs 4-Dec-2008: In FRG-365, this namespace row type is
	// discarding the type inferred by inferUnknownTypes (which was invoked
	// from validateSelect above).  It would be better if that information
	// were used here so that we never saw any untyped nulls during
	// checkTypeAssignment.
	final RelDataType sourceRowType = getNamespace(source).getRowType();
	final RelDataType logicalTargetRowType =
		getLogicalTargetRowType(targetRowType, insert);
	setValidatedNodeType(insert, logicalTargetRowType);
	final RelDataType logicalSourceRowType =
		getLogicalSourceRowType(sourceRowType, insert);

	checkFieldCount(insert.getTargetTable(), table, source,
		logicalSourceRowType, logicalTargetRowType);

	checkTypeAssignment(logicalSourceRowType, logicalTargetRowType, insert);

	checkConstraint(table, source, logicalTargetRowType);

	validateAccess(insert.getTargetTable(), table, SqlAccessEnum.INSERT);
}
 
Example 18
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
protected RelDataType getLogicalSourceRowType(
	RelDataType sourceRowType,
	SqlInsert insert) {
	return sourceRowType;
}
 
Example 19
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
InsertNamespace(SqlValidatorImpl validator, SqlInsert node,
	SqlNode enclosingNode, SqlValidatorScope parentScope) {
	super(validator, node.getTargetTable(), enclosingNode, parentScope);
	this.node = Objects.requireNonNull(node);
}
 
Example 20
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 4 votes vote down vote up
public SqlInsert getNode() {
	return node;
}
 
Example 21
Source Project: Mycat2   Source File: CalciteSqlValidator.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override protected RelDataType getLogicalSourceRowType(
    RelDataType sourceRowType, SqlInsert insert) {
  final RelDataType superType =
      super.getLogicalSourceRowType(sourceRowType, insert);
  return ((JavaTypeFactory) typeFactory).toSql(superType);
}
 
Example 22
Source Project: Mycat2   Source File: CalciteSqlValidator.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override protected RelDataType getLogicalTargetRowType(
    RelDataType targetRowType, SqlInsert insert) {
  final RelDataType superType =
      super.getLogicalTargetRowType(targetRowType, insert);
  return ((JavaTypeFactory) typeFactory).toSql(superType);
}
 
Example 23
Source Project: calcite   Source File: CalciteSqlValidator.java    License: Apache License 2.0 4 votes vote down vote up
@Override protected RelDataType getLogicalSourceRowType(
    RelDataType sourceRowType, SqlInsert insert) {
  final RelDataType superType =
      super.getLogicalSourceRowType(sourceRowType, insert);
  return ((JavaTypeFactory) typeFactory).toSql(superType);
}
 
Example 24
Source Project: calcite   Source File: CalciteSqlValidator.java    License: Apache License 2.0 4 votes vote down vote up
@Override protected RelDataType getLogicalTargetRowType(
    RelDataType targetRowType, SqlInsert insert) {
  final RelDataType superType =
      super.getLogicalTargetRowType(targetRowType, insert);
  return ((JavaTypeFactory) typeFactory).toSql(superType);
}
 
Example 25
Source Project: Bats   Source File: SqlValidator.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Validates an INSERT statement.
 *
 * @param insert INSERT statement
 */
void validateInsert(SqlInsert insert);
 
Example 26
Source Project: calcite   Source File: SqlValidator.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Validates an INSERT statement.
 *
 * @param insert INSERT statement
 */
void validateInsert(SqlInsert insert);