@Override public RowExpression visitConstant(ConstantExpression literal, Void context) { return constant(literal.getValue(), targetType); }
@VisibleForTesting static SwitchGenerationCase checkSwitchGenerationCase(Type type, List<RowExpression> values) { if (values.size() > 32) { // 32 is chosen because // * SET_CONTAINS performs worst when smaller than but close to power of 2 // * Benchmark shows performance of SET_CONTAINS is better at 50, but similar at 25. return SwitchGenerationCase.SET_CONTAINS; } if (!(type instanceof IntegerType || type instanceof BigintType || type instanceof DateType)) { return SwitchGenerationCase.HASH_SWITCH; } for (RowExpression expression : values) { // For non-constant expressions, they will be added to the default case in the generated switch code. They do not affect any of // the cases other than the default one. Therefore, it's okay to skip them when choosing between DIRECT_SWITCH and HASH_SWITCH. // Same argument applies for nulls. if (!(expression instanceof ConstantExpression)) { continue; } Object constant = ((ConstantExpression) expression).getValue(); if (constant == null) { continue; } long longConstant = ((Number) constant).longValue(); if (longConstant < Integer.MIN_VALUE || longConstant > Integer.MAX_VALUE) { return SwitchGenerationCase.HASH_SWITCH; } } return SwitchGenerationCase.DIRECT_SWITCH; }
private static boolean isDeterminateConstant(RowExpression expression, MethodHandle isIndeterminateFunction) { if (!(expression instanceof ConstantExpression)) { return false; } ConstantExpression constantExpression = (ConstantExpression) expression; Object value = constantExpression.getValue(); boolean isNull = value == null; if (isNull) { return false; } try { return !(boolean) isIndeterminateFunction.invoke(value, false); } catch (Throwable t) { throwIfUnchecked(t); throw new RuntimeException(t); } } }
ConstantExpression constantOperand = (ConstantExpression) optimizedOperand; checkState(constantOperand.getType().equals(BOOLEAN), "Operand of IF function should be BOOLEAN type. Get type " + constantOperand.getType().getDisplayName()); if (Boolean.TRUE.equals(constantOperand.getValue())) { return call.getArguments().get(1).accept(this, context); List<Object> constantArguments = new ArrayList<>(); for (RowExpression argument : arguments) { Object value = ((ConstantExpression) argument).getValue();
private Supplier<PageProjection> compileProjectionInternal(RowExpression projection, Optional<String> classNameSuffix) { requireNonNull(projection, "projection is null"); if (projection instanceof InputReferenceExpression) { InputReferenceExpression input = (InputReferenceExpression) projection; InputPageProjection projectionFunction = new InputPageProjection(input.getField(), input.getType()); return () -> projectionFunction; } if (projection instanceof ConstantExpression) { ConstantExpression constant = (ConstantExpression) projection; ConstantPageProjection projectionFunction = new ConstantPageProjection(constant.getValue(), constant.getType()); return () -> projectionFunction; } PageFieldsToInputParametersRewriter.Result result = rewritePageFieldsToInputParameters(projection); CallSiteBinder callSiteBinder = new CallSiteBinder(); // generate Work ClassDefinition pageProjectionWorkDefinition = definePageProjectWorkClass(result.getRewrittenExpression(), callSiteBinder, classNameSuffix); Class<? extends Work> pageProjectionWorkClass; try { pageProjectionWorkClass = defineClass(pageProjectionWorkDefinition, Work.class, callSiteBinder.getBindings(), getClass().getClassLoader()); } catch (Exception e) { throw new PrestoException(COMPILER_ERROR, e); } return () -> new GeneratedPageProjection( result.getRewrittenExpression(), determinismEvaluator.isDeterministic(result.getRewrittenExpression()), result.getInputChannels(), constructorMethodHandle(pageProjectionWorkClass, BlockBuilder.class, ConnectorSession.class, Page.class, SelectedPositions.class)); }
Object object = constant.getValue(); switch (switchGenerationCase) { case DIRECT_SWITCH:
Variable wasNull = generator.wasNull(); Variable rowBlock = generator.getScope().createTempVariable(Block.class); int index = (int) ((ConstantExpression) arguments.get(1)).getValue();
RowExpression resultExpression = optimizer.optimize(jsonCastExpression); assertInstanceOf(resultExpression, ConstantExpression.class); Object resultValue = ((ConstantExpression) resultExpression).getValue(); assertInstanceOf(resultValue, IntArrayBlock.class); assertEquals(toValues(INTEGER, (IntArrayBlock) resultValue), ImmutableList.of(1, 2));
@Override public BytecodeNode visitConstant(ConstantExpression constant, Context context) Object value = constant.getValue(); Class<?> javaType = constant.getType().getJavaType();
private static boolean isConstantExpression(RowExpression expression) { List<RowExpression> rowExpressions = Expressions.subExpressions(ImmutableList.of(expression)); return rowExpressions.size() == 1 && getOnlyElement(rowExpressions) instanceof ConstantExpression && ((ConstantExpression) getOnlyElement(rowExpressions)).getValue() != null; }
@Override public RowExpression visitConstant(ConstantExpression literal, Void context) { return new ConstantExpression(literal.getValue(), targetType); } }
@VisibleForTesting static SwitchGenerationCase checkSwitchGenerationCase(Type type, List<RowExpression> values) { if (values.size() > 32) { // 32 is chosen because // * SET_CONTAINS performs worst when smaller than but close to power of 2 // * Benchmark shows performance of SET_CONTAINS is better at 50, but similar at 25. return SwitchGenerationCase.SET_CONTAINS; } if (!(type instanceof BigintType || type instanceof DateType)) { return SwitchGenerationCase.HASH_SWITCH; } for (RowExpression expression : values) { // For non-constant expressions, they will be added to the default case in the generated switch code. They do not affect any of // the cases other than the default one. Therefore, it's okay to skip them when choosing between DIRECT_SWITCH and HASH_SWITCH. // Same argument applies for nulls. if (!(expression instanceof ConstantExpression)) { continue; } Object constant = ((ConstantExpression) expression).getValue(); if (constant == null) { continue; } long longConstant = (Long) constant; if (longConstant < Integer.MIN_VALUE || longConstant > Integer.MAX_VALUE) { return SwitchGenerationCase.HASH_SWITCH; } } return SwitchGenerationCase.DIRECT_SWITCH; }
ConstantExpression constantOperand = (ConstantExpression) optimizedOperand; checkState(constantOperand.getType().equals(BOOLEAN), "Operand of IF function should be BOOLEAN type. Get type " + constantOperand.getType().getDisplayName()); if (Boolean.TRUE.equals(constantOperand.getValue())) { return call.getArguments().get(1).accept(this, context); List<Object> constantArguments = new ArrayList<>(); for (RowExpression argument : arguments) { Object value = ((ConstantExpression) argument).getValue();
BytecodeNode testBytecode = generatorContext.generate(testValue); if (testValue instanceof ConstantExpression && ((ConstantExpression) testValue).getValue() != null) { ConstantExpression constant = (ConstantExpression) testValue; Object object = constant.getValue(); switch (switchGenerationCase) { case DIRECT_SWITCH:
BytecodeExpression value = loadConstant(callSiteBinder, constantExpression.getValue(), Object.class); body.append(outputBlock.set(invokeStatic(RunLengthEncodedBlock.class, "create", Block.class, type, value, cardinality)));
@Override public BytecodeNode visitConstant(ConstantExpression constant, Scope scope) Object value = constant.getValue(); Class<?> javaType = constant.getType().getJavaType();