use of org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl in project samza by apache.
the class SamzaSqlQueryParser method createPlanner.
private static Planner createPlanner() {
Connection connection;
SchemaPlus rootSchema;
try {
JavaTypeFactory typeFactory = new SamzaSqlJavaTypeFactoryImpl();
SamzaSqlDriver driver = new SamzaSqlDriver(typeFactory);
DriverManager.deregisterDriver(DriverManager.getDriver("jdbc:calcite:"));
DriverManager.registerDriver(driver);
connection = driver.connect("jdbc:calcite:", new Properties());
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
rootSchema = calciteConnection.getRootSchema();
} catch (SQLException e) {
throw new SamzaException(e);
}
final List<RelTraitDef> traitDefs = new ArrayList<>();
traitDefs.add(ConventionTraitDef.INSTANCE);
traitDefs.add(RelCollationTraitDef.INSTANCE);
FrameworkConfig frameworkConfig = Frameworks.newConfigBuilder().parserConfig(SqlParser.configBuilder().setLex(Lex.JAVA).build()).defaultSchema(rootSchema).operatorTable(SqlStdOperatorTable.instance()).traitDefs(traitDefs).context(Contexts.EMPTY_CONTEXT).costFactory(null).build();
return Frameworks.getPlanner(frameworkConfig);
}
use of org.apache.samza.sql.interfaces.SamzaSqlJavaTypeFactoryImpl in project samza by apache.
the class RexToJavaCompiler method compile.
/**
* Compiles a relational expression to a instance of {@link Expression}
*
* for e.g.
* Query : select id from profile
* where profile table has relational schema with id(NUMBER) and name(VARCHAR) columns.
* This query will result in the following relational plan
* LogicalProject(id=[$1])
* LogicalTableScan(table=[[profile]])
*
* And the corresponding expressions are
* inputs : EnumerableTableScan (Which is the output of LogicalTableScan)
* nodes : [$1] Which essentially means take pick the first column from the input
*
* This function converts the LogicalProject expression "[$1]" with input RexNode which is an output of TableScan
* to a java code that implements the interface {@link Expression}
*
* @param inputs Input relations/time-varying relations for this row expression
* @param nodes relational expressions that needs to be converted to java code.
* @return compiled expression of type {@link org.apache.samza.sql.data.Expression}
*/
public org.apache.samza.sql.data.Expression compile(List<RelNode> inputs, List<RexNode> nodes) {
/*
* In case there are multiple input relations, we build a single input row type combining types of all the inputs.
*/
final RelDataTypeFactory.FieldInfoBuilder fieldBuilder = rexBuilder.getTypeFactory().builder();
for (RelNode input : inputs) {
fieldBuilder.addAll(input.getRowType().getFieldList());
}
final RelDataType inputRowType = fieldBuilder.build();
final RexProgramBuilder programBuilder = new RexProgramBuilder(inputRowType, rexBuilder);
for (RexNode node : nodes) {
programBuilder.addProject(node, null);
}
final RexProgram program = programBuilder.getProgram();
final BlockBuilder builder = new BlockBuilder();
final ParameterExpression sqlContext = Expressions.parameter(SamzaSqlExecutionContext.class, "sqlContext");
final ParameterExpression context = Expressions.parameter(Context.class, "context");
final ParameterExpression root = DataContext.ROOT;
final ParameterExpression inputValues = Expressions.parameter(Object[].class, "inputValues");
final ParameterExpression outputValues = Expressions.parameter(Object[].class, "outputValues");
final JavaTypeFactoryImpl javaTypeFactory = new SamzaSqlJavaTypeFactoryImpl(rexBuilder.getTypeFactory().getTypeSystem());
// public void execute(Object[] inputValues, Object[] outputValues)
final RexToLixTranslator.InputGetter inputGetter = new RexToLixTranslator.InputGetterImpl(ImmutableList.of(Pair.of(Expressions.variable(Object[].class, "inputValues"), PhysTypeImpl.of(javaTypeFactory, inputRowType, JavaRowFormat.ARRAY, false))));
final List<org.apache.calcite.linq4j.tree.Expression> list = RexToLixTranslator.translateProjects(program, javaTypeFactory, SqlConformanceEnum.DEFAULT, builder, null, DataContext.ROOT, inputGetter, null);
for (int i = 0; i < list.size(); i++) {
builder.add(Expressions.statement(Expressions.assign(Expressions.arrayIndex(outputValues, Expressions.constant(i)), list.get(i))));
}
return createSamzaExpressionFromCalcite(sqlContext, context, root, inputValues, outputValues, builder.toBlock());
}
Aggregations