@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() 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 } }
@Override protected RowExpression visitSubscriptExpression(SubscriptExpression node, Void context) { RowExpression base = process(node.getBase(), context); RowExpression index = process(node.getIndex(), context); return call( subscriptSignature(types.get(node), base.getType(), index.getType()), types.get(node), base, index); }
@Override protected String visitSubscriptExpression(SubscriptExpression node, Void context) { return sqlFormatter.formatSql(node.getBase()) + "[" + sqlFormatter.formatSql(node.getIndex()) + "]"; }
@Override protected String visitSubscriptExpression(SubscriptExpression node, StackableAstVisitorContext<Integer> indent) { return formatExpression(node.getBase(), parameters, indent.getContext()) + "[" + formatExpression(node.getIndex(), parameters, indent.getContext ()) + "]"; }
@Override protected Boolean visitSubscriptExpression(SubscriptExpression node, Void context) { return process(node.getBase(), context) && process(node.getIndex(), context); }
@Override protected Type visitSubscriptExpression(SubscriptExpression node, StackableAstVisitorContext<AnalysisContext> context) { return getOperator(context, node, SUBSCRIPT, node.getBase(), node.getIndex()); }
@Override public Node visitSubscript(SqlBaseParser.SubscriptContext context) { return new SubscriptExpression(getLocation(context), (Expression) visit(context.value), (Expression) visit(context.index)); }
@Override protected String visitSubscriptExpression(SubscriptExpression node, Boolean unmangleNames) { return formatSql(node.getBase(), unmangleNames) + "[" + formatSql(node.getIndex(), unmangleNames) + "]"; }
@Test public void testSelectWithRowType() throws Exception { assertStatement("SELECT col1.f1, col2, col3.f1.f2.f3 FROM table1", new Query( Optional.empty(), new QuerySpecification( selectList( new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("col1")), "f1"), new QualifiedNameReference(QualifiedName.of("col2")), new DereferenceExpression( new DereferenceExpression(new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("col3")), "f1"), "f2"), "f3")), Optional.of(new Table(QualifiedName.of("table1"))), Optional.empty(), ImmutableList.of(), Optional.empty(), ImmutableList.of(), Optional.empty()), ImmutableList.<SortItem>of(), Optional.empty(), Optional.empty())); assertStatement("SELECT col1.f1[0], col2, col3[2].f2.f3, col4[4] FROM table1", new Query( Optional.empty(), 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"))), Optional.empty(), ImmutableList.of(), Optional.empty(), ImmutableList.of(), Optional.empty()), ImmutableList.<SortItem>of(), Optional.empty(), Optional.empty())); assertStatement("SELECT test_row(11, 12).col0", new Query( Optional.empty(), new QuerySpecification( selectList( new DereferenceExpression(new FunctionCall(QualifiedName.of("test_row"), Lists.newArrayList(new LongLiteral("11"), new LongLiteral("12"))), "col0") ), Optional.empty(), Optional.empty(), ImmutableList.of(), Optional.empty(), ImmutableList.of(), Optional.empty()), ImmutableList.<SortItem>of(), Optional.empty(), Optional.empty())); }