-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-54403][SQL][Metric View] Add YAML serde infrastructure for metric views #53146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
linhongliu-db
wants to merge
2
commits into
apache:master
Choose a base branch
from
linhongliu-db:metric-view-serde
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+957
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
sql/catalyst/src/main/scala/org/apache/spark/sql/metricview/serde/JsonUtils.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * 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.spark.sql.metricview.serde | ||
|
|
||
| import com.fasterxml.jackson.databind.ObjectMapper | ||
| import com.fasterxml.jackson.module.scala.DefaultScalaModule | ||
|
|
||
| object JsonUtils { | ||
| // Singleton ObjectMapper that can be used across the project | ||
| private lazy val mapper: ObjectMapper = { | ||
| val m = new ObjectMapper() | ||
| m.registerModule(DefaultScalaModule) | ||
| m | ||
| } | ||
|
|
||
| def toJson[T: Manifest](obj: T): String = { | ||
| mapper.writeValueAsString(obj) | ||
| } | ||
| } | ||
212 changes: 212 additions & 0 deletions
212
sql/catalyst/src/main/scala/org/apache/spark/sql/metricview/serde/MetricViewCanonical.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| /* | ||
| * 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.spark.sql.metricview.serde | ||
|
|
||
| import scala.util.{Failure, Success, Try} | ||
|
|
||
| import com.fasterxml.jackson.annotation.{JsonIgnoreProperties, JsonInclude, JsonProperty} | ||
| import com.fasterxml.jackson.annotation.JsonInclude.Include | ||
|
|
||
| import org.apache.spark.sql.catalyst.parser.CatalystSqlParser | ||
| import org.apache.spark.sql.metricview.serde.ColumnType.ColumnType | ||
| import org.apache.spark.sql.metricview.serde.SourceType.SourceType | ||
|
|
||
| // Trait representing the capability to validate an object | ||
| trait Validatable { | ||
| def validate(): Try[Unit] | ||
| } | ||
|
|
||
| sealed abstract class MetricViewSerdeException(message: String, cause: Option[Throwable] = None) | ||
| extends Exception(message, cause.orNull) | ||
|
|
||
| case class MetricViewValidationException(message: String, cause: Option[Throwable] = None) | ||
| extends MetricViewSerdeException(message, cause) | ||
|
|
||
| case class MetricViewFromProtoException(message: String, cause: Option[Throwable] = None) | ||
| extends MetricViewSerdeException(message, cause) | ||
|
|
||
| case class MetricViewYAMLParsingException(message: String, cause: Option[Throwable] = None) | ||
| extends MetricViewSerdeException(message, cause) | ||
|
|
||
| // Expression types in a Metric View | ||
| sealed trait Expression extends Validatable { | ||
| def expr: String | ||
|
|
||
| // Validate that expression is not empty | ||
| def validate(): Try[Unit] = { | ||
| if (expr.isEmpty) { | ||
| Failure(MetricViewValidationException("expr cannot be empty")) | ||
| } else Success(()) | ||
| } | ||
| } | ||
|
|
||
| // Dimension expression representing a scalar value | ||
| case class DimensionExpression(expr: String) extends Expression | ||
|
|
||
| // Measure expression representing an aggregated value | ||
| case class MeasureExpression(expr: String) extends Expression | ||
|
|
||
| object SourceType extends Enumeration { | ||
| type SourceType = Value | ||
| val ASSET, SQL = Value | ||
|
|
||
| def fromString(sourceType: String): SourceType = { | ||
| values.find(_.toString.equalsIgnoreCase(sourceType)).getOrElse { | ||
| throw MetricViewFromProtoException( | ||
| s"Unsupported source type: $sourceType" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Representation of a source in the Metric View | ||
| sealed trait Source extends Validatable { | ||
| def sourceType: SourceType | ||
|
|
||
| def validate(): Try[Unit] | ||
| } | ||
|
|
||
| // Asset source, representing a UC table, view, or Metric View, etc. | ||
| case class AssetSource(name: String) extends Source { | ||
| val sourceType: SourceType = SourceType.ASSET | ||
|
|
||
| def validate(): Try[Unit] = { | ||
| if (name.isEmpty) { | ||
| Failure( | ||
| MetricViewValidationException("Source cannot be empty") | ||
| ) | ||
| } else Success(()) | ||
| } | ||
|
|
||
| override def toString: String = this.name | ||
| } | ||
|
|
||
| // SQL source, representing a SQL query | ||
| case class SQLSource(sql: String) extends Source { | ||
| val sourceType: SourceType = SourceType.SQL | ||
|
|
||
| def validate(): Try[Unit] = { | ||
| if (sql.isEmpty) { | ||
| Failure( | ||
| MetricViewValidationException("Source cannot be empty") | ||
| ) | ||
| } else Success(()) | ||
| } | ||
|
|
||
| override def toString: String = this.sql | ||
| } | ||
|
|
||
| object Source { | ||
| def apply(sourceText: String): Source = { | ||
| if (sourceText.isEmpty) { | ||
| throw MetricViewValidationException("Source cannot be empty") | ||
| } | ||
| Try(CatalystSqlParser.parseTableIdentifier(sourceText)) match { | ||
| case Success(_) => AssetSource(sourceText) | ||
| case Failure(_) => | ||
| Try(CatalystSqlParser.parseQuery(sourceText)) match { | ||
| case Success(_) => SQLSource(sourceText) | ||
| case Failure(queryEx) => | ||
| throw MetricViewValidationException( | ||
| s"Invalid source: $sourceText", | ||
| Some(queryEx) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| case class Column[T <: Expression]( | ||
| name: String, | ||
| expression: T, | ||
| ordinal: Int) extends Validatable { | ||
| override def validate(): Try[Unit] = { | ||
| Success(()) | ||
| } | ||
|
|
||
| def columnType: ColumnType = expression match { | ||
| case _: DimensionExpression => ColumnType.Dimension | ||
| case _: MeasureExpression => ColumnType.Measure | ||
| case _ => | ||
| throw MetricViewValidationException( | ||
| s"Unsupported expression type: ${expression.getClass.getName}" | ||
| ) | ||
| } | ||
|
|
||
| def getColumnMetadata: ColumnMetadata = { | ||
| val truncatedExpr = expression.expr.take(Constants.MAXIMUM_PROPERTY_SIZE) | ||
| ColumnMetadata(columnType.toString, truncatedExpr) | ||
| } | ||
| } | ||
|
|
||
| object ColumnType extends Enumeration { | ||
| type ColumnType = Value | ||
| val Dimension: ColumnType = Value("dimension") | ||
| val Measure: ColumnType = Value("measure") | ||
|
|
||
| // Method to match case-insensitively and return the correct value | ||
| def fromString(columnType: String): ColumnType = { | ||
| values.find(_.toString.equalsIgnoreCase(columnType)).getOrElse { | ||
| throw MetricViewFromProtoException( | ||
| s"Unsupported column type: $columnType" | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| @JsonInclude(Include.NON_ABSENT) | ||
| case class ColumnMetadata( | ||
| @JsonProperty(value = Constants.COLUMN_TYPE_PROPERTY_KEY, required = true) | ||
| columnType: String, // "type" -> "metric_view.type" | ||
| @JsonProperty(value = Constants.COLUMN_EXPR_PROPERTY_KEY, required = true) | ||
| expr: String // "expr" -> "metric_view.expr" | ||
| ) | ||
|
|
||
| // Only parse the "version" field and ignore all others | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| case class YAMLVersion(version: String) extends Validatable { | ||
| private def validYAMLVersions: Set[String] = Set("0.1") | ||
| def validate(): Try[Unit] = { | ||
| if (!validYAMLVersions.contains(version)) { | ||
| Failure( | ||
| MetricViewValidationException( | ||
| s"Invalid YAML version: $version" | ||
| ) | ||
| ) | ||
| } else Success(()) | ||
| } | ||
| } | ||
|
|
||
| object YAMLVersion { | ||
| def apply(version: String): YAMLVersion = { | ||
| val yamlVersion = new YAMLVersion(version) | ||
| yamlVersion.validate() match { | ||
| case Success(_) => yamlVersion | ||
| case Failure(e) => throw e | ||
| } | ||
| } | ||
| } | ||
|
|
||
| case class MetricView( | ||
| version: String, | ||
| from: Source, | ||
| where: Option[String] = None, | ||
| select: Seq[Column[_ <: Expression]]) { | ||
|
|
||
| } |
69 changes: 69 additions & 0 deletions
69
sql/catalyst/src/main/scala/org/apache/spark/sql/metricview/serde/MetricViewFactory.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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.spark.sql.metricview.serde | ||
|
|
||
| import scala.util.control.NonFatal | ||
|
|
||
| object MetricViewFactory { | ||
| def fromYAML(yamlContent: String): MetricView = { | ||
| try { | ||
| val yamlVersion = | ||
| YamlMapperProvider.mapperWithAllFields.readValue(yamlContent, classOf[YAMLVersion]) | ||
| yamlVersion.version match { | ||
| case "0.1" => | ||
| MetricViewYAMLDeserializer.parseYaml(yamlContent).toCanonical | ||
| case _ => | ||
| throw MetricViewValidationException( | ||
| s"Invalid YAML version: ${yamlVersion.version}" | ||
| ) | ||
| } | ||
| } catch { | ||
| case e: MetricViewSerdeException => | ||
| throw e | ||
| case NonFatal(e) => | ||
| throw MetricViewYAMLParsingException( | ||
| s"Failed to parse YAML: ${e.getMessage}", | ||
| Some(e) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| def toYAML(metricView: MetricView): String = { | ||
| try { | ||
| val versionSpecific = MetricViewBase.fromCanonical(metricView) | ||
| versionSpecific.version match { | ||
| case "0.1" => | ||
| MetricViewYAMLSerializer.toYaml( | ||
| versionSpecific.asInstanceOf[MetricViewV01] | ||
| ) | ||
| case _ => | ||
| throw MetricViewValidationException( | ||
| s"Invalid YAML version: ${metricView.version}" | ||
| ) | ||
| } | ||
| } catch { | ||
| case e: MetricViewSerdeException => | ||
| throw e | ||
| case NonFatal(e) => | ||
| throw MetricViewYAMLParsingException( | ||
| s"Failed to serialize to YAML: ${e.getMessage}", | ||
| Some(e) | ||
| ) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the comments, should we try to use this singleton as much as possible in the subsequent development?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'm surprised that there is no such util in the Spark repo. For now, I plan to use it for all the metric view development, but not sure how much other code needs this.