@Override public Node visitSubscript(SqlBaseParser.SubscriptContext context) { return new SubscriptExpression(getLocation(context), (Expression) visit(context.value), (Expression) visit(context.index)); }
public static Expression rewriteGroupingOperation(GroupingOperation expression, List<Set<Integer>> groupingSets, Map<NodeRef<Expression>, FieldId> columnReferenceFields, Optional<Symbol> groupIdSymbol) { requireNonNull(groupIdSymbol, "groupIdSymbol is null"); // No GroupIdNode and a GROUPING() operation imply a single grouping, which // means that any columns specified as arguments to GROUPING() will be included // in the group and none of them will be aggregated over. Hence, re-write the // GroupingOperation to a constant literal of 0. // See SQL:2011:4.16.2 and SQL:2011:6.9.10. if (groupingSets.size() == 1) { return new LongLiteral("0"); } else { checkState(groupIdSymbol.isPresent(), "groupId symbol is missing"); RelationId relationId = columnReferenceFields.get(NodeRef.of(expression.getGroupingColumns().get(0))).getRelationId(); List<Integer> columns = expression.getGroupingColumns().stream() .map(NodeRef::of) .peek(groupingColumn -> checkState(columnReferenceFields.containsKey(groupingColumn), "the grouping column is not in the columnReferencesField map")) .map(columnReferenceFields::get) .map(fieldId -> translateFieldToInteger(fieldId, relationId)) .collect(toImmutableList()); List<Expression> groupingResults = groupingSets.stream() .map(groupingSet -> String.valueOf(calculateGrouping(groupingSet, columns))) .map(LongLiteral::new) .collect(toImmutableList()); // It is necessary to add a 1 to the groupId because the underlying array is indexed starting at 1 return new SubscriptExpression( new ArrayConstructor(groupingResults), new ArithmeticBinaryExpression(ADD, groupIdSymbol.get().toSymbolReference(), new GenericLiteral("BIGINT", "1"))); } }
@Test public void testArraySubscript() { assertExpression("ARRAY [1, 2][1]", new SubscriptExpression( new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"))), new LongLiteral("1"))); try { assertExpression("CASE WHEN TRUE THEN ARRAY[1,2] END[1]", null); fail(); } catch (RuntimeException e) { // Expected } }
@Override protected Object visitSubscriptExpression(SubscriptExpression node, Object context) { Object base = process(node.getBase(), context); if (base == null) { return null; } Object index = process(node.getIndex(), context); if (index == null) { return null; } if ((index instanceof Long) && isArray(type(node.getBase()))) { ArraySubscriptOperator.checkArrayIndex((Long) index); } if (hasUnresolvedValue(base, index)) { return new SubscriptExpression(toExpression(base, type(node.getBase())), toExpression(index, type(node.getIndex()))); } return invokeOperator(OperatorType.SUBSCRIPT, types(node.getBase(), node.getIndex()), ImmutableList.of(base, index)); }
new QuerySpecification( selectList( new SubscriptExpression(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new LongLiteral("0")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new SubscriptExpression(new Identifier("col3"), new LongLiteral("2")), identifier("f2")), identifier("f3")), new SubscriptExpression(new Identifier("col4"), new LongLiteral("4"))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(),
@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")))); } }
@Override public Node visitSubscript(SqlBaseParser.SubscriptContext context) { return new SubscriptExpression(getLocation(context), (Expression) visit(context.value), (Expression) visit(context.index)); }
@Override public Node visitSubscript(SqlBaseParser.SubscriptContext context) { return new SubscriptExpression(getLocation(context), (Expression) visit(context.value), (Expression) visit(context.index)); }
@Override protected Object visitSubscriptExpression(SubscriptExpression node, Object context) { Object base = process(node.getBase(), context); if (base == null) { return null; } Object index = process(node.getIndex(), context); if (index == null) { return null; } if ((index instanceof Long) && isArray(expressionTypes.get(node.getBase()))) { ArraySubscriptOperator.checkArrayIndex((Long) index); } if (hasUnresolvedValue(base, index)) { return new SubscriptExpression(toExpression(base, expressionTypes.get(node.getBase())), toExpression(index, expressionTypes.get(node.getIndex()))); } return invokeOperator(OperatorType.SUBSCRIPT, types(node.getBase(), node.getIndex()), ImmutableList.of(base, index)); }
@Test public void testArraySubscript() { assertExpression("ARRAY [1, 2][1]", new SubscriptExpression( new ArrayConstructor(ImmutableList.of(new LongLiteral("1"), new LongLiteral("2"))), new LongLiteral("1"))); try { assertExpression("CASE WHEN TRUE THEN ARRAY[1,2] END[1]", null); fail(); } catch (RuntimeException e) { // Expected } }
@Test public void testArraySubscript() throws Exception { assertExpression("ARRAY [1, 2][1]", new SubscriptExpression( new ArrayConstructor(ImmutableList.<Expression>of(new LongLiteral("1"), new LongLiteral("2"))), new LongLiteral("1")) ); try { assertExpression("CASE WHEN TRUE THEN ARRAY[1,2] END[1]", null); fail(); } catch (RuntimeException e) { // Expected } }
new QuerySpecification( selectList( new SubscriptExpression(new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("col1")), "f1"), new LongLiteral("0")), new QualifiedNameReference(QualifiedName.of("col2")), new DereferenceExpression(new DereferenceExpression(new SubscriptExpression(new QualifiedNameReference(QualifiedName.of("col3")), new LongLiteral("2")), "f2"), "f3"), new SubscriptExpression(new QualifiedNameReference(QualifiedName.of("col4")), new LongLiteral("4")) ), Optional.of(new Table(QualifiedName.of("table1"))),
new QuerySpecification( selectList( new SubscriptExpression(new DereferenceExpression(new Identifier("col1"), identifier("f1")), new LongLiteral("0")), new Identifier("col2"), new DereferenceExpression(new DereferenceExpression(new SubscriptExpression(new Identifier("col3"), new LongLiteral("2")), identifier("f2")), identifier("f3")), new SubscriptExpression(new Identifier("col4"), new LongLiteral("4"))), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(),