use of io.prestosql.sql.tree.ArrayConstructor in project hetu-core by openlookeng.
the class HiveAstBuilder method visitCreateTable.
@Override
public Node visitCreateTable(HiveSqlParser.CreateTableContext context) {
if (context.TEMPORARY() != null) {
addDiff(DiffType.UNSUPPORTED, context.TEMPORARY().getText(), "[TEMPORARY] is not supported");
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: CREATE TEMPORARY TABLE", context);
}
if (context.constraintSpecification() != null) {
HiveSqlParser.ConstraintSpecificationContext constraintContext = context.constraintSpecification();
if (constraintContext.PRIMARY() != null) {
addDiff(DiffType.UNSUPPORTED, constraintContext.PRIMARY().getText(), "[PRIMARY KEY] is not supported");
addDiff(DiffType.UNSUPPORTED, constraintContext.KEY().getText(), null);
}
if (constraintContext.CONSTRAINT() != null) {
addDiff(DiffType.UNSUPPORTED, constraintContext.CONSTRAINT().getText(), "[CONSTRAINT] is not supported");
}
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported constraint statement", context.constraintSpecification());
}
Optional<String> comment = Optional.empty();
if (context.COMMENT() != null) {
comment = Optional.of(((StringLiteral) visit(context.string(0))).getValue());
}
List<Property> properties = new ArrayList<>();
if (context.TRANSACTIONAL() != null) {
Identifier name = new Identifier("transactional");
Expression value = new Identifier("true");
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.TRANSACTIONAL().getText(), "transactional = true", "[TRANSACTIONAL] is formatted");
}
List<TableElement> elements = getTableElements(context.tableElement());
if (context.PARTITIONED() != null) {
Identifier name = new Identifier("partitioned_by");
List<ColumnDefinition> columnDefinitions = getColumnDefinitions(context.partitionedBy().columnDefinition());
List<Expression> expressions = new ArrayList<>();
Iterator<ColumnDefinition> iterator = columnDefinitions.iterator();
while (iterator.hasNext()) {
ColumnDefinition iter = iterator.next();
expressions.add(new StringLiteral(iter.getName().getValue()));
elements.add(new ColumnDefinition(iter.getName(), iter.getType(), true, emptyList(), Optional.empty()));
}
Expression value = new ArrayConstructor(expressions);
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.PARTITIONED().getText(), PARTITIONED_BY, "[PARTITIONED BY] is formatted");
}
if (context.CLUSTERED() != null) {
Identifier name = new Identifier("bucketed_by");
List<Expression> quotedExpressions = new ArrayList<>();
List<Expression> expressions = getExpressions(context.clusteredBy().expression());
for (int i = 0; i < expressions.size(); i++) {
quotedExpressions.add(new StringLiteral(expressions.get(i).toString()));
}
Expression value = new ArrayConstructor(quotedExpressions);
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.CLUSTERED().getText(), "bucketed_by", "[CLUSTERED BY] is formatted");
}
if (context.SORTED() != null) {
Identifier name = new Identifier("sorted_by");
List<Expression> expressions = new ArrayList<>();
List<HiveSqlParser.SortItemContext> sortItemContexts = context.sortedBy().sortItem();
for (int i = 0; i < sortItemContexts.size(); i++) {
HiveSqlParser.SortItemContext sortItemContext = sortItemContexts.get(i);
String sortedBy = sortItemContext.expression().getText();
if (sortItemContext.ordering != null) {
sortedBy += " " + sortItemContext.ordering.getText();
}
expressions.add(new StringLiteral(sortedBy));
}
Expression value = new ArrayConstructor(expressions);
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.SORTED().getText(), SORTED_BY, "[SORTED BY] is formatted");
}
if (context.INTO() != null) {
Identifier name = new Identifier("bucket_count");
Expression value = (Expression) visit(context.bucketcount);
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.INTO().getText(), "bucket_count", "[INTO BUCKETS] is formatted");
}
if (context.SKEWED() != null) {
addDiff(DiffType.UNSUPPORTED, context.SKEWED().getText(), "[SKEWED] is not supported");
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: SKEWED", context.columnAliases());
}
if (context.ROW() != null) {
addDiff(DiffType.UNSUPPORTED, context.ROW().getText(), "[ROW FORMAT] is not supported");
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: ROW FORMAT", context.rowFormat());
}
if (context.STORED() != null) {
addDiff(DiffType.UNSUPPORTED, context.STORED().getText(), "[STORED BY] is not supported");
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: STORED BY", context.storedBy);
}
if (context.stored_as != null) {
Identifier name = new Identifier("format");
String storedAsString = ((Identifier) visit(context.stored_as)).getValue();
Expression value = new StringLiteral(getFileFormat(storedAsString));
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.STORED_AS(0).getText(), FORMAT, "[STORED AS] is formatted");
}
if (context.EXTERNAL() != null) {
if (context.LOCATION() == null) {
addDiff(DiffType.UNSUPPORTED, context.EXTERNAL().getText(), "[EXTERNAL] should be used with location");
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, "Unsupported statement: External attribute should be used with location", context);
}
Identifier name = new Identifier("external");
Expression value = new Identifier("true");
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.EXTERNAL().getText(), "external = true", "[EXTERNAL] is formatted");
}
if (context.LOCATION() != null) {
Identifier name = new Identifier("location");
Expression value = (StringLiteral) visit(context.location);
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.LOCATION().getText(), LOCATION + " = " + value, "[LOCATION] is formatted");
}
if (context.TBLPROPERTIES() != null) {
List<Property> tblProperties = visit(context.tableProperties.property(), Property.class);
for (int i = 0; i < tblProperties.size(); i++) {
Property property = tblProperties.get(i);
if (property.getName().getValue().equalsIgnoreCase("transactional")) {
Identifier name = new Identifier("transactional");
Expression value = new Identifier(unquote(property.getValue().toString()));
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, property.getName().getValue(), "transactional = ", "[TRANSACTIONAL] is formatted");
} else {
addDiff(DiffType.UNSUPPORTED, property.getName().getValue(), "[TBLPROPERTIES] has unsupported properties");
throw unsupportedError(ErrorType.UNSUPPORTED_STATEMENT, format("Unsupported attribute: %s", property.getName().getValue()), context.tableProperties);
}
}
}
return new CreateTable(getLocation(context), getQualifiedName(context.qualifiedName()), elements, context.EXISTS() != null, properties, comment);
}
use of io.prestosql.sql.tree.ArrayConstructor in project hetu-core by openlookeng.
the class ImpalaAstBuilder method visitCreateTable.
@Override
public Node visitCreateTable(ImpalaSqlParser.CreateTableContext context) {
Optional<String> comment = Optional.empty();
if (context.COMMENT() != null) {
comment = Optional.of(((StringLiteral) visit(context.comment)).getValue());
}
List<Property> properties = new ArrayList<>();
// external table
if (context.EXTERNAL() != null) {
if (context.LOCATION() == null) {
throw unsupportedError(ErrorType.SYNTAX_ERROR, "External attribute should be used with location", context);
}
Identifier name = new Identifier("external");
Expression value = new Identifier("true");
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, context.EXTERNAL().getText(), "external = true", "[external] is formatted");
}
// handle partitioned by
List<TableElement> elements = getTableElements(context.tableElement());
if (context.AS() == null && elements.size() == 0) {
throw unsupportedError(ErrorType.SYNTAX_ERROR, "Create table should specify at least one column.", context);
}
if (context.PARTITIONED() != null) {
List<ColumnDefinition> columnDefinitions = getColumnDefinitions(context.partitionedBy().columnDefinition());
List<Expression> expressions = new ArrayList<>();
Iterator<ColumnDefinition> iterator = columnDefinitions.iterator();
while (iterator.hasNext()) {
ColumnDefinition iter = iterator.next();
expressions.add(new StringLiteral(iter.getName().getValue()));
// add the partitioned_by column to table columns
elements.add(new ColumnDefinition(iter.getName(), iter.getType(), true, emptyList(), Optional.empty()));
}
Expression value = new ArrayConstructor(expressions);
properties.add(new Property(new Identifier(PARTITIONED_BY), value));
addDiff(DiffType.MODIFIED, context.PARTITIONED().getText(), PARTITIONED_BY, "[partitioned by] is formatted");
}
// handle sort by
if (context.SORT() != null) {
List<Expression> quotedExpressions = new ArrayList<>();
List<Expression> expressions = getExpressions(context.sortedBy().expression());
for (int i = 0; i < expressions.size(); i++) {
quotedExpressions.add(new StringLiteral(expressions.get(i).toString()));
}
Expression value = new ArrayConstructor(quotedExpressions);
properties.add(new Property(new Identifier(SORTED_BY), value));
addDiff(DiffType.MODIFIED, context.SORT().getText(), SORTED_BY, "[sorted by] is formatted");
}
// row format
if (context.ROW() != null && context.FORMAT() != null) {
addDiff(DiffType.UNSUPPORTED, context.ROW().getText(), "[ROW FORMAT] is not supported");
addDiff(DiffType.UNSUPPORTED, context.FORMAT().getText(), null);
throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "ROW FORMAT", context);
}
// serde properties
if (context.SERDEPROPERTIES() != null) {
addDiff(DiffType.UNSUPPORTED, context.SERDEPROPERTIES().getText(), "[WITH SERDEPROPERTIES] is not supported");
throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "WITH SERDEPROPERTIES", context);
}
// stored as
if (context.STORED_AS() != null) {
String storedAsString = ((Identifier) visit(context.stored_as)).getValue();
Expression value = new StringLiteral(getFileFormat(storedAsString));
properties.add(new Property(new Identifier(FORMAT), value));
addDiff(DiffType.MODIFIED, context.STORED_AS().getText(), FORMAT, "[stored as] is formatted");
}
// location
if (context.LOCATION() != null) {
Expression value = (StringLiteral) visit(context.location);
properties.add(new Property(new Identifier(LOCATION), value));
addDiff(DiffType.MODIFIED, context.LOCATION().getText(), LOCATION + " = " + value, "[location] is formatted");
}
// cached in
if (context.CACHED() != null) {
addDiff(DiffType.UNSUPPORTED, context.CACHED().getText(), "[CACHED IN] is not supported");
throw unsupportedError(ErrorType.UNSUPPORTED_KEYWORDS, "CACHED IN", context);
}
// table properties
if (context.TBLPROPERTIES() != null) {
List<Property> tblProperties = visit(context.tblProp.property(), Property.class);
for (int i = 0; i < tblProperties.size(); i++) {
Property property = tblProperties.get(i);
if (property.getName().getValue().equalsIgnoreCase(TRANSACTIONAL)) {
Identifier name = new Identifier(TRANSACTIONAL);
Expression value = new Identifier(unquote(property.getValue().toString()));
properties.add(new Property(name, value));
addDiff(DiffType.MODIFIED, property.getName().getValue(), "transactional = ", "[transactional] is formatted");
} else {
addDiff(DiffType.UNSUPPORTED, property.getName().getValue(), "[TBLPROPERTIES] has unsupported properties");
throw unsupportedError(ErrorType.UNSUPPORTED_ATTRIBUTE, property.getName().getValue(), context.tblProp);
}
}
}
// create table as
if (context.AS() != null) {
return new CreateTableAsSelect(getLocation(context), getQualifiedName(context.tblName), (Query) visit(context.query()), context.EXISTS() != null, properties, true, Optional.empty(), comment);
}
return new CreateTable(getLocation(context), getQualifiedName(context.tblName), elements, context.EXISTS() != null, properties, comment);
}
use of io.prestosql.sql.tree.ArrayConstructor in project hetu-core by openlookeng.
the class GroupingOperationRewriter method rewriteGroupingOperation.
public static Expression rewriteGroupingOperation(GroupingOperation expression, List<Set<Integer>> groupingSets, Map<NodeRef<Expression>, FieldId> columnReferenceFields, Optional<Symbol> groupIdSymbol) {
requireNonNull(groupIdSymbol, "groupIdSymbol is null");
// 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, toSymbolReference(groupIdSymbol.get()), new GenericLiteral("BIGINT", "1")));
}
}
use of io.prestosql.sql.tree.ArrayConstructor in project hetu-core by openlookeng.
the class TestSqlParser method testAnalyze.
@Test
public void testAnalyze() {
QualifiedName table = QualifiedName.of("foo");
assertStatement("ANALYZE foo", new Analyze(table, ImmutableList.of()));
assertStatement("ANALYZE foo WITH ( \"string\" = 'bar', \"long\" = 42, computed = concat('ban', 'ana'), a = ARRAY[ 'v1', 'v2' ] )", new Analyze(table, ImmutableList.of(new Property(new Identifier("string"), new StringLiteral("bar")), new Property(new Identifier("long"), new LongLiteral("42")), new Property(new Identifier("computed"), new FunctionCall(QualifiedName.of("concat"), ImmutableList.of(new StringLiteral("ban"), new StringLiteral("ana")))), new Property(new Identifier("a"), new ArrayConstructor(ImmutableList.of(new StringLiteral("v1"), new StringLiteral("v2")))))));
assertStatement("EXPLAIN ANALYZE foo", new Explain(new Analyze(table, ImmutableList.of()), false, false, ImmutableList.of()));
assertStatement("EXPLAIN ANALYZE ANALYZE foo", new Explain(new Analyze(table, ImmutableList.of()), true, false, ImmutableList.of()));
}
Aggregations