You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by wuchong <gi...@git.apache.org> on 2016/12/08 14:54:20 UTC

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

GitHub user wuchong opened a pull request:

    https://github.com/apache/flink/pull/2968

    [FLINK-5187] [core] Create analog of Row and RowTypeInfo and RowCompator in core

    Thanks for contributing to Apache Flink. Before you open your pull request, please take the following check list into consideration.
    If your changes take all of the items into account, feel free to open your pull request. For more information and/or questions please refer to the [How To Contribute guide](http://flink.apache.org/how-to-contribute.html).
    In addition to going through the list, please provide a meaningful description of your changes.
    
    - [ ] General
      - The pull request references the related JIRA issue ("[FLINK-XXX] Jira title text")
      - The pull request addresses only one issue
      - Each commit in the PR has a meaningful commit message (including the JIRA id)
    
    - [ ] Documentation
      - Documentation has been added for new functionality
      - Old documentation affected by the pull request has been updated
      - JavaDoc for public methods has been added
    
    - [ ] Tests & Build
      - Functionality added by the pull request is covered by tests
      - `mvn clean verify` has been executed successfully locally or a Travis build has passed
    


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/wuchong/flink row-FLINK-5187

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/flink/pull/2968.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #2968
    
----
commit 6b3c98c43965d2adbed20019cf0e22b512ee7744
Author: Jark Wu <wu...@alibaba-inc.com>
Date:   2016-12-08T14:44:29Z

    [FLINK-5187] [core] Create analog of Row and RowTypeInfo and RowComparator in core

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91754033
  
    --- Diff: docs/dev/types_serialization.md ---
    @@ -92,6 +92,8 @@ Internally, Flink makes the following distinctions between types:
     
       * POJOs: classes that follow a certain bean-like pattern
     
    +  * Row (unlimited length of fields)
    --- End diff --
    
    Tuples with arbitrary number of fields and support for null fields.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91652189
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,698 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    --- End diff --
    
    This constructor is used in `duplicate` which I don't want to generate the Tuple4 again.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91948218
  
    --- Diff: flink-core/src/main/java/org/apache/flink/types/Row.java ---
    @@ -0,0 +1,110 @@
    +/*
    + * 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.flink.types;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +
    +import java.io.Serializable;
    +import java.util.Arrays;
    +
    +/**
    + * A Row has no limited length and contain a set of fields, which may all be different types.
    + * Because Row is not strongly typed, Flink's type extraction mechanism can't extract correct field
    + * types. So that users should manually tell Flink the type information via creating a
    + * {@link RowTypeInfo}.
    + *
    + * <p>
    + * The fields in the Row may be accessed by position (zero-based) {@link #getField(int)}. And can
    + * set fields by {@link #setField(int, Object)}.
    + * <p>
    + * Row is in principle serializable. However, it may contain non-serializable fields,
    + * in which case serialization will fail.
    + *
    + */
    +@PublicEvolving
    +public class Row implements Serializable{
    +
    +	private static final long serialVersionUID = 1L;
    +
    +	/** The array to store actual values. */
    +	private final Object[] fields;
    +
    +	/**
    +	 * Create a new Row instance.
    +	 * @param arity The number of field in the Row
    +	 */
    +	public Row(int arity) {
    +		this.fields = new Object[arity];
    +	}
    +
    +	/**
    +	 * Get the number of field in the Row.
    +	 * @return The number of field in the Row.
    +	 */
    +	public int getArity() {
    +		return fields.length;
    +	}
    +
    +	/**
    +	 * Gets the field at the specified position.
    +	 * @param pos The position of the field, 0-based.
    +	 * @return The field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public Object getField(int pos) {
    +		return fields[pos];
    +	}
    +
    +	/**
    +	 * Sets the field at the specified position.
    +	 *
    +	 * @param pos The position of the field, 0-based.
    +	 * @param value The value to be assigned to the field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public void setField(int pos, Object value) {
    +		fields[pos] = value;
    +	}
    +
    +	@Override
    +	public String toString() {
    +		return Arrays.deepToString(fields);
    +	}
    +
    +	@Override
    +	public boolean equals(Object o) {
    +		if (this == o) {
    +			return true;
    +		}
    +		if (o == null || getClass() != o.getClass()) {
    +			return false;
    +		}
    +
    +		Row row = (Row) o;
    +
    +		// Probably incorrect - comparing Object[] arrays with Arrays.equals
    --- End diff --
    
    What do you mean by this comment?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91949754
  
    --- Diff: flink-core/src/test/java/org/apache/flink/api/common/typeutils/base/RowComparatorWithManyFieldsTests.java ---
    @@ -0,0 +1,103 @@
    +/*
    + * 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.flink.api.common.typeutils.base;
    +
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.ComparatorTestBase;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.types.Row;
    +import org.junit.Before;
    +
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +import static org.apache.flink.util.Preconditions.checkNotNull;
    +import static org.junit.Assert.assertEquals;
    +
    +/**
    + * Tests {@link RowComparator} for wide rows.
    + */
    +public class RowComparatorWithManyFieldsTests extends ComparatorTestBase<Row> {
    +
    +	private final int numberOfFields = 10;
    +	private RowTypeInfo typeInfo;
    +	private final Row[] data = new Row[]{
    +		createRow(null, "b0", "c0", "d0", "e0", "f0", "g0", "h0", "i0", "j0"),
    +		createRow("a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1", "i1", "j1"),
    +		createRow("a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2", "i2", "j2"),
    +		createRow("a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3", "i3", "j3")
    +	};
    +
    +	@Before
    --- End diff --
    
    Can be `@BeforeClass`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91902644
  
    --- Diff: flink-core/src/main/java/org/apache/flink/types/Row.java ---
    @@ -0,0 +1,110 @@
    +/*
    + * 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.flink.types;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +
    +import java.io.Serializable;
    +import java.util.Arrays;
    +
    +/**
    + * A Row has no limited length and contain a set of fields, which may all be different types.
    + * Because Row is not strongly typed, Flink's type extraction mechanism can't extract correct field
    + * types. So that users should manually tell Flink the type information via creating a
    + * {@link RowTypeInfo}.
    + *
    + * <p>
    + * The fields in the Row may be accessed by position (zero-based) {@link #getField(int)}. And can
    + * set fields by {@link #setField(int, Object)}.
    + * <p>
    + * Row is in principle serializable. However, it may contain non-serializable fields,
    + * in which case serialization will fail.
    + *
    + */
    +@PublicEvolving
    +public class Row implements Serializable{
    +
    +	private static final long serialVersionUID = 1L;
    +
    +	/** The array to store actual values. */
    +	private final Object[] fields;
    +
    +	/**
    +	 * Create a new Row instance.
    +	 * @param arity The number of field in the Row
    +	 */
    +	public Row(int arity) {
    +		this.fields = new Object[arity];
    +	}
    +
    +	/**
    +	 * Get the number of field in the Row.
    +	 * @return The number of field in the Row.
    +	 */
    +	public int getArity() {
    +		return fields.length;
    +	}
    +
    +	/**
    +	 * Gets the field at the specified position.
    +	 * @param pos The position of the field, 0-based.
    +	 * @return The field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public Object getField(int pos) {
    +		return fields[pos];
    +	}
    +
    +	/**
    +	 * Sets the field at the specified position.
    +	 *
    +	 * @param pos The position of the field, 0-based.
    +	 * @param value The value to be assigned to the field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public void setField(int pos, Object value) {
    +		fields[pos] = value;
    +	}
    +
    +	@Override
    +	public String toString() {
    +		return Arrays.deepToString(fields);
    --- End diff --
    
    Which "Row test" do you mean ? 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91758061
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,176 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    +		super(Row.class, types);
    +
    +		this.fieldNames = new String[types.length];
    +
    +		for (int i = 0; i < types.length; i++) {
    +			fieldNames[i] = "f" + i;
    --- End diff --
    
    Actually, we had custom field names before and removed them at some point. 
    It might make sense to add support for optional names again now that `Row` is becoming more user-facing. However, I think we can do this as a separate issue.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91941722
  
    --- Diff: flink-core/src/main/java/org/apache/flink/types/Row.java ---
    @@ -0,0 +1,110 @@
    +/*
    + * 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.flink.types;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +
    +import java.io.Serializable;
    +import java.util.Arrays;
    +
    +/**
    + * A Row has no limited length and contain a set of fields, which may all be different types.
    + * Because Row is not strongly typed, Flink's type extraction mechanism can't extract correct field
    + * types. So that users should manually tell Flink the type information via creating a
    + * {@link RowTypeInfo}.
    + *
    + * <p>
    + * The fields in the Row may be accessed by position (zero-based) {@link #getField(int)}. And can
    + * set fields by {@link #setField(int, Object)}.
    + * <p>
    + * Row is in principle serializable. However, it may contain non-serializable fields,
    + * in which case serialization will fail.
    + *
    + */
    +@PublicEvolving
    +public class Row implements Serializable{
    +
    +	private static final long serialVersionUID = 1L;
    +
    +	/** The array to store actual values. */
    +	private final Object[] fields;
    +
    +	/**
    +	 * Create a new Row instance.
    +	 * @param arity The number of field in the Row
    +	 */
    +	public Row(int arity) {
    +		this.fields = new Object[arity];
    +	}
    +
    +	/**
    +	 * Get the number of field in the Row.
    +	 * @return The number of field in the Row.
    +	 */
    +	public int getArity() {
    +		return fields.length;
    +	}
    +
    +	/**
    +	 * Gets the field at the specified position.
    +	 * @param pos The position of the field, 0-based.
    +	 * @return The field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public Object getField(int pos) {
    +		return fields[pos];
    +	}
    +
    +	/**
    +	 * Sets the field at the specified position.
    +	 *
    +	 * @param pos The position of the field, 0-based.
    +	 * @param value The value to be assigned to the field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public void setField(int pos, Object value) {
    +		fields[pos] = value;
    +	}
    +
    +	@Override
    +	public String toString() {
    +		return Arrays.deepToString(fields);
    --- End diff --
    
    For example `org.apache.flink.api.scala.stream.table.AggregationsITCase` or `org.apache.flink.api.scala.batch.table.JoinITCase`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91721721
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,202 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    --- End diff --
    
    And when we set `Array[TypeInformation[_]]` in constructor from scala it has conflict with `TypeInformation<?>...`, so I propose cast it `.toList `.
    What do you think?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91946585
  
    --- Diff: flink-core/src/main/java/org/apache/flink/types/Row.java ---
    @@ -0,0 +1,110 @@
    +/*
    + * 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.flink.types;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +
    +import java.io.Serializable;
    +import java.util.Arrays;
    +
    +/**
    + * A Row has no limited length and contain a set of fields, which may all be different types.
    + * Because Row is not strongly typed, Flink's type extraction mechanism can't extract correct field
    + * types. So that users should manually tell Flink the type information via creating a
    + * {@link RowTypeInfo}.
    + *
    + * <p>
    + * The fields in the Row may be accessed by position (zero-based) {@link #getField(int)}. And can
    + * set fields by {@link #setField(int, Object)}.
    + * <p>
    + * Row is in principle serializable. However, it may contain non-serializable fields,
    + * in which case serialization will fail.
    + *
    + */
    +@PublicEvolving
    +public class Row implements Serializable{
    +
    +	private static final long serialVersionUID = 1L;
    +
    +	/** The array to store actual values. */
    +	private final Object[] fields;
    +
    +	/**
    +	 * Create a new Row instance.
    +	 * @param arity The number of field in the Row
    +	 */
    +	public Row(int arity) {
    +		this.fields = new Object[arity];
    +	}
    +
    +	/**
    +	 * Get the number of field in the Row.
    --- End diff --
    
    field -> fields


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    Hi @tonycox , thanks for your reviewing. I addressed some of your comments.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91927160
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,695 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		int[] normalizedKeyLengths,
    +		int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey) {
    +		this.arity = arity;
    +		this.keyPositions = keyPositions;
    +		this.comparators = comparators;
    +		this.serializers = serializers;
    +		this.normalizedKeyLengths = normalizedKeyLengths;
    +		this.numLeadingNormalizableKeys = numLeadingNormalizableKeys;
    +		this.normalizableKeyPrefixLen = normalizableKeyPrefixLen;
    +		this.invertNormKey = invertNormKey;
    +		this.nullMask1 = new boolean[arity];
    +		this.nullMask2 = new boolean[arity];
    +		deserializedKeyFields1 = instantiateDeserializationFields();
    +		deserializedKeyFields2 = instantiateDeserializationFields();
    +	}
    +
    +	// --------------------------------------------------------------------------------------------
    +	//  Comparator Methods
    +	// --------------------------------------------------------------------------------------------
    +
    +	@Override
    +	public void getFlatComparator(List<TypeComparator> flatComparators) {
    +		for (NullAwareComparator<Object> c : comparators) {
    +			Collections.addAll(flatComparators, c.getFlatComparators());
    +		}
    +	}
    +
    +	@Override
    +	public int hash(Row record) {
    +		int code = 0;
    +		int i = 0;
    +
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				code *= TupleComparatorBase.HASH_SALT[i & 0x1F];
    +				Object element = record.getField(keyPositions[i]); // element can be null
    +				code += comparators[i].hash(element);
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +
    +		return code;
    +	}
    +
    +	@Override
    +	public void setReference(Row toCompare) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = toCompare.getField(keyPositions[i]);
    +				comparator.setReference(element);   // element can be null
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +	}
    +
    +	@Override
    +	public boolean equalToReference(Row candidate) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = candidate.getField(keyPositions[i]);   // element can be null
    +				// check if reference is not equal
    +				if (!comparator.equalToReference(element)) {
    +					return false;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return true;
    +	}
    +
    +	@Override
    +	public int compareToReference(TypeComparator<Row> referencedComparator) {
    +		RowComparator other = (RowComparator) referencedComparator;
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int cmp = comparators[i].compareToReference(other.comparators[i]);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compare(Row first, Row second) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int keyPos = keyPositions[i];
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object firstElement = first.getField(keyPos);   // element can be null
    +				Object secondElement = second.getField(keyPos); // element can be null
    +
    +				int cmp = comparator.compare(firstElement, secondElement);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compareSerialized(
    +		DataInputView firstSource,
    +		DataInputView secondSource) throws IOException {
    +
    +		int len = serializers.length;
    +		int keyLen = keyPositions.length;
    +
    +		readIntoNullMask(arity, firstSource, nullMask1);
    +		readIntoNullMask(arity, secondSource, nullMask2);
    +
    +		// deserialize
    +		for (int i = 0; i < len; i++) {
    +			TypeSerializer<Object> serializer = serializers[i];
    +
    +			// deserialize field 1
    +			if (!nullMask1[i]) {
    +				deserializedKeyFields1[i] = serializer.deserialize(
    +					deserializedKeyFields1[i],
    +					firstSource);
    +			}
    +
    +			// deserialize field 2
    +			if (!nullMask2[i]) {
    +				deserializedKeyFields2[i] = serializer.deserialize(
    +					deserializedKeyFields2[i],
    +					secondSource);
    +			}
    +		}
    +
    +		// compare
    +		for (int i = 0; i < keyLen; i++) {
    +			int keyPos = keyPositions[i];
    +			TypeComparator<Object> comparator = comparators[i];
    +
    +			boolean isNull1 = nullMask1[keyPos];
    +			boolean isNull2 = nullMask2[keyPos];
    +
    +			int cmp = 0;
    +			// both values are null -> equality
    +			if (isNull1 && isNull2) {
    +				cmp = 0;
    +			}
    +			// first value is null -> inequality
    +			else if (isNull1) {
    +				cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
    +			}
    +			// second value is null -> inequality
    +			else if (isNull2) {
    +				cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
    +			}
    +			// no null values
    +			else {
    +				cmp = comparator.compare(
    +					deserializedKeyFields1[keyPos],
    +					deserializedKeyFields2[keyPos]);
    +			}
    +
    +			if (cmp != 0) {
    +				return cmp;
    +			}
    +		}
    +
    +		return 0;
    +	}
    +
    +	@Override
    +	public boolean supportsNormalizedKey() {
    +		return numLeadingNormalizableKeys > 0;
    +	}
    +
    +	@Override
    +	public boolean supportsSerializationWithKeyNormalization() {
    +		return false;
    +	}
    +
    +	@Override
    +	public int getNormalizeKeyLen() {
    +		return normalizableKeyPrefixLen;
    +	}
    +
    +	@Override
    +	public boolean isNormalizedKeyPrefixOnly(int keyBytes) {
    +		return numLeadingNormalizableKeys < keyPositions.length ||
    +				normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    +				normalizableKeyPrefixLen > keyBytes;
    +	}
    +
    +	@Override
    +	public void putNormalizedKey(
    +		Row record, MemorySegment target, int offset, int numBytes) {
    --- End diff --
    
    Inconsistent formatting. Either all args in same line with method name or each separate.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    @tonycox @wuchong Yes, we can move `RowCsvInputFormat` in FLINK-5188. But that issue should also be included in the 1.2 release. @tonycox, can you start with FLINK-5188 and base it on the changes of this PR? We should try to get this done as soon as possible. Thanks!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    Hi @tonycox , I agree we should move `RowCsvInputFormat` also. But I think it would be better to do that in another JIRA like FLINK-5188. Let's keep this PR simple so that it can be in before 1.2 release . What do you think ? 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/flink/pull/2968


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91547968
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,698 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		int[] normalizedKeyLengths,
    +		int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey) {
    +		this.arity = arity;
    +		this.keyPositions = keyPositions;
    +		this.comparators = comparators;
    +		this.serializers = serializers;
    +		this.normalizedKeyLengths = normalizedKeyLengths;
    +		this.numLeadingNormalizableKeys = numLeadingNormalizableKeys;
    +		this.normalizableKeyPrefixLen = normalizableKeyPrefixLen;
    +		this.invertNormKey = invertNormKey;
    +		this.nullMask1 = new boolean[arity];
    +		this.nullMask2 = new boolean[arity];
    +		deserializedKeyFields1 = instantiateDeserializationFields();
    +		deserializedKeyFields2 = instantiateDeserializationFields();
    +	}
    +
    +	// --------------------------------------------------------------------------------------------
    +	//  Comparator Methods
    +	// --------------------------------------------------------------------------------------------
    +
    +	@Override
    +	public void getFlatComparator(List<TypeComparator> flatComparators) {
    +		for (NullAwareComparator<Object> c : comparators) {
    +			Collections.addAll(flatComparators, c.getFlatComparators());
    +		}
    +	}
    +
    +	@Override
    +	public int hash(Row record) {
    +		int code = 0;
    +		int i = 0;
    +
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				code *= TupleComparatorBase.HASH_SALT[i & 0x1F];
    +				Object element = record.getField(keyPositions[i]); // element can be null
    +				code += comparators[i].hash(element);
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +
    +		return code;
    +	}
    +
    +	@Override
    +	public void setReference(Row toCompare) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = toCompare.getField(keyPositions[i]);
    +				comparator.setReference(element);   // element can be null
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +	}
    +
    +	@Override
    +	public boolean equalToReference(Row candidate) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = candidate.getField(keyPositions[i]);   // element can be null
    +				// check if reference is not equal
    +				if (!comparator.equalToReference(element)) {
    +					return false;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return true;
    +	}
    +
    +	@Override
    +	public int compareToReference(TypeComparator<Row> referencedComparator) {
    +		RowComparator other = (RowComparator) referencedComparator;
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int cmp = comparators[i].compareToReference(other.comparators[i]);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compare(Row first, Row second) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int keyPos = keyPositions[i];
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object firstElement = first.getField(keyPos);   // element can be null
    +				Object secondElement = second.getField(keyPos); // element can be null
    +
    +				int cmp = comparator.compare(firstElement, secondElement);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compareSerialized(
    +		DataInputView firstSource,
    +		DataInputView secondSource) throws IOException {
    +
    +		int len = serializers.length;
    +		int keyLen = keyPositions.length;
    +
    +		readIntoNullMask(arity, firstSource, nullMask1);
    +		readIntoNullMask(arity, secondSource, nullMask2);
    +
    +		// deserialize
    +		for (int i = 0; i < len; i++) {
    +			TypeSerializer<Object> serializer = serializers[i];
    +
    +			// deserialize field 1
    +			if (!nullMask1[i]) {
    +				deserializedKeyFields1[i] = serializer.deserialize(
    +					deserializedKeyFields1[i],
    +					firstSource);
    +			}
    +
    +			// deserialize field 2
    +			if (!nullMask2[i]) {
    +				deserializedKeyFields2[i] = serializer.deserialize(
    +					deserializedKeyFields2[i],
    +					secondSource);
    +			}
    +		}
    +
    +		// compare
    +		for (int i = 0; i < keyLen; i++) {
    +			int keyPos = keyPositions[i];
    +			TypeComparator<Object> comparator = comparators[i];
    +
    +			boolean isNull1 = nullMask1[keyPos];
    +			boolean isNull2 = nullMask2[keyPos];
    +
    +			int cmp = 0;
    +			// both values are null -> equality
    +			if (isNull1 && isNull2) {
    +				cmp = 0;
    +			}
    +			// first value is null -> inequality
    +			else if (isNull1) {
    +				cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
    +			}
    +			// second value is null -> inequality
    +			else if (isNull2) {
    +				cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
    +			}
    +			// no null values
    +			else {
    +				cmp = comparator.compare(
    +					deserializedKeyFields1[keyPos],
    +					deserializedKeyFields2[keyPos]);
    +			}
    +
    +			if (cmp != 0) {
    +				return cmp;
    +			}
    +		}
    +
    +		return 0;
    +	}
    +
    +	@Override
    +	public boolean supportsNormalizedKey() {
    +		return numLeadingNormalizableKeys > 0;
    +	}
    +
    +	@Override
    +	public boolean supportsSerializationWithKeyNormalization() {
    +		return false;
    +	}
    +
    +	@Override
    +	public int getNormalizeKeyLen() {
    +		return normalizableKeyPrefixLen;
    +	}
    +
    +	@Override
    +	public boolean isNormalizedKeyPrefixOnly(int keyBytes) {
    +		return numLeadingNormalizableKeys < keyPositions.length ||
    +				normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    +				normalizableKeyPrefixLen > keyBytes;
    +	}
    +
    +	@Override
    +	public void putNormalizedKey(
    +		Row record, MemorySegment target, int offset, int numBytes) {
    +		int bytesLeft = numBytes;
    +		int currentOffset = offset;
    +
    +		for (int i=0;i<numLeadingNormalizableKeys && bytesLeft > 0;i++){
    +			int len = normalizedKeyLengths[i];
    +			len = bytesLeft >= len ? len : bytesLeft;
    +
    +			TypeComparator<Object> comparator = comparators[i];
    +			Object element = record.getField(keyPositions[i]);  // element can be null
    +			// write key
    +			comparator.putNormalizedKey(element, target, currentOffset, len);
    +
    +			bytesLeft -= len;
    +			currentOffset += len;
    +		}
    +
    +	}
    +
    +	@Override
    +	public void writeWithKeyNormalization(
    +		Row record,
    +		DataOutputView target) throws IOException {
    +		throw new UnsupportedOperationException(
    +			"Record serialization with leading normalized keys not supported.");
    +
    +	}
    +
    +	@Override
    +	public Row readWithKeyDenormalization(Row reuse, DataInputView source) throws IOException {
    +		throw new UnsupportedOperationException(
    +			"Record deserialization with leading normalized keys not supported.");
    +	}
    +
    +	@Override
    +	public boolean invertNormalizedKey() {
    +		return invertNormKey;
    +	}
    +
    +	@Override
    +	public TypeComparator<Row> duplicate() {
    +		NullAwareComparator<?>[] comparatorsCopy = new NullAwareComparator<?>[comparators.length];
    +		for (int i = 0; i < comparators.length; i++) {
    +			comparatorsCopy[i] = (NullAwareComparator<?>) comparators[i].duplicate();
    +		}
    +
    +		TypeSerializer<?>[] serializersCopy = new TypeSerializer<?>[serializers.length];
    +		for (int i = 0; i < serializers.length; i++) {
    +			serializersCopy[i] = serializers[i].duplicate();
    +		}
    +
    +		return new RowComparator(
    +			arity,
    +			keyPositions,
    +			(NullAwareComparator<Object>[]) comparatorsCopy,
    +			(TypeSerializer<Object>[]) serializersCopy,
    +			normalizedKeyLengths,
    +			numLeadingNormalizableKeys,
    +			normalizableKeyPrefixLen,
    +			invertNormKey);
    +	}
    +
    +	@Override
    +	public int extractKeys(Object record, Object[] target, int index) {
    +		int len = comparators.length;
    +		int localIndex = index;
    +		for(int i=0;i<len;i++){
    --- End diff --
    
    Please, add some spaces


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91652223
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,698 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		int[] normalizedKeyLengths,
    +		int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey) {
    +		this.arity = arity;
    +		this.keyPositions = keyPositions;
    +		this.comparators = comparators;
    +		this.serializers = serializers;
    +		this.normalizedKeyLengths = normalizedKeyLengths;
    +		this.numLeadingNormalizableKeys = numLeadingNormalizableKeys;
    +		this.normalizableKeyPrefixLen = normalizableKeyPrefixLen;
    +		this.invertNormKey = invertNormKey;
    +		this.nullMask1 = new boolean[arity];
    +		this.nullMask2 = new boolean[arity];
    +		deserializedKeyFields1 = instantiateDeserializationFields();
    +		deserializedKeyFields2 = instantiateDeserializationFields();
    +	}
    +
    +	// --------------------------------------------------------------------------------------------
    +	//  Comparator Methods
    +	// --------------------------------------------------------------------------------------------
    +
    +	@Override
    +	public void getFlatComparator(List<TypeComparator> flatComparators) {
    +		for (NullAwareComparator<Object> c : comparators) {
    +			Collections.addAll(flatComparators, c.getFlatComparators());
    +		}
    +	}
    +
    +	@Override
    +	public int hash(Row record) {
    +		int code = 0;
    +		int i = 0;
    +
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				code *= TupleComparatorBase.HASH_SALT[i & 0x1F];
    +				Object element = record.getField(keyPositions[i]); // element can be null
    +				code += comparators[i].hash(element);
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +
    +		return code;
    +	}
    +
    +	@Override
    +	public void setReference(Row toCompare) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = toCompare.getField(keyPositions[i]);
    +				comparator.setReference(element);   // element can be null
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +	}
    +
    +	@Override
    +	public boolean equalToReference(Row candidate) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = candidate.getField(keyPositions[i]);   // element can be null
    +				// check if reference is not equal
    +				if (!comparator.equalToReference(element)) {
    +					return false;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return true;
    +	}
    +
    +	@Override
    +	public int compareToReference(TypeComparator<Row> referencedComparator) {
    +		RowComparator other = (RowComparator) referencedComparator;
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int cmp = comparators[i].compareToReference(other.comparators[i]);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compare(Row first, Row second) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int keyPos = keyPositions[i];
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object firstElement = first.getField(keyPos);   // element can be null
    +				Object secondElement = second.getField(keyPos); // element can be null
    +
    +				int cmp = comparator.compare(firstElement, secondElement);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compareSerialized(
    +		DataInputView firstSource,
    +		DataInputView secondSource) throws IOException {
    +
    +		int len = serializers.length;
    +		int keyLen = keyPositions.length;
    +
    +		readIntoNullMask(arity, firstSource, nullMask1);
    +		readIntoNullMask(arity, secondSource, nullMask2);
    +
    +		// deserialize
    +		for (int i = 0; i < len; i++) {
    +			TypeSerializer<Object> serializer = serializers[i];
    +
    +			// deserialize field 1
    +			if (!nullMask1[i]) {
    +				deserializedKeyFields1[i] = serializer.deserialize(
    +					deserializedKeyFields1[i],
    +					firstSource);
    +			}
    +
    +			// deserialize field 2
    +			if (!nullMask2[i]) {
    +				deserializedKeyFields2[i] = serializer.deserialize(
    +					deserializedKeyFields2[i],
    +					secondSource);
    +			}
    +		}
    +
    +		// compare
    +		for (int i = 0; i < keyLen; i++) {
    +			int keyPos = keyPositions[i];
    +			TypeComparator<Object> comparator = comparators[i];
    +
    +			boolean isNull1 = nullMask1[keyPos];
    +			boolean isNull2 = nullMask2[keyPos];
    +
    +			int cmp = 0;
    +			// both values are null -> equality
    +			if (isNull1 && isNull2) {
    +				cmp = 0;
    +			}
    +			// first value is null -> inequality
    +			else if (isNull1) {
    +				cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
    +			}
    +			// second value is null -> inequality
    +			else if (isNull2) {
    +				cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
    +			}
    +			// no null values
    +			else {
    +				cmp = comparator.compare(
    +					deserializedKeyFields1[keyPos],
    +					deserializedKeyFields2[keyPos]);
    +			}
    +
    +			if (cmp != 0) {
    +				return cmp;
    +			}
    +		}
    +
    +		return 0;
    +	}
    +
    +	@Override
    +	public boolean supportsNormalizedKey() {
    +		return numLeadingNormalizableKeys > 0;
    +	}
    +
    +	@Override
    +	public boolean supportsSerializationWithKeyNormalization() {
    +		return false;
    +	}
    +
    +	@Override
    +	public int getNormalizeKeyLen() {
    +		return normalizableKeyPrefixLen;
    +	}
    +
    +	@Override
    +	public boolean isNormalizedKeyPrefixOnly(int keyBytes) {
    +		return numLeadingNormalizableKeys < keyPositions.length ||
    +				normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    +				normalizableKeyPrefixLen > keyBytes;
    +	}
    +
    +	@Override
    +	public void putNormalizedKey(
    +		Row record, MemorySegment target, int offset, int numBytes) {
    +		int bytesLeft = numBytes;
    +		int currentOffset = offset;
    +
    +		for (int i=0;i<numLeadingNormalizableKeys && bytesLeft > 0;i++){
    +			int len = normalizedKeyLengths[i];
    +			len = bytesLeft >= len ? len : bytesLeft;
    +
    +			TypeComparator<Object> comparator = comparators[i];
    +			Object element = record.getField(keyPositions[i]);  // element can be null
    +			// write key
    +			comparator.putNormalizedKey(element, target, currentOffset, len);
    +
    +			bytesLeft -= len;
    +			currentOffset += len;
    +		}
    +
    +	}
    +
    +	@Override
    +	public void writeWithKeyNormalization(
    +		Row record,
    +		DataOutputView target) throws IOException {
    +		throw new UnsupportedOperationException(
    +			"Record serialization with leading normalized keys not supported.");
    +
    +	}
    +
    +	@Override
    +	public Row readWithKeyDenormalization(Row reuse, DataInputView source) throws IOException {
    +		throw new UnsupportedOperationException(
    +			"Record deserialization with leading normalized keys not supported.");
    +	}
    +
    +	@Override
    +	public boolean invertNormalizedKey() {
    +		return invertNormKey;
    +	}
    +
    +	@Override
    +	public TypeComparator<Row> duplicate() {
    +		NullAwareComparator<?>[] comparatorsCopy = new NullAwareComparator<?>[comparators.length];
    +		for (int i = 0; i < comparators.length; i++) {
    +			comparatorsCopy[i] = (NullAwareComparator<?>) comparators[i].duplicate();
    +		}
    +
    +		TypeSerializer<?>[] serializersCopy = new TypeSerializer<?>[serializers.length];
    +		for (int i = 0; i < serializers.length; i++) {
    +			serializersCopy[i] = serializers[i].duplicate();
    +		}
    +
    +		return new RowComparator(
    +			arity,
    +			keyPositions,
    +			(NullAwareComparator<Object>[]) comparatorsCopy,
    +			(TypeSerializer<Object>[]) serializersCopy,
    +			normalizedKeyLengths,
    +			numLeadingNormalizableKeys,
    +			normalizableKeyPrefixLen,
    +			invertNormKey);
    +	}
    +
    +	@Override
    +	public int extractKeys(Object record, Object[] target, int index) {
    +		int len = comparators.length;
    +		int localIndex = index;
    +		for(int i=0;i<len;i++){
    +			Object element = ((Row) record).getField(keyPositions[i]);  // element can be null
    +			localIndex += comparators[i].extractKeys(element, target, localIndex);
    +		}
    +		return localIndex - index;
    +	}
    +
    +
    +	private Object[] instantiateDeserializationFields() {
    +		Object[] newFields = new Object[serializers.length];
    +		for (int i = 0; i < serializers.length; i++) {
    +			newFields[i] = serializers[i].createInstance();
    +		}
    +		return newFields;
    +	}
    +
    +	/**
    +	 * @return creates auxiliary fields for normalized key support
    +	 */
    +	private static Tuple4<int[], Integer, Integer, Boolean>
    --- End diff --
    
    Unfortunately, the generic type of Tuple can not be primitive types.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91548803
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,698 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		int[] normalizedKeyLengths,
    +		int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey) {
    +		this.arity = arity;
    +		this.keyPositions = keyPositions;
    +		this.comparators = comparators;
    +		this.serializers = serializers;
    +		this.normalizedKeyLengths = normalizedKeyLengths;
    +		this.numLeadingNormalizableKeys = numLeadingNormalizableKeys;
    +		this.normalizableKeyPrefixLen = normalizableKeyPrefixLen;
    +		this.invertNormKey = invertNormKey;
    +		this.nullMask1 = new boolean[arity];
    +		this.nullMask2 = new boolean[arity];
    +		deserializedKeyFields1 = instantiateDeserializationFields();
    +		deserializedKeyFields2 = instantiateDeserializationFields();
    +	}
    +
    +	// --------------------------------------------------------------------------------------------
    +	//  Comparator Methods
    +	// --------------------------------------------------------------------------------------------
    +
    +	@Override
    +	public void getFlatComparator(List<TypeComparator> flatComparators) {
    +		for (NullAwareComparator<Object> c : comparators) {
    +			Collections.addAll(flatComparators, c.getFlatComparators());
    +		}
    +	}
    +
    +	@Override
    +	public int hash(Row record) {
    +		int code = 0;
    +		int i = 0;
    +
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				code *= TupleComparatorBase.HASH_SALT[i & 0x1F];
    +				Object element = record.getField(keyPositions[i]); // element can be null
    +				code += comparators[i].hash(element);
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +
    +		return code;
    +	}
    +
    +	@Override
    +	public void setReference(Row toCompare) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = toCompare.getField(keyPositions[i]);
    +				comparator.setReference(element);   // element can be null
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +	}
    +
    +	@Override
    +	public boolean equalToReference(Row candidate) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = candidate.getField(keyPositions[i]);   // element can be null
    +				// check if reference is not equal
    +				if (!comparator.equalToReference(element)) {
    +					return false;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return true;
    +	}
    +
    +	@Override
    +	public int compareToReference(TypeComparator<Row> referencedComparator) {
    +		RowComparator other = (RowComparator) referencedComparator;
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int cmp = comparators[i].compareToReference(other.comparators[i]);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compare(Row first, Row second) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int keyPos = keyPositions[i];
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object firstElement = first.getField(keyPos);   // element can be null
    +				Object secondElement = second.getField(keyPos); // element can be null
    +
    +				int cmp = comparator.compare(firstElement, secondElement);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compareSerialized(
    +		DataInputView firstSource,
    +		DataInputView secondSource) throws IOException {
    +
    +		int len = serializers.length;
    +		int keyLen = keyPositions.length;
    +
    +		readIntoNullMask(arity, firstSource, nullMask1);
    +		readIntoNullMask(arity, secondSource, nullMask2);
    +
    +		// deserialize
    +		for (int i = 0; i < len; i++) {
    +			TypeSerializer<Object> serializer = serializers[i];
    +
    +			// deserialize field 1
    +			if (!nullMask1[i]) {
    +				deserializedKeyFields1[i] = serializer.deserialize(
    +					deserializedKeyFields1[i],
    +					firstSource);
    +			}
    +
    +			// deserialize field 2
    +			if (!nullMask2[i]) {
    +				deserializedKeyFields2[i] = serializer.deserialize(
    +					deserializedKeyFields2[i],
    +					secondSource);
    +			}
    +		}
    +
    +		// compare
    +		for (int i = 0; i < keyLen; i++) {
    +			int keyPos = keyPositions[i];
    +			TypeComparator<Object> comparator = comparators[i];
    +
    +			boolean isNull1 = nullMask1[keyPos];
    +			boolean isNull2 = nullMask2[keyPos];
    +
    +			int cmp = 0;
    +			// both values are null -> equality
    +			if (isNull1 && isNull2) {
    +				cmp = 0;
    +			}
    +			// first value is null -> inequality
    +			else if (isNull1) {
    +				cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
    +			}
    +			// second value is null -> inequality
    +			else if (isNull2) {
    +				cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
    +			}
    +			// no null values
    +			else {
    +				cmp = comparator.compare(
    +					deserializedKeyFields1[keyPos],
    +					deserializedKeyFields2[keyPos]);
    +			}
    +
    +			if (cmp != 0) {
    +				return cmp;
    +			}
    +		}
    +
    +		return 0;
    +	}
    +
    +	@Override
    +	public boolean supportsNormalizedKey() {
    +		return numLeadingNormalizableKeys > 0;
    +	}
    +
    +	@Override
    +	public boolean supportsSerializationWithKeyNormalization() {
    +		return false;
    +	}
    +
    +	@Override
    +	public int getNormalizeKeyLen() {
    +		return normalizableKeyPrefixLen;
    +	}
    +
    +	@Override
    +	public boolean isNormalizedKeyPrefixOnly(int keyBytes) {
    +		return numLeadingNormalizableKeys < keyPositions.length ||
    +				normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    +				normalizableKeyPrefixLen > keyBytes;
    +	}
    +
    +	@Override
    +	public void putNormalizedKey(
    +		Row record, MemorySegment target, int offset, int numBytes) {
    +		int bytesLeft = numBytes;
    +		int currentOffset = offset;
    +
    +		for (int i=0;i<numLeadingNormalizableKeys && bytesLeft > 0;i++){
    +			int len = normalizedKeyLengths[i];
    +			len = bytesLeft >= len ? len : bytesLeft;
    +
    +			TypeComparator<Object> comparator = comparators[i];
    +			Object element = record.getField(keyPositions[i]);  // element can be null
    +			// write key
    +			comparator.putNormalizedKey(element, target, currentOffset, len);
    +
    +			bytesLeft -= len;
    +			currentOffset += len;
    +		}
    +
    +	}
    +
    +	@Override
    +	public void writeWithKeyNormalization(
    +		Row record,
    +		DataOutputView target) throws IOException {
    +		throw new UnsupportedOperationException(
    +			"Record serialization with leading normalized keys not supported.");
    +
    +	}
    +
    +	@Override
    +	public Row readWithKeyDenormalization(Row reuse, DataInputView source) throws IOException {
    +		throw new UnsupportedOperationException(
    +			"Record deserialization with leading normalized keys not supported.");
    +	}
    +
    +	@Override
    +	public boolean invertNormalizedKey() {
    +		return invertNormKey;
    +	}
    +
    +	@Override
    +	public TypeComparator<Row> duplicate() {
    +		NullAwareComparator<?>[] comparatorsCopy = new NullAwareComparator<?>[comparators.length];
    +		for (int i = 0; i < comparators.length; i++) {
    +			comparatorsCopy[i] = (NullAwareComparator<?>) comparators[i].duplicate();
    +		}
    +
    +		TypeSerializer<?>[] serializersCopy = new TypeSerializer<?>[serializers.length];
    +		for (int i = 0; i < serializers.length; i++) {
    +			serializersCopy[i] = serializers[i].duplicate();
    +		}
    +
    +		return new RowComparator(
    +			arity,
    +			keyPositions,
    +			(NullAwareComparator<Object>[]) comparatorsCopy,
    +			(TypeSerializer<Object>[]) serializersCopy,
    +			normalizedKeyLengths,
    +			numLeadingNormalizableKeys,
    +			normalizableKeyPrefixLen,
    +			invertNormKey);
    +	}
    +
    +	@Override
    +	public int extractKeys(Object record, Object[] target, int index) {
    +		int len = comparators.length;
    +		int localIndex = index;
    +		for(int i=0;i<len;i++){
    +			Object element = ((Row) record).getField(keyPositions[i]);  // element can be null
    +			localIndex += comparators[i].extractKeys(element, target, localIndex);
    +		}
    +		return localIndex - index;
    +	}
    +
    +
    +	private Object[] instantiateDeserializationFields() {
    +		Object[] newFields = new Object[serializers.length];
    +		for (int i = 0; i < serializers.length; i++) {
    +			newFields[i] = serializers[i].createInstance();
    +		}
    +		return newFields;
    +	}
    +
    +	/**
    +	 * @return creates auxiliary fields for normalized key support
    +	 */
    +	private static Tuple4<int[], Integer, Integer, Boolean>
    --- End diff --
    
    I think it's the same as `<int[], int, int, boolean>`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    Hi @fhueske , thanks for reviewing. I have addressed the comments.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91953233
  
    --- Diff: flink-core/src/main/java/org/apache/flink/types/Row.java ---
    @@ -0,0 +1,110 @@
    +/*
    + * 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.flink.types;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +
    +import java.io.Serializable;
    +import java.util.Arrays;
    +
    +/**
    + * A Row has no limited length and contain a set of fields, which may all be different types.
    + * Because Row is not strongly typed, Flink's type extraction mechanism can't extract correct field
    + * types. So that users should manually tell Flink the type information via creating a
    + * {@link RowTypeInfo}.
    + *
    + * <p>
    + * The fields in the Row may be accessed by position (zero-based) {@link #getField(int)}. And can
    + * set fields by {@link #setField(int, Object)}.
    + * <p>
    + * Row is in principle serializable. However, it may contain non-serializable fields,
    + * in which case serialization will fail.
    + *
    + */
    +@PublicEvolving
    +public class Row implements Serializable{
    +
    +	private static final long serialVersionUID = 1L;
    +
    +	/** The array to store actual values. */
    +	private final Object[] fields;
    +
    +	/**
    +	 * Create a new Row instance.
    +	 * @param arity The number of field in the Row
    +	 */
    +	public Row(int arity) {
    +		this.fields = new Object[arity];
    +	}
    +
    +	/**
    +	 * Get the number of field in the Row.
    +	 * @return The number of field in the Row.
    +	 */
    +	public int getArity() {
    +		return fields.length;
    +	}
    +
    +	/**
    +	 * Gets the field at the specified position.
    +	 * @param pos The position of the field, 0-based.
    +	 * @return The field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public Object getField(int pos) {
    +		return fields[pos];
    +	}
    +
    +	/**
    +	 * Sets the field at the specified position.
    +	 *
    +	 * @param pos The position of the field, 0-based.
    +	 * @param value The value to be assigned to the field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public void setField(int pos, Object value) {
    +		fields[pos] = value;
    +	}
    +
    +	@Override
    +	public String toString() {
    +		return Arrays.deepToString(fields);
    +	}
    +
    +	@Override
    +	public boolean equals(Object o) {
    +		if (this == o) {
    +			return true;
    +		}
    +		if (o == null || getClass() != o.getClass()) {
    +			return false;
    +		}
    +
    +		Row row = (Row) o;
    +
    +		// Probably incorrect - comparing Object[] arrays with Arrays.equals
    --- End diff --
    
    It's a IDEA code generation comment. We can remove this . 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91949817
  
    --- Diff: flink-core/src/test/java/org/apache/flink/api/common/typeutils/base/RowComparatorTest.java ---
    @@ -0,0 +1,156 @@
    +/*
    + * 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.flink.api.common.typeutils.base;
    +
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
    +import org.apache.flink.api.common.typeutils.ComparatorTestBase;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple3;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +import org.apache.flink.api.java.typeutils.TupleTypeInfo;
    +import org.apache.flink.api.java.typeutils.TypeExtractor;
    +import org.apache.flink.types.Row;
    +import org.junit.Before;
    +
    +import java.io.Serializable;
    +
    +import static org.junit.Assert.assertEquals;
    +
    +public class RowComparatorTest extends ComparatorTestBase<Row> {
    +
    +	private final RowTypeInfo typeInfo = new RowTypeInfo(
    +		BasicTypeInfo.INT_TYPE_INFO,
    +		BasicTypeInfo.DOUBLE_TYPE_INFO,
    +		BasicTypeInfo.STRING_TYPE_INFO,
    +		new TupleTypeInfo<Tuple3<Integer, Boolean, Short>>(
    +			BasicTypeInfo.INT_TYPE_INFO,
    +			BasicTypeInfo.BOOLEAN_TYPE_INFO,
    +			BasicTypeInfo.SHORT_TYPE_INFO
    +		),
    +		TypeExtractor.createTypeInfo(MyPojo.class));
    +
    +	private MyPojo testPojo1 = new MyPojo();
    +	private MyPojo testPojo2 = new MyPojo();
    +	private MyPojo testPojo3 = new MyPojo();
    +
    +	private final Row[] data = new Row[]{
    +		createRow(null, null, null, null, null),
    +		createRow(0, null, null, null, null),
    +		createRow(0, 0.0, null, null, null),
    +		createRow(0, 0.0, "a", null, null),
    +		createRow(1, 0.0, "a", null, null),
    +		createRow(1, 1.0, "a", null, null),
    +		createRow(1, 1.0, "b", null, null),
    +		createRow(1, 1.0, "b", new Tuple3<>(1, false, (short) 2), null),
    +		createRow(1, 1.0, "b", new Tuple3<>(2, false, (short) 2), null),
    +		createRow(1, 1.0, "b", new Tuple3<>(2, true, (short) 2), null),
    +		createRow(1, 1.0, "b", new Tuple3<>(2, true, (short) 3), null),
    +		createRow(1, 1.0, "b", new Tuple3<>(2, true, (short) 3), testPojo1),
    +		createRow(1, 1.0, "b", new Tuple3<>(2, true, (short) 3), testPojo2),
    +		createRow(1, 1.0, "b", new Tuple3<>(2, true, (short) 3), testPojo3)
    +	};
    +
    +	@Before
    --- End diff --
    
    Can be `@BeforeClass`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    Hi @wuchong, maybe we should move `RowCsvInputFormat` also


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91670483
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -326,8 +326,8 @@ public int getNormalizeKeyLen() {
     	@Override
     	public boolean isNormalizedKeyPrefixOnly(int keyBytes) {
     		return numLeadingNormalizableKeys < keyPositions.length ||
    -				normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    -				normalizableKeyPrefixLen > keyBytes;
    +		       normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    +		       normalizableKeyPrefixLen > keyBytes;
    --- End diff --
    
    here must be only tabs instead of spaces


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91929660
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,695 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		int[] normalizedKeyLengths,
    +		int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey) {
    +		this.arity = arity;
    +		this.keyPositions = keyPositions;
    +		this.comparators = comparators;
    +		this.serializers = serializers;
    +		this.normalizedKeyLengths = normalizedKeyLengths;
    +		this.numLeadingNormalizableKeys = numLeadingNormalizableKeys;
    +		this.normalizableKeyPrefixLen = normalizableKeyPrefixLen;
    +		this.invertNormKey = invertNormKey;
    +		this.nullMask1 = new boolean[arity];
    +		this.nullMask2 = new boolean[arity];
    +		deserializedKeyFields1 = instantiateDeserializationFields();
    +		deserializedKeyFields2 = instantiateDeserializationFields();
    +	}
    +
    +	// --------------------------------------------------------------------------------------------
    +	//  Comparator Methods
    +	// --------------------------------------------------------------------------------------------
    +
    +	@Override
    +	public void getFlatComparator(List<TypeComparator> flatComparators) {
    +		for (NullAwareComparator<Object> c : comparators) {
    +			Collections.addAll(flatComparators, c.getFlatComparators());
    +		}
    +	}
    +
    +	@Override
    +	public int hash(Row record) {
    +		int code = 0;
    +		int i = 0;
    +
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				code *= TupleComparatorBase.HASH_SALT[i & 0x1F];
    +				Object element = record.getField(keyPositions[i]); // element can be null
    +				code += comparators[i].hash(element);
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +
    +		return code;
    +	}
    +
    +	@Override
    +	public void setReference(Row toCompare) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = toCompare.getField(keyPositions[i]);
    +				comparator.setReference(element);   // element can be null
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +	}
    +
    +	@Override
    +	public boolean equalToReference(Row candidate) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = candidate.getField(keyPositions[i]);   // element can be null
    +				// check if reference is not equal
    +				if (!comparator.equalToReference(element)) {
    +					return false;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return true;
    +	}
    +
    +	@Override
    +	public int compareToReference(TypeComparator<Row> referencedComparator) {
    +		RowComparator other = (RowComparator) referencedComparator;
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int cmp = comparators[i].compareToReference(other.comparators[i]);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compare(Row first, Row second) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int keyPos = keyPositions[i];
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object firstElement = first.getField(keyPos);   // element can be null
    +				Object secondElement = second.getField(keyPos); // element can be null
    +
    +				int cmp = comparator.compare(firstElement, secondElement);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compareSerialized(
    +		DataInputView firstSource,
    +		DataInputView secondSource) throws IOException {
    +
    +		int len = serializers.length;
    +		int keyLen = keyPositions.length;
    +
    +		readIntoNullMask(arity, firstSource, nullMask1);
    +		readIntoNullMask(arity, secondSource, nullMask2);
    +
    +		// deserialize
    +		for (int i = 0; i < len; i++) {
    +			TypeSerializer<Object> serializer = serializers[i];
    +
    +			// deserialize field 1
    +			if (!nullMask1[i]) {
    +				deserializedKeyFields1[i] = serializer.deserialize(
    +					deserializedKeyFields1[i],
    +					firstSource);
    +			}
    +
    +			// deserialize field 2
    +			if (!nullMask2[i]) {
    +				deserializedKeyFields2[i] = serializer.deserialize(
    +					deserializedKeyFields2[i],
    +					secondSource);
    +			}
    +		}
    +
    +		// compare
    +		for (int i = 0; i < keyLen; i++) {
    +			int keyPos = keyPositions[i];
    +			TypeComparator<Object> comparator = comparators[i];
    +
    +			boolean isNull1 = nullMask1[keyPos];
    +			boolean isNull2 = nullMask2[keyPos];
    +
    +			int cmp = 0;
    +			// both values are null -> equality
    +			if (isNull1 && isNull2) {
    +				cmp = 0;
    +			}
    +			// first value is null -> inequality
    +			else if (isNull1) {
    +				cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
    +			}
    +			// second value is null -> inequality
    +			else if (isNull2) {
    +				cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
    +			}
    +			// no null values
    +			else {
    +				cmp = comparator.compare(
    +					deserializedKeyFields1[keyPos],
    +					deserializedKeyFields2[keyPos]);
    +			}
    +
    +			if (cmp != 0) {
    +				return cmp;
    +			}
    +		}
    +
    +		return 0;
    +	}
    +
    +	@Override
    +	public boolean supportsNormalizedKey() {
    +		return numLeadingNormalizableKeys > 0;
    +	}
    +
    +	@Override
    +	public boolean supportsSerializationWithKeyNormalization() {
    +		return false;
    +	}
    +
    +	@Override
    +	public int getNormalizeKeyLen() {
    +		return normalizableKeyPrefixLen;
    +	}
    +
    +	@Override
    +	public boolean isNormalizedKeyPrefixOnly(int keyBytes) {
    +		return numLeadingNormalizableKeys < keyPositions.length ||
    +				normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    +				normalizableKeyPrefixLen > keyBytes;
    +	}
    +
    +	@Override
    +	public void putNormalizedKey(
    +		Row record, MemorySegment target, int offset, int numBytes) {
    +		int bytesLeft = numBytes;
    +		int currentOffset = offset;
    +
    +		for (int i = 0; i < numLeadingNormalizableKeys && bytesLeft > 0; i++) {
    +			int len = normalizedKeyLengths[i];
    +			len = bytesLeft >= len ? len : bytesLeft;
    +
    +			TypeComparator<Object> comparator = comparators[i];
    +			Object element = record.getField(keyPositions[i]);  // element can be null
    +			// write key
    +			comparator.putNormalizedKey(element, target, currentOffset, len);
    +
    +			bytesLeft -= len;
    +			currentOffset += len;
    +		}
    +
    +	}
    +
    +	@Override
    +	public void writeWithKeyNormalization(Row record, DataOutputView target) throws IOException {
    +		throw new UnsupportedOperationException(
    +			"Record serialization with leading normalized keys not supported.");
    +	}
    +
    +	@Override
    +	public Row readWithKeyDenormalization(Row reuse, DataInputView source) throws IOException {
    +		throw new UnsupportedOperationException(
    +			"Record deserialization with leading normalized keys not supported.");
    +	}
    +
    +	@Override
    +	public boolean invertNormalizedKey() {
    +		return invertNormKey;
    +	}
    +
    +	@Override
    +	public TypeComparator<Row> duplicate() {
    +		NullAwareComparator<?>[] comparatorsCopy = new NullAwareComparator<?>[comparators.length];
    +		for (int i = 0; i < comparators.length; i++) {
    +			comparatorsCopy[i] = (NullAwareComparator<?>) comparators[i].duplicate();
    +		}
    +
    +		TypeSerializer<?>[] serializersCopy = new TypeSerializer<?>[serializers.length];
    +		for (int i = 0; i < serializers.length; i++) {
    +			serializersCopy[i] = serializers[i].duplicate();
    +		}
    +
    +		return new RowComparator(
    +			arity,
    +			keyPositions,
    +			(NullAwareComparator<Object>[]) comparatorsCopy,
    +			(TypeSerializer<Object>[]) serializersCopy,
    +			normalizedKeyLengths,
    +			numLeadingNormalizableKeys,
    +			normalizableKeyPrefixLen,
    +			invertNormKey);
    +	}
    +
    +	@Override
    +	public int extractKeys(Object record, Object[] target, int index) {
    +		int len = comparators.length;
    +		int localIndex = index;
    +		for (int i = 0; i < len; i++) {
    +			Object element = ((Row) record).getField(keyPositions[i]);  // element can be null
    +			localIndex += comparators[i].extractKeys(element, target, localIndex);
    +		}
    +		return localIndex - index;
    +	}
    +
    +
    +	private Object[] instantiateDeserializationFields() {
    +		Object[] newFields = new Object[serializers.length];
    +		for (int i = 0; i < serializers.length; i++) {
    +			newFields[i] = serializers[i].createInstance();
    +		}
    +		return newFields;
    +	}
    +
    +	/**
    +	 * @return creates auxiliary fields for normalized key support
    +	 */
    +	private static Tuple4<int[], Integer, Integer, Boolean>
    +	createAuxiliaryFields(int[] keyPositions, NullAwareComparator<Object>[] comparators) {
    +		int[] normalizedKeyLengths = new int[keyPositions.length];
    +		int numLeadingNormalizableKeys = 0;
    +		int normalizableKeyPrefixLen = 0;
    +		boolean inverted = false;
    +
    +		for (int i = 0; i < keyPositions.length; i++) {
    +			NullAwareComparator<Object> k = comparators[i];
    +			// as long as the leading keys support normalized keys, we can build up the composite key
    +			if (k.supportsNormalizedKey()) {
    +				if (i == 0) {
    +					// the first comparator decides whether we need to invert the key direction
    +					inverted = k.invertNormalizedKey();
    +				} else if (k.invertNormalizedKey() != inverted) {
    +					// if a successor does not agree on the inversion direction, it cannot be part of the
    +					// normalized key
    +					return new Tuple4<>(
    +						normalizedKeyLengths,
    +						numLeadingNormalizableKeys,
    +						normalizableKeyPrefixLen,
    +						inverted);
    +				}
    +				numLeadingNormalizableKeys++;
    +				int len = k.getNormalizeKeyLen();
    +				if (len < 0) {
    +					throw new RuntimeException(
    +						"Comparator " + k.getClass().getName() +
    +						" specifies an invalid length for the normalized key: " + len);
    +				}
    +				normalizedKeyLengths[i] = len;
    +				normalizableKeyPrefixLen += len;
    +				if (normalizableKeyPrefixLen < 0) {
    +					// overflow, which means we are out of budget for normalized key space anyways
    +					return new Tuple4<>(
    +						normalizedKeyLengths,
    +						numLeadingNormalizableKeys,
    +						Integer.MAX_VALUE,
    +						inverted);
    +				}
    +			} else {
    +				return new Tuple4<>(
    +					normalizedKeyLengths,
    +					numLeadingNormalizableKeys,
    +					normalizableKeyPrefixLen,
    +					inverted);
    +			}
    +		}
    +		return new Tuple4<>(
    +			normalizedKeyLengths,
    +			numLeadingNormalizableKeys,
    +			normalizableKeyPrefixLen,
    +			inverted);
    +	}
    +
    +	private static NullAwareComparator<Object>[] makeNullAware(
    +		TypeComparator<Object>[] comparators,
    +		boolean[] orders) {
    +		checkArgument(comparators.length == orders.length);
    +		NullAwareComparator<?>[] result = new NullAwareComparator<?>[comparators.length];
    +		for (int i = 0; i < comparators.length; i++) {
    +			result[i] = new NullAwareComparator<Object>(comparators[i], orders[i]);
    +		}
    +		return (NullAwareComparator<Object>[]) result;
    +	}
    +
    +
    +	/**
    +	 * Null-aware comparator that wraps a comparator which does not support null references.
    +	 * <p>
    +	 * NOTE: This class assumes to be used within a composite type comparator (such
    +	 * as {@link RowComparator}) that handles serialized comparison.
    +	 */
    +	public static class NullAwareComparator<T> extends TypeComparator<T> {
    --- End diff --
    
    Can we move this class into a separate file?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91690748
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,202 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    --- End diff --
    
    Actually, `Seq` is not a subclass of `Collection`, provide `Collection<TypeInformation<?>> types` doesn't fix the problem. I think it's better to adapt the Scala code where has used RowTypeInfo in this form: `new RowTypeInfo(Types.INT, Types.LONG, Types.STRING)`. What do you think?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r92037566
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,489 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    --- End diff --
    
    `ArrayList` is unused and needs to be removed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91544114
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,176 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    +		super(Row.class, types);
    +
    +		this.fieldNames = new String[types.length];
    +
    +		for (int i = 0; i < types.length; i++) {
    +			fieldNames[i] = "f" + i;
    +		}
    +	}
    +
    +	@Override
    +	public TypeComparator<Row> createComparator(
    +		int[] logicalKeyFields,
    +		boolean[] orders,
    +		int logicalFieldOffset,
    +		ExecutionConfig config) {
    +		comparatorOrders = orders;
    +		TypeComparator<Row> comparator = super.createComparator(
    +			logicalKeyFields,
    +			orders,
    +			logicalFieldOffset,
    +			config);
    +		comparatorOrders = null;
    +		return comparator;
    +	}
    +
    +	@Override
    +	protected TypeComparatorBuilder<Row> createTypeComparatorBuilder() {
    +		if (comparatorOrders == null) {
    +			throw new IllegalStateException("Cannot create comparator builder without orders.");
    +		}
    +		return new RowTypeComparatorBuilder(comparatorOrders);
    +	}
    +
    +	@Override
    +	public String[] getFieldNames() {
    +		return fieldNames;
    +	}
    +
    +	@Override
    +	public int getFieldIndex(String fieldName) {
    +		for (int i = 0; i < fieldNames.length; i++) {
    +			if (fieldNames[i].equals(fieldName)) {
    +				return i;
    --- End diff --
    
    what if we have more than field one with the same name?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by wuchong <gi...@git.apache.org>.
Github user wuchong commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91652178
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,176 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    +		super(Row.class, types);
    +
    +		this.fieldNames = new String[types.length];
    +
    +		for (int i = 0; i < types.length; i++) {
    +			fieldNames[i] = "f" + i;
    --- End diff --
    
    I think `RowTypeInfo` is something like `TupleTypeInfo`, which can't have custom field names.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91758367
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,202 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    +		super(Row.class, types);
    +
    +		this.fieldNames = new String[types.length];
    +
    +		for (int i = 0; i < types.length; i++) {
    +			fieldNames[i] = "f" + i;
    +		}
    +	}
    +
    +	@Override
    +	public TypeComparator<Row> createComparator(
    +		int[] logicalKeyFields,
    +		boolean[] orders,
    +		int logicalFieldOffset,
    +		ExecutionConfig config) {
    +		comparatorOrders = orders;
    --- End diff --
    
    can you add a new line here to separate parameters from the method body?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91946871
  
    --- Diff: flink-core/src/main/java/org/apache/flink/types/Row.java ---
    @@ -0,0 +1,110 @@
    +/*
    + * 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.flink.types;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +
    +import java.io.Serializable;
    +import java.util.Arrays;
    +
    +/**
    + * A Row has no limited length and contain a set of fields, which may all be different types.
    + * Because Row is not strongly typed, Flink's type extraction mechanism can't extract correct field
    + * types. So that users should manually tell Flink the type information via creating a
    + * {@link RowTypeInfo}.
    + *
    + * <p>
    + * The fields in the Row may be accessed by position (zero-based) {@link #getField(int)}. And can
    + * set fields by {@link #setField(int, Object)}.
    + * <p>
    + * Row is in principle serializable. However, it may contain non-serializable fields,
    + * in which case serialization will fail.
    + *
    + */
    +@PublicEvolving
    +public class Row implements Serializable{
    +
    +	private static final long serialVersionUID = 1L;
    +
    +	/** The array to store actual values. */
    +	private final Object[] fields;
    +
    +	/**
    +	 * Create a new Row instance.
    +	 * @param arity The number of field in the Row
    +	 */
    +	public Row(int arity) {
    +		this.fields = new Object[arity];
    +	}
    +
    +	/**
    +	 * Get the number of field in the Row.
    +	 * @return The number of field in the Row.
    +	 */
    +	public int getArity() {
    +		return fields.length;
    +	}
    +
    +	/**
    +	 * Gets the field at the specified position.
    +	 * @param pos The position of the field, 0-based.
    +	 * @return The field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public Object getField(int pos) {
    +		return fields[pos];
    +	}
    +
    +	/**
    +	 * Sets the field at the specified position.
    +	 *
    +	 * @param pos The position of the field, 0-based.
    +	 * @param value The value to be assigned to the field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public void setField(int pos, Object value) {
    +		fields[pos] = value;
    +	}
    +
    +	@Override
    +	public String toString() {
    +		return Arrays.deepToString(fields);
    --- End diff --
    
    Agree, the `toString` method should return the same representation as before.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91841404
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,695 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		int[] normalizedKeyLengths,
    +		int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey) {
    +		this.arity = arity;
    --- End diff --
    
    new line


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91927195
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,695 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		int[] normalizedKeyLengths,
    +		int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey) {
    +		this.arity = arity;
    +		this.keyPositions = keyPositions;
    +		this.comparators = comparators;
    +		this.serializers = serializers;
    +		this.normalizedKeyLengths = normalizedKeyLengths;
    +		this.numLeadingNormalizableKeys = numLeadingNormalizableKeys;
    +		this.normalizableKeyPrefixLen = normalizableKeyPrefixLen;
    +		this.invertNormKey = invertNormKey;
    +		this.nullMask1 = new boolean[arity];
    +		this.nullMask2 = new boolean[arity];
    +		deserializedKeyFields1 = instantiateDeserializationFields();
    +		deserializedKeyFields2 = instantiateDeserializationFields();
    +	}
    +
    +	// --------------------------------------------------------------------------------------------
    +	//  Comparator Methods
    +	// --------------------------------------------------------------------------------------------
    +
    +	@Override
    +	public void getFlatComparator(List<TypeComparator> flatComparators) {
    +		for (NullAwareComparator<Object> c : comparators) {
    +			Collections.addAll(flatComparators, c.getFlatComparators());
    +		}
    +	}
    +
    +	@Override
    +	public int hash(Row record) {
    +		int code = 0;
    +		int i = 0;
    +
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				code *= TupleComparatorBase.HASH_SALT[i & 0x1F];
    +				Object element = record.getField(keyPositions[i]); // element can be null
    +				code += comparators[i].hash(element);
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +
    +		return code;
    +	}
    +
    +	@Override
    +	public void setReference(Row toCompare) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = toCompare.getField(keyPositions[i]);
    +				comparator.setReference(element);   // element can be null
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +	}
    +
    +	@Override
    +	public boolean equalToReference(Row candidate) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = candidate.getField(keyPositions[i]);   // element can be null
    +				// check if reference is not equal
    +				if (!comparator.equalToReference(element)) {
    +					return false;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return true;
    +	}
    +
    +	@Override
    +	public int compareToReference(TypeComparator<Row> referencedComparator) {
    +		RowComparator other = (RowComparator) referencedComparator;
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int cmp = comparators[i].compareToReference(other.comparators[i]);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compare(Row first, Row second) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int keyPos = keyPositions[i];
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object firstElement = first.getField(keyPos);   // element can be null
    +				Object secondElement = second.getField(keyPos); // element can be null
    +
    +				int cmp = comparator.compare(firstElement, secondElement);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compareSerialized(
    +		DataInputView firstSource,
    +		DataInputView secondSource) throws IOException {
    +
    +		int len = serializers.length;
    +		int keyLen = keyPositions.length;
    +
    +		readIntoNullMask(arity, firstSource, nullMask1);
    +		readIntoNullMask(arity, secondSource, nullMask2);
    +
    +		// deserialize
    +		for (int i = 0; i < len; i++) {
    +			TypeSerializer<Object> serializer = serializers[i];
    +
    +			// deserialize field 1
    +			if (!nullMask1[i]) {
    +				deserializedKeyFields1[i] = serializer.deserialize(
    +					deserializedKeyFields1[i],
    +					firstSource);
    +			}
    +
    +			// deserialize field 2
    +			if (!nullMask2[i]) {
    +				deserializedKeyFields2[i] = serializer.deserialize(
    +					deserializedKeyFields2[i],
    +					secondSource);
    +			}
    +		}
    +
    +		// compare
    +		for (int i = 0; i < keyLen; i++) {
    +			int keyPos = keyPositions[i];
    +			TypeComparator<Object> comparator = comparators[i];
    +
    +			boolean isNull1 = nullMask1[keyPos];
    +			boolean isNull2 = nullMask2[keyPos];
    +
    +			int cmp = 0;
    +			// both values are null -> equality
    +			if (isNull1 && isNull2) {
    +				cmp = 0;
    +			}
    +			// first value is null -> inequality
    +			else if (isNull1) {
    +				cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
    +			}
    +			// second value is null -> inequality
    +			else if (isNull2) {
    +				cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
    +			}
    +			// no null values
    +			else {
    +				cmp = comparator.compare(
    +					deserializedKeyFields1[keyPos],
    +					deserializedKeyFields2[keyPos]);
    +			}
    +
    +			if (cmp != 0) {
    +				return cmp;
    +			}
    +		}
    +
    +		return 0;
    +	}
    +
    +	@Override
    +	public boolean supportsNormalizedKey() {
    +		return numLeadingNormalizableKeys > 0;
    +	}
    +
    +	@Override
    +	public boolean supportsSerializationWithKeyNormalization() {
    +		return false;
    +	}
    +
    +	@Override
    +	public int getNormalizeKeyLen() {
    +		return normalizableKeyPrefixLen;
    +	}
    +
    +	@Override
    +	public boolean isNormalizedKeyPrefixOnly(int keyBytes) {
    +		return numLeadingNormalizableKeys < keyPositions.length ||
    +				normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    +				normalizableKeyPrefixLen > keyBytes;
    +	}
    +
    +	@Override
    +	public void putNormalizedKey(
    +		Row record, MemorySegment target, int offset, int numBytes) {
    +		int bytesLeft = numBytes;
    --- End diff --
    
    New line


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91551621
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,698 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    --- End diff --
    
    I think this constructor is redundant. `int[] normalizedKeyLengths, int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey` can be initialized direct form `Tuple4<>`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91704437
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,202 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    --- End diff --
    
    Scala `scala.collection.Seq` easily converts to `java.util.List` with `import scala.collection.JavaConversions._` in scala code


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    merging


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    @fhueske Yes. I already  started https://github.com/tonycox/flink/tree/FLINK-5188


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91754276
  
    --- Diff: docs/dev/types_serialization.md ---
    @@ -92,6 +92,8 @@ Internally, Flink makes the following distinctions between types:
     
       * POJOs: classes that follow a certain bean-like pattern
     
    +  * Row (unlimited length of fields)
    --- End diff --
    
    Extend the description of the Java and Scala tuples (max 25 / 22 fields, null fields not supported) to make the difference to `Row` clear.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by twalthr <gi...@git.apache.org>.
Github user twalthr commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    Thanks for working on this @wuchong. I will review this next week.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91547877
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/RowComparator.java ---
    @@ -0,0 +1,698 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.api.common.typeutils.CompositeTypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.tuple.Tuple4;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.core.memory.MemorySegment;
    +import org.apache.flink.types.KeyFieldOutOfBoundsException;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.List;
    +
    +import static org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
    +import static org.apache.flink.util.Preconditions.checkArgument;
    +
    +/**
    + * Comparator for {@link Row}
    + */
    +public class RowComparator extends CompositeTypeComparator<Row> {
    +
    +	private static final long serialVersionUID = 1L;
    +	/** The number of fields of the Row */
    +	private final int arity;
    +	/** key positions describe which fields are keys in what order */
    +	private final int[] keyPositions;
    +	/** null-aware comparators for the key fields, in the same order as the key fields */
    +	private final NullAwareComparator<Object>[] comparators;
    +	/** serializers to deserialize the first n fields for comparison */
    +	private final TypeSerializer<Object>[] serializers;
    +	/** auxiliary fields for normalized key support */
    +	private final int[] normalizedKeyLengths;
    +	private final int numLeadingNormalizableKeys;
    +	private final int normalizableKeyPrefixLen;
    +	private final boolean invertNormKey;
    +
    +	// null masks for serialized comparison
    +	private final boolean[] nullMask1;
    +	private final boolean[] nullMask2;
    +
    +	// cache for the deserialized key field objects
    +	transient private final Object[] deserializedKeyFields1;
    +	transient private final Object[] deserializedKeyFields2;
    +
    +	/**
    +	 * General constructor for RowComparator.
    +	 *
    +	 * @param arity        the number of fields of the Row
    +	 * @param keyPositions key positions describe which fields are keys in what order
    +	 * @param comparators  non-null-aware comparators for the key fields, in the same order as
    +	 *                     the key fields
    +	 * @param serializers  serializers to deserialize the first n fields for comparison
    +	 * @param orders       sorting orders for the fields
    +	 */
    +	public RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		TypeComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		boolean[] orders) {
    +		this(arity, keyPositions, makeNullAware(comparators, orders), serializers);
    +	}
    +
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			createAuxiliaryFields(keyPositions, comparators));
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		Tuple4<int[], Integer, Integer, Boolean> auxiliaryFields) {
    +		this(
    +			arity,
    +			keyPositions,
    +			comparators,
    +			serializers,
    +			auxiliaryFields.f0,
    +			auxiliaryFields.f1,
    +			auxiliaryFields.f2,
    +			auxiliaryFields.f3);
    +	}
    +
    +	/**
    +	 * Intermediate constructor for creating auxiliary fields.
    +	 */
    +	private RowComparator(
    +		int arity,
    +		int[] keyPositions,
    +		NullAwareComparator<Object>[] comparators,
    +		TypeSerializer<Object>[] serializers,
    +		int[] normalizedKeyLengths,
    +		int numLeadingNormalizableKeys, int normalizableKeyPrefixLen, boolean invertNormKey) {
    +		this.arity = arity;
    +		this.keyPositions = keyPositions;
    +		this.comparators = comparators;
    +		this.serializers = serializers;
    +		this.normalizedKeyLengths = normalizedKeyLengths;
    +		this.numLeadingNormalizableKeys = numLeadingNormalizableKeys;
    +		this.normalizableKeyPrefixLen = normalizableKeyPrefixLen;
    +		this.invertNormKey = invertNormKey;
    +		this.nullMask1 = new boolean[arity];
    +		this.nullMask2 = new boolean[arity];
    +		deserializedKeyFields1 = instantiateDeserializationFields();
    +		deserializedKeyFields2 = instantiateDeserializationFields();
    +	}
    +
    +	// --------------------------------------------------------------------------------------------
    +	//  Comparator Methods
    +	// --------------------------------------------------------------------------------------------
    +
    +	@Override
    +	public void getFlatComparator(List<TypeComparator> flatComparators) {
    +		for (NullAwareComparator<Object> c : comparators) {
    +			Collections.addAll(flatComparators, c.getFlatComparators());
    +		}
    +	}
    +
    +	@Override
    +	public int hash(Row record) {
    +		int code = 0;
    +		int i = 0;
    +
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				code *= TupleComparatorBase.HASH_SALT[i & 0x1F];
    +				Object element = record.getField(keyPositions[i]); // element can be null
    +				code += comparators[i].hash(element);
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +
    +		return code;
    +	}
    +
    +	@Override
    +	public void setReference(Row toCompare) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = toCompare.getField(keyPositions[i]);
    +				comparator.setReference(element);   // element can be null
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +	}
    +
    +	@Override
    +	public boolean equalToReference(Row candidate) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object element = candidate.getField(keyPositions[i]);   // element can be null
    +				// check if reference is not equal
    +				if (!comparator.equalToReference(element)) {
    +					return false;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return true;
    +	}
    +
    +	@Override
    +	public int compareToReference(TypeComparator<Row> referencedComparator) {
    +		RowComparator other = (RowComparator) referencedComparator;
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int cmp = comparators[i].compareToReference(other.comparators[i]);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compare(Row first, Row second) {
    +		int i = 0;
    +		try {
    +			for (; i < keyPositions.length; i++) {
    +				int keyPos = keyPositions[i];
    +				TypeComparator<Object> comparator = comparators[i];
    +				Object firstElement = first.getField(keyPos);   // element can be null
    +				Object secondElement = second.getField(keyPos); // element can be null
    +
    +				int cmp = comparator.compare(firstElement, secondElement);
    +				if (cmp != 0) {
    +					return cmp;
    +				}
    +			}
    +		} catch (IndexOutOfBoundsException e) {
    +			throw new KeyFieldOutOfBoundsException(keyPositions[i]);
    +		}
    +		return 0;
    +	}
    +
    +	@Override
    +	public int compareSerialized(
    +		DataInputView firstSource,
    +		DataInputView secondSource) throws IOException {
    +
    +		int len = serializers.length;
    +		int keyLen = keyPositions.length;
    +
    +		readIntoNullMask(arity, firstSource, nullMask1);
    +		readIntoNullMask(arity, secondSource, nullMask2);
    +
    +		// deserialize
    +		for (int i = 0; i < len; i++) {
    +			TypeSerializer<Object> serializer = serializers[i];
    +
    +			// deserialize field 1
    +			if (!nullMask1[i]) {
    +				deserializedKeyFields1[i] = serializer.deserialize(
    +					deserializedKeyFields1[i],
    +					firstSource);
    +			}
    +
    +			// deserialize field 2
    +			if (!nullMask2[i]) {
    +				deserializedKeyFields2[i] = serializer.deserialize(
    +					deserializedKeyFields2[i],
    +					secondSource);
    +			}
    +		}
    +
    +		// compare
    +		for (int i = 0; i < keyLen; i++) {
    +			int keyPos = keyPositions[i];
    +			TypeComparator<Object> comparator = comparators[i];
    +
    +			boolean isNull1 = nullMask1[keyPos];
    +			boolean isNull2 = nullMask2[keyPos];
    +
    +			int cmp = 0;
    +			// both values are null -> equality
    +			if (isNull1 && isNull2) {
    +				cmp = 0;
    +			}
    +			// first value is null -> inequality
    +			else if (isNull1) {
    +				cmp = comparator.compare(null, deserializedKeyFields2[keyPos]);
    +			}
    +			// second value is null -> inequality
    +			else if (isNull2) {
    +				cmp = comparator.compare(deserializedKeyFields1[keyPos], null);
    +			}
    +			// no null values
    +			else {
    +				cmp = comparator.compare(
    +					deserializedKeyFields1[keyPos],
    +					deserializedKeyFields2[keyPos]);
    +			}
    +
    +			if (cmp != 0) {
    +				return cmp;
    +			}
    +		}
    +
    +		return 0;
    +	}
    +
    +	@Override
    +	public boolean supportsNormalizedKey() {
    +		return numLeadingNormalizableKeys > 0;
    +	}
    +
    +	@Override
    +	public boolean supportsSerializationWithKeyNormalization() {
    +		return false;
    +	}
    +
    +	@Override
    +	public int getNormalizeKeyLen() {
    +		return normalizableKeyPrefixLen;
    +	}
    +
    +	@Override
    +	public boolean isNormalizedKeyPrefixOnly(int keyBytes) {
    +		return numLeadingNormalizableKeys < keyPositions.length ||
    +				normalizableKeyPrefixLen == Integer.MAX_VALUE ||
    +				normalizableKeyPrefixLen > keyBytes;
    +	}
    +
    +	@Override
    +	public void putNormalizedKey(
    +		Row record, MemorySegment target, int offset, int numBytes) {
    +		int bytesLeft = numBytes;
    +		int currentOffset = offset;
    +
    +		for (int i=0;i<numLeadingNormalizableKeys && bytesLeft > 0;i++){
    +			int len = normalizedKeyLengths[i];
    +			len = bytesLeft >= len ? len : bytesLeft;
    +
    +			TypeComparator<Object> comparator = comparators[i];
    +			Object element = record.getField(keyPositions[i]);  // element can be null
    +			// write key
    +			comparator.putNormalizedKey(element, target, currentOffset, len);
    +
    +			bytesLeft -= len;
    +			currentOffset += len;
    +		}
    +
    +	}
    +
    +	@Override
    +	public void writeWithKeyNormalization(
    +		Row record,
    +		DataOutputView target) throws IOException {
    --- End diff --
    
    Can be in one line


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91674126
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,202 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Arrays;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    --- End diff --
    
    Please add another one constructor with `Collection<TypeInformation<?>> types` to set for example `Seq(Types.INT, Types.LONG, Types.STRING)` from scala


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91546359
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/RowTypeInfo.java ---
    @@ -0,0 +1,176 @@
    +/*
    + * 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.flink.api.java.typeutils;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.common.ExecutionConfig;
    +import org.apache.flink.api.common.typeinfo.TypeInformation;
    +import org.apache.flink.api.common.typeutils.TypeComparator;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.api.java.typeutils.runtime.RowComparator;
    +import org.apache.flink.api.java.typeutils.runtime.RowSerializer;
    +import org.apache.flink.types.Row;
    +
    +import java.util.ArrayList;
    +import java.util.Collections;
    +
    +import static org.apache.flink.util.Preconditions.checkState;
    +
    +/**
    + * TypeInformation for {@link Row}
    + */
    +@PublicEvolving
    +public class RowTypeInfo extends TupleTypeInfoBase<Row> {
    +
    +	private static final long serialVersionUID = 9158518989896601963L;
    +
    +	protected final String[] fieldNames;
    +	/** Temporary variable for directly passing orders to comparators. */
    +	private boolean[] comparatorOrders = null;
    +
    +	public RowTypeInfo(TypeInformation<?>... types) {
    +		super(Row.class, types);
    +
    +		this.fieldNames = new String[types.length];
    +
    +		for (int i = 0; i < types.length; i++) {
    +			fieldNames[i] = "f" + i;
    --- End diff --
    
    Can we somehow get real names of fields?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91748535
  
    --- Diff: flink-core/src/main/java/org/apache/flink/types/Row.java ---
    @@ -0,0 +1,110 @@
    +/*
    + * 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.flink.types;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +
    +import java.io.Serializable;
    +import java.util.Arrays;
    +
    +/**
    + * A Row has no limited length and contain a set of fields, which may all be different types.
    + * Because Row is not strongly typed, Flink's type extraction mechanism can't extract correct field
    + * types. So that users should manually tell Flink the type information via creating a
    + * {@link RowTypeInfo}.
    + *
    + * <p>
    + * The fields in the Row may be accessed by position (zero-based) {@link #getField(int)}. And can
    + * set fields by {@link #setField(int, Object)}.
    + * <p>
    + * Row is in principle serializable. However, it may contain non-serializable fields,
    + * in which case serialization will fail.
    + *
    + */
    +@PublicEvolving
    +public class Row implements Serializable{
    +
    +	private static final long serialVersionUID = 1L;
    +
    +	/** The array to store actual values. */
    +	private final Object[] fields;
    +
    +	/**
    +	 * Create a new Row instance.
    +	 * @param arity The number of field in the Row
    +	 */
    +	public Row(int arity) {
    +		this.fields = new Object[arity];
    +	}
    +
    +	/**
    +	 * Get the number of field in the Row.
    +	 * @return The number of field in the Row.
    +	 */
    +	public int getArity() {
    +		return fields.length;
    +	}
    +
    +	/**
    +	 * Gets the field at the specified position.
    +	 * @param pos The position of the field, 0-based.
    +	 * @return The field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public Object getField(int pos) {
    +		return fields[pos];
    +	}
    +
    +	/**
    +	 * Sets the field at the specified position.
    +	 *
    +	 * @param pos The position of the field, 0-based.
    +	 * @param value The value to be assigned to the field at the specified position.
    +	 * @throws IndexOutOfBoundsException Thrown, if the position is negative, or equal to, or larger than the number of fields.
    +	 */
    +	public void setField(int pos, Object value) {
    +		fields[pos] = value;
    +	}
    +
    +	@Override
    +	public String toString() {
    +		return Arrays.deepToString(fields);
    --- End diff --
    
    What doy think about reduce all '[' and ']' from string?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink issue #2968: [FLINK-5187] [core] Create analog of Row and RowTypeInfo ...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on the issue:

    https://github.com/apache/flink/pull/2968
  
    Just noticed that the row comparator and serializer tests should be moved to `org.apache.flink.api.java.typeutils.runtime`.
    
    I will do that before merging.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by tonycox <gi...@git.apache.org>.
Github user tonycox commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91550465
  
    --- Diff: flink-core/src/main/java/org/apache/flink/types/Row.java ---
    @@ -0,0 +1,118 @@
    +/*
    + * 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.flink.types;
    +
    +import org.apache.flink.annotation.PublicEvolving;
    +import org.apache.flink.api.java.typeutils.RowTypeInfo;
    +
    +import java.io.Serializable;
    +import java.util.Arrays;
    +
    +/**
    + * A Row has no limited length and contain a set of fields, which may all be different types.
    + * Because Row is not strongly typed, Flink's type extraction mechanism can't extract correct field
    + * types. So that users should manually tell Flink the type information via creating a
    + * {@link RowTypeInfo}.
    + *
    + * <p>
    + * The fields in the Row may be accessed by position (zero-based) {@link #getField(int)}. And can
    + * set fields by {@link #setField(int, Object)}.
    + * <p>
    + * Row is in principle serializable. However, it may contain non-serializable fields,
    + * in which case serialization will fail.
    + *
    + */
    +@PublicEvolving
    +public class Row implements Serializable{
    +
    +	private static final long serialVersionUID = 1L;
    +
    +	/** Number of field. */
    +	private final int arity;
    --- End diff --
    
    `fields.length` is similar to `arity`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] flink pull request #2968: [FLINK-5187] [core] Create analog of Row and RowTy...

Posted by fhueske <gi...@git.apache.org>.
Github user fhueske commented on a diff in the pull request:

    https://github.com/apache/flink/pull/2968#discussion_r91761526
  
    --- Diff: flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/NullMaskUtils.java ---
    @@ -0,0 +1,104 @@
    +/*
    + * 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.flink.api.java.typeutils.runtime;
    +
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.DataOutputView;
    +import org.apache.flink.types.Row;
    +
    +import java.io.IOException;
    +
    +public class NullMaskUtils {
    +
    +	public static void writeNullMask(int len, Row value, DataOutputView target) throws IOException {
    +		int b = 0x00;
    +		int bytePos = 0;
    +
    +		int fieldPos = 0;
    +		int numPos = 0;
    +		while (fieldPos < len) {
    +			b = 0x00;
    +			// set bits in byte
    +			bytePos = 0;
    +			numPos = Math.min(8, len - fieldPos);
    +			while (bytePos < numPos) {
    +				b = b << 1;
    +				// set bit if field is null
    +				if (value.getField(fieldPos + bytePos) == null) {
    +					b |= 0x01;
    +				}
    +				bytePos += 1;
    +			}
    +			fieldPos += numPos;
    +			// shift bits if last byte is not completely filled
    +			b <<= (8 - bytePos);
    +			// write byte
    +			target.writeByte(b);
    +		}
    +	}
    +
    +	public static void readIntoNullMask(
    +		int len,
    +		DataInputView source,
    +		boolean[] nullMask) throws IOException {
    +		int b = 0x00;
    --- End diff --
    
    new line


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---