@Override public Node visitNullLiteral(SqlBaseParser.NullLiteralContext context) { return new NullLiteral(getLocation(context)); }
private static NullLiteral nullLiteral() { return new NullLiteral(); }
private static Expression booleanConstant(@Nullable Boolean value) { if (value == null) { return new Cast(new NullLiteral(), BOOLEAN.toString()); } return new BooleanLiteral(value.toString()); }
private static IfExpression createIfExpression(Expression left, Expression right, ComparisonExpression.Operator operator, Expression result, Type trueValueType) { return new IfExpression( new ComparisonExpression(operator, left, right), result, new Cast(new NullLiteral(), trueValueType.getTypeSignature().toString())); } }
private static void assertCast(String type, String expected) { assertExpression("CAST(null AS " + type + ")", new Cast(new NullLiteral(), expected)); }
private static Expression nullLiteral(Type type) { return cast(new NullLiteral(), type); }
private PlanNode appendMarkers(PlanNode source, int markerIndex, List<Symbol> markers, Map<Symbol, SymbolReference> projections) { Assignments.Builder assignments = Assignments.builder(); // add existing intersect symbols to projection for (Map.Entry<Symbol, SymbolReference> entry : projections.entrySet()) { Symbol symbol = symbolAllocator.newSymbol(entry.getKey().getName(), symbolAllocator.getTypes().get(entry.getKey())); assignments.put(symbol, entry.getValue()); } // add extra marker fields to the projection for (int i = 0; i < markers.size(); ++i) { Expression expression = (i == markerIndex) ? TRUE_LITERAL : new Cast(new NullLiteral(), StandardTypes.BOOLEAN); assignments.put(symbolAllocator.newSymbol(markers.get(i).getName(), BOOLEAN), expression); } return new ProjectNode(idAllocator.getNextId(), source, assignments.build()); }
public ApplyNode apply(Assignments subqueryAssignments, List<Symbol> correlation, PlanNode input, PlanNode subquery) { NullLiteral originSubquery = new NullLiteral(); // does not matter for tests return new ApplyNode(idAllocator.getNextId(), input, subquery, subqueryAssignments, correlation, originSubquery); }
NullLiteral nullLiteral = new NullLiteral(); ImmutableList.Builder<Symbol> nullSymbols = ImmutableList.builder(); ImmutableList.Builder<Expression> nullLiterals = ImmutableList.builder();
public Expression rewriteUsingBounds(QuantifiedComparisonExpression quantifiedComparison, Symbol minValue, Symbol maxValue, Symbol countAllValue, Symbol countNonNullValue) { BooleanLiteral emptySetResult = quantifiedComparison.getQuantifier().equals(ALL) ? TRUE_LITERAL : FALSE_LITERAL; Function<List<Expression>, Expression> quantifier = quantifiedComparison.getQuantifier().equals(ALL) ? ExpressionUtils::combineConjuncts : ExpressionUtils::combineDisjuncts; Expression comparisonWithExtremeValue = getBoundComparisons(quantifiedComparison, minValue, maxValue); return new SimpleCaseExpression( countAllValue.toSymbolReference(), ImmutableList.of(new WhenClause( new GenericLiteral("bigint", "0"), emptySetResult)), Optional.of(quantifier.apply(ImmutableList.of( comparisonWithExtremeValue, new SearchedCaseExpression( ImmutableList.of( new WhenClause( new ComparisonExpression(NOT_EQUAL, countAllValue.toSymbolReference(), countNonNullValue.toSymbolReference()), new Cast(new NullLiteral(), BooleanType.BOOLEAN.toString()))), Optional.of(emptySetResult)))))); }
public LateralJoinNode lateral(List<Symbol> correlation, PlanNode input, PlanNode subquery) { NullLiteral originSubquery = new NullLiteral(); // does not matter for tests return new LateralJoinNode(idAllocator.getNextId(), input, subquery, correlation, LateralJoinNode.Type.INNER, originSubquery); }
@Override protected Node visitDescribeInput(DescribeInput node, Void context) throws SemanticException { String sqlString = session.getPreparedStatement(node.getName().getValue()); Statement statement = parser.createStatement(sqlString, createParsingOptions(session)); // create analysis for the query we are describing. Analyzer analyzer = new Analyzer(session, metadata, parser, accessControl, queryExplainer, parameters, warningCollector); Analysis analysis = analyzer.analyze(statement, true); // get all parameters in query List<Parameter> parameters = getParameters(statement); // return the positions and types of all parameters Row[] rows = parameters.stream().map(parameter -> createDescribeInputRow(parameter, analysis)).toArray(Row[]::new); Optional<String> limit = Optional.empty(); if (rows.length == 0) { rows = new Row[] {row(new NullLiteral(), new NullLiteral())}; limit = Optional.of("0"); } return simpleQuery( selectList(identifier("Position"), identifier("Type")), aliased( values(rows), "Parameter Input", ImmutableList.of("Position", "Type")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(ordering(ascending("Position"))), limit); }
public ValuesNode values(PlanNodeId id, int rows, Symbol... columns) { return values( id, ImmutableList.copyOf(columns), nElements(rows, row -> nElements(columns.length, cell -> (Expression) new NullLiteral()))); }
@Test public void testNullIf() { assertExpression("nullif(42, 87)", new NullIfExpression(new LongLiteral("42"), new LongLiteral("87"))); assertExpression("nullif(42, null)", new NullIfExpression(new LongLiteral("42"), new NullLiteral())); assertExpression("nullif(null, null)", new NullIfExpression(new NullLiteral(), new NullLiteral())); assertInvalidExpression("nullif(1)", "Invalid number of arguments for 'nullif' function"); assertInvalidExpression("nullif(1, 2, 3)", "Invalid number of arguments for 'nullif' function"); assertInvalidExpression("nullif(42, 87) filter (where true)", "FILTER not valid for 'nullif' function"); assertInvalidExpression("nullif(42, 87) OVER ()", "OVER clause not valid for 'nullif' function"); }
Row[] rows = analysis.getRootScope().getRelationType().getVisibleFields().stream().map(field -> createDescribeOutputRow(field, analysis)).toArray(Row[]::new); if (rows.length == 0) { NullLiteral nullLiteral = new NullLiteral(); rows = new Row[] {row(nullLiteral, nullLiteral, nullLiteral, nullLiteral, nullLiteral, nullLiteral, nullLiteral)}; limit = Optional.of("0");
@Test public void testCoalesce() { assertInvalidExpression("coalesce()", "The 'coalesce' function must have at least two arguments"); assertInvalidExpression("coalesce(5)", "The 'coalesce' function must have at least two arguments"); assertInvalidExpression("coalesce(1, 2) filter (where true)", "FILTER not valid for 'coalesce' function"); assertInvalidExpression("coalesce(1, 2) OVER ()", "OVER clause not valid for 'coalesce' function"); assertExpression("coalesce(13, 42)", new CoalesceExpression(new LongLiteral("13"), new LongLiteral("42"))); assertExpression("coalesce(6, 7, 8)", new CoalesceExpression(new LongLiteral("6"), new LongLiteral("7"), new LongLiteral("8"))); assertExpression("coalesce(13, null)", new CoalesceExpression(new LongLiteral("13"), new NullLiteral())); assertExpression("coalesce(null, 13)", new CoalesceExpression(new NullLiteral(), new LongLiteral("13"))); assertExpression("coalesce(null, null)", new CoalesceExpression(new NullLiteral(), new NullLiteral())); }
@Test public void testIf() { assertExpression("if(true, 1, 0)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("1"), new LongLiteral("0"))); assertExpression("if(true, 3, null)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), new NullLiteral())); assertExpression("if(false, null, 4)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new LongLiteral("4"))); assertExpression("if(false, null, null)", new IfExpression(new BooleanLiteral("false"), new NullLiteral(), new NullLiteral())); assertExpression("if(true, 3)", new IfExpression(new BooleanLiteral("true"), new LongLiteral("3"), null)); assertInvalidExpression("IF(true)", "Invalid number of arguments for 'if' function"); assertInvalidExpression("IF(true, 1, 0) FILTER (WHERE true)", "FILTER not valid for 'if' function"); assertInvalidExpression("IF(true, 1, 0) OVER()", "OVER clause not valid for 'if' function"); }
@Test public void testExpressionsThatMayReturnNullOnNonNullInput() { List<Expression> candidates = ImmutableList.of( new Cast(nameReference("b"), "BIGINT", true), // try_cast new FunctionCall(QualifiedName.of("try"), ImmutableList.of(nameReference("b"))), new NullIfExpression(nameReference("b"), number(1)), new IfExpression(nameReference("b"), number(1), new NullLiteral()), new DereferenceExpression(nameReference("b"), identifier("x")), new InPredicate(nameReference("b"), new InListExpression(ImmutableList.of(new NullLiteral()))), new SearchedCaseExpression(ImmutableList.of(new WhenClause(new IsNotNullPredicate(nameReference("b")), new NullLiteral())), Optional.empty()), new SimpleCaseExpression(nameReference("b"), ImmutableList.of(new WhenClause(number(1), new NullLiteral())), Optional.empty()), new SubscriptExpression(new ArrayConstructor(ImmutableList.of(new NullLiteral())), nameReference("b"))); for (Expression candidate : candidates) { EqualityInference.Builder builder = new EqualityInference.Builder(); builder.extractInferenceCandidates(equals(nameReference("b"), nameReference("x"))); builder.extractInferenceCandidates(equals(nameReference("a"), candidate)); EqualityInference inference = builder.build(); List<Expression> equalities = inference.generateEqualitiesPartitionedBy(matchesSymbols("b")).getScopeStraddlingEqualities(); assertEquals(equalities.size(), 1); assertTrue(equalities.get(0).equals(equals(nameReference("x"), nameReference("b"))) || equalities.get(0).equals(equals(nameReference("b"), nameReference("x")))); } }
@Test public void testFunctionCall() { assertCalculate( new FunctionCall( QualifiedName.of("length"), ImmutableList.of(new Cast(new NullLiteral(), "VARCHAR(10)")))) .distinctValuesCount(0.0) .lowValueUnknown() .highValueUnknown() .nullsFraction(1.0); assertCalculate( new FunctionCall( QualifiedName.of("length"), ImmutableList.of(new SymbolReference("x"))), PlanNodeStatsEstimate.unknown(), TypeProvider.viewOf(ImmutableMap.of(new Symbol("x"), createVarcharType(2)))) .distinctValuesCountUnknown() .lowValueUnknown() .highValueUnknown() .nullsFractionUnknown(); }
outputSymbols.put(aggregateInfo.getMask(), new NullLiteral());