Java Code Examples for org.apache.calcite.linq4j.tree.Expressions#constant()
The following examples show how to use
org.apache.calcite.linq4j.tree.Expressions#constant() .
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 File: RexImpTable.java From calcite with Apache License 2.0 | 6 votes |
@Override public Expression implement(RexToLixTranslator translator, Expression inputEnumerable, RexCall call, PhysType inputPhysType, PhysType outputPhysType) { RexCall timestampDescriptor = (RexCall) call.getOperands().get(0); RexCall keyDescriptor = (RexCall) call.getOperands().get(1); Expression gapInterval = translator.translate(call.getOperands().get(2)); List<Expression> translatedOperands = new ArrayList<>(); Expression wmColIndexExpr = Expressions.constant(((RexInputRef) timestampDescriptor.getOperands().get(0)).getIndex()); Expression keyColIndexExpr = Expressions.constant(((RexInputRef) keyDescriptor.getOperands().get(0)).getIndex()); translatedOperands.add(wmColIndexExpr); translatedOperands.add(keyColIndexExpr); translatedOperands.add(gapInterval); return Expressions.call(BuiltInMethod.SESSIONIZATION.method, Expressions.list( Expressions.call(inputEnumerable, BuiltInMethod.ENUMERABLE_ENUMERATOR.method), translatedOperands.get(0), translatedOperands.get(1), translatedOperands.get(2))); }
Example 2
Source File: RexImpTable.java From calcite with Apache License 2.0 | 6 votes |
@Override public Expression implement(RexToLixTranslator translator, Expression inputEnumerable, RexCall call, PhysType inputPhysType, PhysType outputPhysType) { Expression slidingInterval = translator.translate(call.getOperands().get(1)); Expression windowSize = translator.translate(call.getOperands().get(2)); RexCall descriptor = (RexCall) call.getOperands().get(0); List<Expression> translatedOperands = new ArrayList<>(); Expression wmColIndexExpr = Expressions.constant(((RexInputRef) descriptor.getOperands().get(0)).getIndex()); translatedOperands.add(wmColIndexExpr); translatedOperands.add(slidingInterval); translatedOperands.add(windowSize); return Expressions.call(BuiltInMethod.HOPPING.method, Expressions.list( Expressions.call(inputEnumerable, BuiltInMethod.ENUMERABLE_ENUMERATOR.method), translatedOperands.get(0), translatedOperands.get(1), translatedOperands.get(2))); }
Example 3
Source File: EnumUtilsTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testMethodCallExpression() { // test for Object.class method parameter type final ConstantExpression arg0 = Expressions.constant(1, int.class); final ConstantExpression arg1 = Expressions.constant("x", String.class); final MethodCallExpression arrayMethodCall = EnumUtils.call(SqlFunctions.class, BuiltInMethod.ARRAY.getMethodName(), Arrays.asList(arg0, arg1)); assertThat(Expressions.toString(arrayMethodCall), is("org.apache.calcite.runtime.SqlFunctions.array(1, \"x\")")); // test for Object.class argument type final ConstantExpression nullLiteral = Expressions.constant(null); final MethodCallExpression xmlExtractMethodCall = EnumUtils.call( XmlFunctions.class, BuiltInMethod.EXTRACT_VALUE.getMethodName(), Arrays.asList(arg1, nullLiteral)); assertThat(Expressions.toString(xmlExtractMethodCall), is("org.apache.calcite.runtime.XmlFunctions.extractValue(\"x\", (String) null)")); // test "mod(decimal, long)" match to "mod(decimal, decimal)" final ConstantExpression arg2 = Expressions.constant(12.5, BigDecimal.class); final ConstantExpression arg3 = Expressions.constant(3, long.class); final MethodCallExpression modMethodCall = EnumUtils.call( SqlFunctions.class, "mod", Arrays.asList(arg2, arg3)); assertThat(Expressions.toString(modMethodCall), is("org.apache.calcite.runtime.SqlFunctions.mod(" + "java.math.BigDecimal.valueOf(125L, 1), " + "new java.math.BigDecimal(\n 3L))")); // test "ST_MakePoint(int, int)" match to "ST_MakePoint(decimal, decimal)" final ConstantExpression arg4 = Expressions.constant(1, int.class); final ConstantExpression arg5 = Expressions.constant(2, int.class); final MethodCallExpression geoMethodCall = EnumUtils.call( GeoFunctions.class, "ST_MakePoint", Arrays.asList(arg4, arg5)); assertThat(Expressions.toString(geoMethodCall), is("org.apache.calcite.runtime.GeoFunctions.ST_MakePoint(" + "new java.math.BigDecimal(\n 1), " + "new java.math.BigDecimal(\n 2))")); }
Example 4
Source File: OptimizerTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testAssign2() { // long x = 0; // final long y = System.currentTimeMillis(); // if (System.currentTimeMillis() > 0) { // x = y; // } // // Make sure we don't fold two calls to System.currentTimeMillis into one. final ParameterExpression x_ = Expressions.parameter(long.class, "x"); final ParameterExpression y_ = Expressions.parameter(long.class, "y"); final Method mT = Linq4j.getMethod("java.lang.System", "currentTimeMillis"); final ConstantExpression zero = Expressions.constant(0L); assertThat( optimize( Expressions.block( Expressions.declare(0, x_, zero), Expressions.declare(Modifier.FINAL, y_, Expressions.call(mT)), Expressions.ifThen( Expressions.greaterThan(Expressions.call(mT), zero), Expressions.statement(Expressions.assign(x_, y_))))), equalTo("{\n" + " long x = 0L;\n" + " if (System.currentTimeMillis() > 0L) {\n" + " x = System.currentTimeMillis();\n" + " }\n" + "}\n")); }
Example 5
Source File: EnumUtilsTest.java From calcite with Apache License 2.0 | 5 votes |
@Test void testTypeConvertToString() { // Constant Expression: "null" final ConstantExpression nullLiteral1 = Expressions.constant(null); // Constant Expression: "(Object) null" final ConstantExpression nullLiteral2 = Expressions.constant(null, Object.class); final Expression e1 = EnumUtils.convert(nullLiteral1, String.class); final Expression e2 = EnumUtils.convert(nullLiteral2, String.class); assertThat(Expressions.toString(e1), is("(String) null")); assertThat(Expressions.toString(e2), is("(String) (Object) null")); }
Example 6
Source File: EnumerableLimit.java From calcite with Apache License 2.0 | 5 votes |
private static Expression getExpression(RexNode offset) { if (offset instanceof RexDynamicParam) { final RexDynamicParam param = (RexDynamicParam) offset; return Expressions.convert_( Expressions.call(DataContext.ROOT, BuiltInMethod.DATA_CONTEXT_GET.method, Expressions.constant("?" + param.getIndex())), Integer.class); } else { return Expressions.constant(RexLiteral.intValue(offset)); } }
Example 7
Source File: RexImpTable.java From calcite with Apache License 2.0 | 5 votes |
static Expression getDefaultValue(Type type) { if (Primitive.is(type)) { Primitive p = Primitive.of(type); return Expressions.constant(p.defaultValue, type); } return Expressions.constant(null, type); }
Example 8
Source File: HiveUDAFImplementor.java From marble with Apache License 2.0 | 5 votes |
@Override public void implementNotNullAdd(AggContext info, AggAddContext context) { Class hiveUDAFClass = HiveSqlOperatorTable.instance() .getHiveUDAFClass(hiveSqlAggFunction.getName()); Expression hiveUDAFClassExpr = Expressions.constant(hiveUDAFClass, Class.class); Expression acc0 = context.accumulator().get(0); Expression argArray = new NewArrayExpression(Object.class, 1, null, context.arguments()); List<Expression> list = new ArrayList<>(); for (int i = 0; i < context.rexArguments().size(); i++) { Expression argExpr; if (TypeInferenceUtil.isOperandConstantForHiveUDAF(hiveUDAFClass, i)) { Expression valExpr = ((NewArrayExpression) argArray).expressions.get(i); argExpr = RelDataTypeHolder.generateExpressionWithConstantValue( context.rexArguments().get(i).getType(), valExpr); } else { argExpr = RelDataTypeHolder.generateExpression( context.rexArguments().get(i).getType()); } list.add(argExpr); } Expression argTypeArray = new NewArrayExpression(RelDataTypeHolder.class, 1, null, list); boolean isDistinct = info.call().isDistinct(); Expression iterateExpr = Expressions.call(METHOD_EXECUTE_ITERATE, hiveUDAFClassExpr, acc0, argArray, argTypeArray, new ConstantExpression(Boolean.class, isDistinct)); context.currentBlock().add(Expressions.statement(iterateExpr)); }
Example 9
Source File: RexImpTable.java From calcite with Apache License 2.0 | 5 votes |
@Override Expression implementSafe(final RexToLixTranslator translator, final RexCall call, final List<Expression> argValueList) { final SqlOperator op = call.getOperator(); final Expression root = translator.getRoot(); if (op == CURRENT_USER || op == SESSION_USER || op == USER) { return Expressions.call(BuiltInMethod.USER.method, root); } else if (op == SYSTEM_USER) { return Expressions.call(BuiltInMethod.SYSTEM_USER.method, root); } else if (op == CURRENT_PATH || op == CURRENT_ROLE || op == CURRENT_CATALOG) { // By default, the CURRENT_ROLE and CURRENT_CATALOG functions return the // empty string because a role or a catalog has to be set explicitly. return Expressions.constant(""); } else if (op == CURRENT_TIMESTAMP) { return Expressions.call(BuiltInMethod.CURRENT_TIMESTAMP.method, root); } else if (op == CURRENT_TIME) { return Expressions.call(BuiltInMethod.CURRENT_TIME.method, root); } else if (op == CURRENT_DATE) { return Expressions.call(BuiltInMethod.CURRENT_DATE.method, root); } else if (op == LOCALTIMESTAMP) { return Expressions.call(BuiltInMethod.LOCAL_TIMESTAMP.method, root); } else if (op == LOCALTIME) { return Expressions.call(BuiltInMethod.LOCAL_TIME.method, root); } else { throw new AssertionError("unknown function " + op); } }
Example 10
Source File: RexImpTable.java From calcite with Apache License 2.0 | 4 votes |
@Override Expression implementSafe(final RexToLixTranslator translator, final RexCall call, final List<Expression> argValueList) { return Expressions.constant(null); }
Example 11
Source File: RexImpTable.java From calcite with Apache License 2.0 | 4 votes |
@Override Expression getIfTrue(Type type, final List<Expression> argValueList) { return Expressions.constant(false, type); }
Example 12
Source File: RexImpTable.java From calcite with Apache License 2.0 | 4 votes |
@Override Expression getIfTrue(Type type, final List<Expression> argValueList) { return Expressions.constant(true, type); }
Example 13
Source File: RexImpTable.java From calcite with Apache License 2.0 | 4 votes |
@Override Expression getIfTrue(Type type, final List<Expression> argValueList) { return Expressions.constant(false, type); }
Example 14
Source File: MockCatalogReader.java From calcite with Apache License 2.0 | 4 votes |
public Expression getExpression(Class clazz) { // Return a true constant just to pass the tests in EnumerableTableScanRule. return Expressions.constant(true); }
Example 15
Source File: RexImpTable.java From calcite with Apache License 2.0 | 4 votes |
@Override Expression implementSafe(RexToLixTranslator translator, RexCall call, List<Expression> argValueList) { final Expression expression; final List<Expression> newOperands = new ArrayList<>(); newOperands.add(argValueList.get(0)); newOperands.add(argValueList.get(1)); List<Expression> leftExprs = Util.skip(argValueList, 2); // Default value for JSON_VALUE behaviors. Expression emptyBehavior = Expressions.constant(SqlJsonValueEmptyOrErrorBehavior.NULL); Expression defaultValueOnEmpty = Expressions.constant(null); Expression errorBehavior = Expressions.constant(SqlJsonValueEmptyOrErrorBehavior.NULL); Expression defaultValueOnError = Expressions.constant(null); // Patched up with user defines. if (leftExprs.size() > 0) { for (int i = 0; i < leftExprs.size(); i++) { Expression expr = leftExprs.get(i); final Object exprVal = translator.getLiteralValue(expr); if (exprVal != null) { int defaultSymbolIdx = i - 2; if (exprVal == SqlJsonEmptyOrError.EMPTY) { if (defaultSymbolIdx >= 0 && translator.getLiteralValue(leftExprs.get(defaultSymbolIdx)) == SqlJsonValueEmptyOrErrorBehavior.DEFAULT) { defaultValueOnEmpty = leftExprs.get(i - 1); emptyBehavior = leftExprs.get(defaultSymbolIdx); } else { emptyBehavior = leftExprs.get(i - 1); } } else if (exprVal == SqlJsonEmptyOrError.ERROR) { if (defaultSymbolIdx >= 0 && translator.getLiteralValue(leftExprs.get(defaultSymbolIdx)) == SqlJsonValueEmptyOrErrorBehavior.DEFAULT) { defaultValueOnError = leftExprs.get(i - 1); errorBehavior = leftExprs.get(defaultSymbolIdx); } else { errorBehavior = leftExprs.get(i - 1); } } } } } newOperands.add(emptyBehavior); newOperands.add(defaultValueOnEmpty); newOperands.add(errorBehavior); newOperands.add(defaultValueOnError); Class clazz = method.getDeclaringClass(); expression = EnumUtils.call(clazz, method.getName(), newOperands); final Type returnType = translator.typeFactory.getJavaClass(call.getType()); return EnumUtils.convert(expression, returnType); }
Example 16
Source File: RexImpTable.java From calcite with Apache License 2.0 | 4 votes |
public Expression implementResult(AggContext info, AggResultContext result) { WinAggResultContext winResult = (WinAggResultContext) result; List<RexNode> rexArgs = winResult.rexArguments(); ParameterExpression res = Expressions.parameter(0, info.returnType(), result.currentBlock().newName(isLead ? "lead" : "lag")); Expression offset; RexToLixTranslator currentRowTranslator = winResult.rowTranslator( winResult.computeIndex(Expressions.constant(0), SeekType.SET)); if (rexArgs.size() >= 2) { // lead(x, offset) or lead(x, offset, default) offset = currentRowTranslator.translate( rexArgs.get(1), int.class); } else { offset = Expressions.constant(1); } if (!isLead) { offset = Expressions.negate(offset); } Expression dstIndex = winResult.computeIndex(offset, SeekType.SET); Expression rowInRange = winResult.rowInPartition(dstIndex); BlockBuilder thenBlock = result.nestBlock(); Expression lagResult = winResult.rowTranslator(dstIndex).translate( rexArgs.get(0), res.type); thenBlock.add(Expressions.statement(Expressions.assign(res, lagResult))); result.exitBlock(); BlockStatement thenBranch = thenBlock.toBlock(); Expression defaultValue = rexArgs.size() == 3 ? currentRowTranslator.translate(rexArgs.get(2), res.type) : getDefaultValue(res.type); result.currentBlock().add(Expressions.declare(0, res, null)); result.currentBlock().add( Expressions.ifThenElse(rowInRange, thenBranch, Expressions.statement(Expressions.assign(res, defaultValue)))); return res; }
Example 17
Source File: EnumUtils.java From calcite with Apache License 2.0 | 4 votes |
/** * Generates a window selector which appends attribute of the window based on * the parameters. * * Note that it only works for batch scenario. E.g. all data is known and there is no late data. */ static Expression tumblingWindowSelector( PhysType inputPhysType, PhysType outputPhysType, Expression wmColExpr, Expression intervalExpr) { // Generate all fields. final List<Expression> expressions = new ArrayList<>(); // If input item is just a primitive, we do not generate specialized // primitive apply override since it won't be called anyway // Function<T> always operates on boxed arguments final ParameterExpression parameter = Expressions.parameter(Primitive.box(inputPhysType.getJavaRowType()), "_input"); final int fieldCount = inputPhysType.getRowType().getFieldCount(); for (int i = 0; i < fieldCount; i++) { Expression expression = inputPhysType.fieldReference(parameter, i, outputPhysType.getJavaFieldType(expressions.size())); expressions.add(expression); } final Expression wmColExprToLong = EnumUtils.convert(wmColExpr, long.class); final Expression shiftExpr = Expressions.constant(1, long.class); // Find the fixed window for a timestamp given a window size and return the window start. // Fixed windows counts from timestamp 0. // wmMillis / intervalMillis * intervalMillis expressions.add( Expressions.multiply( Expressions.divide( wmColExprToLong, intervalExpr), intervalExpr)); // Find the fixed window for a timestamp given a window size and return the window end. // Fixed windows counts from timestamp 0. // (wmMillis / sizeMillis + 1L) * sizeMillis; expressions.add( Expressions.multiply( Expressions.add( Expressions.divide( wmColExprToLong, intervalExpr), shiftExpr), intervalExpr)); return Expressions.lambda( Function1.class, outputPhysType.record(expressions), parameter); }
Example 18
Source File: TypeFinderTest.java From calcite with Apache License 2.0 | 4 votes |
@Test void testConstantExpression() { ConstantExpression expr = Expressions.constant(null, Integer.class); assertJavaCodeContains("(Integer) null\n", expr); assertTypeContains(Integer.class, expr); }
Example 19
Source File: ElasticsearchToEnumerableConverter.java From dk-fitting with Apache License 2.0 | 4 votes |
public Result implement(EnumerableRelImplementor implementor, Prefer pref) { final ElasticsearchRelNode.Implementor esImplementor = new ElasticsearchRelNode.Implementor(); esImplementor.visitChild(0, getInput()); List<String> listField = esImplementor.getListField(); List<String> listField2 = new ArrayList<String>(); for (String field : listField){ //数据处理函数expr$0 转为lower.fieldName listField2.add(field); } ElasticsearchTable esTable = esImplementor.elasticsearchTable; final RelDataType rowType2 = esImplementor.elasticsearchTable.getRowType2(); final List<String> fieldNames1 = rowType2.getFieldNames(); //得到输出顺序 PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType2, pref.prefer(JavaRowFormat.ARRAY)); final RelDataType rowType = esImplementor.elasticsearchTable.getRowType(); PhysType physType2 = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY)); // esTable.setOutNames(fieldNames1); List<String> fieldNames = ElasticsearchRules.elasticsearchFieldNames(rowType2); List<ElasticSearchFieldType> typeList = new ArrayList<ElasticSearchFieldType>(); for(int i = 0 ; i < fieldNames.size(); i++){ Class type = physType.fieldClass(i); String typeName = type.toString().substring(type.toString().lastIndexOf(".") + 1).toLowerCase(); if("integer".equals(typeName)){ typeName = "int"; } typeList.add(ElasticSearchFieldType.of(typeName)); } boolean oneColumFlag = false; if(typeList.size() == 1){ oneColumFlag = true; } TwoTuple<List<Object>, List<Object[]>> listTwoTuple = esTable.find(typeList,listField,oneColumFlag); // List<Object[]> resultList = esTable.find(typeList, oneColumFlag); ConstantExpression constant = oneColumFlag ? Expressions.constant(listTwoTuple.first.toArray()) : Expressions.constant(listTwoTuple.second.toArray()); Result result = implementor.result(physType2, Blocks.toBlock(Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method, constant))); return result; }
Example 20
Source File: ElasticsearchToEnumerableConverter.java From dk-fitting with Apache License 2.0 | 4 votes |
public Result implement(EnumerableRelImplementor implementor, Prefer pref) { final ElasticsearchRelNode.Implementor esImplementor = new ElasticsearchRelNode.Implementor(); esImplementor.visitChild(0, getInput()); List<String> listField = esImplementor.getListField(); List<String> listField2 = new ArrayList<String>(); for (String field : listField){ //数据处理函数expr$0 转为lower.fieldName listField2.add(field); } ElasticsearchTable esTable = esImplementor.elasticsearchTable; final RelDataType rowType2 = esImplementor.elasticsearchTable.getRowType2(); final List<String> fieldNames1 = rowType2.getFieldNames(); //得到输出顺序 PhysType physType = PhysTypeImpl.of(implementor.getTypeFactory(), rowType2, pref.prefer(JavaRowFormat.ARRAY)); final RelDataType rowType = esImplementor.elasticsearchTable.getRowType(); PhysType physType2 = PhysTypeImpl.of(implementor.getTypeFactory(), rowType, pref.prefer(JavaRowFormat.ARRAY)); // esTable.setOutNames(fieldNames1); List<String> fieldNames = ElasticsearchRules.elasticsearchFieldNames(rowType2); List<ElasticSearchFieldType> typeList = new ArrayList<ElasticSearchFieldType>(); for(int i = 0 ; i < fieldNames.size(); i++){ Class type = physType.fieldClass(i); String typeName = type.toString().substring(type.toString().lastIndexOf(".") + 1).toLowerCase(); if("integer".equals(typeName)){ typeName = "int"; } typeList.add(ElasticSearchFieldType.of(typeName)); } boolean oneColumFlag = false; if(typeList.size() == 1){ oneColumFlag = true; } TwoTuple<List<Object>, List<Object[]>> listTwoTuple = esTable.find(typeList,listField,oneColumFlag); // List<Object[]> resultList = esTable.find(typeList, oneColumFlag); ConstantExpression constant = oneColumFlag ? Expressions.constant(listTwoTuple.first.toArray()) : Expressions.constant(listTwoTuple.second.toArray()); Result result = implementor.result(physType2, Blocks.toBlock(Expressions.call(BuiltInMethod.AS_ENUMERABLE2.method, constant))); return result; }