You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/10/07 16:43:03 UTC

[GitHub] [flink] sjwiesman commented on a change in pull request #13535: [FLINK-19496][table] DataGen source DECIMAL always returns null

sjwiesman commented on a change in pull request #13535:
URL: https://github.com/apache/flink/pull/13535#discussion_r501158416



##########
File path: flink-table/flink-table-api-java-bridge/src/main/java/org/apache/flink/table/factories/datagen/types/DecimalDataRandomGenerator.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.table.factories.datagen.types;
+
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.runtime.state.FunctionInitializationContext;
+import org.apache.flink.streaming.api.functions.source.datagen.DataGenerator;
+import org.apache.flink.table.data.DecimalData;
+
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.math.RoundingMode;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Generates random {@link DecimalData} values.
+ */
+public class DecimalDataRandomGenerator implements DataGenerator<DecimalData> {
+
+	private final int precision;
+
+	private final int scale;
+
+	private final double min;
+
+	private final double max;
+
+	private final String fmt;
+
+	public DecimalDataRandomGenerator(int precision, int scale, double min, double max) {
+		double largest = largestValue(precision, scale);
+		this.precision = precision;
+		this.scale = scale;
+		this.min = Math.min(-1 * largest, min);
+		this.max = Math.min(largest, max);
+		this.fmt = "%" + precision + "." + scale + "f";
+	}
+
+	@Override
+	public void open(String name, FunctionInitializationContext context, RuntimeContext runtimeContext) throws Exception {
+	}
+
+	@Override
+	public boolean hasNext() {
+		return true;
+	}
+
+	@Override
+	public DecimalData next() {
+		String value = String.format(fmt, ThreadLocalRandom.current().nextDouble(min, max));
+
+		BigDecimal decimal = new BigDecimal(value.trim(), new MathContext(precision, RoundingMode.DOWN));

Review comment:
       yes




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org