Java Code Examples for org.apache.calcite.sql.util.SqlShuttle

The following examples show how to use org.apache.calcite.sql.util.SqlShuttle. 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: quark   Source File: SqlQueryParser.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Strips namespace from identifiers of sql
 *
 * @param node
 * @param namespace
 * @param dialect
 * @return
 */
private String stripNamespace(final SqlNode node,
                              final String namespace,
                              final SqlDialect dialect) {
  final SqlNode transformedNode = node.accept(
      new SqlShuttle() {
        @Override
        public SqlNode visit(SqlIdentifier id) {
          if (id.names.size() > 1
              && id.names.get(0).toUpperCase().equals(namespace.toUpperCase())) {
            return id.getComponent(1, id.names.size());
          } else {
            return id;
          }
        }
      });
  String result = transformedNode.toSqlString(dialect).toString();
  return result.replace("\n", " ");
}
 
Example 2
Source Project: calcite   Source File: Hoist.java    License: Apache License 2.0 6 votes vote down vote up
/** Hoists literals in a given SQL string, returning a {@link Hoisted}. */
public Hoisted hoist(String sql) {
  final List<Variable> variables = new ArrayList<>();
  final SqlParser parser = SqlParser.create(sql, config.parserConfig());
  final SqlNode node;
  try {
    node = parser.parseQuery();
  } catch (SqlParseException e) {
    throw new RuntimeException(e);
  }
  node.accept(new SqlShuttle() {
    @Override public SqlNode visit(SqlLiteral literal) {
      variables.add(new Variable(sql, variables.size(), literal));
      return super.visit(literal);
    }
  });
  return new Hoisted(sql, variables);
}
 
Example 3
Source Project: Flink-CEPplus   Source File: SqlValidatorImpl.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType getParameterRowType(SqlNode sqlQuery) {
	// NOTE: We assume that bind variables occur in depth-first tree
	// traversal in the same order that they occurred in the SQL text.
	final List<RelDataType> types = new ArrayList<>();
	// NOTE: but parameters on fetch/offset would be counted twice
	// as they are counted in the SqlOrderBy call and the inner SqlSelect call
	final Set<SqlNode> alreadyVisited = new HashSet<>();
	sqlQuery.accept(
		new SqlShuttle() {

			@Override public SqlNode visit(SqlDynamicParam param) {
				if (alreadyVisited.add(param)) {
					RelDataType type = getValidatedNodeType(param);
					types.add(type);
				}
				return param;
			}
		});
	return typeFactory.createStructType(
		types,
		new AbstractList<String>() {
			@Override public String get(int index) {
				return "?" + index;
			}

			@Override public int size() {
				return types.size();
			}
		});
}
 
Example 4
Source Project: flink   Source File: SqlValidatorImpl.java    License: Apache License 2.0 5 votes vote down vote up
public RelDataType getParameterRowType(SqlNode sqlQuery) {
	// NOTE: We assume that bind variables occur in depth-first tree
	// traversal in the same order that they occurred in the SQL text.
	final List<RelDataType> types = new ArrayList<>();
	// NOTE: but parameters on fetch/offset would be counted twice
	// as they are counted in the SqlOrderBy call and the inner SqlSelect call
	final Set<SqlNode> alreadyVisited = new HashSet<>();
	sqlQuery.accept(
		new SqlShuttle() {

			@Override public SqlNode visit(SqlDynamicParam param) {
				if (alreadyVisited.add(param)) {
					RelDataType type = getValidatedNodeType(param);
					types.add(type);
				}
				return param;
			}
		});
	return typeFactory.createStructType(
		types,
		new AbstractList<String>() {
			@Override public String get(int index) {
				return "?" + index;
			}

			@Override public int size() {
				return types.size();
			}
		});
}
 
Example 5
@Override
public PlanRunner plan(List<Object> params) {
    SqlNode accept = params.isEmpty() ? sqlNode : SqlNode.clone(sqlNode).accept(
            new SqlShuttle() {
                int index = 0;

                @Override
                public SqlNode visit(SqlDynamicParam param) {
                    Object o = params.get(index);
                    index++;
                    return literal(o);
                }
            });
    return new MycatSqlPlanner(this,getSql(), accept,dataContext);
}
 
Example 6
Source Project: Mycat2   Source File: HBTQueryConvertor.java    License: GNU General Public License v3.0 5 votes vote down vote up
private RelDataType tryGetRelDataTypeByParse(String targetName, String sql) {
    try {
        RelDataType relDataType;
        MycatCalcitePlanner planner = MycatCalciteSupport.INSTANCE.createPlanner(context);
        SQLStatement sqlStatement = SQLUtils.parseSingleMysqlStatement(sql);
        SqlNode parse = planner.parse(MycatSqlUtil.getCalciteSQL(sqlStatement));
        parse = parse.accept(new SqlShuttle() {
            @Override
            public SqlNode visit(SqlIdentifier id) {
                if (id.names.size() == 2) {
                    String schema = id.names.get(0);
                    String table = id.names.get(1);
                    MycatLogicTable logicTable = context.getLogicTable(targetName, schema, table);
                    if (logicTable!=null) {
                        TableHandler table1 = logicTable.getTable();
                        return new SqlIdentifier(Arrays.asList(table1.getSchemaName(), table1.getTableName()), SqlParserPos.ZERO);
                    }
                }
                return super.visit(id);
            }
        });
        parse = planner.validate(parse);
        relDataType = planner.convert(parse).getRowType();
        return relDataType;
    } catch (Throwable e) {
        log.warn("", e);
    }
    return null;
}