public static Object evaluateConstantExpression(Expression expression, Type expectedType, Metadata metadata, Session session, List<Expression> parameters) { ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP); analyzer.analyze(expression, Scope.create()); Type actualType = analyzer.getExpressionTypes().get(NodeRef.of(expression)); if (!metadata.getTypeManager().canCoerce(actualType, expectedType)) { throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, format("Cannot cast type %s to %s", actualType.getTypeSignature(), expectedType.getTypeSignature())); } Map<NodeRef<Expression>, Type> coercions = ImmutableMap.<NodeRef<Expression>, Type>builder() .putAll(analyzer.getExpressionCoercions()) .put(NodeRef.of(expression), expectedType) .build(); return evaluateConstantExpression(expression, coercions, analyzer.getTypeOnlyCoercions(), metadata, session, ImmutableSet.of(), parameters); }
@Override protected Scope visitProperty(Property node, Optional<Scope> scope) { // Property value expressions must be constant createConstantAnalyzer(metadata, session, analysis.getParameters(), WarningCollector.NOOP, analysis.isDescribe()) .analyze(node.getValue(), createScope(scope)); return createAndAssignScope(node, scope); }
@Override protected SymbolStatsEstimate visitLiteral(Literal node, Void context) { Object value = evaluate(metadata, session.toConnectorSession(), node); Type type = ExpressionAnalyzer.createConstantAnalyzer(metadata, session, ImmutableList.of(), WarningCollector.NOOP).analyze(node, Scope.create()); OptionalDouble doubleValue = toStatsRepresentation(metadata, session, type, value); SymbolStatsEstimate.Builder estimate = SymbolStatsEstimate.builder() .setNullsFraction(0) .setDistinctValuesCount(1); if (doubleValue.isPresent()) { estimate.setLowValue(doubleValue.getAsDouble()); estimate.setHighValue(doubleValue.getAsDouble()); } return estimate.build(); }
ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP); analyzer.analyze(rewrite, Scope.create()); analyzer = createConstantAnalyzer(metadata, session, parameters, WarningCollector.NOOP); analyzer.analyze(canonicalized, Scope.create());
public static Object evaluateConstantExpression(Expression expression, Type expectedType, Metadata metadata, Session session) { ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session); analyzer.analyze(expression, new RelationType(), new AnalysisContext()); Type actualType = analyzer.getExpressionTypes().get(expression); if (!canCoerce(actualType, expectedType)) { throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, String.format("Cannot cast type %s to %s", expectedType.getTypeSignature(), actualType.getTypeSignature())); } IdentityHashMap<Expression, Type> coercions = new IdentityHashMap<>(); coercions.putAll(analyzer.getExpressionCoercions()); coercions.put(expression, expectedType); return evaluateConstantExpression(expression, coercions, metadata, session, ImmutableSet.of()); }
ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session); analyzer.analyze(canonicalized, new RelationType(), new AnalysisContext());
@Override protected RelationType visitCreateTableAsSelect(CreateTableAsSelect node, AnalysisContext context) { analysis.setUpdateType("CREATE TABLE"); // turn this into a query that has a new table writer node on top. QualifiedObjectName targetTable = createQualifiedObjectName(session, node, node.getName()); analysis.setCreateTableDestination(targetTable); Optional<TableHandle> targetTableHandle = metadata.getTableHandle(session, targetTable); if (targetTableHandle.isPresent()) { if (node.isNotExists()) { analysis.setCreateTableAsSelectNoOp(true); analysis.setStatement(node); return new RelationType(Field.newUnqualified("rows", BIGINT)); } throw new SemanticException(TABLE_ALREADY_EXISTS, node, "Destination table '%s' already exists", targetTable); } for (Expression expression : node.getProperties().values()) { // analyze table property value expressions which must be constant createConstantAnalyzer(metadata, session) .analyze(expression, new RelationType(), context); } analysis.setCreateTableProperties(node.getProperties()); accessControl.checkCanCreateTable(session.getRequiredTransactionId(), session.getIdentity(), targetTable); analysis.setCreateTableAsSelectWithData(node.isWithData()); // analyze the query that creates the table RelationType descriptor = process(node.getQuery(), context); analysis.setStatement(node); validateColumns(node, descriptor); return new RelationType(Field.newUnqualified("rows", BIGINT)); }