@Override protected Object visitArrayConstructor(ArrayConstructor node, Object context) { Type elementType = ((ArrayType) expressionTypes.get(node)).getElementType(); BlockBuilder arrayBlockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), node.getValues().size()); for (Expression expression : node.getValues()) { Object value = process(expression, context); if (value instanceof Expression) { return visitFunctionCall(new FunctionCall(QualifiedName.of(ArrayConstructor.ARRAY_CONSTRUCTOR), node.getValues()), context); } writeNativeValue(elementType, arrayBlockBuilder, value); } return arrayBlockBuilder.build(); }
@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 String visitArrayConstructor(ArrayConstructor node, Void context) { ImmutableList.Builder<String> valueStrings = ImmutableList.builder(); for (Expression value : node.getValues()) { valueStrings.add(sqlFormatter.formatSql(value)); } return "ARRAY[" + Joiner.on(",").join(valueStrings.build()) + "]"; }
@Override protected String visitArrayConstructor(ArrayConstructor node, StackableAstVisitorContext<Integer> indent) { ImmutableList.Builder<String> valueStrings = ImmutableList.builder(); for (Expression value : node.getValues()) { valueStrings.add(formatExpression(value, parameters, indent.getContext() + 1)); } return "ARRAY[" + Joiner.on(",").join(valueStrings.build()) + "]"; }
@Override protected RowExpression visitArrayConstructor(ArrayConstructor node, Void context) { List<RowExpression> arguments = node.getValues().stream() .map(value -> process(value, context)) .collect(toImmutableList()); List<Type> argumentTypes = arguments.stream() .map(RowExpression::getType) .collect(toImmutableList()); return call(arrayConstructorSignature(types.get(node), argumentTypes), types.get(node), arguments); }
@Override protected Type visitArrayConstructor(ArrayConstructor node, StackableAstVisitorContext<AnalysisContext> context) { Type type = coerceToSingleType(context, "All ARRAY elements must be the same type: %s", node.getValues()); Type arrayType = typeManager.getParameterizedType(ARRAY.getName(), ImmutableList.of(type.getTypeSignature()), ImmutableList.of()); expressionTypes.put(node, arrayType); return arrayType; }
@Override protected String visitArrayConstructor(ArrayConstructor node, Boolean unmangleNames) { ImmutableList.Builder<String> valueStrings = ImmutableList.builder(); for (Expression value : node.getValues()) { valueStrings.add(formatSql(value, unmangleNames)); } return "ARRAY[" + Joiner.on(",").join(valueStrings.build()) + "]"; }
@Test public void testArrayConstructor() throws Exception { assertExpression("ARRAY []", new ArrayConstructor(ImmutableList.<Expression>of())); assertExpression("ARRAY [1, 2]", new ArrayConstructor(ImmutableList.<Expression>of(new LongLiteral("1"), new LongLiteral("2")))); assertExpression("ARRAY [1.0, 2.5]", new ArrayConstructor(ImmutableList.<Expression>of(new DoubleLiteral("1.0"), new DoubleLiteral("2.5")))); assertExpression("ARRAY ['hi']", new ArrayConstructor(ImmutableList.<Expression>of(new StringLiteral("hi")))); assertExpression("ARRAY ['hi', 'hello']", new ArrayConstructor(ImmutableList.<Expression>of(new StringLiteral("hi"), new StringLiteral("hello")))); }
@Override protected Boolean visitArrayConstructor(ArrayConstructor node, Void context) { return node.getValues().stream().allMatch(expression -> process(expression, context)); }
@Override public Node visitArrayConstructor(SqlBaseParser.ArrayConstructorContext context) { return new ArrayConstructor(getLocation(context), visit(context.expression(), Expression.class)); }