Pochatkin commented on code in PR #3267:
URL: https://github.com/apache/ignite-3/pull/3267#discussion_r1509163317


##########
modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/Colocate.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.catalog.sql;
+
+import static 
org.apache.ignite.internal.catalog.sql.QueryPartCollection.partsList;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+class Colocate extends QueryPart {
+
+    private final List<Name> names;
+
+    Colocate(String... columnNames) {
+        this(Arrays.asList(columnNames));

Review Comment:
   I absolutely don't understand what happened here. Class Name accept vararg 
of String but here we map String vararg to List and after that we create 
List<Name> where each name contains only one columnName. Is it really correct 
code?



##########
modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/CreateTableImpl.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.catalog.sql;
+
+import static java.util.Arrays.asList;
+import static 
org.apache.ignite.internal.catalog.sql.IndexColumnImpl.parseIndexColumnList;
+import static 
org.apache.ignite.internal.catalog.sql.QueryPartCollection.partsList;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import org.apache.ignite.catalog.ColumnSorted;
+import org.apache.ignite.catalog.ColumnType;
+import org.apache.ignite.catalog.IndexType;
+import org.apache.ignite.catalog.Options;
+import org.apache.ignite.sql.IgniteSql;
+
+class CreateTableImpl extends AbstractCatalogQuery {
+    private Name tableName;
+
+    private boolean ifNotExists;
+
+    private final List<Column> columns = new ArrayList<>();
+
+    private final List<Constraint> constraints = new ArrayList<>();
+
+    private final List<WithOption> withOptions = new ArrayList<>();
+
+    private Colocate colocate;
+
+    private final List<CreateIndexImpl> indexes = new ArrayList<>();
+
+    /**
+     * Constructor for internal usage.
+     *
+     * @see CreateFromAnnotationsImpl
+     */
+    CreateTableImpl(IgniteSql sql, Options options) {
+        super(sql, options);
+    }
+
+    CreateTableImpl name(String... names) {
+        Objects.requireNonNull(names, "Table name must not be null.");
+
+        this.tableName = new Name(names);
+        return this;
+    }
+
+    CreateTableImpl ifNotExists() {
+        this.ifNotExists = true;
+        return this;
+    }
+
+    CreateTableImpl addColumn(String name, String definition) {
+        Objects.requireNonNull(name, "Column name must not be null.");
+        Objects.requireNonNull(definition, "Column type must not be null.");
+
+        columns.add(new Column(name, definition));
+        return this;
+    }
+
+    CreateTableImpl addColumn(String name, ColumnType<?> type) {
+        Objects.requireNonNull(name, "Column name must not be null.");
+        Objects.requireNonNull(type, "Column type must not be null.");
+
+        columns.add(new Column(name, ColumnTypeImpl.wrap(type)));
+        return this;
+    }
+
+    CreateTableImpl primaryKey(String columnList) {
+        return primaryKey(IndexType.DEFAULT, columnList);
+    }
+
+    CreateTableImpl primaryKey(IndexType type, String columnList) {
+        return primaryKey(type, parseIndexColumnList(columnList));
+    }
+
+    CreateTableImpl primaryKey(IndexType type, List<ColumnSorted> columns) {
+        Objects.requireNonNull(columns, "PK columns must not be null.");
+
+        constraints.add(new Constraint().primaryKey(type, columns));
+        return this;
+    }
+
+    CreateTableImpl colocateBy(String columnList) {
+        return colocateBy(columnList.split("\\s*,\\s*"));

Review Comment:
   Extract Pattern here



##########
modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/CreateFromAnnotationsImpl.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.catalog.sql;
+
+import static org.apache.ignite.catalog.ColumnSorted.column;
+import static org.apache.ignite.internal.catalog.sql.QueryUtils.mapArrayToList;
+import static org.apache.ignite.table.mapper.Mapper.nativelySupported;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.catalog.ColumnSorted;
+import org.apache.ignite.catalog.ColumnType;
+import org.apache.ignite.catalog.DefaultZone;
+import org.apache.ignite.catalog.IndexType;
+import org.apache.ignite.catalog.Options;
+import org.apache.ignite.catalog.annotations.Column;
+import org.apache.ignite.catalog.annotations.ColumnRef;
+import org.apache.ignite.catalog.annotations.Id;
+import org.apache.ignite.catalog.annotations.Index;
+import org.apache.ignite.catalog.annotations.Table;
+import org.apache.ignite.catalog.annotations.Zone;
+import org.apache.ignite.sql.IgniteSql;
+
+class CreateFromAnnotationsImpl extends AbstractCatalogQuery {
+    private CreateZoneImpl createZone;
+
+    private CreateTableImpl createTable;
+
+    private IndexType pkType;
+
+    CreateFromAnnotationsImpl(IgniteSql sql, Options options) {
+        super(sql, options);
+    }
+
+    CreateFromAnnotationsImpl processKeyValueClasses(Class<?> keyClass, 
Class<?> valueClass) {
+        if (keyClass.getAnnotation(Table.class) == null && 
valueClass.getAnnotation(Table.class) == null) {

Review Comment:
   If contains && but error message contains "or". What is correct?



##########
modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/IgniteCatalogSqlImpl.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.catalog.sql;
+
+import org.apache.ignite.catalog.IgniteCatalog;
+import org.apache.ignite.catalog.Options;
+import org.apache.ignite.catalog.Query;
+import org.apache.ignite.sql.IgniteSql;
+
+/**
+ * Implementation of the catalog.
+ */
+public class IgniteCatalogSqlImpl implements IgniteCatalog {
+    private final IgniteSql sql;
+
+    private final Options options;
+
+    public IgniteCatalogSqlImpl(IgniteSql sql, Options options) {
+        this.options = options;
+        this.sql = sql;
+    }
+
+    @Override
+    public Query create(Class<?> keyClass, Class<?> valueClass) {
+        return new CreateFromAnnotationsImpl(sql, 
options).processKeyValueClasses(keyClass, valueClass);

Review Comment:
   Why we allocate CreateFromAnnotationsImpl each invocation?



##########
modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/CreateTableImpl.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.catalog.sql;
+
+import static java.util.Arrays.asList;
+import static 
org.apache.ignite.internal.catalog.sql.IndexColumnImpl.parseIndexColumnList;
+import static 
org.apache.ignite.internal.catalog.sql.QueryPartCollection.partsList;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import org.apache.ignite.catalog.ColumnSorted;
+import org.apache.ignite.catalog.ColumnType;
+import org.apache.ignite.catalog.IndexType;
+import org.apache.ignite.catalog.Options;
+import org.apache.ignite.sql.IgniteSql;
+
+class CreateTableImpl extends AbstractCatalogQuery {
+    private Name tableName;
+
+    private boolean ifNotExists;
+
+    private final List<Column> columns = new ArrayList<>();
+
+    private final List<Constraint> constraints = new ArrayList<>();
+
+    private final List<WithOption> withOptions = new ArrayList<>();
+
+    private Colocate colocate;
+
+    private final List<CreateIndexImpl> indexes = new ArrayList<>();
+
+    /**
+     * Constructor for internal usage.
+     *
+     * @see CreateFromAnnotationsImpl
+     */
+    CreateTableImpl(IgniteSql sql, Options options) {
+        super(sql, options);
+    }
+
+    CreateTableImpl name(String... names) {
+        Objects.requireNonNull(names, "Table name must not be null.");

Review Comment:
   A lot of places where you have this check before Name constructor. Maybe 
just move it to constructor?



##########
modules/catalog-dsl/src/main/java/org/apache/ignite/internal/catalog/sql/CreateFromAnnotationsImpl.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.catalog.sql;
+
+import static org.apache.ignite.catalog.ColumnSorted.column;
+import static org.apache.ignite.internal.catalog.sql.QueryUtils.mapArrayToList;
+import static org.apache.ignite.table.mapper.Mapper.nativelySupported;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.ignite.catalog.ColumnSorted;
+import org.apache.ignite.catalog.ColumnType;
+import org.apache.ignite.catalog.DefaultZone;
+import org.apache.ignite.catalog.IndexType;
+import org.apache.ignite.catalog.Options;
+import org.apache.ignite.catalog.annotations.Column;
+import org.apache.ignite.catalog.annotations.ColumnRef;
+import org.apache.ignite.catalog.annotations.Id;
+import org.apache.ignite.catalog.annotations.Index;
+import org.apache.ignite.catalog.annotations.Table;
+import org.apache.ignite.catalog.annotations.Zone;
+import org.apache.ignite.sql.IgniteSql;
+
+class CreateFromAnnotationsImpl extends AbstractCatalogQuery {
+    private CreateZoneImpl createZone;
+
+    private CreateTableImpl createTable;
+
+    private IndexType pkType;
+
+    CreateFromAnnotationsImpl(IgniteSql sql, Options options) {
+        super(sql, options);
+    }
+
+    CreateFromAnnotationsImpl processKeyValueClasses(Class<?> keyClass, 
Class<?> valueClass) {
+        if (keyClass.getAnnotation(Table.class) == null && 
valueClass.getAnnotation(Table.class) == null) {
+            throw new IllegalArgumentException(
+                    "Cannot find @Table annotation on " + keyClass.getName() + 
" or " + valueClass.getName()
+                            + ". At least one of these classes must be 
annotated in order to create a query object."
+            );
+        }
+
+        processAnnotations(keyClass, true);
+        processAnnotations(valueClass, false);
+        return this;
+    }
+
+    CreateFromAnnotationsImpl processRecordClass(Class<?> recordCls) {
+        if (recordCls.getAnnotation(Table.class) == null) {
+            throw new IllegalArgumentException("Cannot find @Table annotation 
on " + recordCls.getName()
+                    + ". This class must be annotated with in order to create 
a query object.");
+        }
+
+        processAnnotations(recordCls, true);
+        return this;
+    }
+
+    @Override
+    protected void accept(QueryContext ctx) {
+        if (createZone != null) {
+            ctx.visit(createZone).formatSeparator();
+        }
+        if (createTable != null) {
+            ctx.visit(createTable).formatSeparator();
+        }
+    }
+
+    private void processAnnotations(Class<?> clazz, boolean isKeyClass) {
+        if (createTable == null) {
+            createTable = new CreateTableImpl(sql, options).ifNotExists();
+        }
+
+        Table table = clazz.getAnnotation(Table.class);
+        if (table != null) {
+            createTable.name(table.value().isEmpty() ? clazz.getSimpleName() : 
table.value());
+
+            processZone(table);
+            processTable(table);
+        }
+
+        processColumns(createTable, pkType, clazz, isKeyClass);
+    }
+
+    private void processZone(Table table) {
+        Class<?> zoneRef = table.zone();
+        if (zoneRef == DefaultZone.class) {
+            return;
+        }
+        Zone zone = zoneRef.getAnnotation(Zone.class);
+        if (zone != null) {
+            createZone = new CreateZoneImpl(sql, options).ifNotExists();
+
+            String zoneName = zone.value().isEmpty() ? zoneRef.getSimpleName() 
: zone.value();
+            createTable.zone(zoneName);
+            createZone.name(zoneName);
+            createZone.engine(zone.engine());
+            if (zone.partitions() > 0) {
+                createZone.partitions(zone.partitions());
+            }
+            if (zone.replicas() > 0) {
+                createZone.replicas(zone.replicas());
+            }
+
+            // TODO https://issues.apache.org/jira/browse/IGNITE-21428
+        }
+    }
+
+    private void processTable(Table table) {
+        for (Index ix : table.indexes()) {
+            List<ColumnSorted> indexColumns = mapArrayToList(ix.columns(), col 
-> column(col.value(), col.sort()));
+            String name = toIndexName(ix);
+            createTable.addIndex(name, ix.type(), indexColumns);
+        }
+
+        ColumnRef[] colocateBy = table.colocateBy();
+        if (colocateBy != null && colocateBy.length > 0) {
+            createTable.colocateBy(mapArrayToList(colocateBy, 
ColumnRef::value));
+        }
+
+        pkType = table.primaryKeyType();
+    }
+
+    private static String toIndexName(Index ix) {
+        if (!ix.value().isEmpty()) {
+            return ix.value();
+        }
+        List<String> list = new ArrayList<>();
+        list.add("ix");
+        for (ColumnRef columnRef : ix.columns()) {
+            list.add(columnRef.value());
+        }
+        return String.join("_", list);
+    }
+
+    static void processColumns(CreateTableImpl createTable, IndexType pkType, 
Class<?> clazz, boolean isKeyClass) {

Review Comment:
   Can be private



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to