public RelNode convertSqlToRel(String sql) { Util.pre(sql != null, "sql != null"); final SqlNode sqlQuery; try { sqlQuery = parseQuery(sql); } catch (Exception e) { throw Util.newInternal(e); // todo: better handling } final RelDataTypeFactory typeFactory = getTypeFactory(); final Prepare.CatalogReader catalogReader = createCatalogReader(typeFactory); final SqlValidator validator = createValidator(catalogReader, typeFactory); final SqlToRelConverter converter = createSqlToRelConverter(validator, catalogReader, typeFactory); converter.setTrimUnusedFields(true); final SqlNode validatedQuery = validator.validate(sqlQuery); final RelNode rel = converter.convertQuery(validatedQuery, false, true); Util.post(rel != null, "return != null"); return rel; }
public ParseResult parse(Context context, String sql) { final JavaTypeFactory typeFactory = context.getTypeFactory(); OptiqCatalogReader catalogReader = new OptiqCatalogReader(context.getRootSchema(), typeFactory); final OptiqPreparingStmt preparingStmt = new OptiqPreparingStmt(catalogReader, typeFactory, context.getRootSchema()); preparingStmt.setResultCallingConvention(CallingConvention.ENUMERABLE); SqlParser parser = new SqlParser(sql); SqlNode sqlNode; try { sqlNode = parser.parseQuery(); } catch (SqlParseException e) { throw new RuntimeException("parse failed", e); } SqlValidator validator = new SqlValidatorImpl( SqlStdOperatorTable.instance(), catalogReader, typeFactory, SqlConformance.Default) {}; SqlNode sqlNode1 = validator.validate(sqlNode); return new ParseResult(sql, sqlNode1, validator.getValidatedNodeType(sqlNode1)); }
@Override public RelNode expandView(RelDataType rowType, String queryString) { expansionDepth++; SqlParser parser = new SqlParser(queryString); SqlNode sqlNode; try { sqlNode = parser.parseQuery(); } catch (SqlParseException e) { throw new RuntimeException("parse failed", e); } SqlValidator validator = getSqlValidator(); SqlNode sqlNode1 = validator.validate(sqlNode); SqlToRelConverter sqlToRelConverter = getSqlToRelConverter(validator, catalogReader); RelNode relNode = sqlToRelConverter.convertQuery(sqlNode1, true, false); --expansionDepth; return relNode; }
<T> PrepareResult<T> prepare_( Context context, String sql, Queryable<T> queryable, Type elementType) { final JavaTypeFactory typeFactory = context.getTypeFactory(); OptiqCatalogReader catalogReader = new OptiqCatalogReader(context.getRootSchema(), typeFactory); final OptiqPreparingStmt preparingStmt = new OptiqPreparingStmt(catalogReader, typeFactory, context.getRootSchema()); preparingStmt.setResultCallingConvention(CallingConvention.ENUMERABLE); final RelDataType x; final PreparedResult preparedResult; if (sql != null) { assert queryable == null; SqlParser parser = new SqlParser(sql); SqlNode sqlNode; try { sqlNode = parser.parseQuery(); } catch (SqlParseException e) { throw new RuntimeException("parse failed", e); } final Schema rootSchema = context.getRootSchema(); SqlValidator validator = new SqlValidatorImpl( new ChainedSqlOperatorTable( Arrays.<SqlOperatorTable>asList( SqlStdOperatorTable.instance(), new MySqlOperatorTable(rootSchema, typeFactory))), catalogReader, typeFactory, SqlConformance.Default) {}; preparedResult = preparingStmt.prepareSql(sqlNode, Object.class, validator, true); x = validator.getValidatedNodeType(sqlNode); } else { assert queryable != null; x = context.getTypeFactory().createType(elementType); preparedResult = preparingStmt.prepareQueryable(queryable, x); } // TODO: parameters final List<Parameter> parameters = Collections.emptyList(); // TODO: column meta data final List<ColumnMetaData> columns = new ArrayList<ColumnMetaData>(); RelDataType jdbcType = makeStruct(typeFactory, x); for (RelDataTypeField field : jdbcType.getFields()) { RelDataType type = field.getType(); SqlTypeName sqlTypeName = type.getSqlTypeName(); columns.add( new ColumnMetaData( columns.size(), false, true, false, false, type.isNullable() ? 1 : 0, true, 0, field.getName(), field.getName(), null, sqlTypeName.allowsPrec() && false ? type.getPrecision() : -1, sqlTypeName.allowsScale() ? type.getScale() : -1, null, null, sqlTypeName.getJdbcOrdinal(), sqlTypeName.getName(), true, false, false, null)); } return new PrepareResult<T>(sql, parameters, columns, (Enumerable<T>) preparedResult.execute()); }